notification_settings_app.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include <furi.h>
  2. #include "notification_app.h"
  3. #include <gui/modules/variable_item_list.h>
  4. #include <gui/view_dispatcher.h>
  5. #define MAX_NOTIFICATION_SETTINGS 4
  6. typedef struct {
  7. NotificationApp* notification;
  8. Gui* gui;
  9. ViewDispatcher* view_dispatcher;
  10. VariableItemList* variable_item_list;
  11. } NotificationAppSettings;
  12. static const NotificationSequence sequence_note_c = {
  13. &message_note_c5,
  14. &message_delay_100,
  15. &message_sound_off,
  16. NULL,
  17. };
  18. #define BACKLIGHT_COUNT 5
  19. const char* const backlight_text[BACKLIGHT_COUNT] = {
  20. "0%",
  21. "25%",
  22. "50%",
  23. "75%",
  24. "100%",
  25. };
  26. const float backlight_value[BACKLIGHT_COUNT] = {
  27. 0.0f,
  28. 0.25f,
  29. 0.5f,
  30. 0.75f,
  31. 1.0f,
  32. };
  33. #define VOLUME_COUNT 5
  34. const char* const volume_text[VOLUME_COUNT] = {
  35. "0%",
  36. "25%",
  37. "50%",
  38. "75%",
  39. "100%",
  40. };
  41. const float volume_value[VOLUME_COUNT] = {0.0f, 0.04f, 0.1f, 0.2f, 1.0f};
  42. #define DELAY_COUNT 6
  43. const char* const delay_text[DELAY_COUNT] = {
  44. "1s",
  45. "5s",
  46. "15s",
  47. "30s",
  48. "60s",
  49. "120s",
  50. };
  51. const uint32_t delay_value[DELAY_COUNT] = {1000, 5000, 15000, 30000, 60000, 120000};
  52. #define VIBRO_COUNT 2
  53. const char* const vibro_text[VIBRO_COUNT] = {
  54. "OFF",
  55. "ON",
  56. };
  57. const bool vibro_value[VIBRO_COUNT] = {false, true};
  58. uint8_t float_value_index(const float value, const float values[], uint8_t values_count) {
  59. const float epsilon = 0.01f;
  60. float last_value = values[0];
  61. uint8_t index = 0;
  62. for(uint8_t i = 0; i < values_count; i++) {
  63. if((value >= last_value - epsilon) && (value <= values[i] + epsilon)) {
  64. index = i;
  65. break;
  66. }
  67. last_value = values[i];
  68. }
  69. return index;
  70. }
  71. uint8_t uint32_value_index(const uint32_t value, const uint32_t values[], uint8_t values_count) {
  72. int64_t last_value = INT64_MIN;
  73. uint8_t index = 0;
  74. for(uint8_t i = 0; i < values_count; i++) {
  75. if((value >= last_value) && (value <= values[i])) {
  76. index = i;
  77. break;
  78. }
  79. last_value = values[i];
  80. }
  81. return index;
  82. }
  83. uint8_t bool_value_index(const bool value, const bool values[], uint8_t values_count) {
  84. uint8_t index = 0;
  85. for(uint8_t i = 0; i < values_count; i++) {
  86. if(value == values[i]) {
  87. index = i;
  88. break;
  89. }
  90. }
  91. return index;
  92. }
  93. static void backlight_changed(VariableItem* item) {
  94. NotificationAppSettings* app = variable_item_get_context(item);
  95. uint8_t index = variable_item_get_current_value_index(item);
  96. variable_item_set_current_value_text(item, backlight_text[index]);
  97. app->notification->settings.display_brightness = backlight_value[index];
  98. notification_message(app->notification, &sequence_display_on);
  99. }
  100. static void screen_changed(VariableItem* item) {
  101. NotificationAppSettings* app = variable_item_get_context(item);
  102. uint8_t index = variable_item_get_current_value_index(item);
  103. variable_item_set_current_value_text(item, delay_text[index]);
  104. app->notification->settings.display_off_delay_ms = delay_value[index];
  105. notification_message(app->notification, &sequence_display_on);
  106. }
  107. static void led_changed(VariableItem* item) {
  108. NotificationAppSettings* app = variable_item_get_context(item);
  109. uint8_t index = variable_item_get_current_value_index(item);
  110. variable_item_set_current_value_text(item, backlight_text[index]);
  111. app->notification->settings.led_brightness = backlight_value[index];
  112. notification_message(app->notification, &sequence_blink_white_100);
  113. }
  114. static void volume_changed(VariableItem* item) {
  115. NotificationAppSettings* app = variable_item_get_context(item);
  116. uint8_t index = variable_item_get_current_value_index(item);
  117. variable_item_set_current_value_text(item, volume_text[index]);
  118. app->notification->settings.speaker_volume = volume_value[index];
  119. notification_message(app->notification, &sequence_note_c);
  120. }
  121. static void vibro_changed(VariableItem* item) {
  122. NotificationAppSettings* app = variable_item_get_context(item);
  123. uint8_t index = variable_item_get_current_value_index(item);
  124. variable_item_set_current_value_text(item, vibro_text[index]);
  125. app->notification->settings.vibro_on = vibro_value[index];
  126. notification_message(app->notification, &sequence_single_vibro);
  127. }
  128. static uint32_t notification_app_settings_exit(void* context) {
  129. return VIEW_NONE;
  130. }
  131. static NotificationAppSettings* alloc_settings() {
  132. NotificationAppSettings* app = malloc(sizeof(NotificationAppSettings));
  133. app->notification = furi_record_open("notification");
  134. app->gui = furi_record_open("gui");
  135. app->variable_item_list = variable_item_list_alloc();
  136. View* view = variable_item_list_get_view(app->variable_item_list);
  137. view_set_previous_callback(view, notification_app_settings_exit);
  138. VariableItem* item;
  139. uint8_t value_index;
  140. item = variable_item_list_add(
  141. app->variable_item_list, "LCD backlight", BACKLIGHT_COUNT, backlight_changed, app);
  142. value_index = float_value_index(
  143. app->notification->settings.display_brightness, backlight_value, BACKLIGHT_COUNT);
  144. variable_item_set_current_value_index(item, value_index);
  145. variable_item_set_current_value_text(item, backlight_text[value_index]);
  146. item = variable_item_list_add(
  147. app->variable_item_list, "Backlight time", DELAY_COUNT, screen_changed, app);
  148. value_index = uint32_value_index(
  149. app->notification->settings.display_off_delay_ms, delay_value, DELAY_COUNT);
  150. variable_item_set_current_value_index(item, value_index);
  151. variable_item_set_current_value_text(item, delay_text[value_index]);
  152. item = variable_item_list_add(
  153. app->variable_item_list, "LED brightness", BACKLIGHT_COUNT, led_changed, app);
  154. value_index = float_value_index(
  155. app->notification->settings.led_brightness, backlight_value, BACKLIGHT_COUNT);
  156. variable_item_set_current_value_index(item, value_index);
  157. variable_item_set_current_value_text(item, backlight_text[value_index]);
  158. item = variable_item_list_add(
  159. app->variable_item_list, "Volume", VOLUME_COUNT, volume_changed, app);
  160. value_index =
  161. float_value_index(app->notification->settings.speaker_volume, volume_value, VOLUME_COUNT);
  162. variable_item_set_current_value_index(item, value_index);
  163. variable_item_set_current_value_text(item, volume_text[value_index]);
  164. item =
  165. variable_item_list_add(app->variable_item_list, "Vibro", VIBRO_COUNT, vibro_changed, app);
  166. value_index = bool_value_index(app->notification->settings.vibro_on, vibro_value, VIBRO_COUNT);
  167. variable_item_set_current_value_index(item, value_index);
  168. variable_item_set_current_value_text(item, vibro_text[value_index]);
  169. app->view_dispatcher = view_dispatcher_alloc();
  170. view_dispatcher_enable_queue(app->view_dispatcher);
  171. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  172. view_dispatcher_add_view(app->view_dispatcher, 0, view);
  173. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  174. return app;
  175. }
  176. static void free_settings(NotificationAppSettings* app) {
  177. view_dispatcher_remove_view(app->view_dispatcher, 0);
  178. variable_item_list_free(app->variable_item_list);
  179. view_dispatcher_free(app->view_dispatcher);
  180. furi_record_close("gui");
  181. furi_record_close("notification");
  182. free(app);
  183. }
  184. int32_t notification_settings_app(void* p) {
  185. NotificationAppSettings* app = alloc_settings();
  186. view_dispatcher_run(app->view_dispatcher);
  187. notification_message_save_settings(app->notification);
  188. free_settings(app);
  189. return 0;
  190. }