flipper_geiger.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 cps, cpm;
  24. uint32_t line[SCREEN_SIZE_X/2];
  25. float coef;
  26. } mutexStruct;
  27. static void draw_callback(Canvas* canvas, void* ctx)
  28. {
  29. UNUSED(ctx);
  30. mutexStruct displayStruct;
  31. mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block((ValueMutex*)ctx);
  32. memcpy(&displayStruct, lfsrMutex, sizeof(mutexStruct));
  33. release_mutex((ValueMutex*)ctx, lfsrMutex);
  34. char buffer[32];
  35. snprintf(buffer, sizeof(buffer), "%ld cps - %ld cpm", displayStruct.cps, displayStruct.cpm);
  36. for (int i=0;i<SCREEN_SIZE_X;i+=2)
  37. {
  38. float Y = SCREEN_SIZE_Y-(displayStruct.line[i/2]*displayStruct.coef);
  39. canvas_draw_line(canvas, i, Y, i, SCREEN_SIZE_Y);
  40. canvas_draw_line(canvas, i+1, Y, i+1, SCREEN_SIZE_Y);
  41. }
  42. canvas_set_font(canvas, FontPrimary);
  43. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, buffer);
  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. uint32_t randomNumber = furi_hal_random_get();
  55. randomNumber &= 0xFFF;
  56. if (randomNumber == 0) randomNumber = 1;
  57. furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, randomNumber, 50);
  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.cps = 0;
  76. mutexVal.cpm = 0;
  77. for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
  78. mutexVal.coef = 1;
  79. uint32_t counter = 0;
  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. uint8_t screenRefresh = 0;
  94. if (event_status == FuriStatusOk)
  95. {
  96. if(event.type == EventTypeInput)
  97. {
  98. if(event.input.key == InputKeyBack)
  99. {
  100. break;
  101. }
  102. else if(event.input.key == InputKeyOk && event.input.type == InputTypeShort)
  103. {
  104. counter = 0;
  105. mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block(&state_mutex);
  106. mutexVal.cps = 0;
  107. mutexVal.cpm = 0;
  108. for (int i=0;i<SCREEN_SIZE_X/2;i++) mutexVal.line[i] = 0;
  109. screenRefresh = 1;
  110. release_mutex(&state_mutex, lfsrMutex);
  111. }
  112. }
  113. else if (event.type == ClockEventTypeTick)
  114. {
  115. mutexStruct* lfsrMutex = (mutexStruct*)acquire_mutex_block(&state_mutex);
  116. 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];
  117. lfsrMutex->line[0] = counter;
  118. lfsrMutex->cps = counter;
  119. counter = 0;
  120. lfsrMutex->cpm = lfsrMutex->line[0];
  121. uint32_t max = lfsrMutex->line[0];
  122. for (int i=1;i<SCREEN_SIZE_X/2;i++)
  123. {
  124. if (i < 60) lfsrMutex->cpm += lfsrMutex->line[i];
  125. if (lfsrMutex->line[i] > max) max = lfsrMutex->line[i];
  126. }
  127. if (max > 0) lfsrMutex->coef = ((float)(SCREEN_SIZE_Y-15))/((float)max);
  128. else lfsrMutex->coef = 1;
  129. screenRefresh = 1;
  130. release_mutex(&state_mutex, lfsrMutex);
  131. }
  132. else if (event.type == EventGPIO)
  133. {
  134. counter++;
  135. }
  136. }
  137. if (screenRefresh == 1) view_port_update(view_port);
  138. }
  139. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  140. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  141. furi_hal_pwm_stop(FuriHalPwmOutputIdLptim2PA4);
  142. furi_message_queue_free(event_queue);
  143. delete_mutex(&state_mutex);
  144. gui_remove_view_port(gui, view_port);
  145. view_port_free(view_port);
  146. furi_timer_free(timer);
  147. furi_record_close(RECORD_GUI);
  148. return 0;
  149. }