flipper_atomicdiceroller.c 18 KB

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