notification-app-settings.c 7.6 KB

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