flipper_geiger.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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. #define SCREEN_SIZE_X 128
  13. #define SCREEN_SIZE_Y 64
  14. // FOR J305 GEIGER TUBE
  15. #define CONVERSION_FACTOR 0.0081
  16. typedef enum {
  17. EventTypeInput,
  18. ClockEventTypeTick,
  19. EventGPIO,
  20. } EventType;
  21. typedef struct {
  22. EventType type;
  23. InputEvent input;
  24. } EventApp;
  25. typedef struct {
  26. FuriMutex* mutex;
  27. uint32_t cps, cpm;
  28. uint32_t line[SCREEN_SIZE_X / 2];
  29. float coef;
  30. uint8_t data;
  31. } mutexStruct;
  32. static void draw_callback(Canvas* canvas, void* ctx) {
  33. furi_assert(ctx);
  34. mutexStruct displayStruct;
  35. mutexStruct* geigerMutex = ctx;
  36. furi_mutex_acquire(geigerMutex->mutex, FuriWaitForever);
  37. memcpy(&displayStruct, geigerMutex, sizeof(mutexStruct));
  38. furi_mutex_release(geigerMutex->mutex);
  39. char buffer[32];
  40. if(displayStruct.data == 0)
  41. snprintf(
  42. buffer, sizeof(buffer), "%ld cps - %ld cpm", displayStruct.cps, displayStruct.cpm);
  43. else if(displayStruct.data == 1)
  44. snprintf(
  45. buffer,
  46. sizeof(buffer),
  47. "%ld cps - %.2f uSv/h",
  48. displayStruct.cps,
  49. ((double)displayStruct.cpm * (double)CONVERSION_FACTOR));
  50. else
  51. snprintf(
  52. buffer,
  53. sizeof(buffer),
  54. "%ld cps - %.2f mSv/y",
  55. displayStruct.cps,
  56. (((double)displayStruct.cpm * (double)CONVERSION_FACTOR)) * (double)8.76);
  57. for(int i = 0; i < SCREEN_SIZE_X; i += 2) {
  58. float Y = SCREEN_SIZE_Y - (displayStruct.line[i / 2] * displayStruct.coef);
  59. canvas_draw_line(canvas, i, Y, i, SCREEN_SIZE_Y);
  60. canvas_draw_line(canvas, i + 1, Y, i + 1, SCREEN_SIZE_Y);
  61. }
  62. canvas_set_font(canvas, FontPrimary);
  63. canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignBottom, buffer);
  64. }
  65. static void input_callback(InputEvent* input_event, void* ctx) {
  66. furi_assert(ctx);
  67. FuriMessageQueue* event_queue = ctx;
  68. EventApp event = {.type = EventTypeInput, .input = *input_event};
  69. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  70. }
  71. static void clock_tick(void* ctx) {
  72. furi_assert(ctx);
  73. uint32_t randomNumber = furi_hal_random_get();
  74. randomNumber &= 0xFFF;
  75. if(randomNumber == 0) randomNumber = 1;
  76. furi_hal_pwm_set_params(FuriHalPwmOutputIdLptim2PA4, randomNumber, 50);
  77. FuriMessageQueue* queue = ctx;
  78. EventApp event = {.type = ClockEventTypeTick};
  79. furi_message_queue_put(queue, &event, 0);
  80. }
  81. static void gpiocallback(void* ctx) {
  82. furi_assert(ctx);
  83. FuriMessageQueue* queue = ctx;
  84. EventApp event = {.type = EventGPIO};
  85. furi_message_queue_put(queue, &event, 0);
  86. }
  87. int32_t flipper_geiger_app(void* p) {
  88. UNUSED(p);
  89. EventApp event;
  90. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(EventApp));
  91. furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInterruptFall, GpioPullUp, GpioSpeedVeryHigh);
  92. furi_hal_pwm_start(FuriHalPwmOutputIdLptim2PA4, 5, 50);
  93. mutexStruct mutexVal;
  94. mutexVal.cps = 0;
  95. mutexVal.cpm = 0;
  96. for(int i = 0; i < SCREEN_SIZE_X / 2; i++) mutexVal.line[i] = 0;
  97. mutexVal.coef = 1;
  98. mutexVal.data = 0;
  99. uint32_t counter = 0;
  100. mutexVal.mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  101. if(!mutexVal.mutex) {
  102. furi_message_queue_free(event_queue);
  103. return 255;
  104. }
  105. ViewPort* view_port = view_port_alloc();
  106. view_port_draw_callback_set(view_port, draw_callback, &mutexVal);
  107. view_port_input_callback_set(view_port, input_callback, event_queue);
  108. furi_hal_gpio_add_int_callback(&gpio_ext_pa7, gpiocallback, event_queue);
  109. Gui* gui = furi_record_open(RECORD_GUI);
  110. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  111. FuriTimer* timer = furi_timer_alloc(clock_tick, FuriTimerTypePeriodic, event_queue);
  112. furi_timer_start(timer, 1000);
  113. // ENABLE 5V pin
  114. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  115. uint8_t attempts = 0;
  116. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  117. furi_hal_power_enable_otg();
  118. furi_delay_ms(10);
  119. }
  120. while(1) {
  121. FuriStatus event_status = furi_message_queue_get(event_queue, &event, FuriWaitForever);
  122. uint8_t screenRefresh = 0;
  123. if(event_status == FuriStatusOk) {
  124. if(event.type == EventTypeInput) {
  125. if(event.input.key == InputKeyBack) {
  126. break;
  127. } else if(event.input.key == InputKeyOk && event.input.type == InputTypeShort) {
  128. counter = 0;
  129. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  130. mutexVal.cps = 0;
  131. mutexVal.cpm = 0;
  132. for(int i = 0; i < SCREEN_SIZE_X / 2; i++) mutexVal.line[i] = 0;
  133. screenRefresh = 1;
  134. furi_mutex_release(mutexVal.mutex);
  135. } else if((event.input.key == InputKeyLeft &&
  136. event.input.type == InputTypeShort)) {
  137. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  138. if(mutexVal.data != 0)
  139. mutexVal.data--;
  140. else
  141. mutexVal.data = 2;
  142. screenRefresh = 1;
  143. furi_mutex_release(mutexVal.mutex);
  144. } else if((event.input.key == InputKeyRight &&
  145. event.input.type == InputTypeShort)) {
  146. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  147. if(mutexVal.data != 2)
  148. mutexVal.data++;
  149. else
  150. mutexVal.data = 0;
  151. screenRefresh = 1;
  152. furi_mutex_release(mutexVal.mutex);
  153. }
  154. } else if(event.type == ClockEventTypeTick) {
  155. furi_mutex_acquire(mutexVal.mutex, FuriWaitForever);
  156. for(int i = 0; i < SCREEN_SIZE_X / 2 - 1; i++)
  157. mutexVal.line[SCREEN_SIZE_X / 2 - 1 - i] =
  158. mutexVal.line[SCREEN_SIZE_X / 2 - 2 - i];
  159. mutexVal.line[0] = counter;
  160. mutexVal.cps = counter;
  161. counter = 0;
  162. mutexVal.cpm = mutexVal.line[0];
  163. uint32_t max = mutexVal.line[0];
  164. for(int i = 1; i < SCREEN_SIZE_X / 2; i++) {
  165. if(i < 60) mutexVal.cpm += mutexVal.line[i];
  166. if(mutexVal.line[i] > max) max = mutexVal.line[i];
  167. }
  168. if(max > 0)
  169. mutexVal.coef = ((float)(SCREEN_SIZE_Y - 15)) / ((float)max);
  170. else
  171. mutexVal.coef = 1;
  172. screenRefresh = 1;
  173. furi_mutex_release(mutexVal.mutex);
  174. } else if(event.type == EventGPIO) {
  175. counter++;
  176. }
  177. }
  178. if(screenRefresh == 1) view_port_update(view_port);
  179. }
  180. // Disable 5v power
  181. if(furi_hal_power_is_otg_enabled()) {
  182. furi_hal_power_disable_otg();
  183. }
  184. furi_hal_gpio_disable_int_callback(&gpio_ext_pa7);
  185. furi_hal_gpio_remove_int_callback(&gpio_ext_pa7);
  186. furi_hal_pwm_stop(FuriHalPwmOutputIdLptim2PA4);
  187. furi_message_queue_free(event_queue);
  188. furi_mutex_free(mutexVal.mutex);
  189. gui_remove_view_port(gui, view_port);
  190. view_port_free(view_port);
  191. furi_timer_free(timer);
  192. furi_record_close(RECORD_GUI);
  193. return 0;
  194. }