flipper_geiger.c 9.1 KB

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