flipper_atomicdiceroller.c 13 KB

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