flipper_geiger.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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++)
  167. mutexVal.line[i] = 0;
  168. mutexVal.coef = 1;
  169. mutexVal.data = 0;
  170. mutexVal.zoom = 2;
  171. mutexVal.newLinePosition = 0;
  172. mutexVal.version = 0;
  173. uint32_t counter = 0;
  174. mutexVal.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  175. if(!mutexVal.mutex) {
  176. furi_message_queue_free(event_queue);
  177. expansion_enable(expansion);
  178. furi_record_close(RECORD_EXPANSION);
  179. return 255;
  180. }
  181. ViewPort* view_port = view_port_alloc();
  182. view_port_draw_callback_set(view_port, draw_callback, &mutexVal.mutex);
  183. view_port_input_callback_set(view_port, input_callback, event_queue);
  184. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  185. furi_hal_gpio_enable_int_callback(&gpio_ext_pa7);
  186. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  187. Gui* gui = furi_record_open(RECORD_GUI);
  188. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  189. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  190. furi_timer_start(timer, 1000);
  191. // ENABLE 5V pin
  192. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  193. uint8_t attempts = 0;
  194. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  195. furi_hal_power_enable_otg();
  196. furi_delay_ms(10);
  197. }
  198. Storage* storage = furi_record_open(RECORD_STORAGE);
  199. Stream* file_stream = buffered_file_stream_alloc(storage);
  200. FuriString* dataString = furi_string_alloc();
  201. uint32_t epoch = 0;
  202. uint8_t recordData = 0;
  203. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  204. while(1) {
  205. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  206. uint8_t screenRefresh = 0;
  207. if(event_status == FuriStatusOk) {
  208. if(event.type == EventTypeInput) {
  209. if(event.input.key == InputKeyBack &&
  210. (event.input.type == InputTypeShort || event.input.type == InputTypeLong)) {
  211. break;
  212. } else if(event.input.key == InputKeyOk && event.input.type == InputTypeLong) {
  213. counter = 0;
  214. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  215. mutexVal.cps = 0;
  216. mutexVal.cpm = 0;
  217. for(uint8_t i = 0; i < SCREEN_SIZE_X; i++)
  218. mutexVal.line[i] = 0;
  219. mutexVal.newLinePosition = 0;
  220. screenRefresh = 1;
  221. furi_mutex_release(mutexVal.mutex);
  222. } else if(event.input.key == InputKeyUp && event.input.type == InputTypeLong) {
  223. if(recordData == 0) {
  224. notification_message(notification, &sequence_set_only_red_255);
  225. DateTime datetime;
  226. furi_hal_rtc_get_datetime(&datetime);
  227. char path[64];
  228. snprintf(
  229. path,
  230. sizeof(path),
  231. EXT_PATH("/geiger-%.4d-%.2d-%.2d--%.2d-%.2d-%.2d.csv"),
  232. datetime.year,
  233. datetime.month,
  234. datetime.day,
  235. datetime.hour,
  236. datetime.minute,
  237. datetime.second);
  238. buffered_file_stream_open(
  239. file_stream, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  240. furi_string_printf(dataString, "epoch,cps\n");
  241. stream_write_string(file_stream, dataString);
  242. epoch = 0;
  243. recordData = 1;
  244. } else {
  245. buffered_file_stream_close(file_stream);
  246. notification_message(notification, &sequence_reset_red);
  247. recordData = 0;
  248. }
  249. } else if((event.input.key == InputKeyLeft &&
  250. event.input.type == InputTypeShort)) {
  251. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  252. if(mutexVal.data != 0)
  253. mutexVal.data--;
  254. else
  255. mutexVal.data = 5;
  256. screenRefresh = 1;
  257. furi_mutex_release(mutexVal.mutex);
  258. } else if((event.input.key == InputKeyRight &&
  259. event.input.type == InputTypeShort)) {
  260. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  261. if(mutexVal.data != 5)
  262. mutexVal.data++;
  263. else
  264. mutexVal.data = 0;
  265. screenRefresh = 1;
  266. furi_mutex_release(mutexVal.mutex);
  267. } else if((event.input.key == InputKeyUp && event.input.type == InputTypeShort)) {
  268. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  269. if(mutexVal.zoom != 0) mutexVal.zoom--;
  270. screenRefresh = 1;
  271. furi_mutex_release(mutexVal.mutex);
  272. } else if((event.input.key == InputKeyDown &&
  273. event.input.type == InputTypeShort)) {
  274. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  275. if(mutexVal.zoom != 3) mutexVal.zoom++;
  276. screenRefresh = 1;
  277. furi_mutex_release(mutexVal.mutex);
  278. } else if((event.input.key == InputKeyDown && event.input.type == InputTypeLong)) {
  279. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  280. if(mutexVal.version == 0)
  281. mutexVal.version = 1;
  282. else
  283. mutexVal.version = 0;
  284. screenRefresh = 1;
  285. furi_mutex_release(mutexVal.mutex);
  286. }
  287. } else if(event.type == ClockEventTypeTick) {
  288. if(recordData == 1) {
  289. furi_string_printf(dataString, "%lu,%lu\n", epoch++, counter);
  290. stream_write_string(file_stream, dataString);
  291. }
  292. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  293. mutexVal.line[mutexVal.newLinePosition] = counter;
  294. mutexVal.cps = counter;
  295. counter = 0;
  296. mutexVal.cpm = mutexVal.line[mutexVal.newLinePosition];
  297. uint32_t max = mutexVal.line[mutexVal.newLinePosition];
  298. uint8_t linePosition = mutexVal.newLinePosition;
  299. for(int i = 1; i < SCREEN_SIZE_X; i++) {
  300. if(linePosition != 0)
  301. linePosition--;
  302. else
  303. linePosition = SCREEN_SIZE_X - 1;
  304. if(i < 60) mutexVal.cpm += mutexVal.line[linePosition];
  305. if(mutexVal.line[linePosition] > max) max = mutexVal.line[linePosition];
  306. }
  307. if(max > 0)
  308. mutexVal.coef = ((float)(SCREEN_SIZE_Y - 15)) / ((float)max);
  309. else
  310. mutexVal.coef = 1;
  311. if(mutexVal.newLinePosition != SCREEN_SIZE_X - 1)
  312. mutexVal.newLinePosition++;
  313. else
  314. mutexVal.newLinePosition = 0;
  315. screenRefresh = 1;
  316. furi_mutex_release(mutexVal.mutex);
  317. } else if(event.type == EventGPIO) {
  318. counter++;
  319. }
  320. }
  321. if(screenRefresh == 1) view_port_update(view_port);
  322. }
  323. if(recordData == 1) {
  324. buffered_file_stream_close(file_stream);
  325. notification_message(notification, &sequence_reset_red);
  326. }
  327. furi_string_free(dataString);
  328. furi_record_close(RECORD_NOTIFICATION);
  329. stream_free(file_stream);
  330. furi_record_close(RECORD_STORAGE);
  331. // Disable 5v power
  332. if(furi_hal_power_is_otg_enabled()) {
  333. furi_hal_power_disable_otg();
  334. }
  335. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  336. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  337. furi_hal_pwm_stop(FuriHalPwmOutputIdLptim2PA4);
  338. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  339. furi_message_queue_free(event_queue);
  340. furi_mutex_free(mutexVal.mutex);
  341. gui_remove_view_port(gui, view_port);
  342. view_port_free(view_port);
  343. furi_timer_free(timer);
  344. furi_record_close(RECORD_GUI);
  345. expansion_enable(expansion);
  346. furi_record_close(RECORD_EXPANSION);
  347. return 0;
  348. }