| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- #include "UnitempViews.h"
- #include "unitemp_icons.h"
- static View* view;
- static const uint8_t temp_positions[3][2] = {{37, 24}, {37, 16}, {9, 16}};
- static const uint8_t hum_positions[2][2] = {{37, 38}, {65, 16}};
- static uint8_t sensor_index = 0;
- static char buff[5];
- static void _draw_noSensors(Canvas* canvas) {
- canvas_draw_str(canvas, 0, 24, "Sensors not found");
- }
- static void _draw_temp(Canvas* canvas, float temp, uint8_t pos) {
- //Рисование рамки
- canvas_draw_rframe(canvas, temp_positions[pos][0], temp_positions[pos][1], 54, 20, 3);
- canvas_draw_rframe(canvas, temp_positions[pos][0], temp_positions[pos][1], 54, 19, 3);
- int8_t temp_int = temp;
- int8_t temp_dec = abs((int16_t)(temp * 10) % 10);
- //Рисование иконки
- canvas_draw_icon(
- canvas, temp_positions[pos][0] + 3, temp_positions[pos][1] + 3, &I_temp_C_11x14);
- if((int8_t)temp == -128) {
- snprintf(buff, 5, "--");
- canvas_set_font(canvas, FontBigNumbers);
- canvas_draw_str_aligned(
- canvas,
- temp_positions[pos][0] + 27,
- temp_positions[pos][1] + 10,
- AlignCenter,
- AlignCenter,
- buff);
- snprintf(buff, 4, ". -");
- canvas_set_font(canvas, FontPrimary);
- canvas_draw_str_aligned(
- canvas,
- temp_positions[pos][0] + 50,
- temp_positions[pos][1] + 10 + 3,
- AlignRight,
- AlignCenter,
- buff);
- return;
- }
- //Целая часть температуры
- snprintf(buff, 5, "%d", temp_int);
- canvas_set_font(canvas, FontBigNumbers);
- canvas_draw_str_aligned(
- canvas,
- temp_positions[pos][0] + 27 + ((temp_int <= -10) ? 5 : 0),
- temp_positions[pos][1] + 10,
- AlignCenter,
- AlignCenter,
- buff);
- //Печать дробной части температуры в диапазоне от -9 до 99 (когда два знака в числе)
- if(temp_int > -10 && temp_int <= 99) {
- uint8_t int_len = canvas_string_width(canvas, buff);
- snprintf(buff, 4, ".%d", temp_dec);
- canvas_set_font(canvas, FontPrimary);
- canvas_draw_str(
- canvas,
- temp_positions[pos][0] + 27 + int_len / 2 + 2,
- temp_positions[pos][1] + 10 + 7,
- buff);
- }
- }
- static void _draw_hum(Canvas* canvas, float hum, uint8_t pos) {
- //Рисование рамки
- canvas_draw_rframe(canvas, hum_positions[pos][0], hum_positions[pos][1], 54, 20, 3);
- canvas_draw_rframe(canvas, hum_positions[pos][0], hum_positions[pos][1], 54, 19, 3);
- //Рисование иконки
- canvas_draw_icon(canvas, hum_positions[pos][0] + 3, hum_positions[pos][1] + 2, &I_hum_9x15);
- if((int8_t)hum == -128) {
- snprintf(buff, 5, "--");
- canvas_set_font(canvas, FontBigNumbers);
- canvas_draw_str_aligned(
- canvas,
- hum_positions[pos][0] + 27,
- hum_positions[pos][1] + 10,
- AlignCenter,
- AlignCenter,
- buff);
- snprintf(buff, 4, ". -");
- canvas_set_font(canvas, FontPrimary);
- canvas_draw_str_aligned(
- canvas,
- hum_positions[pos][0] + 50,
- hum_positions[pos][1] + 10 + 3,
- AlignRight,
- AlignCenter,
- buff);
- return;
- }
- //Целая часть влажности
- snprintf(buff, 5, "%d", (uint8_t)hum);
- canvas_set_font(canvas, FontBigNumbers);
- canvas_draw_str_aligned(
- canvas,
- hum_positions[pos][0] + 27,
- hum_positions[pos][1] + 10,
- AlignCenter,
- AlignCenter,
- buff);
- uint8_t int_len = canvas_string_width(canvas, buff);
- //Единица измерения
- canvas_set_font(canvas, FontPrimary);
- canvas_draw_str(
- canvas, hum_positions[pos][0] + 27 + int_len / 2 + 2, hum_positions[pos][1] + 10 + 7, "%");
- }
- static void _draw_sensorsCarousel(Canvas* canvas) {
- //Рисование рамки
- canvas_draw_rframe(canvas, 3, 0, 122, 63, 7);
- canvas_draw_rframe(canvas, 3, 0, 122, 64, 7);
- //Печать имени
- canvas_set_font(canvas, FontPrimary);
- canvas_draw_str_aligned(
- canvas, 64, 7, AlignCenter, AlignCenter, app->sensors[sensor_index]->name);
- //Подчёркивание
- uint8_t line_len = canvas_string_width(canvas, app->sensors[sensor_index]->name) + 2;
- canvas_draw_line(canvas, 64 - line_len / 2, 12, 64 + line_len / 2, 12);
- //Стрелка вправо
- if(unitemp_sensors_getTypesCount() > 0 && sensor_index < unitemp_sensors_getCount() - 1) {
- canvas_draw_icon(canvas, 64 + line_len / 2 + 4, 3, &I_arrow_right_5x9);
- }
- //Стрелка влево
- if(sensor_index > 0) {
- canvas_draw_icon(canvas, 64 - line_len / 2 - 8, 3, &I_arrow_left_5x9);
- }
- //Печать значения температуры
- _draw_temp(canvas, app->sensors[sensor_index]->temp, 1);
- _draw_hum(canvas, app->sensors[sensor_index]->hum, 0);
- }
- static void _draw_callback(Canvas* canvas, void* _model) {
- UNUSED(_model);
- app->sensors_ready = true;
- uint8_t sensors_count = unitemp_sensors_getCount();
- if(sensors_count == 0) {
- _draw_noSensors(canvas);
- }
- if(sensors_count > 0) {
- _draw_sensorsCarousel(canvas);
- }
- }
- static bool _input_callback(InputEvent* event, void* context) {
- Unitemp* app = context;
- //Выход по короткому нажатию "назад"
- if(event->key == InputKeyBack && event->type == InputTypeShort) {
- app->processing = false;
- }
- //Пролистывание карусели по короткому нажатию "право"
- if(event->key == InputKeyRight && event->type == InputTypeShort) {
- if(++sensor_index >= unitemp_sensors_getCount()) sensor_index = 0;
- }
- //Пролистывание карусели по короткому нажатию "лево"
- if(event->key == InputKeyLeft && event->type == InputTypeShort) {
- if(--sensor_index >= unitemp_sensors_getCount())
- sensor_index = unitemp_sensors_getCount() - 1;
- }
- //Вход в главное меню по короткому нажатию "Ок"
- if(event->key == InputKeyOk && event->type == InputTypeShort) {
- app->sensors_ready = false;
- unitemp_MainMenu_switch();
- }
- return true;
- }
- void unitemp_General_alloc(void) {
- view = view_alloc();
- view_set_context(view, app);
- view_set_draw_callback(view, _draw_callback);
- view_set_input_callback(view, _input_callback);
- view_dispatcher_add_view(app->view_dispatcher, GENERAL_VIEW, view);
- }
- void unitemp_General_switch(void) {
- app->sensors_ready = true;
- view_dispatcher_switch_to_view(app->view_dispatcher, GENERAL_VIEW);
- }
- void unitemp_General_free(void) {
- view_free(view);
- }
|