flipper_geiger.c 15 KB

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