flipper_geiger.c 7.5 KB

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