Widgets_view.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. void unitemp_widgets_alloc(void) {
  18. app->widget = widget_alloc();
  19. view_dispatcher_add_view(
  20. app->view_dispatcher, UnitempViewWidget, widget_get_view(app->widget));
  21. }
  22. void unitemp_widgets_free(void) {
  23. view_dispatcher_remove_view(app->view_dispatcher, UnitempViewWidget);
  24. widget_free(app->widget);
  25. }
  26. /* ================== Подтверждение удаления ================== */
  27. Sensor* current_sensor;
  28. /**
  29. * @brief Функция обработки нажатия кнопки "Назад"
  30. *
  31. * @param context Указатель на данные приложения
  32. * @return ID вида в который нужно переключиться
  33. */
  34. static uint32_t _delete_exit_callback(void* context) {
  35. UNUSED(context);
  36. //Возвращаем ID вида, в который нужно вернуться
  37. return UnitempViewSensorActions;
  38. }
  39. /**
  40. * @brief Обработчик нажатий на кнопку в виджете
  41. *
  42. * @param result Какая из кнопок была нажата
  43. * @param type Тип нажатия
  44. * @param context Указатель на данные плагина
  45. */
  46. static void _delete_click_callback(GuiButtonType result, InputType type, void* context) {
  47. UNUSED(context);
  48. //Коротко нажата левая кнопка (Cancel)
  49. if(result == GuiButtonTypeLeft && type == InputTypeShort) {
  50. unitemp_SensorActions_switch(current_sensor);
  51. }
  52. //Коротко нажата правая кнопка (Delete)
  53. if(result == GuiButtonTypeRight && type == InputTypeShort) {
  54. //Удаление датчика
  55. unitemp_sensor_delete(current_sensor);
  56. //Выход из меню
  57. unitemp_General_switch();
  58. }
  59. }
  60. /**
  61. * @brief Переключение в виджет удаления датчика
  62. */
  63. void unitemp_widget_delete_switch(Sensor* sensor) {
  64. current_sensor = sensor;
  65. //Очистка виджета
  66. widget_reset(app->widget);
  67. //Добавление кнопок
  68. widget_add_button_element(
  69. app->widget, GuiButtonTypeLeft, "Cancel", _delete_click_callback, app);
  70. widget_add_button_element(
  71. app->widget, GuiButtonTypeRight, "Delete", _delete_click_callback, app);
  72. snprintf(app->buff, BUFF_SIZE, "\e#Delete %s?\e#", current_sensor->name);
  73. widget_add_text_box_element(
  74. app->widget, 0, 0, 128, 23, AlignCenter, AlignCenter, app->buff, false);
  75. if(current_sensor->type->interface == &ONE_WIRE) {
  76. OneWireSensor* s = current_sensor->instance;
  77. snprintf(
  78. app->buff,
  79. BUFF_SIZE,
  80. "\e#Type:\e# %s",
  81. unitemp_onewire_sensor_getModel(current_sensor));
  82. widget_add_text_box_element(
  83. app->widget, 0, 16, 128, 23, AlignLeft, AlignTop, app->buff, false);
  84. snprintf(app->buff, BUFF_SIZE, "\e#GPIO:\e# %s", s->bus->gpio->name);
  85. widget_add_text_box_element(
  86. app->widget, 0, 28, 128, 23, AlignLeft, AlignTop, app->buff, false);
  87. snprintf(
  88. app->buff,
  89. BUFF_SIZE,
  90. "\e#ID:\e# %02X%02X%02X%02X%02X%02X%02X%02X",
  91. s->deviceID[0],
  92. s->deviceID[1],
  93. s->deviceID[2],
  94. s->deviceID[3],
  95. s->deviceID[4],
  96. s->deviceID[5],
  97. s->deviceID[6],
  98. s->deviceID[7]);
  99. widget_add_text_box_element(
  100. app->widget, 0, 40, 128, 23, AlignLeft, AlignTop, app->buff, false);
  101. }
  102. if(current_sensor->type->interface == &SINGLE_WIRE) {
  103. snprintf(app->buff, BUFF_SIZE, "\e#Type:\e# %s", current_sensor->type->typename);
  104. widget_add_text_box_element(
  105. app->widget, 0, 16, 128, 23, AlignLeft, AlignTop, app->buff, false);
  106. snprintf(
  107. app->buff,
  108. BUFF_SIZE,
  109. "\e#GPIO:\e# %s",
  110. ((SingleWireSensor*)current_sensor->instance)->gpio->name);
  111. widget_add_text_box_element(
  112. app->widget, 0, 28, 128, 23, AlignLeft, AlignTop, app->buff, false);
  113. }
  114. if(current_sensor->type->interface == &I2C) {
  115. snprintf(app->buff, BUFF_SIZE, "\e#Type:\e# %s", current_sensor->type->typename);
  116. widget_add_text_box_element(
  117. app->widget, 0, 16, 128, 23, AlignLeft, AlignTop, app->buff, false);
  118. snprintf(
  119. app->buff,
  120. BUFF_SIZE,
  121. "\e#I2C addr:\e# 0x%02X",
  122. ((I2CSensor*)current_sensor->instance)->currentI2CAdr >> 1);
  123. widget_add_text_box_element(
  124. app->widget, 0, 28, 128, 23, AlignLeft, AlignTop, app->buff, false);
  125. }
  126. view_set_previous_callback(widget_get_view(app->widget), _delete_exit_callback);
  127. view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewWidget);
  128. }
  129. /* ========================== Помощь ========================== */
  130. /**
  131. * @brief Функция обработки нажатия кнопки "Назад"
  132. *
  133. * @param context Указатель на данные приложения
  134. * @return ID вида в который нужно переключиться
  135. */
  136. static uint32_t _help_exit_callback(void* context) {
  137. UNUSED(context);
  138. //Возвращаем ID вида, в который нужно вернуться
  139. return UnitempViewGeneral;
  140. }
  141. /**
  142. * @brief Переключение в виджет помощи
  143. */
  144. void unitemp_widget_help_switch(void) {
  145. //Очистка виджета
  146. widget_reset(app->widget);
  147. widget_add_icon_element(app->widget, 3, 7, &I_repo_qr_50x50);
  148. widget_add_icon_element(app->widget, 71, 15, &I_DolphinCommon_56x48);
  149. widget_add_string_multiline_element(
  150. app->widget, 55, 5, AlignLeft, AlignTop, FontSecondary, "You can find help\nthere");
  151. widget_add_frame_element(app->widget, 0, 0, 128, 63, 7);
  152. widget_add_frame_element(app->widget, 0, 0, 128, 64, 7);
  153. view_set_previous_callback(widget_get_view(app->widget), _help_exit_callback);
  154. view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewWidget);
  155. }
  156. /* ========================== О приложении ========================== */
  157. /**
  158. * @brief Переключение в виджет о приложении
  159. */
  160. void unitemp_widget_about_switch(void) {
  161. //Очистка виджета
  162. widget_reset(app->widget);
  163. widget_add_frame_element(app->widget, 0, 0, 128, 63, 7);
  164. widget_add_frame_element(app->widget, 0, 0, 128, 64, 7);
  165. snprintf(app->buff, BUFF_SIZE, "#Unitemp %s#", UNITEMP_APP_VER);
  166. widget_add_text_box_element(
  167. app->widget, 0, 4, 128, 12, AlignCenter, AlignCenter, app->buff, false);
  168. widget_add_text_scroll_element(
  169. app->widget,
  170. 4,
  171. 16,
  172. 121,
  173. 44,
  174. "Universal plugin for viewing the values of temperature\nsensors\n\e#Author: Quenon\ngithub.com/quen0n\n\e#Designer: Svaarich\ngithub.com/Svaarich\n\e#Issues & suggestions\ntiny.one/unitemp");
  175. view_set_previous_callback(widget_get_view(app->widget), _help_exit_callback);
  176. view_dispatcher_switch_to_view(app->view_dispatcher, UnitempViewWidget);
  177. }