flipper_atomicdiceroller.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. uint8_t diceAvailiable;
  31. uint8_t dice;
  32. uint8_t method;
  33. uint8_t pause;
  34. uint8_t tickCounter;
  35. uint8_t range;
  36. } mutexStruct;
  37. static void draw_callback(Canvas* canvas, void* ctx) {
  38. mutexStruct* mutexVal = ctx;
  39. mutexStruct mutexDraw;
  40. furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
  41. memcpy(&mutexDraw, mutexVal, sizeof(mutexStruct));
  42. furi_mutex_release(mutexVal->mutex);
  43. canvas_set_font(canvas, FontPrimary);
  44. char buffer[32];
  45. snprintf(buffer, sizeof(buffer), "%ld cps", mutexDraw.cps);
  46. canvas_draw_str_aligned(canvas, 0, 10, AlignLeft, AlignBottom, buffer);
  47. snprintf(buffer, sizeof(buffer), "%u/64", mutexDraw.diceAvailiable);
  48. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X, 10, AlignRight, AlignBottom, buffer);
  49. if(mutexDraw.method == 0) {
  50. if(mutexDraw.tickCounter < 2) {
  51. buffer[0] = '-';
  52. buffer[1] = '-';
  53. buffer[2] = '-';
  54. buffer[3] = '\0';
  55. } else if(mutexDraw.tickCounter < 4) {
  56. buffer[0] = '+';
  57. buffer[1] = '-';
  58. buffer[2] = '-';
  59. buffer[3] = '\0';
  60. } else if(mutexDraw.tickCounter < 6) {
  61. buffer[0] = '+';
  62. buffer[1] = '+';
  63. buffer[2] = '-';
  64. buffer[3] = '\0';
  65. } else {
  66. buffer[0] = '+';
  67. buffer[1] = '+';
  68. buffer[2] = '+';
  69. buffer[3] = '\0';
  70. }
  71. } else {
  72. if(mutexDraw.tickCounter < 8) {
  73. buffer[0] = '-';
  74. buffer[1] = '-';
  75. buffer[2] = '-';
  76. buffer[3] = '\0';
  77. } else if(mutexDraw.tickCounter < 16) {
  78. buffer[0] = '+';
  79. buffer[1] = '-';
  80. buffer[2] = '-';
  81. buffer[3] = '\0';
  82. } else if(mutexDraw.tickCounter < 24) {
  83. buffer[0] = '+';
  84. buffer[1] = '+';
  85. buffer[2] = '-';
  86. buffer[3] = '\0';
  87. } else {
  88. buffer[0] = '+';
  89. buffer[1] = '+';
  90. buffer[2] = '+';
  91. buffer[3] = '\0';
  92. }
  93. }
  94. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X - 5, 20, AlignRight, AlignBottom, buffer);
  95. if(mutexDraw.method == 0)
  96. canvas_draw_str_aligned(canvas, 0, 22, AlignLeft, AlignBottom, "Hash: CRC32");
  97. else
  98. canvas_draw_str_aligned(canvas, 0, 22, AlignLeft, AlignBottom, "Hash: MD5");
  99. if(mutexDraw.range == 0)
  100. canvas_draw_str_aligned(canvas, 0, 34, AlignLeft, AlignBottom, "Range: 0-1");
  101. else
  102. canvas_draw_str_aligned(canvas, 0, 34, AlignLeft, AlignBottom, "Range: 1-6");
  103. if(mutexDraw.pause == 0) {
  104. canvas_set_font(canvas, FontBigNumbers);
  105. snprintf(buffer, sizeof(buffer), "%u", mutexDraw.dice);
  106. canvas_draw_str_aligned(canvas, SCREEN_SIZE_X / 2, 54, AlignCenter, AlignBottom, buffer);
  107. }
  108. }
  109. static void input_callback(InputEvent* input_event, void* ctx) {
  110. furi_assert(ctx);
  111. FuriMessageQueue* event_queue = ctx;
  112. EventApp event = {.type = EventTypeInput, .input = *input_event};
  113. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  114. }
  115. static void clock_tick(void* ctx) {
  116. furi_assert(ctx);
  117. FuriMessageQueue* queue = ctx;
  118. EventApp event = {.type = ClockEventTypeTick};
  119. furi_message_queue_put(queue, &event, 0);
  120. }
  121. static void clock_tick_pause(void* ctx) {
  122. furi_assert(ctx);
  123. FuriMessageQueue* queue = ctx;
  124. EventApp event = {.type = ClockEventTypeTickPause};
  125. furi_message_queue_put(queue, &event, 0);
  126. }
  127. static void gpiocallback(void* ctx) {
  128. furi_assert(ctx);
  129. FuriMessageQueue* queue = ctx;
  130. EventApp event = {.type = EventGPIO};
  131. furi_message_queue_put(queue, &event, 0);
  132. }
  133. int32_t flipper_atomicdiceroller_app() {
  134. Expansion* expansion = furi_record_open(RECORD_EXPANSION);
  135. expansion_disable(expansion);
  136. furi_hal_bus_enable(FuriHalBusTIM2);
  137. LL_TIM_SetCounterMode(TIM2, LL_TIM_COUNTERMODE_UP);
  138. LL_TIM_SetClockDivision(TIM2, LL_TIM_CLOCKDIVISION_DIV1);
  139. LL_TIM_SetPrescaler(TIM2, 0);
  140. LL_TIM_SetAutoReload(TIM2, 0xFFFFFFFF);
  141. LL_TIM_SetCounter(TIM2, 0);
  142. LL_TIM_EnableCounter(TIM2);
  143. EventApp event;
  144. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  145. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  146. mutexStruct mutexVal;
  147. mutexVal.cps = 0;
  148. mutexVal.dice = 0;
  149. mutexVal.diceAvailiable = 0;
  150. mutexVal.method = 0;
  151. mutexVal.tickCounter = 0;
  152. mutexVal.range = 0;
  153. uint32_t counter = 0;
  154. mutexVal.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  155. if(!mutexVal.mutex) {
  156. furi_message_queue_free(event_queue);
  157. expansion_enable(expansion);
  158. furi_record_close(RECORD_EXPANSION);
  159. return 255;
  160. }
  161. ViewPort* view_port = view_port_alloc();
  162. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  163. view_port_input_callback_set(view_port, input_callback, event_queue);
  164. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  165. Gui* gui = furi_record_open(RECORD_GUI);
  166. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  167. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  168. furi_timer_start(timer, 1000);
  169. FuriTimer* timerPause = furi_timer_alloc(clock_tick_pause, FuriTimerTypePeriodic, event_queue);
  170. // ENABLE 5V pin
  171. uint8_t attempts = 0;
  172. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  173. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  174. furi_hal_power_enable_otg();
  175. furi_delay_ms(10);
  176. }
  177. uint8_t diceBuffer[64];
  178. for(uint8_t i = 0; i < 64; i++) diceBuffer[i] = 0;
  179. uint8_t diceBufferCounter = 0;
  180. uint8_t diceBufferPositionWrite = 0;
  181. uint8_t diceBufferPositionRead = 0;
  182. uint8_t tickCounter = 0;
  183. uint32_t CRC32 = 0;
  184. uint8_t method = 0;
  185. uint8_t range = 0;
  186. // MD5
  187. mbedtls_md5_context md5_ctx;
  188. uint8_t* hash = malloc(sizeof(uint8_t) * 16);
  189. uint8_t* bufferTim2 = malloc(4);
  190. mbedtls_md5_init(&md5_ctx);
  191. mbedtls_md5_starts(&md5_ctx);
  192. uint8_t pause = 0;
  193. while(1) {
  194. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  195. uint8_t screenRefresh = 0;
  196. if(event_status == FuriStatusOk) {
  197. if(event.type == EventTypeInput) {
  198. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong) {
  199. break;
  200. } else if(pause == 0) {
  201. if(event.input.key == InputKeyOk && event.input.type == InputTypeShort) {
  202. if(diceBufferCounter > 0) {
  203. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  204. mutexVal.dice = diceBuffer[diceBufferPositionRead];
  205. mutexVal.diceAvailiable = --diceBufferCounter;
  206. mutexVal.pause = 1;
  207. furi_mutex_release(mutexVal.mutex);
  208. if(diceBufferPositionRead != 63)
  209. diceBufferPositionRead++;
  210. else
  211. diceBufferPositionRead = 0;
  212. pause = 1;
  213. furi_timer_start(timerPause, 500);
  214. screenRefresh = 1;
  215. }
  216. } else if(event.input.key == InputKeyLeft && event.input.type == InputTypeLong) {
  217. if(method == 1) {
  218. method = 0;
  219. diceBufferPositionWrite = 0;
  220. diceBufferPositionRead = 0;
  221. diceBufferCounter = 0;
  222. CRC32 = 0;
  223. tickCounter = 0;
  224. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  225. mutexVal.method = 0;
  226. mutexVal.pause = 1;
  227. mutexVal.diceAvailiable = 0;
  228. mutexVal.tickCounter = 0;
  229. furi_mutex_release(mutexVal.mutex);
  230. screenRefresh = 1;
  231. }
  232. } else if(event.input.key == InputKeyRight && event.input.type == InputTypeLong) {
  233. if(method == 0) {
  234. method = 1;
  235. diceBufferPositionWrite = 0;
  236. diceBufferPositionRead = 0;
  237. diceBufferCounter = 0;
  238. mbedtls_md5_starts(&md5_ctx);
  239. tickCounter = 0;
  240. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  241. mutexVal.method = 1;
  242. mutexVal.pause = 1;
  243. mutexVal.diceAvailiable = 0;
  244. mutexVal.tickCounter = 0;
  245. furi_mutex_release(mutexVal.mutex);
  246. screenRefresh = 1;
  247. }
  248. } else if(event.input.key == InputKeyUp && event.input.type == InputTypeLong) {
  249. if(range > 0) {
  250. range--;
  251. diceBufferPositionWrite = 0;
  252. diceBufferPositionRead = 0;
  253. diceBufferCounter = 0;
  254. mbedtls_md5_starts(&md5_ctx);
  255. tickCounter = 0;
  256. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  257. mutexVal.pause = 1;
  258. mutexVal.diceAvailiable = 0;
  259. mutexVal.tickCounter = 0;
  260. mutexVal.range = range;
  261. furi_mutex_release(mutexVal.mutex);
  262. screenRefresh = 1;
  263. }
  264. } else if(event.input.key == InputKeyDown && event.input.type == InputTypeLong) {
  265. if(range < 1) {
  266. range++;
  267. diceBufferPositionWrite = 0;
  268. diceBufferPositionRead = 0;
  269. diceBufferCounter = 0;
  270. mbedtls_md5_starts(&md5_ctx);
  271. tickCounter = 0;
  272. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  273. mutexVal.pause = 1;
  274. mutexVal.diceAvailiable = 0;
  275. mutexVal.tickCounter = 0;
  276. mutexVal.range = range;
  277. furi_mutex_release(mutexVal.mutex);
  278. screenRefresh = 1;
  279. }
  280. }
  281. }
  282. } else if(event.type == ClockEventTypeTick) {
  283. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  284. mutexVal.cps = counter;
  285. mutexVal.tickCounter = tickCounter;
  286. furi_mutex_release(mutexVal.mutex);
  287. counter = 0;
  288. screenRefresh = 1;
  289. } else if(event.type == ClockEventTypeTickPause) {
  290. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  291. mutexVal.pause = 0;
  292. furi_mutex_release(mutexVal.mutex);
  293. furi_timer_stop(timerPause);
  294. pause = 0;
  295. screenRefresh = 1;
  296. } else if(event.type == EventGPIO) {
  297. if(diceBufferCounter < 64) {
  298. // CRC32
  299. if(method == 0) {
  300. uint32_t TIM2Tick = TIM2->CNT;
  301. bufferTim2[0] = (uint8_t)(TIM2Tick >> 24);
  302. bufferTim2[1] = (uint8_t)(TIM2Tick >> 16);
  303. bufferTim2[2] = (uint8_t)(TIM2Tick >> 8);
  304. bufferTim2[3] = (uint8_t)TIM2Tick;
  305. CRC32 = crc32_calc_buffer(CRC32, bufferTim2, 4);
  306. tickCounter++;
  307. if(tickCounter == 8) {
  308. if(range == 0) {
  309. uint8_t localDice = CRC32 & 0b1;
  310. diceBuffer[diceBufferPositionWrite] = localDice;
  311. diceBufferCounter++;
  312. if(diceBufferPositionWrite != 63)
  313. diceBufferPositionWrite++;
  314. else
  315. diceBufferPositionWrite = 0;
  316. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  317. mutexVal.diceAvailiable = diceBufferCounter;
  318. furi_mutex_release(mutexVal.mutex);
  319. screenRefresh = 1;
  320. } else if(range == 1) {
  321. uint8_t localDice = CRC32 & 0b111;
  322. if(localDice == 0 || localDice == 7) {
  323. localDice = (diceBuffer[diceBufferPositionRead] >> 3) & 0b111;
  324. }
  325. if(localDice >= 1 && localDice <= 6) {
  326. diceBuffer[diceBufferPositionWrite] = localDice;
  327. diceBufferCounter++;
  328. if(diceBufferPositionWrite != 63)
  329. diceBufferPositionWrite++;
  330. else
  331. diceBufferPositionWrite = 0;
  332. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  333. mutexVal.diceAvailiable = diceBufferCounter;
  334. furi_mutex_release(mutexVal.mutex);
  335. screenRefresh = 1;
  336. }
  337. }
  338. CRC32 = 0;
  339. tickCounter = 0;
  340. }
  341. }
  342. // MD5
  343. else {
  344. uint32_t tick = TIM2->CNT;
  345. bufferTim2[0] = (uint8_t)(tick >> 24);
  346. bufferTim2[1] = (uint8_t)(tick >> 16);
  347. bufferTim2[2] = (uint8_t)(tick >> 8);
  348. bufferTim2[3] = (uint8_t)tick;
  349. mbedtls_md5_update(&md5_ctx, bufferTim2, 4);
  350. tickCounter++;
  351. if(tickCounter == 32) {
  352. mbedtls_md5_finish(&md5_ctx, hash);
  353. uint8_t localDice = 0;
  354. for(uint8_t i = 0; i < 16; i++) {
  355. if(range == 0) {
  356. localDice = hash[i] & 0b1;
  357. diceBuffer[diceBufferPositionWrite] = localDice;
  358. diceBufferCounter++;
  359. if(diceBufferPositionWrite != 63)
  360. diceBufferPositionWrite++;
  361. else
  362. diceBufferPositionWrite = 0;
  363. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  364. mutexVal.diceAvailiable = diceBufferCounter;
  365. furi_mutex_release(mutexVal.mutex);
  366. screenRefresh = 1;
  367. break;
  368. } else if(range == 1) {
  369. localDice = hash[i] & 0b111;
  370. if(localDice >= 1 && localDice <= 6) {
  371. diceBuffer[diceBufferPositionWrite] = localDice;
  372. diceBufferCounter++;
  373. if(diceBufferPositionWrite != 63)
  374. diceBufferPositionWrite++;
  375. else
  376. diceBufferPositionWrite = 0;
  377. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  378. mutexVal.diceAvailiable = diceBufferCounter;
  379. furi_mutex_release(mutexVal.mutex);
  380. screenRefresh = 1;
  381. break;
  382. }
  383. }
  384. }
  385. mbedtls_md5_starts(&md5_ctx);
  386. tickCounter = 0;
  387. }
  388. }
  389. }
  390. counter++;
  391. }
  392. }
  393. if(screenRefresh == 1) view_port_update(view_port);
  394. }
  395. LL_TIM_DisableCounter(TIM2);
  396. furi_hal_bus_disable(FuriHalBusTIM2);
  397. mbedtls_md5_free(&md5_ctx);
  398. //free(md5_ctx);
  399. free(bufferTim2);
  400. free(hash);
  401. furi_record_close(RECORD_NOTIFICATION);
  402. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  403. furi_hal_power_disable_otg();
  404. }
  405. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  406. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  407. furi_hal_gpio_init_simple(&gpio_ext_pa7, GpioModeAnalog);
  408. furi_message_queue_free(event_queue);
  409. furi_mutex_free(mutexVal.mutex);
  410. gui_remove_view_port(gui, view_port);
  411. view_port_free(view_port);
  412. furi_timer_free(timer);
  413. furi_timer_free(timerPause);
  414. furi_record_close(RECORD_GUI);
  415. expansion_enable(expansion);
  416. furi_record_close(RECORD_EXPANSION);
  417. return 0;
  418. }