flipper_atomicdiceroller.c 18 KB

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