flipper_geiger.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 / 2];
  32. float coef;
  33. uint8_t data;
  34. } mutexStruct;
  35. static void draw_callback(Canvas* canvas, void* ctx) {
  36. furi_assert(ctx);
  37. mutexStruct displayStruct;
  38. mutexStruct* geigerMutex = ctx;
  39. furi_mutex_acquire(geigerMutex->mutex, FuriWaitForever);
  40. memcpy(&displayStruct, geigerMutex, sizeof(mutexStruct));
  41. furi_mutex_release(geigerMutex->mutex);
  42. char buffer[32];
  43. if(displayStruct.data == 0)
  44. snprintf(
  45. buffer, sizeof(buffer), "%ld cps - %ld cpm", displayStruct.cps, displayStruct.cpm);
  46. else if(displayStruct.data == 1)
  47. snprintf(
  48. buffer,
  49. sizeof(buffer),
  50. "%ld cps - %.2f uSv/h",
  51. displayStruct.cps,
  52. ((double)displayStruct.cpm * (double)CONVERSION_FACTOR));
  53. else
  54. snprintf(
  55. buffer,
  56. sizeof(buffer),
  57. "%ld cps - %.2f mSv/y",
  58. displayStruct.cps,
  59. (((double)displayStruct.cpm * (double)CONVERSION_FACTOR)) * (double)8.76);
  60. for(int i = 0; i < SCREEN_SIZE_X; i += 2) {
  61. float Y = SCREEN_SIZE_Y - (displayStruct.line[i / 2] * displayStruct.coef);
  62. canvas_draw_line(canvas, i, Y, i, SCREEN_SIZE_Y);
  63. canvas_draw_line(canvas, i + 1, Y, i + 1, SCREEN_SIZE_Y);
  64. }
  65. canvas_set_font(canvas, FontPrimary);
  66. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, buffer);
  67. }
  68. static void input_callback(InputEvent* input_event, void* ctx) {
  69. furi_assert(ctx);
  70. FuriMessageQueue* event_queue = ctx;
  71. EventApp event = {.type = EventTypeInput, .input = *input_event};
  72. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  73. }
  74. static void clock_tick(void* ctx) {
  75. furi_assert(ctx);
  76. uint32_t randomNumber = furi_hal_random_get();
  77. randomNumber &= 0xFFF;
  78. if(randomNumber == 0) randomNumber = 1;
  79. furi_hal_pwm_set_params(FuriHalPwmOutputIdLptim2PA4, randomNumber, 50);
  80. FuriMessageQueue* queue = ctx;
  81. EventApp event = {.type = ClockEventTypeTick};
  82. furi_message_queue_put(queue, &event, 0);
  83. }
  84. static void gpiocallback(void* ctx) {
  85. furi_assert(ctx);
  86. FuriMessageQueue* queue = ctx;
  87. EventApp event = {.type = EventGPIO};
  88. furi_message_queue_put(queue, &event, 0);
  89. }
  90. int32_t flipper_geiger_app(void* p) {
  91. UNUSED(p);
  92. EventApp event;
  93. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  94. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  95. furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, 5, 50);
  96. mutexStruct mutexVal;
  97. mutexVal.cps = 0;
  98. mutexVal.cpm = 0;
  99. for(int i = 0; i < SCREEN_SIZE_X / 2; i++) mutexVal.line[i] = 0;
  100. mutexVal.coef = 1;
  101. mutexVal.data = 0;
  102. uint32_t counter = 0;
  103. mutexVal.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  104. if(!mutexVal.mutex) {
  105. furi_message_queue_free(event_queue);
  106. return 255;
  107. }
  108. ViewPort* view_port = view_port_alloc();
  109. view_port_draw_callback_set(view_port, draw_callback, &mutexVal);
  110. view_port_input_callback_set(view_port, input_callback, event_queue);
  111. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  112. Gui* gui = furi_record_open(RECORD_GUI);
  113. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  114. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  115. furi_timer_start(timer, 1000);
  116. // ENABLE 5V pin
  117. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  118. uint8_t attempts = 0;
  119. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  120. furi_hal_power_enable_otg();
  121. furi_delay_ms(10);
  122. }
  123. Storage* storage = furi_record_open(RECORD_STORAGE);
  124. Stream* file_stream = buffered_file_stream_alloc(storage);
  125. FuriString* dataString = furi_string_alloc();
  126. uint32_t epoch = 0;
  127. uint8_t recordData = 0;
  128. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  129. while(1) {
  130. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  131. uint8_t screenRefresh = 0;
  132. if(event_status == FuriStatusOk) {
  133. if(event.type == EventTypeInput) {
  134. if(event.input.key == InputKeyBack) {
  135. break;
  136. } else if(event.input.key == InputKeyOk && event.input.type == InputTypeShort) {
  137. counter = 0;
  138. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  139. mutexVal.cps = 0;
  140. mutexVal.cpm = 0;
  141. for(int i = 0; i < SCREEN_SIZE_X / 2; i++) mutexVal.line[i] = 0;
  142. screenRefresh = 1;
  143. furi_mutex_release(mutexVal.mutex);
  144. } else if(event.input.key == InputKeyUp && event.input.type == InputTypeLong) {
  145. if(recordData == 0) {
  146. notification_message(notification, &sequence_set_only_red_255);
  147. FuriHalRtcDateTime datetime;
  148. furi_hal_rtc_get_datetime(&datetime);
  149. char path[64];
  150. snprintf(
  151. path,
  152. sizeof(path),
  153. EXT_PATH("/geiger-%.4d-%.2d-%.2d--%.2d-%.2d-%.2d.csv"),
  154. datetime.year,
  155. datetime.month,
  156. datetime.day,
  157. datetime.hour,
  158. datetime.minute,
  159. datetime.second);
  160. buffered_file_stream_open(
  161. file_stream, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  162. furi_string_printf(dataString, "epoch,cps\n");
  163. stream_write_string(file_stream, dataString);
  164. epoch = 0;
  165. recordData = 1;
  166. } else {
  167. buffered_file_stream_close(file_stream);
  168. notification_message(notification, &sequence_reset_red);
  169. recordData = 0;
  170. }
  171. } else if((event.input.key == InputKeyLeft &&
  172. event.input.type == InputTypeShort)) {
  173. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  174. if(mutexVal.data != 0)
  175. mutexVal.data--;
  176. else
  177. mutexVal.data = 2;
  178. screenRefresh = 1;
  179. furi_mutex_release(mutexVal.mutex);
  180. } else if((event.input.key == InputKeyRight &&
  181. event.input.type == InputTypeShort)) {
  182. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  183. if(mutexVal.data != 2)
  184. mutexVal.data++;
  185. else
  186. mutexVal.data = 0;
  187. screenRefresh = 1;
  188. furi_mutex_release(mutexVal.mutex);
  189. }
  190. } else if(event.type == ClockEventTypeTick) {
  191. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  192. if(recordData == 1) {
  193. furi_string_printf(dataString, "%lu,%lu\n", epoch++, counter);
  194. stream_write_string(file_stream, dataString);
  195. }
  196. for(int i = 0; i < SCREEN_SIZE_X / 2 - 1; i++)
  197. mutexVal.line[SCREEN_SIZE_X / 2 - 1 - i] =
  198. mutexVal.line[SCREEN_SIZE_X / 2 - 2 - i];
  199. mutexVal.line[0] = counter;
  200. mutexVal.cps = counter;
  201. counter = 0;
  202. mutexVal.cpm = mutexVal.line[0];
  203. uint32_t max = mutexVal.line[0];
  204. for(int i = 1; i < SCREEN_SIZE_X / 2; i++) {
  205. if(i < 60) mutexVal.cpm += mutexVal.line[i];
  206. if(mutexVal.line[i] > max) max = mutexVal.line[i];
  207. }
  208. if(max > 0)
  209. mutexVal.coef = ((float)(SCREEN_SIZE_Y - 15)) / ((float)max);
  210. else
  211. mutexVal.coef = 1;
  212. screenRefresh = 1;
  213. furi_mutex_release(mutexVal.mutex);
  214. } else if(event.type == EventGPIO) {
  215. counter++;
  216. }
  217. }
  218. if(screenRefresh == 1) view_port_update(view_port);
  219. }
  220. if(recordData == 1) {
  221. buffered_file_stream_close(file_stream);
  222. notification_message(notification, &sequence_reset_red);
  223. }
  224. furi_string_free(dataString);
  225. furi_record_close(RECORD_NOTIFICATION);
  226. stream_free(file_stream);
  227. furi_record_close(RECORD_STORAGE);
  228. // Disable 5v power
  229. if(furi_hal_power_is_otg_enabled()) {
  230. furi_hal_power_disable_otg();
  231. }
  232. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  233. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  234. furi_hal_pwm_stop(FuriHalPwmOutputIdLptim2PA4);
  235. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeAnalog, GpioPullNo, GpioSpeedLow);
  236. furi_message_queue_free(event_queue);
  237. furi_mutex_free(mutexVal.mutex);
  238. gui_remove_view_port(gui, view_port);
  239. view_port_free(view_port);
  240. furi_timer_free(timer);
  241. furi_record_close(RECORD_GUI);
  242. return 0;
  243. }