mh_z19app.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "mh_z19_icons.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include <gui/gui.h>
  5. #include <gui/elements.h>
  6. #include <input/input.h>
  7. #include <notification/notification_messages.h>
  8. typedef enum {
  9. GreenStatus,
  10. YellowStatus,
  11. RedStatus,
  12. } StatusPPM;
  13. typedef enum { RANGE_2000 = 2000, RANGE_5000 = 5000 } SensorRange;
  14. struct MHZ19App {
  15. Gui* gui;
  16. ViewPort* view_port;
  17. FuriMessageQueue* event_queue;
  18. FuriMutex* mutex;
  19. NotificationApp* notifications;
  20. bool have_5v;
  21. int32_t current_page;
  22. StatusPPM status_ppm;
  23. SensorRange sensor_range;
  24. const GpioPin* input_pin;
  25. int32_t co2_ppm;
  26. };
  27. typedef struct MHZ19App MHZ19App;
  28. const NotificationSequence green_led_sequence = {
  29. &message_green_255,
  30. &message_do_not_reset,
  31. NULL,
  32. };
  33. const NotificationSequence yellow_led_sequence = {
  34. &message_green_255,
  35. &message_red_255,
  36. &message_do_not_reset,
  37. &message_vibro_on,
  38. &message_note_c5,
  39. &message_delay_50,
  40. &message_vibro_off,
  41. &message_sound_off,
  42. NULL,
  43. };
  44. const NotificationSequence red_led_sequence = {
  45. &message_red_255,
  46. &message_do_not_reset,
  47. &message_vibro_on,
  48. &message_note_c5,
  49. &message_delay_50,
  50. &message_vibro_off,
  51. &message_sound_off,
  52. NULL,
  53. };
  54. void mh_z19app_draw_callback(Canvas* canvas, void* ctx) {
  55. furi_assert(ctx);
  56. MHZ19App* app = ctx;
  57. canvas_clear(canvas);
  58. if(!app->have_5v) {
  59. canvas_set_font(canvas, FontPrimary);
  60. elements_multiline_text_aligned(
  61. canvas,
  62. 4,
  63. 28,
  64. AlignLeft,
  65. AlignTop,
  66. "5V on GPIO must be\nenabled, or USB must\nbe connected.");
  67. return;
  68. } else if(app->current_page == 0) {
  69. canvas_set_font(canvas, FontPrimary);
  70. elements_multiline_text_aligned(
  71. canvas,
  72. 4,
  73. 1,
  74. AlignLeft,
  75. AlignTop,
  76. "Connect sensor MH-Z19 to pins:\n5V -> 1\nGND -> 8\nPWM -> 3\nPress center button...");
  77. return;
  78. } else if(app->current_page == 1) {
  79. canvas_set_font(canvas, FontPrimary);
  80. elements_multiline_text_aligned(
  81. canvas,
  82. 4,
  83. 1,
  84. AlignLeft,
  85. AlignTop,
  86. "Select sensor measuring range by arrows.\nAvailable:\n\t- 0-2000ppm\n\t- 0-5000ppm\nPress center button...");
  87. return;
  88. }
  89. FuriString* strbuf = furi_string_alloc();
  90. const Icon* icon;
  91. FuriString* status_text = furi_string_alloc();
  92. switch(app->status_ppm) {
  93. case GreenStatus:
  94. icon = &I_passport_okay1_46x49;
  95. furi_string_set_str(status_text, "it's OK!");
  96. break;
  97. case YellowStatus:
  98. icon = &I_passport_bad1_46x49;
  99. furi_string_set_str(status_text, "Not good!");
  100. break;
  101. case RedStatus:
  102. icon = &I_passport_bad3_46x49;
  103. furi_string_set_str(status_text, "Very bad!");
  104. break;
  105. default:
  106. icon = &I_passport_okay1_46x49;
  107. furi_string_set_str(status_text, "It's OK!");
  108. break;
  109. }
  110. const Icon* co2_icon = &I_co2;
  111. canvas_draw_icon(canvas, 9, 7, icon);
  112. canvas_draw_icon(canvas, 59, 8, co2_icon);
  113. furi_string_printf(strbuf, "%ld", app->co2_ppm);
  114. canvas_set_font(canvas, FontBigNumbers);
  115. canvas_draw_str(canvas, 60, 40, furi_string_get_cstr(strbuf));
  116. canvas_set_font(canvas, FontPrimary);
  117. canvas_draw_str(canvas, 60, 55, furi_string_get_cstr(status_text));
  118. canvas_set_font(canvas, FontSecondary);
  119. canvas_draw_str(canvas, 110, 40, "ppm");
  120. furi_string_printf(strbuf, "%d", app->sensor_range);
  121. canvas_set_font(canvas, FontKeyboard);
  122. canvas_draw_str(canvas, 104, 8, furi_string_get_cstr(strbuf));
  123. furi_string_free(strbuf);
  124. furi_string_free(status_text);
  125. }
  126. void mh_z19app_input_callback(InputEvent* input_event, void* ctx) {
  127. furi_assert(ctx);
  128. FuriMessageQueue* event_queue = ctx;
  129. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  130. }
  131. MHZ19App* mh_z19app_alloc() {
  132. MHZ19App* app = (MHZ19App*)malloc(sizeof(MHZ19App));
  133. app->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  134. if(!app->mutex) {
  135. FURI_LOG_E("MH-Z19", "cannot create mutex\r\n");
  136. free(app);
  137. return NULL;
  138. }
  139. furi_mutex_acquire(app->mutex, FuriWaitForever);
  140. app->view_port = view_port_alloc();
  141. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  142. view_port_draw_callback_set(app->view_port, mh_z19app_draw_callback, app);
  143. view_port_input_callback_set(app->view_port, mh_z19app_input_callback, app->event_queue);
  144. app->gui = furi_record_open(RECORD_GUI);
  145. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  146. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  147. uint8_t attempts = 0;
  148. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  149. furi_hal_power_enable_otg();
  150. furi_delay_ms(10);
  151. }
  152. if(furi_hal_power_is_otg_enabled() || furi_hal_power_is_charging()) {
  153. app->have_5v = true;
  154. } else {
  155. app->have_5v = false;
  156. }
  157. app->input_pin = &gpio_ext_pa6;
  158. furi_hal_gpio_init(app->input_pin, GpioModeInput, GpioPullUp, GpioSpeedVeryHigh);
  159. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  160. furi_mutex_release(app->mutex);
  161. return app;
  162. }
  163. void mh_z19app_free(MHZ19App* app) {
  164. furi_assert(app);
  165. if(furi_hal_power_is_otg_enabled()) {
  166. furi_hal_power_disable_otg();
  167. }
  168. view_port_enabled_set(app->view_port, false);
  169. gui_remove_view_port(app->gui, app->view_port);
  170. view_port_free(app->view_port);
  171. furi_message_queue_free(app->event_queue);
  172. furi_hal_light_set(LightRed | LightGreen | LightBlue, 0x00);
  173. furi_record_close(RECORD_NOTIFICATION);
  174. furi_record_close(RECORD_GUI);
  175. furi_mutex_free(app->mutex);
  176. free(app);
  177. }
  178. void mh_z19app_init(MHZ19App* app) {
  179. furi_mutex_acquire(app->mutex, FuriWaitForever);
  180. app->co2_ppm = 0;
  181. app->status_ppm = GreenStatus;
  182. app->current_page = 0;
  183. app->sensor_range = RANGE_2000;
  184. app->have_5v = true;
  185. notification_message(app->notifications, &green_led_sequence);
  186. furi_mutex_release(app->mutex);
  187. }
  188. int32_t calculate_ppm(
  189. int32_t* prevVal,
  190. int32_t val,
  191. int32_t* th,
  192. int32_t* tl,
  193. int32_t* h,
  194. int32_t* l,
  195. SensorRange range) {
  196. int32_t tt = furi_get_tick();
  197. if(val == 1) {
  198. if(val != *prevVal) {
  199. *h = tt;
  200. *tl = *h - *l;
  201. *prevVal = val;
  202. }
  203. } else {
  204. if(val != *prevVal) {
  205. *l = tt;
  206. *th = *l - *h;
  207. *prevVal = val;
  208. return range * (*th - 2) / (*th + *tl - 4);
  209. }
  210. }
  211. return -1;
  212. }
  213. void change_sensor_range(MHZ19App* app) {
  214. if(app->sensor_range == RANGE_2000) {
  215. app->sensor_range = RANGE_5000;
  216. } else {
  217. app->sensor_range = RANGE_2000;
  218. }
  219. }
  220. void send_notification_if_needed(MHZ19App* app, int32_t ppm) {
  221. if(ppm > 0) {
  222. if(ppm > 1000) {
  223. if(app->status_ppm != RedStatus) {
  224. notification_message(app->notifications, &red_led_sequence);
  225. app->status_ppm = RedStatus;
  226. }
  227. } else if(ppm > 800) {
  228. if(app->status_ppm != YellowStatus) {
  229. notification_message(app->notifications, &yellow_led_sequence);
  230. app->status_ppm = YellowStatus;
  231. }
  232. } else {
  233. if(app->status_ppm != GreenStatus) {
  234. notification_message(app->notifications, &green_led_sequence);
  235. app->status_ppm = GreenStatus;
  236. }
  237. }
  238. }
  239. }
  240. int32_t mh_z19_app(void* p) {
  241. UNUSED(p);
  242. MHZ19App* app = mh_z19app_alloc();
  243. if(!app) {
  244. FURI_LOG_E("MH-Z19", "cannot create app\r\n");
  245. return -1;
  246. }
  247. mh_z19app_init(app);
  248. InputEvent event;
  249. int32_t prevVal = 0;
  250. int32_t th, tl, h = 0;
  251. int32_t l = 0;
  252. int32_t ppm = 0;
  253. for(bool processing = true; processing;) {
  254. furi_mutex_acquire(app->mutex, FuriWaitForever);
  255. ppm = calculate_ppm(
  256. &prevVal, furi_hal_gpio_read(app->input_pin), &th, &tl, &h, &l, app->sensor_range);
  257. if(ppm > 0) {
  258. app->co2_ppm = ppm;
  259. }
  260. send_notification_if_needed(app, app->co2_ppm);
  261. if(furi_message_queue_get(app->event_queue, &event, 100) == FuriStatusOk) {
  262. if(event.type == InputTypeShort) {
  263. switch(event.key) {
  264. case InputKeyBack:
  265. processing = false;
  266. break;
  267. case InputKeyOk:
  268. app->current_page++;
  269. break;
  270. case InputKeyLeft:
  271. case InputKeyRight:
  272. change_sensor_range(app);
  273. break;
  274. default:
  275. break;
  276. }
  277. }
  278. }
  279. furi_mutex_release(app->mutex);
  280. view_port_update(app->view_port);
  281. }
  282. mh_z19app_free(app);
  283. return 0;
  284. }