flipper_geiger.c 14 KB

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