flipper_atomicdiceroller.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // CC0 1.0 Universal (CC0 1.0)
  2. // Public Domain Dedication
  3. // https://github.com/nmrr
  4. #include <stdio.h>
  5. #include <furi.h>
  6. #include <gui/gui.h>
  7. #include <input/input.h>
  8. #include <notification/notification_messages.h>
  9. #include <furi_hal_power.h>
  10. #include <locale/locale.h>
  11. #include <toolbox/crc32_calc.h>
  12. #include <lib/toolbox/md5.h>
  13. #define SCREEN_SIZE_X 128
  14. #define SCREEN_SIZE_Y 64
  15. typedef enum {
  16. EventTypeInput,
  17. ClockEventTypeTick,
  18. ClockEventTypeTickPause,
  19. EventGPIO,
  20. } EventType;
  21. typedef struct {
  22. EventType type;
  23. InputEvent input;
  24. } EventApp;
  25. #define lineArraySize 128
  26. typedef struct {
  27. FuriMutex* mutex;
  28. uint32_t cps;
  29. uint32_t diceAvailiable;
  30. uint8_t dice;
  31. uint8_t method;
  32. uint8_t pause;
  33. } mutexStruct;
  34. static void draw_callback(Canvas* canvas, void* ctx)
  35. {
  36. mutexStruct* mutexVal = ctx;
  37. mutexStruct mutexDraw;
  38. furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
  39. memcpy(&mutexDraw, mutexVal, sizeof(mutexStruct));
  40. furi_mutex_release(mutexVal->mutex);
  41. canvas_set_font(canvas, FontPrimary);
  42. char buffer[32];
  43. snprintf(buffer, sizeof(buffer), "%ld cps", mutexDraw.cps);
  44. canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignBottom, buffer);
  45. snprintf(buffer, sizeof(buffer), "%lu/64", mutexDraw.diceAvailiable);
  46. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X, 10, AlignRight, AlignBottom, buffer);
  47. if (mutexDraw.method == 0) canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: CRC32");
  48. else canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: MD5");
  49. if (mutexDraw.dice != 0 && mutexDraw.pause == 0)
  50. {
  51. canvas_set_font(canvas, FontBigNumbers);
  52. snprintf(buffer, sizeof(buffer), "%u", mutexDraw.dice);
  53. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X/2, 50, AlignCenter, AlignBottom, buffer);
  54. }
  55. }
  56. static void input_callback(InputEvent* input_event, void* ctx)
  57. {
  58. furi_assert(ctx);
  59. FuriMessageQueue* event_queue = ctx;
  60. EventApp event = {.type = EventTypeInput, .input = *input_event};
  61. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  62. }
  63. static void clock_tick(void* ctx) {
  64. furi_assert(ctx);
  65. FuriMessageQueue* queue = ctx;
  66. EventApp event = {.type = ClockEventTypeTick};
  67. furi_message_queue_put(queue, &event, 0);
  68. }
  69. static void clock_tick_pause(void* ctx) {
  70. furi_assert(ctx);
  71. FuriMessageQueue* queue = ctx;
  72. EventApp event = {.type = ClockEventTypeTickPause};
  73. furi_message_queue_put(queue, &event, 0);
  74. }
  75. static void gpiocallback(void* ctx) {
  76. furi_assert(ctx);
  77. FuriMessageQueue* queue = ctx;
  78. EventApp event = {.type = EventGPIO};
  79. furi_message_queue_put(queue, &event, 0);
  80. }
  81. int32_t flipper_atomicdiceroller_app()
  82. {
  83. furi_hal_bus_enable(FuriHalBusTIM2);
  84. LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP);
  85. LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1);
  86. LL_TIM_SetPrescaler(TIM2, 0);
  87. LL_TIM_SetAutoReload(TIM2, 0xFFFFFFFF);
  88. LL_TIM_SetCounter(TIM2, 0);
  89. LL_TIM_EnableCounter(TIM2);
  90. EventApp event;
  91. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  92. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  93. mutexStruct mutexVal;
  94. mutexVal.cps = 0;
  95. mutexVal.dice = 0;
  96. mutexVal.diceAvailiable = 0;
  97. mutexVal.method = 0;
  98. uint32_t counter = 0;
  99. mutexVal.mutex= furi_mutex_alloc(FuriMutexTypeNormal);
  100. if(!mutexVal.mutex) {
  101. furi_message_queue_free(event_queue);
  102. return 255;
  103. }
  104. ViewPort* view_port = view_port_alloc();
  105. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  106. view_port_input_callback_set(view_port, input_callback, event_queue);
  107. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  108. Gui* gui = furi_record_open(RECORD_GUI);
  109. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  110. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  111. furi_timer_start(timer, 1000);
  112. FuriTimer* timerPause = furi_timer_alloc(clock_tick_pause, FuriTimerTypePeriodic, event_queue);
  113. // ENABLE 5V pin
  114. furi_hal_power_enable_otg();
  115. uint8_t diceBuffer[64];
  116. for (uint8_t i=0;i<64;i++) diceBuffer[i] = 0;
  117. uint8_t diceBufferCounter = 0;
  118. uint8_t diceBufferPositionWrite = 0;
  119. uint8_t diceBufferPositionRead = 0;
  120. uint8_t tickCounter = 0;
  121. uint32_t CRC32 = 0;
  122. uint8_t method = 0;
  123. // MD5
  124. md5_context* md5_ctx = malloc(sizeof(md5_context));
  125. uint8_t* hash = malloc(sizeof(uint8_t) * 16);
  126. uint8_t* bufferTim2 = malloc(4);
  127. md5_starts(md5_ctx);
  128. uint8_t pause = 0;
  129. while(1)
  130. {
  131. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  132. uint8_t screenRefresh = 0;
  133. if (event_status == FuriStatusOk)
  134. {
  135. if(event.type == EventTypeInput)
  136. {
  137. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong)
  138. {
  139. break;
  140. }
  141. else if (pause == 0)
  142. {
  143. if (event.input.key == InputKeyOk && event.input.type == InputTypeShort)
  144. {
  145. if (diceBufferCounter > 0)
  146. {
  147. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  148. mutexVal.dice = diceBuffer[diceBufferPositionRead];
  149. mutexVal.diceAvailiable = --diceBufferCounter;
  150. mutexVal.pause = 1;
  151. furi_mutex_release(mutexVal.mutex);
  152. if (diceBufferPositionRead != 63) diceBufferPositionRead++;
  153. else diceBufferPositionRead = 0;
  154. pause = 1;
  155. furi_timer_start(timerPause, 500);
  156. screenRefresh = 1;
  157. }
  158. }
  159. else if (event.input.key == InputKeyLeft && event.input.type == InputTypeLong)
  160. {
  161. if (method == 1)
  162. {
  163. method = 0;
  164. diceBufferPositionWrite = 0;
  165. diceBufferPositionRead = 0;
  166. diceBufferCounter = 0;
  167. CRC32 = 0;
  168. tickCounter = 0;
  169. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  170. mutexVal.method = 0;
  171. mutexVal.dice = 0;
  172. mutexVal.diceAvailiable = 0;
  173. furi_mutex_release(mutexVal.mutex);
  174. screenRefresh = 1;
  175. }
  176. }
  177. else if (event.input.key == InputKeyRight && event.input.type == InputTypeLong)
  178. {
  179. if (method == 0)
  180. {
  181. method = 1;
  182. diceBufferPositionWrite = 0;
  183. diceBufferPositionRead = 0;
  184. diceBufferCounter = 0;
  185. md5_starts(md5_ctx);
  186. tickCounter = 0;
  187. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  188. mutexVal.method = 1;
  189. mutexVal.dice = 0;
  190. mutexVal.diceAvailiable = 0;
  191. furi_mutex_release(mutexVal.mutex);
  192. screenRefresh = 1;
  193. }
  194. }
  195. }
  196. }
  197. else if (event.type == ClockEventTypeTick)
  198. {
  199. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  200. mutexVal.cps = counter;
  201. furi_mutex_release(mutexVal.mutex);
  202. counter = 0;
  203. screenRefresh = 1;
  204. }
  205. else if (event.type == ClockEventTypeTickPause)
  206. {
  207. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  208. mutexVal.pause = 0;
  209. furi_mutex_release(mutexVal.mutex);
  210. furi_timer_stop(timerPause);
  211. pause = 0;
  212. screenRefresh = 1;
  213. }
  214. else if (event.type == EventGPIO)
  215. {
  216. if (diceBufferCounter < 64)
  217. {
  218. // CRC32
  219. if (method == 0)
  220. {
  221. uint32_t TIM2Tick = TIM2->CNT;
  222. bufferTim2[0] = (uint8_t)(TIM2Tick >> 24);
  223. bufferTim2[1] = (uint8_t)(TIM2Tick >> 16);
  224. bufferTim2[2] = (uint8_t)(TIM2Tick >> 8);
  225. bufferTim2[3] = (uint8_t)TIM2Tick;
  226. CRC32 = crc32_calc_buffer(CRC32, bufferTim2, 4);
  227. tickCounter++;
  228. if (tickCounter == 8)
  229. {
  230. uint8_t localDice = CRC32 & 0b111;
  231. if (localDice == 0 || localDice == 7)
  232. {
  233. localDice = (diceBuffer[diceBufferPositionRead] >> 3) & 0b111;
  234. }
  235. if (localDice >= 1 && localDice <= 6)
  236. {
  237. diceBuffer[diceBufferPositionWrite] = localDice;
  238. diceBufferCounter++;
  239. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  240. else diceBufferPositionWrite = 0;
  241. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  242. mutexVal.diceAvailiable = diceBufferCounter;
  243. furi_mutex_release(mutexVal.mutex);
  244. screenRefresh = 1;
  245. }
  246. CRC32 = 0;
  247. tickCounter = 0;
  248. }
  249. }
  250. // MD5
  251. else
  252. {
  253. uint32_t tick = TIM2->CNT;
  254. bufferTim2[0] = (uint8_t)(tick >> 24);
  255. bufferTim2[1] = (uint8_t)(tick >> 16);
  256. bufferTim2[2] = (uint8_t)(tick >> 8);
  257. bufferTim2[3] = (uint8_t)tick;
  258. md5_update(md5_ctx, bufferTim2, 4);
  259. tickCounter++;
  260. if (tickCounter == 32)
  261. {
  262. md5_finish(md5_ctx, hash);
  263. uint8_t localDice = 0;
  264. for (uint8_t i=0;i<16;i++)
  265. {
  266. localDice = hash[i] & 0b111;
  267. if (localDice >= 1 && localDice <= 6)
  268. {
  269. diceBuffer[diceBufferPositionWrite] = localDice;
  270. diceBufferCounter++;
  271. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  272. else diceBufferPositionWrite = 0;
  273. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  274. mutexVal.diceAvailiable = diceBufferCounter;
  275. furi_mutex_release(mutexVal.mutex);
  276. screenRefresh = 1;
  277. break;
  278. }
  279. }
  280. md5_starts(md5_ctx);
  281. tickCounter = 0;
  282. }
  283. }
  284. }
  285. counter++;
  286. }
  287. }
  288. if (screenRefresh == 1) view_port_update(view_port);
  289. }
  290. LL_TIM_DisableCounter(TIM2);
  291. furi_hal_bus_disable(FuriHalBusTIM2);
  292. free(md5_ctx);
  293. free(bufferTim2);
  294. free(hash);
  295. furi_record_close(RECORD_NOTIFICATION);
  296. furi_hal_power_disable_otg();
  297. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  298. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  299. furi_message_queue_free(event_queue);
  300. furi_mutex_free(mutexVal.mutex);
  301. gui_remove_view_port(gui, view_port);
  302. view_port_free(view_port);
  303. furi_timer_free(timer);
  304. furi_timer_free(timerPause);
  305. furi_record_close(RECORD_GUI);
  306. return 0;
  307. }