flipper_geiger.c 13 KB

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