flipper_geiger.c 15 KB

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