General_view.c 6.9 KB

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