General_view.c 27 KB

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