notification-app.c 16 KB

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