notification_settings_app.c 6.9 KB

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