flipper_geiger.c 7.1 KB

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