General_view.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. /*
  2. Unitemp - Universal temperature reader
  3. Copyright (C) 2022-2023 Victor Nikitchuk (https://github.com/quen0n)
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. */
  15. #include "UnitempViews.h"
  16. #include "unitemp_icons.h"
  17. extern const Icon I_ButtonRight_4x7;
  18. extern const Icon I_ButtonLeft_4x7;
  19. extern const Icon I_Ok_btn_9x9;
  20. static View* view;
  21. typedef enum general_views {
  22. G_NO_SENSORS_VIEW, //Нет датчиков
  23. G_LIST_VIEW, //Вид в ввиде списка
  24. G_CAROUSEL_VIEW, //Карусель
  25. } general_view;
  26. typedef enum carousel_info {
  27. CAROUSEL_VALUES, //Отображение значений датчиков
  28. CAROUSEL_INFO, //Отображение информации о датчике
  29. } carousel_info;
  30. static general_view current_view;
  31. carousel_info carousel_info_selector = CAROUSEL_VALUES;
  32. uint8_t generalview_sensor_index = 0;
  33. static void _draw_temperature(Canvas* canvas, Sensor* sensor, uint8_t x, uint8_t y, Color color) {
  34. //Рисование рамки
  35. canvas_draw_rframe(canvas, x, y, 54, 20, 3);
  36. if(color == ColorBlack) {
  37. canvas_draw_rbox(canvas, x, y, 54, 19, 3);
  38. canvas_invert_color(canvas);
  39. } else {
  40. canvas_draw_rframe(canvas, x, y, 54, 19, 3);
  41. }
  42. int8_t temp_dec = abs((int16_t)(sensor->temp * 10) % 10);
  43. //Рисование иконки
  44. canvas_draw_icon(
  45. canvas,
  46. x + 3,
  47. y + 3,
  48. (app->settings.temp_unit == UT_TEMP_CELSIUS ? &I_temp_C_11x14 : &I_temp_F_11x14));
  49. if((int16_t)sensor->temp == -128 || sensor->status == UT_SENSORSTATUS_TIMEOUT) {
  50. canvas_set_font(canvas, FontBigNumbers);
  51. canvas_draw_str_aligned(canvas, x + 27, y + 10, AlignCenter, AlignCenter, "--");
  52. canvas_set_font(canvas, FontPrimary);
  53. canvas_draw_str_aligned(canvas, x + 50, y + 10 + 3, AlignRight, AlignCenter, ". -");
  54. if(color == ColorBlack) canvas_invert_color(canvas);
  55. return;
  56. }
  57. //Целая часть температуры
  58. //Костыль для отображения знака числа меньше 0
  59. uint8_t offset = 0;
  60. if(sensor->temp < 0 && sensor->temp > -1) {
  61. app->buff[0] = '-';
  62. offset = 1;
  63. }
  64. snprintf((char*)(app->buff + offset), BUFF_SIZE, "%d", (int16_t)sensor->temp);
  65. canvas_set_font(canvas, FontBigNumbers);
  66. canvas_draw_str_aligned(
  67. canvas,
  68. x + 27 + ((sensor->temp <= -10 || sensor->temp > 99) ? 5 : 0),
  69. y + 10,
  70. AlignCenter,
  71. AlignCenter,
  72. app->buff);
  73. //Печать дробной части температуры в диапазоне от -9 до 99 (когда два знака в числе)
  74. if(sensor->temp > -10 && sensor->temp <= 99) {
  75. uint8_t int_len = canvas_string_width(canvas, app->buff);
  76. snprintf(app->buff, BUFF_SIZE, ".%d", temp_dec);
  77. canvas_set_font(canvas, FontPrimary);
  78. canvas_draw_str(canvas, x + 27 + int_len / 2 + 2, y + 10 + 7, app->buff);
  79. }
  80. if(color == ColorBlack) canvas_invert_color(canvas);
  81. }
  82. static void _draw_humidity(Canvas* canvas, Sensor* sensor, const uint8_t pos[2]) {
  83. //Рисование рамки
  84. canvas_draw_rframe(canvas, pos[0], pos[1], 54, 20, 3);
  85. canvas_draw_rframe(canvas, pos[0], pos[1], 54, 19, 3);
  86. //Рисование иконки
  87. canvas_draw_icon(canvas, pos[0] + 3, pos[1] + 2, &I_hum_9x15);
  88. //Целая часть влажности
  89. snprintf(app->buff, BUFF_SIZE, "%d", (uint8_t)sensor->hum);
  90. canvas_set_font(canvas, FontBigNumbers);
  91. canvas_draw_str_aligned(canvas, pos[0] + 27, pos[1] + 10, AlignCenter, AlignCenter, app->buff);
  92. uint8_t int_len = canvas_string_width(canvas, app->buff);
  93. //Единица измерения
  94. canvas_set_font(canvas, FontPrimary);
  95. canvas_draw_str(canvas, pos[0] + 27 + int_len / 2 + 4, pos[1] + 10 + 7, "%");
  96. }
  97. static void _draw_heat_index(Canvas* canvas, Sensor* sensor, const uint8_t pos[2]) {
  98. canvas_draw_rframe(canvas, pos[0], pos[1], 54, 20, 3);
  99. canvas_draw_rframe(canvas, pos[0], pos[1], 54, 19, 3);
  100. canvas_draw_icon(canvas, pos[0] + 3, pos[1] + 3, &I_heat_index_11x14);
  101. int16_t heat_index_int = sensor->heat_index;
  102. int8_t heat_index_dec = abs((int16_t)(sensor->heat_index * 10) % 10);
  103. snprintf(app->buff, BUFF_SIZE, "%d", heat_index_int);
  104. canvas_set_font(canvas, FontBigNumbers);
  105. canvas_draw_str_aligned(
  106. canvas,
  107. pos[0] + 27 + ((sensor->heat_index <= -10 || sensor->heat_index > 99) ? 5 : 0),
  108. pos[1] + 10,
  109. AlignCenter,
  110. AlignCenter,
  111. app->buff);
  112. if(heat_index_int <= 99) {
  113. uint8_t int_len = canvas_string_width(canvas, app->buff);
  114. snprintf(app->buff, BUFF_SIZE, ".%d", heat_index_dec);
  115. canvas_set_font(canvas, FontPrimary);
  116. canvas_draw_str(canvas, pos[0] + 27 + int_len / 2 + 2, pos[1] + 10 + 7, app->buff);
  117. }
  118. }
  119. static void _draw_pressure(Canvas* canvas, Sensor* sensor) {
  120. uint8_t x = 29, y = 39;
  121. //Slide the canvas over slightly to account for the larger hPa values
  122. if(app->settings.pressure_unit == UT_PRESSURE_HPA) {
  123. x = 21;
  124. } else {
  125. x = 29;
  126. }
  127. //Рисование рамки
  128. if(app->settings.pressure_unit == UT_PRESSURE_HPA) {
  129. canvas_draw_rframe(canvas, x, y, 84, 20, 3);
  130. canvas_draw_rframe(canvas, x, y, 84, 19, 3);
  131. } else {
  132. canvas_draw_rframe(canvas, x, y, 69, 20, 3);
  133. canvas_draw_rframe(canvas, x, y, 69, 19, 3);
  134. }
  135. //Рисование иконки
  136. canvas_draw_icon(canvas, x + 3, y + 4, &I_pressure_7x13);
  137. int16_t press_int = sensor->pressure;
  138. // Change Temp for Pressure
  139. int8_t press_dec = (int16_t)(sensor->pressure * 10) % 10;
  140. //Целая часть давления
  141. snprintf(app->buff, BUFF_SIZE, "%d", press_int);
  142. canvas_set_font(canvas, FontBigNumbers);
  143. canvas_draw_str_aligned(
  144. canvas, x + 27 + ((press_int > 99) ? 5 : 0), y + 10, AlignCenter, AlignCenter, app->buff);
  145. //Печать дробной части давления в диапазоне от 0 до 99 (когда два знака в числе)
  146. if(press_int <= 99) {
  147. uint8_t int_len = canvas_string_width(canvas, app->buff);
  148. snprintf(app->buff, BUFF_SIZE, ".%d", press_dec);
  149. canvas_set_font(canvas, FontPrimary);
  150. canvas_draw_str(canvas, x + 27 + int_len / 2 + 2, y + 10 + 7, app->buff);
  151. } else if(app->settings.pressure_unit == UT_PRESSURE_HPA) {
  152. uint8_t int_len = canvas_string_width(canvas, app->buff);
  153. snprintf(app->buff, BUFF_SIZE, ".%d", press_dec);
  154. canvas_set_font(canvas, FontPrimary);
  155. canvas_draw_str(canvas, x + 32 + int_len / 2 + 2, y + 10 + 7, app->buff);
  156. }
  157. canvas_set_font(canvas, FontSecondary);
  158. //Единица измерения
  159. if(app->settings.pressure_unit == UT_PRESSURE_MM_HG) {
  160. canvas_draw_icon(canvas, x + 50, y + 2, &I_mm_hg_15x15);
  161. } else if(app->settings.pressure_unit == UT_PRESSURE_IN_HG) {
  162. canvas_draw_icon(canvas, x + 50, y + 2, &I_in_hg_15x15);
  163. } else if(app->settings.pressure_unit == UT_PRESSURE_KPA) {
  164. canvas_draw_str(canvas, x + 52, y + 13, "kPa");
  165. } else if(app->settings.pressure_unit == UT_PRESSURE_HPA) {
  166. canvas_draw_str(canvas, x + 67, y + 13, "hPa");
  167. }
  168. }
  169. static void _draw_co2(Canvas* canvas, Sensor* sensor, Color color) {
  170. const uint8_t x = 29, y = 39;
  171. //Рисование рамки
  172. canvas_draw_rframe(canvas, x, y, 75, 20, 3);
  173. if(color == ColorBlack) {
  174. canvas_draw_rbox(canvas, x, y, 75, 19, 3);
  175. canvas_invert_color(canvas);
  176. } else {
  177. canvas_draw_rframe(canvas, x, y, 75, 19, 3);
  178. }
  179. //Рисование иконки
  180. canvas_draw_icon(canvas, x + 3, y + 3, &I_co2_11x14);
  181. int16_t concentration_int = sensor->co2;
  182. // int8_t concentration_dec = (int16_t)(sensor->co2 * 10) % 10;
  183. //Целая часть
  184. if(concentration_int > 9999) {
  185. snprintf(app->buff, BUFF_SIZE, "MAX ");
  186. canvas_set_font(canvas, FontPrimary);
  187. } else {
  188. snprintf(app->buff, BUFF_SIZE, "%d", concentration_int);
  189. canvas_set_font(canvas, FontBigNumbers);
  190. }
  191. canvas_draw_str_aligned(canvas, x + 70, y + 10, AlignRight, AlignCenter, app->buff);
  192. }
  193. static void _draw_singleSensor(Canvas* canvas, Sensor* sensor, const uint8_t pos[2], Color color) {
  194. canvas_set_font(canvas, FontPrimary);
  195. const uint8_t max_width = 56;
  196. char sensor_name[12] = {0};
  197. memcpy(sensor_name, sensor->name, 10);
  198. if(canvas_string_width(canvas, sensor_name) > max_width) {
  199. uint8_t i = 10;
  200. while((canvas_string_width(canvas, sensor_name) > max_width - 6) && (i != 0)) {
  201. sensor_name[i--] = '\0';
  202. }
  203. sensor_name[++i] = '.';
  204. sensor_name[++i] = '.';
  205. }
  206. canvas_draw_str_aligned(
  207. canvas, pos[0] + 27, pos[1] + 3, AlignCenter, AlignCenter, sensor_name);
  208. _draw_temperature(canvas, sensor, pos[0], pos[1] + 8, color);
  209. }
  210. static void _draw_view_noSensors(Canvas* canvas) {
  211. canvas_draw_icon(canvas, 7, 17, &I_sherlok_53x45);
  212. //Рисование рамки
  213. canvas_draw_rframe(canvas, 0, 0, 128, 63, 7);
  214. canvas_draw_rframe(canvas, 0, 0, 128, 64, 7);
  215. canvas_set_font(canvas, FontPrimary);
  216. canvas_draw_str_aligned(canvas, 63, 10, AlignCenter, AlignCenter, "No sensors found");
  217. canvas_set_font(canvas, FontSecondary);
  218. const uint8_t x = 65, y = 32;
  219. canvas_draw_rframe(canvas, x - 4, y - 11, 54, 33, 3);
  220. canvas_draw_rframe(canvas, x - 4, y - 11, 54, 34, 3);
  221. canvas_draw_str(canvas, x, y, "To add the");
  222. canvas_draw_str(canvas, x, y + 9, "new sensor");
  223. canvas_draw_str(canvas, x, y + 18, "press OK");
  224. canvas_draw_icon(canvas, x + 37, y + 10, &I_Ok_btn_9x9);
  225. }
  226. static void _draw_view_sensorsList(Canvas* canvas) {
  227. //Текущая страница
  228. uint8_t page = generalview_sensor_index / 4;
  229. //Количество датчиков, которые будут отображаться на странице
  230. uint8_t page_sensors_count;
  231. if((unitemp_sensors_getActiveCount() - page * 4) / 4) {
  232. page_sensors_count = 4;
  233. } else {
  234. page_sensors_count = (unitemp_sensors_getActiveCount() - page * 4) % 4;
  235. }
  236. //Количество страниц
  237. uint8_t pages =
  238. unitemp_sensors_getActiveCount() / 4 + (unitemp_sensors_getActiveCount() % 4 ? 1 : 0);
  239. //Стрелка влево
  240. if(page > 0) {
  241. canvas_draw_icon(canvas, 2, 32, &I_ButtonLeft_4x7);
  242. }
  243. //Стрелка вправо
  244. if(pages > 0 && page < pages - 1) {
  245. canvas_draw_icon(canvas, 122, 32, &I_ButtonRight_4x7);
  246. }
  247. const uint8_t value_positions[][4][2] = {
  248. {{36, 18}}, //1 датчик
  249. {{7, 18}, {67, 18}}, //2 датчика
  250. {{7, 3}, {67, 3}, {37, 33}}, //3 датчика
  251. {{7, 3}, {67, 3}, {7, 33}, {67, 33}}}; //4 датчика
  252. //Рисование рамки
  253. canvas_draw_rframe(canvas, 0, 0, 128, 63, 7);
  254. canvas_draw_rframe(canvas, 0, 0, 128, 64, 7);
  255. for(uint8_t i = 0; i < page_sensors_count; i++) {
  256. _draw_singleSensor(
  257. canvas,
  258. unitemp_sensor_getActive(page * 4 + i),
  259. value_positions[page_sensors_count - 1][i],
  260. ColorWhite);
  261. }
  262. }
  263. static void _draw_carousel_values(Canvas* canvas) {
  264. UnitempStatus sensor_status = unitemp_sensor_getActive(generalview_sensor_index)->status;
  265. if(sensor_status == UT_SENSORSTATUS_ERROR || sensor_status == UT_SENSORSTATUS_TIMEOUT) {
  266. const Icon* frames[] = {
  267. &I_flipper_happy_60x38, &I_flipper_happy_2_60x38, &I_flipper_sad_60x38};
  268. canvas_draw_icon(canvas, 34, 23, frames[furi_get_tick() % 2250 / 750]);
  269. canvas_set_font(canvas, FontSecondary);
  270. //TODO: Оптимизировать эту срань
  271. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SINGLE_WIRE) {
  272. snprintf(
  273. app->buff,
  274. BUFF_SIZE,
  275. "Waiting for module on pin %d",
  276. ((SingleWireSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance)
  277. ->gpio->num);
  278. }
  279. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &ONE_WIRE) {
  280. snprintf(
  281. app->buff,
  282. BUFF_SIZE,
  283. "Waiting for module on pin %d",
  284. ((OneWireSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance)
  285. ->bus->gpio->num);
  286. }
  287. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &I2C) {
  288. snprintf(app->buff, BUFF_SIZE, "Waiting for module on I2C pins");
  289. }
  290. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SPI) {
  291. snprintf(app->buff, BUFF_SIZE, "Waiting for module on SPI pins");
  292. }
  293. canvas_draw_str_aligned(canvas, 64, 19, AlignCenter, AlignCenter, app->buff);
  294. return;
  295. }
  296. static const uint8_t temp_positions[3][2] = {{37, 23}, {37, 16}, {9, 16}};
  297. static const uint8_t hum_positions[2][2] = {{37, 38}, {65, 16}};
  298. //Селектор значений для отображения
  299. switch(unitemp_sensor_getActive(generalview_sensor_index)->type->datatype) {
  300. case UT_DATA_TYPE_TEMP:
  301. _draw_temperature(
  302. canvas,
  303. unitemp_sensor_getActive(generalview_sensor_index),
  304. temp_positions[0][0],
  305. temp_positions[0][1],
  306. ColorWhite);
  307. break;
  308. case UT_DATA_TYPE_TEMP_HUM:
  309. if(!app->settings.heat_index) {
  310. _draw_temperature(
  311. canvas,
  312. unitemp_sensor_getActive(generalview_sensor_index),
  313. temp_positions[1][0],
  314. temp_positions[1][1],
  315. ColorWhite);
  316. } else {
  317. _draw_temperature(
  318. canvas,
  319. unitemp_sensor_getActive(generalview_sensor_index),
  320. temp_positions[2][0],
  321. temp_positions[2][1],
  322. ColorWhite);
  323. _draw_heat_index(
  324. canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[1]);
  325. }
  326. _draw_humidity(
  327. canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[0]);
  328. break;
  329. case UT_DATA_TYPE_TEMP_PRESS:
  330. _draw_temperature(
  331. canvas,
  332. unitemp_sensor_getActive(generalview_sensor_index),
  333. temp_positions[1][0],
  334. temp_positions[1][1],
  335. ColorWhite);
  336. _draw_pressure(canvas, unitemp_sensor_getActive(generalview_sensor_index));
  337. break;
  338. case UT_DATA_TYPE_TEMP_HUM_PRESS:
  339. _draw_temperature(
  340. canvas,
  341. unitemp_sensor_getActive(generalview_sensor_index),
  342. temp_positions[2][0],
  343. temp_positions[2][1],
  344. ColorWhite);
  345. _draw_humidity(
  346. canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[1]);
  347. _draw_pressure(canvas, unitemp_sensor_getActive(generalview_sensor_index));
  348. break;
  349. case UT_DATA_TYPE_TEMP_HUM_CO2:
  350. _draw_temperature(
  351. canvas,
  352. unitemp_sensor_getActive(generalview_sensor_index),
  353. temp_positions[2][0],
  354. temp_positions[2][1],
  355. ColorWhite);
  356. _draw_humidity(
  357. canvas, unitemp_sensor_getActive(generalview_sensor_index), hum_positions[1]);
  358. _draw_co2(canvas, unitemp_sensor_getActive(generalview_sensor_index), ColorWhite);
  359. break;
  360. }
  361. }
  362. //TODO: Оптимизировать вывод информации
  363. static void _draw_carousel_info(Canvas* canvas) {
  364. canvas_set_font(canvas, FontPrimary);
  365. canvas_draw_str(canvas, 10, 23, "Type:");
  366. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &ONE_WIRE) {
  367. OneWireSensor* s = unitemp_sensor_getActive(generalview_sensor_index)->instance;
  368. canvas_set_font(canvas, FontPrimary);
  369. canvas_draw_str(canvas, 10, 35, "GPIO:");
  370. canvas_draw_str(canvas, 10, 47, "ID:");
  371. canvas_set_font(canvas, FontSecondary);
  372. canvas_draw_str(
  373. canvas,
  374. 41,
  375. 23,
  376. unitemp_onewire_sensor_getModel(unitemp_sensor_getActive(generalview_sensor_index)));
  377. canvas_draw_str(canvas, 41, 35, s->bus->gpio->name);
  378. snprintf(
  379. app->buff,
  380. BUFF_SIZE,
  381. "%02X%02X%02X%02X%02X%02X%02X%02X",
  382. s->deviceID[0],
  383. s->deviceID[1],
  384. s->deviceID[2],
  385. s->deviceID[3],
  386. s->deviceID[4],
  387. s->deviceID[5],
  388. s->deviceID[6],
  389. s->deviceID[7]);
  390. canvas_draw_str(canvas, 24, 47, app->buff);
  391. }
  392. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SINGLE_WIRE) {
  393. canvas_set_font(canvas, FontPrimary);
  394. canvas_draw_str(canvas, 10, 35, "GPIO:");
  395. canvas_set_font(canvas, FontSecondary);
  396. canvas_draw_str(
  397. canvas, 41, 23, unitemp_sensor_getActive(generalview_sensor_index)->type->typename);
  398. canvas_draw_str(
  399. canvas,
  400. 41,
  401. 35,
  402. ((SingleWireSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance)
  403. ->gpio->name);
  404. }
  405. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &SPI) {
  406. canvas_set_font(canvas, FontPrimary);
  407. canvas_draw_str(canvas, 10, 35, "MISO pin:");
  408. canvas_draw_str(canvas, 10, 46, "CS pin:");
  409. canvas_draw_str(canvas, 10, 58, "SCK pin:");
  410. canvas_set_font(canvas, FontSecondary);
  411. canvas_draw_str(
  412. canvas, 41, 23, unitemp_sensor_getActive(generalview_sensor_index)->type->typename);
  413. canvas_draw_str(canvas, 60, 35, unitemp_gpio_getFromInt(3)->name);
  414. canvas_draw_str(
  415. canvas,
  416. 47,
  417. 46,
  418. ((SPISensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance)
  419. ->CS_pin->name);
  420. canvas_draw_str(canvas, 54, 58, unitemp_gpio_getFromInt(5)->name);
  421. }
  422. if(unitemp_sensor_getActive(generalview_sensor_index)->type->interface == &I2C) {
  423. canvas_set_font(canvas, FontPrimary);
  424. canvas_draw_str(canvas, 10, 35, "I2C addr:");
  425. canvas_draw_str(canvas, 10, 46, "SDA pin:");
  426. canvas_draw_str(canvas, 10, 58, "SCL pin:");
  427. canvas_set_font(canvas, FontSecondary);
  428. canvas_draw_str(
  429. canvas, 41, 23, unitemp_sensor_getActive(generalview_sensor_index)->type->typename);
  430. snprintf(
  431. app->buff,
  432. BUFF_SIZE,
  433. "0x%02X",
  434. ((I2CSensor*)unitemp_sensor_getActive(generalview_sensor_index)->instance)
  435. ->currentI2CAdr >>
  436. 1);
  437. canvas_draw_str(canvas, 57, 35, app->buff);
  438. canvas_draw_str(canvas, 54, 46, "15 (C1)");
  439. canvas_draw_str(canvas, 54, 58, "16 (C0)");
  440. }
  441. }
  442. static void _draw_view_sensorsCarousel(Canvas* canvas) {
  443. //Рисование рамки
  444. canvas_draw_rframe(canvas, 0, 0, 128, 63, 7);
  445. canvas_draw_rframe(canvas, 0, 0, 128, 64, 7);
  446. //Печать имени
  447. canvas_set_font(canvas, FontPrimary);
  448. canvas_draw_str_aligned(
  449. canvas,
  450. 64,
  451. 7,
  452. AlignCenter,
  453. AlignCenter,
  454. unitemp_sensor_getActive(generalview_sensor_index)->name);
  455. //Подчёркивание
  456. uint8_t line_len =
  457. canvas_string_width(canvas, unitemp_sensor_getActive(generalview_sensor_index)->name) + 2;
  458. canvas_draw_line(canvas, 64 - line_len / 2, 12, 64 + line_len / 2, 12);
  459. //Стрелка вправо
  460. if(unitemp_sensors_getTypesCount() > 0 &&
  461. generalview_sensor_index < unitemp_sensors_getActiveCount() - 1) {
  462. canvas_draw_icon(canvas, 122, 29, &I_ButtonRight_4x7);
  463. }
  464. //Стрелка влево
  465. if(generalview_sensor_index > 0) {
  466. canvas_draw_icon(canvas, 2, 29, &I_ButtonLeft_4x7);
  467. }
  468. switch(carousel_info_selector) {
  469. case CAROUSEL_VALUES:
  470. _draw_carousel_values(canvas);
  471. break;
  472. case CAROUSEL_INFO:
  473. _draw_carousel_info(canvas);
  474. break;
  475. }
  476. }
  477. static void _draw_callback(Canvas* canvas, void* _model) {
  478. UNUSED(_model);
  479. app->sensors_ready = true;
  480. uint8_t sensors_count = unitemp_sensors_getActiveCount();
  481. if(generalview_sensor_index + 1 > sensors_count) generalview_sensor_index = 0;
  482. if(sensors_count == 0) {
  483. current_view = G_NO_SENSORS_VIEW;
  484. _draw_view_noSensors(canvas);
  485. } else {
  486. if(sensors_count == 1) current_view = G_CAROUSEL_VIEW;
  487. if(current_view == G_NO_SENSORS_VIEW) current_view = G_CAROUSEL_VIEW;
  488. if(current_view == G_LIST_VIEW) _draw_view_sensorsList(canvas);
  489. if(current_view == G_CAROUSEL_VIEW) _draw_view_sensorsCarousel(canvas);
  490. }
  491. }
  492. static bool _input_callback(InputEvent* event, void* context) {
  493. UNUSED(context);
  494. //Обработка короткого нажатия "ок"
  495. if(event->key == InputKeyOk && event->type == InputTypeShort) {
  496. //Меню добавления датчика при их отсутствии
  497. if(current_view == G_NO_SENSORS_VIEW) {
  498. app->sensors_ready = false;
  499. unitemp_SensorsList_switch();
  500. } else if(current_view == G_LIST_VIEW) {
  501. //Переход в главное меню при выключенном селекторе
  502. app->sensors_ready = false;
  503. unitemp_MainMenu_switch();
  504. } else if(current_view == G_CAROUSEL_VIEW) {
  505. app->sensors_ready = false;
  506. unitemp_SensorActions_switch(unitemp_sensor_getActive(generalview_sensor_index));
  507. }
  508. }
  509. //Обработка короткого нажатия "вниз"
  510. if(event->key == InputKeyDown && event->type == InputTypeShort) {
  511. //Переход из значений в информацию в карусели
  512. if(current_view == G_CAROUSEL_VIEW && carousel_info_selector == CAROUSEL_VALUES) {
  513. carousel_info_selector = CAROUSEL_INFO;
  514. return true;
  515. }
  516. //Переход в карусель из списка
  517. if(current_view == G_LIST_VIEW) {
  518. current_view = G_CAROUSEL_VIEW;
  519. return true;
  520. }
  521. }
  522. //Обработка короткого нажатия "вверх"
  523. if(event->key == InputKeyUp && event->type == InputTypeShort) {
  524. //Переход из информации в значения в карусели
  525. if(current_view == G_CAROUSEL_VIEW && carousel_info_selector == CAROUSEL_INFO) {
  526. carousel_info_selector = CAROUSEL_VALUES;
  527. return true;
  528. }
  529. //Переход в список из карусели
  530. if(current_view == G_CAROUSEL_VIEW && carousel_info_selector == CAROUSEL_VALUES &&
  531. unitemp_sensors_getActiveCount() > 1) {
  532. current_view = G_LIST_VIEW;
  533. return true;
  534. }
  535. }
  536. //Обработка короткого нажатия "вправо"
  537. if(event->key == InputKeyRight && event->type == InputTypeShort) {
  538. //Пролистывание карусели вперёд
  539. if(current_view == G_CAROUSEL_VIEW) {
  540. if(++generalview_sensor_index >= unitemp_sensors_getActiveCount()) {
  541. generalview_sensor_index = 0;
  542. if(carousel_info_selector == CAROUSEL_VALUES) current_view = G_LIST_VIEW;
  543. }
  544. return true;
  545. }
  546. //Пролистывание списка вперёд
  547. if(current_view == G_LIST_VIEW) {
  548. generalview_sensor_index += 4;
  549. if(generalview_sensor_index >= unitemp_sensors_getActiveCount()) {
  550. generalview_sensor_index = 0;
  551. current_view = G_CAROUSEL_VIEW;
  552. }
  553. return true;
  554. }
  555. }
  556. //Обработка короткого нажатия "влево"
  557. if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  558. //Пролистывание карусели назад
  559. if(current_view == G_CAROUSEL_VIEW) {
  560. if(--generalview_sensor_index >= unitemp_sensors_getActiveCount()) {
  561. generalview_sensor_index = unitemp_sensors_getActiveCount() - 1;
  562. if(carousel_info_selector == CAROUSEL_VALUES) current_view = G_LIST_VIEW;
  563. }
  564. return true;
  565. }
  566. //Пролистывание списка назад
  567. if(current_view == G_LIST_VIEW) {
  568. generalview_sensor_index -= 4;
  569. if(generalview_sensor_index >= unitemp_sensors_getActiveCount()) {
  570. generalview_sensor_index = unitemp_sensors_getActiveCount() - 1;
  571. current_view = G_CAROUSEL_VIEW;
  572. }
  573. return true;
  574. }
  575. }
  576. //Обработка короткого нажатия "назад"
  577. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  578. //Выход из приложения при карусели или отсутствии датчиков
  579. if(current_view == G_NO_SENSORS_VIEW ||
  580. ((current_view == G_CAROUSEL_VIEW) && (carousel_info_selector == CAROUSEL_VALUES))) {
  581. app->processing = false;
  582. return true;
  583. }
  584. //Переключение селектора вида карусели
  585. if((current_view == G_CAROUSEL_VIEW) && (carousel_info_selector != CAROUSEL_VALUES)) {
  586. carousel_info_selector = CAROUSEL_VALUES;
  587. return true;
  588. }
  589. //Переход в карусель из списка
  590. if(current_view == G_LIST_VIEW) {
  591. current_view = G_CAROUSEL_VIEW;
  592. return true;
  593. }
  594. }
  595. //Обработка длинного нажатия "Ок"
  596. if(event->key == InputKeyOk && event->type == InputTypeLong) {
  597. app->settings.temp_unit = !app->settings.temp_unit;
  598. }
  599. return true;
  600. }
  601. void unitemp_General_alloc(void) {
  602. view = view_alloc();
  603. view_set_context(view, app);
  604. view_set_draw_callback(view, _draw_callback);
  605. view_set_input_callback(view, _input_callback);
  606. view_dispatcher_add_view(app->view_dispatcher, UnitempViewGeneral, view);
  607. }
  608. void unitemp_General_switch(void) {
  609. app->sensors_ready = true;
  610. view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewGeneral);
  611. }
  612. void unitemp_General_free(void) {
  613. view_dispatcher_remove_view(app->view_dispatcher, UnitempViewGeneral);
  614. view_free(view);
  615. }