notification_app.c 20 KB

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