notification_app.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. if(furi_hal_speaker_is_mine() || furi_hal_speaker_acquire(30)) {
  130. furi_hal_speaker_start(freq, volume);
  131. }
  132. }
  133. void notification_sound_off() {
  134. if(furi_hal_speaker_is_mine()) {
  135. furi_hal_speaker_stop();
  136. furi_hal_speaker_release();
  137. }
  138. }
  139. // display timer
  140. static void notification_display_timer(void* ctx) {
  141. furi_assert(ctx);
  142. NotificationApp* app = ctx;
  143. notification_message(app, &sequence_display_backlight_off);
  144. }
  145. // message processing
  146. void notification_process_notification_message(
  147. NotificationApp* app,
  148. NotificationAppMessage* message) {
  149. uint32_t notification_message_index = 0;
  150. const NotificationMessage* notification_message;
  151. notification_message = (*message->sequence)[notification_message_index];
  152. bool led_active = false;
  153. uint8_t led_values[NOTIFICATION_LED_COUNT] = {0x00, 0x00, 0x00};
  154. bool reset_notifications = true;
  155. float speaker_volume_setting = app->settings.speaker_volume;
  156. bool vibro_setting = app->settings.vibro_on;
  157. float display_brightness_setting = app->settings.display_brightness;
  158. uint8_t reset_mask = 0;
  159. while(notification_message != NULL) {
  160. switch(notification_message->type) {
  161. case NotificationMessageTypeLedDisplayBacklight:
  162. // if on - switch on and start timer
  163. // if off - switch off and stop timer
  164. // on timer - switch off
  165. if(notification_message->data.led.value > 0x00) {
  166. notification_apply_notification_led_layer(
  167. &app->display,
  168. notification_message->data.led.value * display_brightness_setting);
  169. } else {
  170. notification_reset_notification_led_layer(&app->display);
  171. if(furi_timer_is_running(app->display_timer)) {
  172. furi_timer_stop(app->display_timer);
  173. }
  174. }
  175. reset_mask |= reset_display_mask;
  176. break;
  177. case NotificationMessageTypeLedDisplayBacklightEnforceOn:
  178. furi_assert(app->display_led_lock < UINT8_MAX);
  179. app->display_led_lock++;
  180. if(app->display_led_lock == 1) {
  181. notification_apply_internal_led_layer(
  182. &app->display,
  183. notification_message->data.led.value * display_brightness_setting);
  184. }
  185. break;
  186. case NotificationMessageTypeLedDisplayBacklightEnforceAuto:
  187. furi_assert(app->display_led_lock > 0);
  188. app->display_led_lock--;
  189. if(app->display_led_lock == 0) {
  190. notification_apply_internal_led_layer(
  191. &app->display,
  192. notification_message->data.led.value * display_brightness_setting);
  193. }
  194. break;
  195. case NotificationMessageTypeLedRed:
  196. // store and send on delay or after seq
  197. led_active = true;
  198. led_values[0] = notification_message->data.led.value;
  199. app->led[0].value_last[LayerNotification] = led_values[0];
  200. reset_mask |= reset_red_mask;
  201. break;
  202. case NotificationMessageTypeLedGreen:
  203. // store and send on delay or after seq
  204. led_active = true;
  205. led_values[1] = notification_message->data.led.value;
  206. app->led[1].value_last[LayerNotification] = led_values[1];
  207. reset_mask |= reset_green_mask;
  208. break;
  209. case NotificationMessageTypeLedBlue:
  210. // store and send on delay or after seq
  211. led_active = true;
  212. led_values[2] = notification_message->data.led.value;
  213. app->led[2].value_last[LayerNotification] = led_values[2];
  214. reset_mask |= reset_blue_mask;
  215. break;
  216. case NotificationMessageTypeLedBlinkStart:
  217. // store and send on delay or after seq
  218. led_active = true;
  219. furi_hal_light_blink_start(
  220. notification_message->data.led_blink.color,
  221. app->settings.led_brightness * 255,
  222. notification_message->data.led_blink.on_time,
  223. notification_message->data.led_blink.period);
  224. reset_mask |= reset_blink_mask;
  225. reset_mask |= reset_red_mask;
  226. reset_mask |= reset_green_mask;
  227. reset_mask |= reset_blue_mask;
  228. break;
  229. case NotificationMessageTypeLedBlinkColor:
  230. led_active = true;
  231. furi_hal_light_blink_set_color(notification_message->data.led_blink.color);
  232. break;
  233. case NotificationMessageTypeLedBlinkStop:
  234. furi_hal_light_blink_stop();
  235. reset_mask &= ~reset_blink_mask;
  236. reset_mask |= reset_red_mask;
  237. reset_mask |= reset_green_mask;
  238. reset_mask |= reset_blue_mask;
  239. break;
  240. case NotificationMessageTypeVibro:
  241. if(notification_message->data.vibro.on) {
  242. if(vibro_setting) notification_vibro_on();
  243. } else {
  244. notification_vibro_off();
  245. }
  246. reset_mask |= reset_vibro_mask;
  247. break;
  248. case NotificationMessageTypeSoundOn:
  249. notification_sound_on(
  250. notification_message->data.sound.frequency,
  251. notification_message->data.sound.volume * speaker_volume_setting);
  252. reset_mask |= reset_sound_mask;
  253. break;
  254. case NotificationMessageTypeSoundOff:
  255. notification_sound_off();
  256. reset_mask |= reset_sound_mask;
  257. break;
  258. case NotificationMessageTypeDelay:
  259. if(led_active) {
  260. if(notification_is_any_led_layer_internal_and_not_empty(app)) {
  261. notification_apply_notification_leds(app, led_off_values);
  262. furi_delay_ms(minimal_delay);
  263. }
  264. led_active = false;
  265. notification_apply_notification_leds(app, led_values);
  266. reset_mask |= reset_red_mask;
  267. reset_mask |= reset_green_mask;
  268. reset_mask |= reset_blue_mask;
  269. }
  270. furi_delay_ms(notification_message->data.delay.length);
  271. break;
  272. case NotificationMessageTypeDoNotReset:
  273. reset_notifications = false;
  274. break;
  275. case NotificationMessageTypeForceSpeakerVolumeSetting:
  276. speaker_volume_setting = notification_message->data.forced_settings.speaker_volume;
  277. break;
  278. case NotificationMessageTypeForceVibroSetting:
  279. vibro_setting = notification_message->data.forced_settings.vibro;
  280. break;
  281. case NotificationMessageTypeForceDisplayBrightnessSetting:
  282. display_brightness_setting =
  283. notification_message->data.forced_settings.display_brightness;
  284. break;
  285. case NotificationMessageTypeLedBrightnessSettingApply:
  286. led_active = true;
  287. for(uint8_t i = 0; i < NOTIFICATION_LED_COUNT; i++) {
  288. led_values[i] = app->led[i].value_last[LayerNotification];
  289. }
  290. reset_mask |= reset_red_mask;
  291. reset_mask |= reset_green_mask;
  292. reset_mask |= reset_blue_mask;
  293. break;
  294. }
  295. notification_message_index++;
  296. notification_message = (*message->sequence)[notification_message_index];
  297. };
  298. // send and do minimal delay
  299. if(led_active) {
  300. bool need_minimal_delay = false;
  301. if(notification_is_any_led_layer_internal_and_not_empty(app)) {
  302. need_minimal_delay = true;
  303. }
  304. notification_apply_notification_leds(app, led_values);
  305. reset_mask |= reset_red_mask;
  306. reset_mask |= reset_green_mask;
  307. reset_mask |= reset_blue_mask;
  308. if((need_minimal_delay) && (reset_notifications)) {
  309. notification_apply_notification_leds(app, led_off_values);
  310. furi_delay_ms(minimal_delay);
  311. }
  312. }
  313. if(reset_notifications) {
  314. notification_reset_notification_layer(app, reset_mask);
  315. }
  316. }
  317. void notification_process_internal_message(NotificationApp* app, NotificationAppMessage* message) {
  318. uint32_t notification_message_index = 0;
  319. const NotificationMessage* notification_message;
  320. notification_message = (*message->sequence)[notification_message_index];
  321. while(notification_message != NULL) {
  322. switch(notification_message->type) {
  323. case NotificationMessageTypeLedDisplayBacklight:
  324. notification_apply_internal_led_layer(
  325. &app->display,
  326. notification_settings_get_display_brightness(
  327. app, notification_message->data.led.value));
  328. break;
  329. case NotificationMessageTypeLedRed:
  330. app->led[0].value_last[LayerInternal] = notification_message->data.led.value;
  331. notification_apply_internal_led_layer(
  332. &app->led[0],
  333. notification_settings_get_rgb_led_brightness(
  334. app, notification_message->data.led.value));
  335. break;
  336. case NotificationMessageTypeLedGreen:
  337. app->led[1].value_last[LayerInternal] = notification_message->data.led.value;
  338. notification_apply_internal_led_layer(
  339. &app->led[1],
  340. notification_settings_get_rgb_led_brightness(
  341. app, notification_message->data.led.value));
  342. break;
  343. case NotificationMessageTypeLedBlue:
  344. app->led[2].value_last[LayerInternal] = notification_message->data.led.value;
  345. notification_apply_internal_led_layer(
  346. &app->led[2],
  347. notification_settings_get_rgb_led_brightness(
  348. app, notification_message->data.led.value));
  349. break;
  350. case NotificationMessageTypeLedBrightnessSettingApply:
  351. for(uint8_t i = 0; i < NOTIFICATION_LED_COUNT; i++) {
  352. uint8_t new_val = notification_settings_get_rgb_led_brightness(
  353. app, app->led[i].value_last[LayerInternal]);
  354. notification_apply_internal_led_layer(&app->led[i], new_val);
  355. }
  356. break;
  357. default:
  358. break;
  359. }
  360. notification_message_index++;
  361. notification_message = (*message->sequence)[notification_message_index];
  362. }
  363. }
  364. static bool notification_load_settings(NotificationApp* app) {
  365. NotificationSettings settings;
  366. File* file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
  367. const size_t settings_size = sizeof(NotificationSettings);
  368. FURI_LOG_I(TAG, "loading settings from \"%s\"", NOTIFICATION_SETTINGS_PATH);
  369. bool fs_result =
  370. storage_file_open(file, NOTIFICATION_SETTINGS_PATH, FSAM_READ, FSOM_OPEN_EXISTING);
  371. if(fs_result) {
  372. uint16_t bytes_count = storage_file_read(file, &settings, settings_size);
  373. if(bytes_count != settings_size) {
  374. fs_result = false;
  375. }
  376. }
  377. if(fs_result) {
  378. FURI_LOG_I(TAG, "load success");
  379. if(settings.version != NOTIFICATION_SETTINGS_VERSION) {
  380. FURI_LOG_E(
  381. TAG, "version(%d != %d) mismatch", settings.version, NOTIFICATION_SETTINGS_VERSION);
  382. } else {
  383. furi_kernel_lock();
  384. memcpy(&app->settings, &settings, settings_size);
  385. furi_kernel_unlock();
  386. }
  387. } else {
  388. FURI_LOG_E(TAG, "load failed, %s", storage_file_get_error_desc(file));
  389. }
  390. storage_file_close(file);
  391. storage_file_free(file);
  392. furi_record_close(RECORD_STORAGE);
  393. return fs_result;
  394. };
  395. static bool notification_save_settings(NotificationApp* app) {
  396. NotificationSettings settings;
  397. File* file = storage_file_alloc(furi_record_open(RECORD_STORAGE));
  398. const size_t settings_size = sizeof(NotificationSettings);
  399. FURI_LOG_I(TAG, "saving settings to \"%s\"", NOTIFICATION_SETTINGS_PATH);
  400. furi_kernel_lock();
  401. memcpy(&settings, &app->settings, settings_size);
  402. furi_kernel_unlock();
  403. bool fs_result =
  404. storage_file_open(file, NOTIFICATION_SETTINGS_PATH, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  405. if(fs_result) {
  406. uint16_t bytes_count = storage_file_write(file, &settings, settings_size);
  407. if(bytes_count != settings_size) {
  408. fs_result = false;
  409. }
  410. }
  411. if(fs_result) {
  412. FURI_LOG_I(TAG, "save success");
  413. } else {
  414. FURI_LOG_E(TAG, "save failed, %s", storage_file_get_error_desc(file));
  415. }
  416. storage_file_close(file);
  417. storage_file_free(file);
  418. furi_record_close(RECORD_STORAGE);
  419. return fs_result;
  420. };
  421. static void input_event_callback(const void* value, void* context) {
  422. furi_assert(value);
  423. furi_assert(context);
  424. NotificationApp* app = context;
  425. notification_message(app, &sequence_display_backlight_on);
  426. }
  427. // App alloc
  428. static NotificationApp* notification_app_alloc() {
  429. NotificationApp* app = malloc(sizeof(NotificationApp));
  430. app->queue = furi_message_queue_alloc(8, sizeof(NotificationAppMessage));
  431. app->display_timer = furi_timer_alloc(notification_display_timer, FuriTimerTypeOnce, app);
  432. app->settings.speaker_volume = 1.0f;
  433. app->settings.display_brightness = 1.0f;
  434. app->settings.led_brightness = 1.0f;
  435. app->settings.display_off_delay_ms = 30000;
  436. app->settings.vibro_on = true;
  437. app->display.value[LayerInternal] = 0x00;
  438. app->display.value[LayerNotification] = 0x00;
  439. app->display.index = LayerInternal;
  440. app->display.light = LightBacklight;
  441. app->led[0].value[LayerInternal] = 0x00;
  442. app->led[0].value[LayerNotification] = 0x00;
  443. app->led[0].index = LayerInternal;
  444. app->led[0].light = LightRed;
  445. app->led[1].value[LayerInternal] = 0x00;
  446. app->led[1].value[LayerNotification] = 0x00;
  447. app->led[1].index = LayerInternal;
  448. app->led[1].light = LightGreen;
  449. app->led[2].value[LayerInternal] = 0x00;
  450. app->led[2].value[LayerNotification] = 0x00;
  451. app->led[2].index = LayerInternal;
  452. app->led[2].light = LightBlue;
  453. app->settings.version = NOTIFICATION_SETTINGS_VERSION;
  454. // display backlight control
  455. app->event_record = furi_record_open(RECORD_INPUT_EVENTS);
  456. furi_pubsub_subscribe(app->event_record, input_event_callback, app);
  457. notification_message(app, &sequence_display_backlight_on);
  458. return app;
  459. };
  460. // App
  461. int32_t notification_srv(void* p) {
  462. UNUSED(p);
  463. NotificationApp* app = notification_app_alloc();
  464. if(!notification_load_settings(app)) {
  465. notification_save_settings(app);
  466. }
  467. notification_vibro_off();
  468. notification_sound_off();
  469. notification_apply_internal_led_layer(&app->display, 0x00);
  470. notification_apply_internal_led_layer(&app->led[0], 0x00);
  471. notification_apply_internal_led_layer(&app->led[1], 0x00);
  472. notification_apply_internal_led_layer(&app->led[2], 0x00);
  473. furi_record_create(RECORD_NOTIFICATION, app);
  474. NotificationAppMessage message;
  475. while(1) {
  476. furi_check(furi_message_queue_get(app->queue, &message, FuriWaitForever) == FuriStatusOk);
  477. switch(message.type) {
  478. case NotificationLayerMessage:
  479. notification_process_notification_message(app, &message);
  480. break;
  481. case InternalLayerMessage:
  482. notification_process_internal_message(app, &message);
  483. break;
  484. case SaveSettingsMessage:
  485. notification_save_settings(app);
  486. break;
  487. }
  488. if(message.back_event != NULL) {
  489. furi_event_flag_set(message.back_event, NOTIFICATION_EVENT_COMPLETE);
  490. }
  491. }
  492. return 0;
  493. };