General_view.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 const uint8_t hum_positions[2][2] = {{37, 38}, {65, 16}};
  6. static uint8_t sensor_index = 0;
  7. static char buff[5];
  8. static void _draw_noSensors(Canvas* canvas) {
  9. canvas_draw_str(canvas, 0, 24, "Sensors not found");
  10. }
  11. static void _draw_temp(Canvas* canvas, float temp, uint8_t pos) {
  12. //Рисование рамки
  13. canvas_draw_rframe(canvas, temp_positions[pos][0], temp_positions[pos][1], 54, 20, 3);
  14. canvas_draw_rframe(canvas, temp_positions[pos][0], temp_positions[pos][1], 54, 19, 3);
  15. int8_t temp_int = temp;
  16. int8_t temp_dec = abs((int16_t)(temp * 10) % 10);
  17. //Рисование иконки
  18. canvas_draw_icon(
  19. canvas, temp_positions[pos][0] + 3, temp_positions[pos][1] + 3, &I_temp_C_11x14);
  20. if((int8_t)temp == -128) {
  21. snprintf(buff, 5, "--");
  22. canvas_set_font(canvas, FontBigNumbers);
  23. canvas_draw_str_aligned(
  24. canvas,
  25. temp_positions[pos][0] + 27,
  26. temp_positions[pos][1] + 10,
  27. AlignCenter,
  28. AlignCenter,
  29. buff);
  30. snprintf(buff, 4, ". -");
  31. canvas_set_font(canvas, FontPrimary);
  32. canvas_draw_str_aligned(
  33. canvas,
  34. temp_positions[pos][0] + 50,
  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(
  57. canvas,
  58. temp_positions[pos][0] + 27 + int_len / 2 + 2,
  59. temp_positions[pos][1] + 10 + 7,
  60. buff);
  61. }
  62. }
  63. static void _draw_hum(Canvas* canvas, float hum, uint8_t pos) {
  64. //Рисование рамки
  65. canvas_draw_rframe(canvas, hum_positions[pos][0], hum_positions[pos][1], 54, 20, 3);
  66. canvas_draw_rframe(canvas, hum_positions[pos][0], hum_positions[pos][1], 54, 19, 3);
  67. //Рисование иконки
  68. canvas_draw_icon(canvas, hum_positions[pos][0] + 3, hum_positions[pos][1] + 2, &I_hum_9x15);
  69. if((int8_t)hum == -128) {
  70. snprintf(buff, 5, "--");
  71. canvas_set_font(canvas, FontBigNumbers);
  72. canvas_draw_str_aligned(
  73. canvas,
  74. hum_positions[pos][0] + 27,
  75. hum_positions[pos][1] + 10,
  76. AlignCenter,
  77. AlignCenter,
  78. buff);
  79. snprintf(buff, 4, ". -");
  80. canvas_set_font(canvas, FontPrimary);
  81. canvas_draw_str_aligned(
  82. canvas,
  83. hum_positions[pos][0] + 50,
  84. hum_positions[pos][1] + 10 + 3,
  85. AlignRight,
  86. AlignCenter,
  87. buff);
  88. return;
  89. }
  90. //Целая часть влажности
  91. snprintf(buff, 5, "%d", (uint8_t)hum);
  92. canvas_set_font(canvas, FontBigNumbers);
  93. canvas_draw_str_aligned(
  94. canvas,
  95. hum_positions[pos][0] + 27,
  96. hum_positions[pos][1] + 10,
  97. AlignCenter,
  98. AlignCenter,
  99. buff);
  100. uint8_t int_len = canvas_string_width(canvas, buff);
  101. //Единица измерения
  102. canvas_set_font(canvas, FontPrimary);
  103. canvas_draw_str(
  104. canvas, hum_positions[pos][0] + 27 + int_len / 2 + 2, hum_positions[pos][1] + 10 + 7, "%");
  105. }
  106. static void _draw_sensorsCarousel(Canvas* canvas) {
  107. //Рисование рамки
  108. canvas_draw_rframe(canvas, 3, 0, 122, 63, 7);
  109. canvas_draw_rframe(canvas, 3, 0, 122, 64, 7);
  110. //Печать имени
  111. canvas_set_font(canvas, FontPrimary);
  112. canvas_draw_str_aligned(
  113. canvas, 64, 7, AlignCenter, AlignCenter, app->sensors[sensor_index]->name);
  114. //Подчёркивание
  115. uint8_t line_len = canvas_string_width(canvas, app->sensors[sensor_index]->name) + 2;
  116. canvas_draw_line(canvas, 64 - line_len / 2, 12, 64 + line_len / 2, 12);
  117. //Стрелка вправо
  118. if(unitemp_sensors_getTypesCount() > 0 && sensor_index < unitemp_sensors_getCount() - 1) {
  119. canvas_draw_icon(canvas, 64 + line_len / 2 + 4, 3, &I_arrow_right_5x9);
  120. }
  121. //Стрелка влево
  122. if(sensor_index > 0) {
  123. canvas_draw_icon(canvas, 64 - line_len / 2 - 8, 3, &I_arrow_left_5x9);
  124. }
  125. //Печать значения температуры
  126. _draw_temp(canvas, app->sensors[sensor_index]->temp, 1);
  127. _draw_hum(canvas, app->sensors[sensor_index]->hum, 0);
  128. }
  129. static void _draw_callback(Canvas* canvas, void* _model) {
  130. UNUSED(_model);
  131. app->sensors_ready = true;
  132. uint8_t sensors_count = unitemp_sensors_getCount();
  133. if(sensors_count == 0) {
  134. _draw_noSensors(canvas);
  135. }
  136. if(sensors_count > 0) {
  137. _draw_sensorsCarousel(canvas);
  138. }
  139. }
  140. static bool _input_callback(InputEvent* event, void* context) {
  141. Unitemp* app = context;
  142. //Выход по короткому нажатию "назад"
  143. if(event->key == InputKeyBack && event->type == InputTypeShort) {
  144. app->processing = false;
  145. }
  146. //Пролистывание карусели по короткому нажатию "право"
  147. if(event->key == InputKeyRight && event->type == InputTypeShort) {
  148. if(++sensor_index >= unitemp_sensors_getCount()) sensor_index = 0;
  149. }
  150. //Пролистывание карусели по короткому нажатию "лево"
  151. if(event->key == InputKeyLeft && event->type == InputTypeShort) {
  152. if(--sensor_index >= unitemp_sensors_getCount())
  153. sensor_index = unitemp_sensors_getCount() - 1;
  154. }
  155. //Вход в главное меню по короткому нажатию "Ок"
  156. if(event->key == InputKeyOk && event->type == InputTypeShort) {
  157. app->sensors_ready = false;
  158. unitemp_MainMenu_switch();
  159. }
  160. return true;
  161. }
  162. void unitemp_General_alloc(void) {
  163. view = view_alloc();
  164. view_set_context(view, app);
  165. view_set_draw_callback(view, _draw_callback);
  166. view_set_input_callback(view, _input_callback);
  167. view_dispatcher_add_view(app->view_dispatcher, GENERAL_VIEW, view);
  168. }
  169. void unitemp_General_switch(void) {
  170. app->sensors_ready = true;
  171. view_dispatcher_switch_to_view(app->view_dispatcher, GENERAL_VIEW);
  172. }
  173. void unitemp_General_free(void) {
  174. view_free(view);
  175. }