flipper_geiger.c 14 KB

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