flipper_atomicdiceroller.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. #include <expansion/expansion.h>
  14. #define SCREEN_SIZE_X 128
  15. #define SCREEN_SIZE_Y 64
  16. typedef enum {
  17. EventTypeInput,
  18. ClockEventTypeTick,
  19. ClockEventTypeTickPause,
  20. EventGPIO,
  21. } EventType;
  22. typedef struct {
  23. EventType type;
  24. InputEvent input;
  25. } EventApp;
  26. #define lineArraySize 128
  27. typedef struct {
  28. FuriMutex* mutex;
  29. uint32_t cps;
  30. uint32_t diceAvailiable;
  31. uint8_t dice;
  32. uint8_t method;
  33. uint8_t pause;
  34. } mutexStruct;
  35. static void draw_callback(Canvas* canvas, void* ctx) {
  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)
  48. canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: CRC32");
  49. else
  50. canvas_draw_str_aligned(canvas, 0, 20, AlignLeft, AlignBottom, "Hash: MD5");
  51. if(mutexDraw.dice != 0 && mutexDraw.pause == 0) {
  52. canvas_set_font(canvas, FontBigNumbers);
  53. snprintf(buffer, sizeof(buffer), "%u", mutexDraw.dice);
  54. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X / 2, 50, AlignCenter, AlignBottom, buffer);
  55. }
  56. }
  57. static void input_callback(InputEvent* input_event, void* ctx) {
  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. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  83. expansion_disable(expansion);
  84. furi_hal_bus_enable(FuriHalBusTIM2);
  85. LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP);
  86. LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1);
  87. LL_TIM_SetPrescaler(TIM2, 0);
  88. LL_TIM_SetAutoReload(TIM2, 0xFFFFFFFF);
  89. LL_TIM_SetCounter(TIM2, 0);
  90. LL_TIM_EnableCounter(TIM2);
  91. EventApp event;
  92. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  93. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  94. mutexStruct mutexVal;
  95. mutexVal.cps = 0;
  96. mutexVal.dice = 0;
  97. mutexVal.diceAvailiable = 0;
  98. mutexVal.method = 0;
  99. uint32_t counter = 0;
  100. mutexVal.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  101. if(!mutexVal.mutex) {
  102. furi_message_queue_free(event_queue);
  103. expansion_enable(expansion);
  104. furi_record_close(RECORD_EXPANSION);
  105. return 255;
  106. }
  107. ViewPort* view_port = view_port_alloc();
  108. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  109. view_port_input_callback_set(view_port, input_callback, event_queue);
  110. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  111. Gui* gui = furi_record_open(RECORD_GUI);
  112. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  113. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  114. furi_timer_start(timer, 1000);
  115. FuriTimer* timerPause = furi_timer_alloc(clock_tick_pause, FuriTimerTypePeriodic, event_queue);
  116. // ENABLE 5V pin
  117. uint8_t attempts = 0;
  118. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  119. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  120. furi_hal_power_enable_otg();
  121. furi_delay_ms(10);
  122. }
  123. uint8_t diceBuffer[64];
  124. for(uint8_t i = 0; i < 64; i++) diceBuffer[i] = 0;
  125. uint8_t diceBufferCounter = 0;
  126. uint8_t diceBufferPositionWrite = 0;
  127. uint8_t diceBufferPositionRead = 0;
  128. uint8_t tickCounter = 0;
  129. uint32_t CRC32 = 0;
  130. uint8_t method = 0;
  131. // MD5
  132. mbedtls_md5_context md5_ctx;
  133. uint8_t* hash = malloc(sizeof(uint8_t) * 16);
  134. uint8_t* bufferTim2 = malloc(4);
  135. mbedtls_md5_init(&md5_ctx);
  136. mbedtls_md5_starts(&md5_ctx);
  137. uint8_t pause = 0;
  138. while(1) {
  139. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  140. uint8_t screenRefresh = 0;
  141. if(event_status == FuriStatusOk) {
  142. if(event.type == EventTypeInput) {
  143. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) {
  144. break;
  145. } else if(pause == 0) {
  146. if(event.input.key == InputKeyOk && event.input.type == InputTypeShort) {
  147. if(diceBufferCounter > 0) {
  148. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  149. mutexVal.dice = diceBuffer[diceBufferPositionRead];
  150. mutexVal.diceAvailiable = --diceBufferCounter;
  151. mutexVal.pause = 1;
  152. furi_mutex_release(mutexVal.mutex);
  153. if(diceBufferPositionRead != 63)
  154. diceBufferPositionRead++;
  155. else
  156. diceBufferPositionRead = 0;
  157. pause = 1;
  158. furi_timer_start(timerPause, 500);
  159. screenRefresh = 1;
  160. }
  161. } else if(event.input.key == InputKeyLeft && event.input.type == InputTypeLong) {
  162. if(method == 1) {
  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. } else if(event.input.key == InputKeyRight && event.input.type == InputTypeLong) {
  177. if(method == 0) {
  178. method = 1;
  179. diceBufferPositionWrite = 0;
  180. diceBufferPositionRead = 0;
  181. diceBufferCounter = 0;
  182. mbedtls_md5_starts(&md5_ctx);
  183. tickCounter = 0;
  184. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  185. mutexVal.method = 1;
  186. mutexVal.dice = 0;
  187. mutexVal.diceAvailiable = 0;
  188. furi_mutex_release(mutexVal.mutex);
  189. screenRefresh = 1;
  190. }
  191. }
  192. }
  193. } else if(event.type == ClockEventTypeTick) {
  194. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  195. mutexVal.cps = counter;
  196. furi_mutex_release(mutexVal.mutex);
  197. counter = 0;
  198. screenRefresh = 1;
  199. } else if(event.type == ClockEventTypeTickPause) {
  200. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  201. mutexVal.pause = 0;
  202. furi_mutex_release(mutexVal.mutex);
  203. furi_timer_stop(timerPause);
  204. pause = 0;
  205. screenRefresh = 1;
  206. } else if(event.type == EventGPIO) {
  207. if(diceBufferCounter < 64) {
  208. // CRC32
  209. if(method == 0) {
  210. uint32_t TIM2Tick = TIM2->CNT;
  211. bufferTim2[0] = (uint8_t)(TIM2Tick >> 24);
  212. bufferTim2[1] = (uint8_t)(TIM2Tick >> 16);
  213. bufferTim2[2] = (uint8_t)(TIM2Tick >> 8);
  214. bufferTim2[3] = (uint8_t)TIM2Tick;
  215. CRC32 = crc32_calc_buffer(CRC32, bufferTim2, 4);
  216. tickCounter++;
  217. if(tickCounter == 8) {
  218. uint8_t localDice = CRC32 & 0b111;
  219. if(localDice == 0 || localDice == 7) {
  220. localDice = (diceBuffer[diceBufferPositionRead] >> 3) & 0b111;
  221. }
  222. if(localDice >= 1 && localDice <= 6) {
  223. diceBuffer[diceBufferPositionWrite] = localDice;
  224. diceBufferCounter++;
  225. if(diceBufferPositionWrite != 63)
  226. diceBufferPositionWrite++;
  227. else
  228. diceBufferPositionWrite = 0;
  229. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  230. mutexVal.diceAvailiable = diceBufferCounter;
  231. furi_mutex_release(mutexVal.mutex);
  232. screenRefresh = 1;
  233. }
  234. CRC32 = 0;
  235. tickCounter = 0;
  236. }
  237. }
  238. // MD5
  239. else {
  240. uint32_t tick = TIM2->CNT;
  241. bufferTim2[0] = (uint8_t)(tick >> 24);
  242. bufferTim2[1] = (uint8_t)(tick >> 16);
  243. bufferTim2[2] = (uint8_t)(tick >> 8);
  244. bufferTim2[3] = (uint8_t)tick;
  245. mbedtls_md5_update(&md5_ctx, bufferTim2, 4);
  246. tickCounter++;
  247. if(tickCounter == 32) {
  248. mbedtls_md5_finish(&md5_ctx, hash);
  249. uint8_t localDice = 0;
  250. for(uint8_t i = 0; i < 16; i++) {
  251. localDice = hash[i] & 0b111;
  252. if(localDice >= 1 && localDice <= 6) {
  253. diceBuffer[diceBufferPositionWrite] = localDice;
  254. diceBufferCounter++;
  255. if(diceBufferPositionWrite != 63)
  256. diceBufferPositionWrite++;
  257. else
  258. diceBufferPositionWrite = 0;
  259. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  260. mutexVal.diceAvailiable = diceBufferCounter;
  261. furi_mutex_release(mutexVal.mutex);
  262. screenRefresh = 1;
  263. break;
  264. }
  265. }
  266. mbedtls_md5_starts(&md5_ctx);
  267. tickCounter = 0;
  268. }
  269. }
  270. }
  271. counter++;
  272. }
  273. }
  274. if(screenRefresh == 1) view_port_update(view_port);
  275. }
  276. LL_TIM_DisableCounter(TIM2);
  277. furi_hal_bus_disable(FuriHalBusTIM2);
  278. mbedtls_md5_free(&md5_ctx);
  279. //free(md5_ctx);
  280. free(bufferTim2);
  281. free(hash);
  282. furi_record_close(RECORD_NOTIFICATION);
  283. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  284. furi_hal_power_disable_otg();
  285. }
  286. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  287. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  288. furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeAnalog);
  289. furi_message_queue_free(event_queue);
  290. furi_mutex_free(mutexVal.mutex);
  291. gui_remove_view_port(gui, view_port);
  292. view_port_free(view_port);
  293. furi_timer_free(timer);
  294. furi_timer_free(timerPause);
  295. furi_record_close(RECORD_GUI);
  296. expansion_enable(expansion);
  297. furi_record_close(RECORD_EXPANSION);
  298. return 0;
  299. }