notification_settings_app.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <furi.h>
  2. #include "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_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_on);
  72. }
  73. static void led_changed(VariableItem* item) {
  74. NotificationAppSettings* app = variable_item_get_context(item);
  75. uint8_t index = variable_item_get_current_value_index(item);
  76. variable_item_set_current_value_text(item, backlight_text[index]);
  77. app->notification->settings.led_brightness = backlight_value[index];
  78. notification_message(app->notification, &sequence_blink_white_100);
  79. }
  80. static void volume_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, volume_text[index]);
  84. app->notification->settings.speaker_volume = volume_value[index];
  85. notification_message(app->notification, &sequence_note_c);
  86. }
  87. static void vibro_changed(VariableItem* item) {
  88. NotificationAppSettings* app = variable_item_get_context(item);
  89. uint8_t index = variable_item_get_current_value_index(item);
  90. variable_item_set_current_value_text(item, vibro_text[index]);
  91. app->notification->settings.vibro_on = vibro_value[index];
  92. notification_message(app->notification, &sequence_single_vibro);
  93. }
  94. static uint32_t notification_app_settings_exit(void* context) {
  95. return VIEW_NONE;
  96. }
  97. static NotificationAppSettings* alloc_settings() {
  98. NotificationAppSettings* app = malloc(sizeof(NotificationAppSettings));
  99. app->notification = furi_record_open("notification");
  100. app->gui = furi_record_open("gui");
  101. app->variable_item_list = variable_item_list_alloc();
  102. View* view = variable_item_list_get_view(app->variable_item_list);
  103. view_set_previous_callback(view, notification_app_settings_exit);
  104. VariableItem* item;
  105. uint8_t value_index;
  106. item = variable_item_list_add(
  107. app->variable_item_list, "LCD Backlight", BACKLIGHT_COUNT, backlight_changed, app);
  108. value_index = value_index_float(
  109. app->notification->settings.display_brightness, backlight_value, BACKLIGHT_COUNT);
  110. variable_item_set_current_value_index(item, value_index);
  111. variable_item_set_current_value_text(item, backlight_text[value_index]);
  112. item = variable_item_list_add(
  113. app->variable_item_list, "Backlight Time", DELAY_COUNT, screen_changed, app);
  114. value_index = value_index_uint32(
  115. app->notification->settings.display_off_delay_ms, delay_value, DELAY_COUNT);
  116. variable_item_set_current_value_index(item, value_index);
  117. variable_item_set_current_value_text(item, delay_text[value_index]);
  118. item = variable_item_list_add(
  119. app->variable_item_list, "LED Brightness", BACKLIGHT_COUNT, led_changed, app);
  120. value_index = value_index_float(
  121. app->notification->settings.led_brightness, backlight_value, BACKLIGHT_COUNT);
  122. variable_item_set_current_value_index(item, value_index);
  123. variable_item_set_current_value_text(item, backlight_text[value_index]);
  124. item = variable_item_list_add(
  125. app->variable_item_list, "Volume", VOLUME_COUNT, volume_changed, app);
  126. value_index =
  127. value_index_float(app->notification->settings.speaker_volume, volume_value, VOLUME_COUNT);
  128. variable_item_set_current_value_index(item, value_index);
  129. variable_item_set_current_value_text(item, volume_text[value_index]);
  130. item =
  131. variable_item_list_add(app->variable_item_list, "Vibro", VIBRO_COUNT, vibro_changed, app);
  132. value_index = value_index_bool(app->notification->settings.vibro_on, vibro_value, VIBRO_COUNT);
  133. variable_item_set_current_value_index(item, value_index);
  134. variable_item_set_current_value_text(item, vibro_text[value_index]);
  135. app->view_dispatcher = view_dispatcher_alloc();
  136. view_dispatcher_enable_queue(app->view_dispatcher);
  137. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  138. view_dispatcher_add_view(app->view_dispatcher, 0, view);
  139. view_dispatcher_switch_to_view(app->view_dispatcher, 0);
  140. return app;
  141. }
  142. static void free_settings(NotificationAppSettings* app) {
  143. view_dispatcher_remove_view(app->view_dispatcher, 0);
  144. variable_item_list_free(app->variable_item_list);
  145. view_dispatcher_free(app->view_dispatcher);
  146. furi_record_close("gui");
  147. furi_record_close("notification");
  148. free(app);
  149. }
  150. int32_t notification_settings_app(void* p) {
  151. NotificationAppSettings* app = alloc_settings();
  152. view_dispatcher_run(app->view_dispatcher);
  153. notification_message_save_settings(app->notification);
  154. free_settings(app);
  155. return 0;
  156. }