flipper_atomicdiceroller.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. uint8_t attempts = 0;
  115. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  116. furi_hal_power_enable_otg();
  117. furi_delay_ms(10);
  118. }
  119. uint8_t diceBuffer[64];
  120. for (uint8_t i=0;i<64;i++) diceBuffer[i] = 0;
  121. uint8_t diceBufferCounter = 0;
  122. uint8_t diceBufferPositionWrite = 0;
  123. uint8_t diceBufferPositionRead = 0;
  124. uint8_t tickCounter = 0;
  125. uint32_t CRC32 = 0;
  126. uint8_t method = 0;
  127. // MD5
  128. md5_context* md5_ctx = malloc(sizeof(md5_context));
  129. uint8_t* hash = malloc(sizeof(uint8_t) * 16);
  130. uint8_t* bufferTim2 = malloc(4);
  131. md5_starts(md5_ctx);
  132. uint8_t pause = 0;
  133. while(1)
  134. {
  135. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  136. uint8_t screenRefresh = 0;
  137. if (event_status == FuriStatusOk)
  138. {
  139. if(event.type == EventTypeInput)
  140. {
  141. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong)
  142. {
  143. break;
  144. }
  145. else if (pause == 0)
  146. {
  147. if (event.input.key == InputKeyOk && event.input.type == InputTypeShort)
  148. {
  149. if (diceBufferCounter > 0)
  150. {
  151. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  152. mutexVal.dice = diceBuffer[diceBufferPositionRead];
  153. mutexVal.diceAvailiable = --diceBufferCounter;
  154. mutexVal.pause = 1;
  155. furi_mutex_release(mutexVal.mutex);
  156. if (diceBufferPositionRead != 63) diceBufferPositionRead++;
  157. else diceBufferPositionRead = 0;
  158. pause = 1;
  159. furi_timer_start(timerPause, 500);
  160. screenRefresh = 1;
  161. }
  162. }
  163. else if (event.input.key == InputKeyLeft && event.input.type == InputTypeLong)
  164. {
  165. if (method == 1)
  166. {
  167. method = 0;
  168. diceBufferPositionWrite = 0;
  169. diceBufferPositionRead = 0;
  170. diceBufferCounter = 0;
  171. CRC32 = 0;
  172. tickCounter = 0;
  173. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  174. mutexVal.method = 0;
  175. mutexVal.dice = 0;
  176. mutexVal.diceAvailiable = 0;
  177. furi_mutex_release(mutexVal.mutex);
  178. screenRefresh = 1;
  179. }
  180. }
  181. else if (event.input.key == InputKeyRight && event.input.type == InputTypeLong)
  182. {
  183. if (method == 0)
  184. {
  185. method = 1;
  186. diceBufferPositionWrite = 0;
  187. diceBufferPositionRead = 0;
  188. diceBufferCounter = 0;
  189. md5_starts(md5_ctx);
  190. tickCounter = 0;
  191. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  192. mutexVal.method = 1;
  193. mutexVal.dice = 0;
  194. mutexVal.diceAvailiable = 0;
  195. furi_mutex_release(mutexVal.mutex);
  196. screenRefresh = 1;
  197. }
  198. }
  199. }
  200. }
  201. else if (event.type == ClockEventTypeTick)
  202. {
  203. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  204. mutexVal.cps = counter;
  205. furi_mutex_release(mutexVal.mutex);
  206. counter = 0;
  207. screenRefresh = 1;
  208. }
  209. else if (event.type == ClockEventTypeTickPause)
  210. {
  211. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  212. mutexVal.pause = 0;
  213. furi_mutex_release(mutexVal.mutex);
  214. furi_timer_stop(timerPause);
  215. pause = 0;
  216. screenRefresh = 1;
  217. }
  218. else if (event.type == EventGPIO)
  219. {
  220. if (diceBufferCounter < 64)
  221. {
  222. // CRC32
  223. if (method == 0)
  224. {
  225. uint32_t TIM2Tick = TIM2->CNT;
  226. bufferTim2[0] = (uint8_t)(TIM2Tick >> 24);
  227. bufferTim2[1] = (uint8_t)(TIM2Tick >> 16);
  228. bufferTim2[2] = (uint8_t)(TIM2Tick >> 8);
  229. bufferTim2[3] = (uint8_t)TIM2Tick;
  230. CRC32 = crc32_calc_buffer(CRC32, bufferTim2, 4);
  231. tickCounter++;
  232. if (tickCounter == 8)
  233. {
  234. uint8_t localDice = CRC32 & 0b111;
  235. if (localDice == 0 || localDice == 7)
  236. {
  237. localDice = (diceBuffer[diceBufferPositionRead] >> 3) & 0b111;
  238. }
  239. if (localDice >= 1 && localDice <= 6)
  240. {
  241. diceBuffer[diceBufferPositionWrite] = localDice;
  242. diceBufferCounter++;
  243. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  244. else diceBufferPositionWrite = 0;
  245. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  246. mutexVal.diceAvailiable = diceBufferCounter;
  247. furi_mutex_release(mutexVal.mutex);
  248. screenRefresh = 1;
  249. }
  250. CRC32 = 0;
  251. tickCounter = 0;
  252. }
  253. }
  254. // MD5
  255. else
  256. {
  257. uint32_t tick = TIM2->CNT;
  258. bufferTim2[0] = (uint8_t)(tick >> 24);
  259. bufferTim2[1] = (uint8_t)(tick >> 16);
  260. bufferTim2[2] = (uint8_t)(tick >> 8);
  261. bufferTim2[3] = (uint8_t)tick;
  262. md5_update(md5_ctx, bufferTim2, 4);
  263. tickCounter++;
  264. if (tickCounter == 32)
  265. {
  266. md5_finish(md5_ctx, hash);
  267. uint8_t localDice = 0;
  268. for (uint8_t i=0;i<16;i++)
  269. {
  270. localDice = hash[i] & 0b111;
  271. if (localDice >= 1 && localDice <= 6)
  272. {
  273. diceBuffer[diceBufferPositionWrite] = localDice;
  274. diceBufferCounter++;
  275. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  276. else diceBufferPositionWrite = 0;
  277. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  278. mutexVal.diceAvailiable = diceBufferCounter;
  279. furi_mutex_release(mutexVal.mutex);
  280. screenRefresh = 1;
  281. break;
  282. }
  283. }
  284. md5_starts(md5_ctx);
  285. tickCounter = 0;
  286. }
  287. }
  288. }
  289. counter++;
  290. }
  291. }
  292. if (screenRefresh == 1) view_port_update(view_port);
  293. }
  294. LL_TIM_DisableCounter(TIM2);
  295. furi_hal_bus_disable(FuriHalBusTIM2);
  296. free(md5_ctx);
  297. free(bufferTim2);
  298. free(hash);
  299. furi_record_close(RECORD_NOTIFICATION);
  300. // Disable 5v power
  301. if(furi_hal_power_is_otg_enabled()) {
  302. furi_hal_power_disable_otg();
  303. }
  304. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  305. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  306. furi_message_queue_free(event_queue);
  307. furi_mutex_free(mutexVal.mutex);
  308. gui_remove_view_port(gui, view_port);
  309. view_port_free(view_port);
  310. furi_timer_free(timer);
  311. furi_timer_free(timerPause);
  312. furi_record_close(RECORD_GUI);
  313. return 0;
  314. }