flipper_atomicdiceroller.c 18 KB

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