mh_z19app.c 9.0 KB

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