notification_app.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <storage/storage.h>
  4. #include <input/input.h>
  5. #include "notification.h"
  6. #include "notification_messages.h"
  7. #include "notification_app.h"
  8. #define TAG "NotificationSrv"
  9. static const uint8_t minimal_delay = 100;
  10. static const uint8_t led_off_values[NOTIFICATION_LED_COUNT] = {0x00, 0x00, 0x00};
  11. static const uint8_t reset_red_mask = 1 << 0;
  12. static const uint8_t reset_green_mask = 1 << 1;
  13. static const uint8_t reset_blue_mask = 1 << 2;
  14. static const uint8_t reset_vibro_mask = 1 << 3;
  15. static const uint8_t reset_sound_mask = 1 << 4;
  16. static const uint8_t reset_display_mask = 1 << 5;
  17. void notification_vibro_on();
  18. void notification_vibro_off();
  19. void notification_sound_on(float pwm, float freq);
  20. void notification_sound_off();
  21. uint8_t notification_settings_get_display_brightness(NotificationApp* app, uint8_t value);
  22. uint8_t notification_settings_get_rgb_led_brightness(NotificationApp* app, uint8_t value);
  23. uint32_t notification_settings_display_off_delay_ticks(NotificationApp* app);
  24. void notification_message_save_settings(NotificationApp* app) {
  25. NotificationAppMessage m = {.type = SaveSettingsMessage, .back_event = osEventFlagsNew(NULL)};
  26. furi_check(osMessageQueuePut(app->queue, &m, 0, osWaitForever) == osOK);
  27. osEventFlagsWait(m.back_event, NOTIFICATION_EVENT_COMPLETE, osFlagsWaitAny, osWaitForever);
  28. osEventFlagsDelete(m.back_event);
  29. };
  30. // internal layer
  31. void notification_apply_internal_led_layer(NotificationLedLayer* layer, uint8_t layer_value) {
  32. furi_assert(layer);
  33. furi_assert(layer->index < LayerMAX);
  34. // set value
  35. layer->value[LayerInternal] = layer_value;
  36. // apply if current layer is internal
  37. if(layer->index == LayerInternal) {
  38. furi_hal_light_set(layer->light, layer->value[LayerInternal]);
  39. }
  40. }
  41. bool notification_is_any_led_layer_internal_and_not_empty(NotificationApp* app) {
  42. bool result = false;
  43. if((app->led[0].index == LayerInternal) || (app->led[1].index == LayerInternal) ||
  44. (app->led[2].index == LayerInternal)) {
  45. if((app->led[0].value[LayerInternal] != 0x00) ||
  46. (app->led[1].value[LayerInternal] != 0x00) ||
  47. (app->led[2].value[LayerInternal] != 0x00)) {
  48. result = true;
  49. }
  50. }
  51. return result;
  52. }
  53. // notification layer
  54. void notification_apply_notification_led_layer(
  55. NotificationLedLayer* layer,
  56. const uint8_t layer_value) {
  57. furi_assert(layer);
  58. furi_assert(layer->index < LayerMAX);
  59. // set value
  60. layer->index = LayerNotification;
  61. // set layer
  62. layer->value[LayerNotification] = layer_value;
  63. // apply
  64. furi_hal_light_set(layer->light, layer->value[LayerNotification]);
  65. }
  66. void notification_reset_notification_led_layer(NotificationLedLayer* layer) {
  67. furi_assert(layer);
  68. furi_assert(layer->index < LayerMAX);
  69. // set value
  70. layer->value[LayerNotification] = 0;
  71. // set layer
  72. layer->index = LayerInternal;
  73. // apply
  74. furi_hal_light_set(layer->light, layer->value[LayerInternal]);
  75. }
  76. void notification_reset_notification_layer(NotificationApp* app, uint8_t reset_mask) {
  77. if(reset_mask & reset_red_mask) {
  78. notification_reset_notification_led_layer(&app->led[0]);
  79. }
  80. if(reset_mask & reset_green_mask) {
  81. notification_reset_notification_led_layer(&app->led[1]);
  82. }
  83. if(reset_mask & reset_blue_mask) {
  84. notification_reset_notification_led_layer(&app->led[2]);
  85. }
  86. if(reset_mask & reset_vibro_mask) {
  87. notification_vibro_off();
  88. }
  89. if(reset_mask & reset_sound_mask) {
  90. notification_sound_off();
  91. }
  92. if(reset_mask & reset_display_mask) {
  93. osTimerStart(app->display_timer, notification_settings_display_off_delay_ticks(app));
  94. }
  95. }
  96. static void notification_apply_notification_leds(NotificationApp* app, const uint8_t* values) {
  97. for(uint8_t i = 0; i < NOTIFICATION_LED_COUNT; i++) {
  98. notification_apply_notification_led_layer(
  99. &app->led[i], notification_settings_get_rgb_led_brightness(app, values[i]));
  100. }
  101. }
  102. // settings
  103. uint8_t notification_settings_get_display_brightness(NotificationApp* app, uint8_t value) {
  104. return (value * app->settings.display_brightness);
  105. }
  106. uint8_t notification_settings_get_rgb_led_brightness(NotificationApp* app, uint8_t value) {
  107. return (value * app->settings.led_brightness);
  108. }
  109. uint32_t notification_settings_display_off_delay_ticks(NotificationApp* app) {
  110. return ((float)(app->settings.display_off_delay_ms) / (1000.0f / osKernelGetTickFreq()));
  111. }
  112. // generics
  113. void notification_vibro_on() {
  114. furi_hal_vibro_on(true);
  115. }
  116. void notification_vibro_off() {
  117. furi_hal_vibro_on(false);
  118. }
  119. void notification_sound_on(float freq, float volume) {
  120. furi_hal_speaker_start(freq, volume);
  121. }
  122. void notification_sound_off() {
  123. furi_hal_speaker_stop();
  124. }
  125. // display timer
  126. static void notification_display_timer(void* ctx) {
  127. furi_assert(ctx);
  128. NotificationApp* app = ctx;
  129. notification_message(app, &sequence_display_backlight_off);
  130. }
  131. // message processing
  132. void notification_process_notification_message(
  133. NotificationApp* app,
  134. NotificationAppMessage* message) {
  135. uint32_t notification_message_index = 0;
  136. const NotificationMessage* notification_message;
  137. notification_message = (*message->sequence)[notification_message_index];
  138. bool led_active = false;
  139. uint8_t led_values[NOTIFICATION_LED_COUNT] = {0x00, 0x00, 0x00};
  140. bool reset_notifications = true;
  141. float speaker_volume_setting = app->settings.speaker_volume;
  142. bool vibro_setting = app->settings.vibro_on;
  143. float display_brightness_setting = app->settings.display_brightness;
  144. uint8_t reset_mask = 0;
  145. while(notification_message != NULL) {
  146. switch(notification_message->type) {
  147. case NotificationMessageTypeLedDisplayBacklight:
  148. // if on - switch on and start timer
  149. // if off - switch off and stop timer
  150. // on timer - switch off
  151. if(notification_message->data.led.value > 0x00) {
  152. notification_apply_notification_led_layer(
  153. &app->display,
  154. notification_message->data.led.value * display_brightness_setting);
  155. } else {
  156. notification_reset_notification_led_layer(&app->display);
  157. if(osTimerIsRunning(app->display_timer)) {
  158. osTimerStop(app->display_timer);
  159. }
  160. }
  161. reset_mask |= reset_display_mask;
  162. break;
  163. case NotificationMessageTypeLedDisplayBacklightEnforceOn:
  164. furi_assert(app->display_led_lock < UINT8_MAX);
  165. app->display_led_lock++;
  166. if(app->display_led_lock == 1) {
  167. notification_apply_internal_led_layer(
  168. &app->display,
  169. notification_message->data.led.value * display_brightness_setting);
  170. }
  171. break;
  172. case NotificationMessageTypeLedDisplayBacklightEnforceAuto:
  173. furi_assert(app->display_led_lock > 0);
  174. app->display_led_lock--;
  175. if(app->display_led_lock == 0) {
  176. notification_apply_internal_led_layer(
  177. &app->display,
  178. notification_message->data.led.value * display_brightness_setting);
  179. }
  180. break;
  181. case NotificationMessageTypeLedRed:
  182. // store and send on delay or after seq
  183. led_active = true;
  184. led_values[0] = notification_message->data.led.value;
  185. app->led[0].value_last[LayerNotification] = led_values[0];
  186. reset_mask |= reset_red_mask;
  187. break;
  188. case NotificationMessageTypeLedGreen:
  189. // store and send on delay or after seq
  190. led_active = true;
  191. led_values[1] = notification_message->data.led.value;
  192. app->led[1].value_last[LayerNotification] = led_values[1];
  193. reset_mask |= reset_green_mask;
  194. break;
  195. case NotificationMessageTypeLedBlue:
  196. // store and send on delay or after seq
  197. led_active = true;
  198. led_values[2] = notification_message->data.led.value;
  199. app->led[2].value_last[LayerNotification] = led_values[2];
  200. reset_mask |= reset_blue_mask;
  201. break;
  202. case NotificationMessageTypeVibro:
  203. if(notification_message->data.vibro.on) {
  204. if(vibro_setting) notification_vibro_on();
  205. } else {
  206. notification_vibro_off();
  207. }
  208. reset_mask |= reset_vibro_mask;
  209. break;
  210. case NotificationMessageTypeSoundOn:
  211. notification_sound_on(
  212. notification_message->data.sound.frequency,
  213. notification_message->data.sound.volume * speaker_volume_setting);
  214. reset_mask |= reset_sound_mask;
  215. break;
  216. case NotificationMessageTypeSoundOff:
  217. notification_sound_off();
  218. reset_mask |= reset_sound_mask;
  219. break;
  220. case NotificationMessageTypeDelay:
  221. if(led_active) {
  222. if(notification_is_any_led_layer_internal_and_not_empty(app)) {
  223. notification_apply_notification_leds(app, led_off_values);
  224. furi_hal_delay_ms(minimal_delay);
  225. }
  226. led_active = false;
  227. notification_apply_notification_leds(app, led_values);
  228. reset_mask |= reset_red_mask;
  229. reset_mask |= reset_green_mask;
  230. reset_mask |= reset_blue_mask;
  231. }
  232. furi_hal_delay_ms(notification_message->data.delay.length);
  233. break;
  234. case NotificationMessageTypeDoNotReset:
  235. reset_notifications = false;
  236. break;
  237. case NotificationMessageTypeForceSpeakerVolumeSetting:
  238. speaker_volume_setting = notification_message->data.forced_settings.speaker_volume;
  239. break;
  240. case NotificationMessageTypeForceVibroSetting:
  241. vibro_setting = notification_message->data.forced_settings.vibro;
  242. break;
  243. case NotificationMessageTypeForceDisplayBrightnessSetting:
  244. display_brightness_setting =
  245. notification_message->data.forced_settings.display_brightness;
  246. break;
  247. case NotificationMessageTypeLedBrightnessSettingApply:
  248. led_active = true;
  249. for(uint8_t i = 0; i < NOTIFICATION_LED_COUNT; i++) {
  250. led_values[i] = app->led[i].value_last[LayerNotification];
  251. }
  252. reset_mask |= reset_red_mask;
  253. reset_mask |= reset_green_mask;
  254. reset_mask |= reset_blue_mask;
  255. break;
  256. }
  257. notification_message_index++;
  258. notification_message = (*message->sequence)[notification_message_index];
  259. };
  260. // send and do minimal delay
  261. if(led_active) {
  262. bool need_minimal_delay = false;
  263. if(notification_is_any_led_layer_internal_and_not_empty(app)) {
  264. need_minimal_delay = true;
  265. }
  266. led_active = false;
  267. notification_apply_notification_leds(app, led_values);
  268. reset_mask |= reset_red_mask;
  269. reset_mask |= reset_green_mask;
  270. reset_mask |= reset_blue_mask;
  271. if(need_minimal_delay) {
  272. notification_apply_notification_leds(app, led_off_values);
  273. furi_hal_delay_ms(minimal_delay);
  274. }
  275. }
  276. if(reset_notifications) {
  277. notification_reset_notification_layer(app, reset_mask);
  278. }
  279. }
  280. void notification_process_internal_message(NotificationApp* app, NotificationAppMessage* message) {
  281. uint32_t notification_message_index = 0;
  282. const NotificationMessage* notification_message;
  283. notification_message = (*message->sequence)[notification_message_index];
  284. while(notification_message != NULL) {
  285. switch(notification_message->type) {
  286. case NotificationMessageTypeLedDisplayBacklight:
  287. notification_apply_internal_led_layer(
  288. &app->display,
  289. notification_settings_get_display_brightness(
  290. app, notification_message->data.led.value));
  291. break;
  292. case NotificationMessageTypeLedRed:
  293. app->led[0].value_last[LayerInternal] = notification_message->data.led.value;
  294. notification_apply_internal_led_layer(
  295. &app->led[0],
  296. notification_settings_get_rgb_led_brightness(
  297. app, notification_message->data.led.value));
  298. break;
  299. case NotificationMessageTypeLedGreen:
  300. app->led[1].value_last[LayerInternal] = notification_message->data.led.value;
  301. notification_apply_internal_led_layer(
  302. &app->led[1],
  303. notification_settings_get_rgb_led_brightness(
  304. app, notification_message->data.led.value));
  305. break;
  306. case NotificationMessageTypeLedBlue:
  307. app->led[2].value_last[LayerInternal] = notification_message->data.led.value;
  308. notification_apply_internal_led_layer(
  309. &app->led[2],
  310. notification_settings_get_rgb_led_brightness(
  311. app, notification_message->data.led.value));
  312. break;
  313. case NotificationMessageTypeLedBrightnessSettingApply:
  314. for(uint8_t i = 0; i < NOTIFICATION_LED_COUNT; i++) {
  315. uint8_t new_val = notification_settings_get_rgb_led_brightness(
  316. app, app->led[i].value_last[LayerInternal]);
  317. notification_apply_internal_led_layer(&app->led[i], new_val);
  318. }
  319. break;
  320. default:
  321. break;
  322. }
  323. notification_message_index++;
  324. notification_message = (*message->sequence)[notification_message_index];
  325. }
  326. }
  327. static bool notification_load_settings(NotificationApp* app) {
  328. NotificationSettings settings;
  329. File* file = storage_file_alloc(furi_record_open("storage"));
  330. const size_t settings_size = sizeof(NotificationSettings);
  331. FURI_LOG_I(TAG, "loading settings from \"%s\"", NOTIFICATION_SETTINGS_PATH);
  332. bool fs_result =
  333. storage_file_open(file, NOTIFICATION_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  334. if(fs_result) {
  335. uint16_t bytes_count = storage_file_read(file, &settings, settings_size);
  336. if(bytes_count != settings_size) {
  337. fs_result = false;
  338. }
  339. }
  340. if(fs_result) {
  341. FURI_LOG_I(TAG, "load success");
  342. if(settings.version != NOTIFICATION_SETTINGS_VERSION) {
  343. FURI_LOG_E(
  344. TAG, "version(%d != %d) mismatch", settings.version, NOTIFICATION_SETTINGS_VERSION);
  345. } else {
  346. osKernelLock();
  347. memcpy(&app->settings, &settings, settings_size);
  348. osKernelUnlock();
  349. }
  350. } else {
  351. FURI_LOG_E(TAG, "load failed, %s", storage_file_get_error_desc(file));
  352. }
  353. storage_file_close(file);
  354. storage_file_free(file);
  355. furi_record_close("storage");
  356. return fs_result;
  357. };
  358. static bool notification_save_settings(NotificationApp* app) {
  359. NotificationSettings settings;
  360. File* file = storage_file_alloc(furi_record_open("storage"));
  361. const size_t settings_size = sizeof(NotificationSettings);
  362. FURI_LOG_I(TAG, "saving settings to \"%s\"", NOTIFICATION_SETTINGS_PATH);
  363. osKernelLock();
  364. memcpy(&settings, &app->settings, settings_size);
  365. osKernelUnlock();
  366. bool fs_result =
  367. storage_file_open(file, NOTIFICATION_SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  368. if(fs_result) {
  369. uint16_t bytes_count = storage_file_write(file, &settings, settings_size);
  370. if(bytes_count != settings_size) {
  371. fs_result = false;
  372. }
  373. }
  374. if(fs_result) {
  375. FURI_LOG_I(TAG, "save success");
  376. } else {
  377. FURI_LOG_E(TAG, "save failed, %s", storage_file_get_error_desc(file));
  378. }
  379. storage_file_close(file);
  380. storage_file_free(file);
  381. furi_record_close("storage");
  382. return fs_result;
  383. };
  384. static void input_event_callback(const void* value, void* context) {
  385. furi_assert(value);
  386. furi_assert(context);
  387. NotificationApp* app = context;
  388. notification_message(app, &sequence_display_backlight_on);
  389. }
  390. // App alloc
  391. static NotificationApp* notification_app_alloc() {
  392. NotificationApp* app = malloc(sizeof(NotificationApp));
  393. app->queue = osMessageQueueNew(8, sizeof(NotificationAppMessage), NULL);
  394. app->display_timer = osTimerNew(notification_display_timer, osTimerOnce, app, NULL);
  395. app->settings.speaker_volume = 1.0f;
  396. app->settings.display_brightness = 1.0f;
  397. app->settings.led_brightness = 1.0f;
  398. app->settings.display_off_delay_ms = 30000;
  399. app->settings.vibro_on = true;
  400. app->display.value[LayerInternal] = 0x00;
  401. app->display.value[LayerNotification] = 0x00;
  402. app->display.index = LayerInternal;
  403. app->display.light = LightBacklight;
  404. app->led[0].value[LayerInternal] = 0x00;
  405. app->led[0].value[LayerNotification] = 0x00;
  406. app->led[0].index = LayerInternal;
  407. app->led[0].light = LightRed;
  408. app->led[1].value[LayerInternal] = 0x00;
  409. app->led[1].value[LayerNotification] = 0x00;
  410. app->led[1].index = LayerInternal;
  411. app->led[1].light = LightGreen;
  412. app->led[2].value[LayerInternal] = 0x00;
  413. app->led[2].value[LayerNotification] = 0x00;
  414. app->led[2].index = LayerInternal;
  415. app->led[2].light = LightBlue;
  416. app->settings.version = NOTIFICATION_SETTINGS_VERSION;
  417. // display backlight control
  418. app->event_record = furi_record_open("input_events");
  419. furi_pubsub_subscribe(app->event_record, input_event_callback, app);
  420. notification_message(app, &sequence_display_backlight_on);
  421. return app;
  422. };
  423. // App
  424. int32_t notification_srv(void* p) {
  425. NotificationApp* app = notification_app_alloc();
  426. if(!notification_load_settings(app)) {
  427. notification_save_settings(app);
  428. }
  429. notification_vibro_off();
  430. notification_sound_off();
  431. notification_apply_internal_led_layer(&app->display, 0x00);
  432. notification_apply_internal_led_layer(&app->led[0], 0x00);
  433. notification_apply_internal_led_layer(&app->led[1], 0x00);
  434. notification_apply_internal_led_layer(&app->led[2], 0x00);
  435. furi_record_create("notification", app);
  436. NotificationAppMessage message;
  437. while(1) {
  438. furi_check(osMessageQueueGet(app->queue, &message, NULL, osWaitForever) == osOK);
  439. switch(message.type) {
  440. case NotificationLayerMessage:
  441. notification_process_notification_message(app, &message);
  442. break;
  443. case InternalLayerMessage:
  444. notification_process_internal_message(app, &message);
  445. break;
  446. case SaveSettingsMessage:
  447. notification_save_settings(app);
  448. break;
  449. }
  450. if(message.back_event != NULL) {
  451. osEventFlagsSet(message.back_event, NOTIFICATION_EVENT_COMPLETE);
  452. }
  453. }
  454. return 0;
  455. };