Widgets_view.c 7.8 KB

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