flipper_atomicdiceroller.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  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. Gui* gui = furi_record_open(RECORD_GUI);
  178. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  179. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  180. furi_timer_start(timer, 1000);
  181. FuriTimer* timerPause = furi_timer_alloc(clock_tick_pause, FuriTimerTypePeriodic, event_queue);
  182. // ENABLE 5V pin
  183. furi_hal_power_enable_otg();
  184. uint8_t diceBuffer[64];
  185. for (uint8_t i=0;i<64;i++) diceBuffer[i] = 0;
  186. uint8_t diceBufferCounter = 0;
  187. uint8_t diceBufferPositionWrite = 0;
  188. uint8_t diceBufferPositionRead = 0;
  189. uint8_t tickCounter = 0;
  190. uint32_t CRC32 = 0;
  191. uint8_t method = 0;
  192. uint8_t range = 0;
  193. // MD5
  194. md5_context* md5_ctx = malloc(sizeof(md5_context));
  195. uint8_t* hash = malloc(sizeof(uint8_t) * 16);
  196. uint8_t* bufferTim2 = malloc(4);
  197. md5_starts(md5_ctx);
  198. uint8_t pause = 0;
  199. while(1)
  200. {
  201. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  202. uint8_t screenRefresh = 0;
  203. if (event_status == FuriStatusOk)
  204. {
  205. if(event.type == EventTypeInput)
  206. {
  207. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong)
  208. {
  209. break;
  210. }
  211. else if (pause == 0)
  212. {
  213. if (event.input.key == InputKeyOk && event.input.type == InputTypeShort)
  214. {
  215. if (diceBufferCounter > 0)
  216. {
  217. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  218. mutexVal.dice = diceBuffer[diceBufferPositionRead];
  219. mutexVal.diceAvailiable = --diceBufferCounter;
  220. mutexVal.pause = 1;
  221. furi_mutex_release(mutexVal.mutex);
  222. if (diceBufferPositionRead != 63) diceBufferPositionRead++;
  223. else diceBufferPositionRead = 0;
  224. pause = 1;
  225. furi_timer_start(timerPause, 500);
  226. screenRefresh = 1;
  227. }
  228. }
  229. else if (event.input.key == InputKeyLeft && event.input.type == InputTypeLong)
  230. {
  231. if (method == 1)
  232. {
  233. method = 0;
  234. diceBufferPositionWrite = 0;
  235. diceBufferPositionRead = 0;
  236. diceBufferCounter = 0;
  237. CRC32 = 0;
  238. tickCounter = 0;
  239. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  240. mutexVal.method = 0;
  241. mutexVal.pause = 1;
  242. mutexVal.diceAvailiable = 0;
  243. mutexVal.tickCounter = 0;
  244. furi_mutex_release(mutexVal.mutex);
  245. screenRefresh = 1;
  246. }
  247. }
  248. else if (event.input.key == InputKeyRight && event.input.type == InputTypeLong)
  249. {
  250. if (method == 0)
  251. {
  252. method = 1;
  253. diceBufferPositionWrite = 0;
  254. diceBufferPositionRead = 0;
  255. diceBufferCounter = 0;
  256. md5_starts(md5_ctx);
  257. tickCounter = 0;
  258. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  259. mutexVal.method = 1;
  260. mutexVal.pause = 1;
  261. mutexVal.diceAvailiable = 0;
  262. mutexVal.tickCounter = 0;
  263. furi_mutex_release(mutexVal.mutex);
  264. screenRefresh = 1;
  265. }
  266. }
  267. else if (event.input.key == InputKeyUp && event.input.type == InputTypeLong)
  268. {
  269. if (range > 0)
  270. {
  271. range--;
  272. diceBufferPositionWrite = 0;
  273. diceBufferPositionRead = 0;
  274. diceBufferCounter = 0;
  275. md5_starts(md5_ctx);
  276. tickCounter = 0;
  277. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  278. mutexVal.pause = 1;
  279. mutexVal.diceAvailiable = 0;
  280. mutexVal.tickCounter = 0;
  281. mutexVal.range = range;
  282. furi_mutex_release(mutexVal.mutex);
  283. screenRefresh = 1;
  284. }
  285. }
  286. else if (event.input.key == InputKeyDown && event.input.type == InputTypeLong)
  287. {
  288. if (range < 1)
  289. {
  290. range++;
  291. diceBufferPositionWrite = 0;
  292. diceBufferPositionRead = 0;
  293. diceBufferCounter = 0;
  294. md5_starts(md5_ctx);
  295. tickCounter = 0;
  296. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  297. mutexVal.pause = 1;
  298. mutexVal.diceAvailiable = 0;
  299. mutexVal.tickCounter = 0;
  300. mutexVal.range = range;
  301. furi_mutex_release(mutexVal.mutex);
  302. screenRefresh = 1;
  303. }
  304. }
  305. }
  306. }
  307. else if (event.type == ClockEventTypeTick)
  308. {
  309. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  310. mutexVal.cps = counter;
  311. mutexVal.tickCounter = tickCounter;
  312. furi_mutex_release(mutexVal.mutex);
  313. counter = 0;
  314. screenRefresh = 1;
  315. }
  316. else if (event.type == ClockEventTypeTickPause)
  317. {
  318. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  319. mutexVal.pause = 0;
  320. furi_mutex_release(mutexVal.mutex);
  321. furi_timer_stop(timerPause);
  322. pause = 0;
  323. screenRefresh = 1;
  324. }
  325. else if (event.type == EventGPIO)
  326. {
  327. if (diceBufferCounter < 64)
  328. {
  329. // CRC32
  330. if (method == 0)
  331. {
  332. uint32_t TIM2Tick = TIM2->CNT;
  333. bufferTim2[0] = (uint8_t)(TIM2Tick >> 24);
  334. bufferTim2[1] = (uint8_t)(TIM2Tick >> 16);
  335. bufferTim2[2] = (uint8_t)(TIM2Tick >> 8);
  336. bufferTim2[3] = (uint8_t)TIM2Tick;
  337. CRC32 = crc32_calc_buffer(CRC32, bufferTim2, 4);
  338. tickCounter++;
  339. if (tickCounter == 8)
  340. {
  341. if (range == 0)
  342. {
  343. uint8_t localDice = CRC32 & 0b1;
  344. diceBuffer[diceBufferPositionWrite] = localDice;
  345. diceBufferCounter++;
  346. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  347. else diceBufferPositionWrite = 0;
  348. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  349. mutexVal.diceAvailiable = diceBufferCounter;
  350. furi_mutex_release(mutexVal.mutex);
  351. screenRefresh = 1;
  352. }
  353. else if (range == 1)
  354. {
  355. uint8_t localDice = CRC32 & 0b111;
  356. if (localDice == 0 || localDice == 7)
  357. {
  358. localDice = (diceBuffer[diceBufferPositionRead] >> 3) & 0b111;
  359. }
  360. if (localDice >= 1 && localDice <= 6)
  361. {
  362. diceBuffer[diceBufferPositionWrite] = localDice;
  363. diceBufferCounter++;
  364. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  365. else diceBufferPositionWrite = 0;
  366. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  367. mutexVal.diceAvailiable = diceBufferCounter;
  368. furi_mutex_release(mutexVal.mutex);
  369. screenRefresh = 1;
  370. }
  371. }
  372. CRC32 = 0;
  373. tickCounter = 0;
  374. }
  375. }
  376. // MD5
  377. else
  378. {
  379. uint32_t tick = TIM2->CNT;
  380. bufferTim2[0] = (uint8_t)(tick >> 24);
  381. bufferTim2[1] = (uint8_t)(tick >> 16);
  382. bufferTim2[2] = (uint8_t)(tick >> 8);
  383. bufferTim2[3] = (uint8_t)tick;
  384. md5_update(md5_ctx, bufferTim2, 4);
  385. tickCounter++;
  386. if (tickCounter == 32)
  387. {
  388. md5_finish(md5_ctx, hash);
  389. uint8_t localDice = 0;
  390. for (uint8_t i=0;i<16;i++)
  391. {
  392. if (range == 0)
  393. {
  394. localDice = hash[i] & 0b1;
  395. diceBuffer[diceBufferPositionWrite] = localDice;
  396. diceBufferCounter++;
  397. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  398. else diceBufferPositionWrite = 0;
  399. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  400. mutexVal.diceAvailiable = diceBufferCounter;
  401. furi_mutex_release(mutexVal.mutex);
  402. screenRefresh = 1;
  403. break;
  404. }
  405. else if (range == 1)
  406. {
  407. localDice = hash[i] & 0b111;
  408. if (localDice >= 1 && localDice <= 6)
  409. {
  410. diceBuffer[diceBufferPositionWrite] = localDice;
  411. diceBufferCounter++;
  412. if (diceBufferPositionWrite != 63) diceBufferPositionWrite++;
  413. else diceBufferPositionWrite = 0;
  414. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  415. mutexVal.diceAvailiable = diceBufferCounter;
  416. furi_mutex_release(mutexVal.mutex);
  417. screenRefresh = 1;
  418. break;
  419. }
  420. }
  421. }
  422. md5_starts(md5_ctx);
  423. tickCounter = 0;
  424. }
  425. }
  426. }
  427. counter++;
  428. }
  429. }
  430. if (screenRefresh == 1) view_port_update(view_port);
  431. }
  432. LL_TIM_DisableCounter(TIM2);
  433. furi_hal_bus_disable(FuriHalBusTIM2);
  434. free(md5_ctx);
  435. free(bufferTim2);
  436. free(hash);
  437. furi_record_close(RECORD_NOTIFICATION);
  438. furi_hal_power_disable_otg();
  439. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  440. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  441. furi_message_queue_free(event_queue);
  442. furi_mutex_free(mutexVal.mutex);
  443. gui_remove_view_port(gui, view_port);
  444. view_port_free(view_port);
  445. furi_timer_free(timer);
  446. furi_timer_free(timerPause);
  447. furi_record_close(RECORD_GUI);
  448. return 0;
  449. }