General_view.c 27 KB

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