flipper_geiger.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_random.h>
  10. #include <furi_hal_pwm.h>
  11. #define SCREEN_SIZE_X 128
  12. #define SCREEN_SIZE_Y 64
  13. typedef enum {
  14. EventTypeInput,
  15. ClockEventTypeTick,
  16. EventGPIO,
  17. } EventType;
  18. typedef struct {
  19. EventType type;
  20. InputEvent input;
  21. } EventApp;
  22. typedef struct {
  23. uint32_t counter;
  24. uint32_t cps, cpm;
  25. uint8_t line[SCREEN_SIZE_X/2];
  26. float coef;
  27. } mutexStruct;
  28. static void draw_callback(Canvas* canvas, void* ctx)
  29. {
  30. UNUSED(ctx);
  31. mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block((ValueMutex*)ctx);
  32. char buffer[32];
  33. snprintf(buffer, sizeof(buffer), "%ld cps - %ld cpm", lfsrMutex->cps, lfsrMutex->cpm);
  34. for (int i=0;i<SCREEN_SIZE_X;i+=2)
  35. {
  36. float Y = SCREEN_SIZE_Y-(lfsrMutex->line[i/2]*lfsrMutex->coef);
  37. if (Y < 0) Y = 0;
  38. canvas_draw_line(canvas, i, Y, i, SCREEN_SIZE_Y);
  39. canvas_draw_line(canvas, i+1, Y, i+1, SCREEN_SIZE_Y);
  40. }
  41. canvas_set_font(canvas, FontPrimary);
  42. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, buffer);
  43. release_mutex((ValueMutex*)ctx, lfsrMutex);
  44. }
  45. static void input_callback(InputEvent* input_event, void* ctx)
  46. {
  47. furi_assert(ctx);
  48. FuriMessageQueue* event_queue = ctx;
  49. EventApp event = {.type = EventTypeInput, .input = *input_event};
  50. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  51. }
  52. static void clock_tick(void* ctx) {
  53. furi_assert(ctx);
  54. uint8_t randomuint8[1];
  55. furi_hal_random_fill_buf(randomuint8,1);
  56. randomuint8[0] &= 0b00001111;
  57. furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, randomuint8[0], 20);
  58. FuriMessageQueue* queue = ctx;
  59. EventApp event = {.type = ClockEventTypeTick};
  60. furi_message_queue_put(queue, &event, 0);
  61. }
  62. static void gpiocallback(void* ctx) {
  63. furi_assert(ctx);
  64. FuriMessageQueue* queue = ctx;
  65. EventApp event = {.type = EventGPIO};
  66. furi_message_queue_put(queue, &event, 0);
  67. }
  68. int32_t flipper_geiger_app()
  69. {
  70. EventApp event;
  71. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  72. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  73. furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, 5, 20);
  74. mutexStruct mutexVal;
  75. mutexVal.counter = 0;
  76. mutexVal.cps = 0;
  77. mutexVal.cpm = 0;
  78. for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
  79. mutexVal.coef = 1;
  80. ValueMutex state_mutex;
  81. init_mutex(&state_mutex, &mutexVal, sizeof(mutexVal));
  82. ViewPort* view_port = view_port_alloc();
  83. view_port_draw_callback_set(view_port, draw_callback, &state_mutex);
  84. view_port_input_callback_set(view_port, input_callback, event_queue);
  85. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  86. Gui* gui = furi_record_open(RECORD_GUI);
  87. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  88. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  89. furi_timer_start(timer, 1000);
  90. while(1)
  91. {
  92. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  93. mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block(&state_mutex);
  94. if (event_status == FuriStatusOk)
  95. {
  96. if(event.type == EventTypeInput)
  97. {
  98. if(event.input.key == InputKeyBack)
  99. {
  100. release_mutex(&state_mutex, lfsrMutex);
  101. break;
  102. }
  103. else if(event.input.key == InputKeyOk && event.input.type == InputTypeShort)
  104. {
  105. mutexVal.counter = 0;
  106. mutexVal.cps = 0;
  107. mutexVal.cpm = 0;
  108. for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
  109. }
  110. }
  111. else if (event.type == ClockEventTypeTick)
  112. {
  113. for (int i=0;i<SCREEN_SIZE_X/2-1;i++) lfsrMutex->line[SCREEN_SIZE_X/2-1-i] = lfsrMutex->line[SCREEN_SIZE_X/2-2-i];
  114. lfsrMutex->line[0] = lfsrMutex->counter;
  115. lfsrMutex->cps = lfsrMutex->counter;
  116. lfsrMutex->counter = 0;
  117. lfsrMutex->cpm = 0;
  118. for (int i=0;i<60;i++) lfsrMutex->cpm += lfsrMutex->line[i];
  119. uint32_t max = lfsrMutex->line[0];
  120. for (int i=1;i<SCREEN_SIZE_X/2;i++)
  121. {
  122. if (lfsrMutex->line[i] > max) max = lfsrMutex->line[i];
  123. }
  124. if (max > 0)
  125. {
  126. lfsrMutex->coef = ((float)(SCREEN_SIZE_Y-15))/((float)max);
  127. }
  128. }
  129. else if (event.type == EventGPIO)
  130. {
  131. lfsrMutex->counter = lfsrMutex->counter + 1;
  132. }
  133. }
  134. release_mutex(&state_mutex, lfsrMutex);
  135. view_port_update(view_port);
  136. }
  137. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  138. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  139. furi_hal_pwm_stop(FuriHalPwmOutputIdLptim2PA4);
  140. furi_message_queue_free(event_queue);
  141. delete_mutex(&state_mutex);
  142. gui_remove_view_port(gui, view_port);
  143. view_port_free(view_port);
  144. furi_timer_free(timer);
  145. furi_record_close(RECORD_GUI);
  146. return 0;
  147. }