General_view.c 27 KB

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