flipper_geiger.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. #include <storage/storage.h>
  13. #include <stream/buffered_file_stream.h>
  14. #include <locale/locale.h>
  15. #define SCREEN_SIZE_X 128
  16. #define SCREEN_SIZE_Y 64
  17. // FOR J305 GEIGER TUBE
  18. #define CONVERSION_FACTOR 0.0081
  19. typedef enum {
  20. EventTypeInput,
  21. ClockEventTypeTick,
  22. EventGPIO,
  23. } EventType;
  24. typedef struct {
  25. EventType type;
  26. InputEvent input;
  27. } EventApp;
  28. #define lineArraySize 128
  29. typedef struct {
  30. FuriMutex* mutex;
  31. uint32_t cps, cpm;
  32. uint32_t line[lineArraySize];
  33. float coef;
  34. uint8_t data;
  35. uint8_t zoom;
  36. } mutexStruct;
  37. static void draw_callback(Canvas* canvas, void* ctx)
  38. {
  39. furi_assert(ctx);
  40. mutexStruct* mutexVal = ctx;
  41. mutexStruct mutexDraw;
  42. furi_mutex_acquire(mutexVal->mutex, FuriWaitForever);
  43. memcpy(&mutexDraw, mutexVal, sizeof(mutexStruct));
  44. furi_mutex_release(mutexVal->mutex);
  45. char buffer[32];
  46. if (mutexDraw.data == 0) snprintf(buffer, sizeof(buffer), "%ld cps - %ld cpm", mutexDraw.cps, mutexDraw.cpm);
  47. else if (mutexDraw.data == 1) snprintf(buffer, sizeof(buffer), "%ld cps - %.2f uSv/h", mutexDraw.cps, ((double)mutexDraw.cpm*(double)CONVERSION_FACTOR));
  48. 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);
  49. else if (mutexDraw.data == 3) snprintf(buffer, sizeof(buffer), "%ld cps - %.4f Rad/h", mutexDraw.cps, ((double)mutexDraw.cpm*(double)CONVERSION_FACTOR)/(double)10000);
  50. else if (mutexDraw.data == 4) snprintf(buffer, sizeof(buffer), "%ld cps - %.2f mR/h", mutexDraw.cps, ((double)mutexDraw.cpm*(double)CONVERSION_FACTOR)/(double)10);
  51. else snprintf(buffer, sizeof(buffer), "%ld cps - %.2f uR/h", mutexDraw.cps, ((double)mutexDraw.cpm*(double)CONVERSION_FACTOR)*(double)100);
  52. if (mutexDraw.zoom == 0)
  53. {
  54. for (int i=0;i<SCREEN_SIZE_X;i+=8)
  55. {
  56. float Y = SCREEN_SIZE_Y-(mutexDraw.line[i/8]*mutexDraw.coef);
  57. for (int j=0;j<8;j++)canvas_draw_line(canvas, i+j, Y, i+j, SCREEN_SIZE_Y);
  58. }
  59. }
  60. else if (mutexDraw.zoom == 1)
  61. {
  62. for (int i=0;i<SCREEN_SIZE_X;i+=4)
  63. {
  64. float Y = SCREEN_SIZE_Y-(mutexDraw.line[i/4]*mutexDraw.coef);
  65. for (int j=0;j<4;j++)canvas_draw_line(canvas, i+j, Y, i+j, SCREEN_SIZE_Y);
  66. }
  67. }
  68. else if (mutexDraw.zoom == 2)
  69. {
  70. for (int i=0;i<SCREEN_SIZE_X;i+=2)
  71. {
  72. float Y = SCREEN_SIZE_Y-(mutexDraw.line[i/2]*mutexDraw.coef);
  73. for (int j=0;j<2;j++)canvas_draw_line(canvas, i+j, Y, i+j, SCREEN_SIZE_Y);
  74. }
  75. }
  76. else if (mutexDraw.zoom == 3)
  77. {
  78. for (int i=0;i<SCREEN_SIZE_X;i++)
  79. {
  80. float Y = SCREEN_SIZE_Y-(mutexDraw.line[i]*mutexDraw.coef);
  81. canvas_draw_line(canvas, i, Y, i, SCREEN_SIZE_Y);
  82. }
  83. }
  84. canvas_set_font(canvas, FontPrimary);
  85. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, buffer);
  86. }
  87. static void input_callback(InputEvent* input_event, void* ctx)
  88. {
  89. furi_assert(ctx);
  90. FuriMessageQueue* event_queue = ctx;
  91. EventApp event = {.type = EventTypeInput, .input = *input_event};
  92. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  93. }
  94. static void clock_tick(void* ctx) {
  95. furi_assert(ctx);
  96. uint32_t randomNumber = furi_hal_random_get();
  97. randomNumber &= 0xFFF;
  98. if (randomNumber == 0) randomNumber = 1;
  99. furi_hal_pwm_set_params(FuriHalPwmOutputIdLptim2PA4, randomNumber, 50);
  100. FuriMessageQueue* queue = ctx;
  101. EventApp event = {.type = ClockEventTypeTick};
  102. furi_message_queue_put(queue, &event, 0);
  103. }
  104. static void gpiocallback(void* ctx) {
  105. furi_assert(ctx);
  106. FuriMessageQueue* queue = ctx;
  107. EventApp event = {.type = EventGPIO};
  108. furi_message_queue_put(queue, &event, 0);
  109. }
  110. int32_t flipper_geiger_app()
  111. {
  112. EventApp event;
  113. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  114. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  115. furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, 5, 50);
  116. mutexStruct mutexVal;
  117. mutexVal.cps = 0;
  118. mutexVal.cpm = 0;
  119. for (int i=0;i<lineArraySize;i++) mutexVal.line[i] = 0;
  120. mutexVal.coef = 1;
  121. mutexVal.data = 0;
  122. mutexVal.zoom = 2;
  123. uint32_t counter = 0;
  124. mutexVal.mutex= furi_mutex_alloc(FuriMutexTypeNormal);
  125. if(!mutexVal.mutex) {
  126. furi_message_queue_free(event_queue);
  127. return 255;
  128. }
  129. ViewPort* view_port = view_port_alloc();
  130. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  131. view_port_input_callback_set(view_port, input_callback, event_queue);
  132. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  133. Gui* gui = furi_record_open(RECORD_GUI);
  134. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  135. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  136. furi_timer_start(timer, 1000);
  137. // ENABLE 5V pin
  138. furi_hal_power_enable_otg();
  139. Storage* storage = furi_record_open(RECORD_STORAGE);
  140. Stream* file_stream = buffered_file_stream_alloc(storage);
  141. FuriString* dataString = furi_string_alloc();
  142. uint32_t epoch = 0;
  143. uint8_t recordData = 0;
  144. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  145. while(1)
  146. {
  147. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  148. uint8_t screenRefresh = 0;
  149. if (event_status == FuriStatusOk)
  150. {
  151. if(event.type == EventTypeInput)
  152. {
  153. if(event.input.key == InputKeyBack && event.input.type == InputTypeLong)
  154. {
  155. break;
  156. }
  157. else if(event.input.key == InputKeyOk && event.input.type == InputTypeLong)
  158. {
  159. counter = 0;
  160. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  161. mutexVal.cps = 0;
  162. mutexVal.cpm = 0;
  163. for (int i=0;i<lineArraySize;i++) mutexVal.line[i] = 0;
  164. screenRefresh = 1;
  165. furi_mutex_release(mutexVal.mutex);
  166. }
  167. else if(event.input.key == InputKeyUp && event.input.type == InputTypeLong)
  168. {
  169. if (recordData == 0)
  170. {
  171. notification_message(notification, &sequence_set_only_red_255);
  172. FuriHalRtcDateTime datetime;
  173. furi_hal_rtc_get_datetime(&datetime);
  174. char path[64];
  175. snprintf(path, sizeof(path), EXT_PATH("/geiger-%.4d-%.2d-%.2d--%.2d-%.2d-%.2d.csv"), datetime.year, datetime.month, datetime.day, datetime.hour, datetime.minute, datetime.second);
  176. buffered_file_stream_open(file_stream, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  177. furi_string_printf(dataString, "epoch,cps\n");
  178. stream_write_string(file_stream, dataString);
  179. epoch = 0;
  180. recordData = 1;
  181. }
  182. else
  183. {
  184. buffered_file_stream_close(file_stream);
  185. notification_message(notification, &sequence_reset_red);
  186. recordData = 0;
  187. }
  188. }
  189. else if((event.input.key == InputKeyLeft && event.input.type == InputTypeShort))
  190. {
  191. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  192. if (mutexVal.data != 0) mutexVal.data--;
  193. else mutexVal.data = 5;
  194. screenRefresh = 1;
  195. furi_mutex_release(mutexVal.mutex);
  196. }
  197. else if((event.input.key == InputKeyRight && event.input.type == InputTypeShort))
  198. {
  199. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  200. if (mutexVal.data != 5) mutexVal.data++;
  201. else mutexVal.data = 0;
  202. screenRefresh = 1;
  203. furi_mutex_release(mutexVal.mutex);
  204. }
  205. else if((event.input.key == InputKeyUp && event.input.type == InputTypeShort))
  206. {
  207. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  208. if (mutexVal.zoom != 0) mutexVal.zoom--;
  209. screenRefresh = 1;
  210. furi_mutex_release(mutexVal.mutex);
  211. }
  212. else if((event.input.key == InputKeyDown && event.input.type == InputTypeShort))
  213. {
  214. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  215. if (mutexVal.zoom != 3) mutexVal.zoom++;
  216. screenRefresh = 1;
  217. furi_mutex_release(mutexVal.mutex);
  218. }
  219. }
  220. else if (event.type == ClockEventTypeTick)
  221. {
  222. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  223. if (recordData == 1)
  224. {
  225. furi_string_printf(dataString, "%lu,%lu\n", epoch++, counter);
  226. stream_write_string(file_stream, dataString);
  227. }
  228. for (int i=0;i<lineArraySize-1;i++) mutexVal.line[lineArraySize-1-i] = mutexVal.line[lineArraySize-2-i];
  229. mutexVal.line[0] = counter;
  230. mutexVal.cps = counter;
  231. counter = 0;
  232. mutexVal.cpm = mutexVal.line[0];
  233. uint32_t max = mutexVal.line[0];
  234. for (int i=1;i<lineArraySize;i++)
  235. {
  236. if (i < 60) mutexVal.cpm += mutexVal.line[i];
  237. if (mutexVal.line[i] > max) max = mutexVal.line[i];
  238. }
  239. if (max > 0) mutexVal.coef = ((float)(SCREEN_SIZE_Y-15))/((float)max);
  240. else mutexVal.coef = 1;
  241. screenRefresh = 1;
  242. furi_mutex_release(mutexVal.mutex);
  243. }
  244. else if (event.type == EventGPIO)
  245. {
  246. counter++;
  247. }
  248. }
  249. if (screenRefresh == 1) view_port_update(view_port);
  250. }
  251. if (recordData == 1)
  252. {
  253. buffered_file_stream_close(file_stream);
  254. notification_message(notification, &sequence_reset_red);
  255. }
  256. furi_string_free(dataString);
  257. furi_record_close(RECORD_NOTIFICATION);
  258. stream_free(file_stream);
  259. furi_record_close(RECORD_STORAGE);
  260. furi_hal_power_disable_otg();
  261. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  262. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  263. furi_hal_pwm_stop(FuriHalPwmOutputIdLptim2PA4);
  264. furi_message_queue_free(event_queue);
  265. furi_mutex_free(mutexVal.mutex);
  266. gui_remove_view_port(gui, view_port);
  267. view_port_free(view_port);
  268. furi_timer_free(timer);
  269. furi_record_close(RECORD_GUI);
  270. return 0;
  271. }