General_view.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #include "UnitempViews.h"
  2. #include "unitemp_icons.h"
  3. static View* view;
  4. static const uint8_t temp_positions[3][2] = {{37, 24}, {37, 16}, {9, 16}};
  5. static uint8_t sensor_index = 0;
  6. static void _draw_noSensors(Canvas* canvas) {
  7. canvas_draw_str(canvas, 0, 24, "Sensors not found");
  8. }
  9. static void _draw_temp(Canvas* canvas, float temp, uint8_t pos) {
  10. //Рисование рамки
  11. canvas_draw_rframe(canvas, temp_positions[pos][0], temp_positions[pos][1], 54, 20, 3);
  12. canvas_draw_rframe(canvas, temp_positions[pos][0], temp_positions[pos][1], 54, 19, 3);
  13. int8_t temp_int = temp;
  14. int8_t temp_dec = abs((int16_t)(temp * 10) % 10);
  15. //Рисование иконки
  16. canvas_draw_icon(
  17. canvas, temp_positions[pos][0] + 3, temp_positions[pos][1] + 3, &I_temp_C_11x14);
  18. char buff[5];
  19. if((int8_t)temp == -128) {
  20. snprintf(buff, 5, "--");
  21. canvas_set_font(canvas, FontBigNumbers);
  22. canvas_draw_str_aligned(
  23. canvas,
  24. temp_positions[pos][0] + 27,
  25. temp_positions[pos][1] + 10,
  26. AlignCenter,
  27. AlignCenter,
  28. buff);
  29. uint8_t int_len = canvas_string_width(canvas, buff);
  30. snprintf(buff, 4, ". -");
  31. canvas_set_font(canvas, FontPrimary);
  32. canvas_draw_str_aligned(
  33. canvas,
  34. temp_positions[pos][0] + 27 + int_len,
  35. temp_positions[pos][1] + 10 + 3,
  36. AlignRight,
  37. AlignCenter,
  38. buff);
  39. return;
  40. }
  41. //Целая часть температуры
  42. snprintf(buff, 5, "%d", temp_int);
  43. canvas_set_font(canvas, FontBigNumbers);
  44. canvas_draw_str_aligned(
  45. canvas,
  46. temp_positions[pos][0] + 27 + ((temp_int <= -10) ? 5 : 0),
  47. temp_positions[pos][1] + 10,
  48. AlignCenter,
  49. AlignCenter,
  50. buff);
  51. //Печать дробной части температуры в диапазоне от -9 до 99 (когда два знака в числе)
  52. if(temp_int > -10 && temp_int <= 99) {
  53. uint8_t int_len = canvas_string_width(canvas, buff);
  54. snprintf(buff, 4, ".%d", temp_dec);
  55. canvas_set_font(canvas, FontPrimary);
  56. canvas_draw_str_aligned(
  57. canvas,
  58. temp_positions[pos][0] + 27 + int_len + ((temp_int >= 0 && temp_int < 10) ? 5 : 0),
  59. temp_positions[pos][1] + 10 + 3,
  60. AlignRight,
  61. AlignCenter,
  62. buff);
  63. }
  64. }
  65. static void _draw_sensorsCarousel(Canvas* canvas) {
  66. //Рисование рамки
  67. canvas_draw_rframe(canvas, 3, 0, 122, 63, 7);
  68. canvas_draw_rframe(canvas, 3, 0, 122, 64, 7);
  69. //Печать имени
  70. canvas_set_font(canvas, FontPrimary);
  71. canvas_draw_str_aligned(
  72. canvas, 64, 7, AlignCenter, AlignCenter, app->sensors[sensor_index]->name);
  73. //Подчёркивание
  74. uint8_t line_len = canvas_string_width(canvas, app->sensors[sensor_index]->name) + 2;
  75. canvas_draw_line(canvas, 64 - line_len / 2, 12, 64 + line_len / 2, 12);
  76. //Стрелка вправо
  77. if(unitemp_sensors_getTypesCount() > 0 && sensor_index < unitemp_sensors_getCount() - 1) {
  78. canvas_draw_icon(canvas, 64 + line_len / 2 + 4, 3, &I_arrow_right_5x9);
  79. }
  80. //Стрелка влево
  81. if(sensor_index > 0) {
  82. canvas_draw_icon(canvas, 64 - line_len / 2 - 8, 3, &I_arrow_left_5x9);
  83. }
  84. //Печать значения температуры
  85. _draw_temp(canvas, app->sensors[sensor_index]->temp, 0);
  86. }
  87. static void _draw_callback(Canvas* canvas, void* _model) {
  88. UNUSED(_model);
  89. app->sensors_ready = true;
  90. uint8_t sensors_count = unitemp_sensors_getCount();
  91. if(sensors_count == 0) {
  92. _draw_noSensors(canvas);
  93. }
  94. if(sensors_count > 0) {
  95. _draw_sensorsCarousel(canvas);
  96. }
  97. }
  98. static bool _input_callback(InputEvent* event, void* context) {
  99. Unitemp* app = context;
  100. //Выход по короткому нажатию "назад"
  101. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  102. app->processing = false;
  103. }
  104. //Пролистывание карусели по короткому нажатию "право"
  105. if(event->key == InputKeyRight && event->type == InputTypeShort) {
  106. if(++sensor_index >= unitemp_sensors_getCount()) sensor_index = 0;
  107. }
  108. //Пролистывание карусели по короткому нажатию "лево"
  109. if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  110. if(--sensor_index >= unitemp_sensors_getCount())
  111. sensor_index = unitemp_sensors_getCount() - 1;
  112. }
  113. //Вход в главное меню по короткому нажатию "Ок"
  114. if(event->key == InputKeyOk && event->type == InputTypeShort) {
  115. app->sensors_ready = false;
  116. unitemp_MainMenu_switch();
  117. }
  118. return true;
  119. }
  120. void unitemp_General_alloc(void) {
  121. view = view_alloc();
  122. view_set_context(view, app);
  123. view_set_draw_callback(view, _draw_callback);
  124. view_set_input_callback(view, _input_callback);
  125. view_dispatcher_add_view(app->view_dispatcher, GENERAL_VIEW, view);
  126. }
  127. void unitemp_General_switch(void) {
  128. app->sensors_ready = true;
  129. view_dispatcher_switch_to_view(app->view_dispatcher, GENERAL_VIEW);
  130. }
  131. void unitemp_General_free(void) {
  132. view_free(view);
  133. }