diff --git a/applications/services/notification/notification_app.c b/applications/services/notification/notification_app.c index cf03d4389..3eca2fd2d 100644 --- a/applications/services/notification/notification_app.c +++ b/applications/services/notification/notification_app.c @@ -33,6 +33,51 @@ static uint8_t notification_settings_get_display_brightness(NotificationApp* app static uint8_t notification_settings_get_rgb_led_brightness(NotificationApp* app, uint8_t value); static uint32_t notification_settings_display_off_delay_ticks(NotificationApp* app); +// --- NIGHT SHIFT --- + +void night_shift_timer_start(NotificationApp* app) { + if(app->settings.night_shift != 1) { + furi_timer_start(app->night_shift_timer, furi_ms_to_ticks(2000)); + } +} + +void night_shift_timer_stop(NotificationApp* app) { + if(furi_timer_is_running(app->night_shift_timer)) { + furi_timer_stop(app->night_shift_timer); + } +} + +// every callback time we check current time and current night_shift_settings value +void night_shift_timer_callback(void* context) { + furi_assert(context); + NotificationApp* app = context; + DateTime current_date_time; + + // save current night_shift; + // float old_night_shift = app->current_night_shift; + + // take system time and convert to minutes + furi_hal_rtc_get_datetime(¤t_date_time); + uint32_t time = current_date_time.hour * 60 + current_date_time.minute; + + // if current time not in night_shift range then current_night_shift = 1 else = settings value; + // set values to stock and rgb backlights + if((time > app->settings.night_shift_end) && (time < app->settings.night_shift_start)) { + app->current_night_shift = 1.0f; + app->rgb_srv->current_night_shift = 1.0f; + } else { + app->current_night_shift = app->settings.night_shift; + app->rgb_srv->current_night_shift = app->settings.night_shift; + } + + // // if night shift was changed then update stock and rgb backlight to new value + // if(old_night_shift != app->current_night_shift) { + // notification_message(app, &sequence_display_backlight_on); + // } +} + +// --- NIGHT SHIFT END --- + void notification_message_save_settings(NotificationApp* app) { NotificationAppMessage m = { .type = SaveSettingsMessage, .back_event = furi_event_flag_alloc()}; @@ -129,7 +174,11 @@ static void notification_reset_notification_layer( } if(reset_mask & reset_display_mask) { if(!float_is_equal(display_brightness_set, app->settings.display_brightness)) { - furi_hal_light_set(LightBacklight, app->settings.display_brightness * 0xFF); + // --- NIGHT SHIFT --- + furi_hal_light_set( + LightBacklight, + app->settings.display_brightness * 0xFF * app->current_night_shift); + // --- NIGHT SHIFT END--- } furi_timer_start(app->display_timer, notification_settings_display_off_delay_ticks(app)); } @@ -214,15 +263,17 @@ static void notification_process_notification_message( // if on - switch on and start timer // if off - switch off and stop timer // on timer - switch off + // --- NIGHT SHIFT --- if(notification_message->data.led.value > 0x00) { notification_apply_notification_led_layer( &app->display, - notification_message->data.led.value * display_brightness_setting); + notification_message->data.led.value * display_brightness_setting * + app->current_night_shift); reset_mask |= reset_display_mask; //start rgb_mod_rainbow_timer when display backlight is ON and all corresponding settings is ON too rainbow_timer_starter(app->rgb_srv); - + // --- NIGHT SHIFT END --- } else { reset_mask &= ~reset_display_mask; notification_reset_notification_led_layer(&app->display); @@ -238,10 +289,12 @@ static void notification_process_notification_message( case NotificationMessageTypeLedDisplayBacklightEnforceOn: furi_check(app->display_led_lock < UINT8_MAX); app->display_led_lock++; + // --- NIGHT SHIFT --- if(app->display_led_lock == 1) { notification_apply_internal_led_layer( &app->display, - notification_message->data.led.value * display_brightness_setting); + notification_message->data.led.value * display_brightness_setting * + app->current_night_shift); } break; case NotificationMessageTypeLedDisplayBacklightEnforceAuto: @@ -250,8 +303,10 @@ static void notification_process_notification_message( if(app->display_led_lock == 0) { notification_apply_internal_led_layer( &app->display, - notification_message->data.led.value * display_brightness_setting); + notification_message->data.led.value * display_brightness_setting * + app->current_night_shift); } + // --- NIGHT SHIFT END --- } else { FURI_LOG_E(TAG, "Incorrect BacklightEnforce use"); } @@ -559,6 +614,17 @@ static NotificationApp* notification_app_alloc(void) { furi_pubsub_subscribe(app->event_record, input_event_callback, app); notification_message(app, &sequence_display_backlight_on); + // --- NIGHT SHIFT --- + app->rgb_srv = furi_record_open(RECORD_RGB_BACKLIGHT); + app->rgb_srv->current_night_shift = 1.0f; + app->current_night_shift = 1.0f; + app->settings.night_shift = 1.0f; + app->settings.night_shift_start = 1020; + app->settings.night_shift_end = 300; + app->night_shift_timer = + furi_timer_alloc(night_shift_timer_callback, FuriTimerTypePeriodic, app); + // --- NIGHT SHIFT END --- + return app; } @@ -582,6 +648,13 @@ static void notification_apply_settings(NotificationApp* app) { } notification_apply_lcd_contrast(app); + + // --- NIGHT SHIFT --- + // if night_shift_enabled start timer for controlling current_night_shift multiplicator value depent from current time + if(app->settings.night_shift != 1) { + night_shift_timer_start(app); + } + // --- NIGHT SHIFT END --- } static void notification_init_settings(NotificationApp* app) { @@ -600,7 +673,7 @@ static void notification_init_settings(NotificationApp* app) { int32_t notification_srv(void* p) { UNUSED(p); NotificationApp* app = notification_app_alloc(); - app->rgb_srv = furi_record_open(RECORD_RGB_BACKLIGHT); + notification_init_settings(app); notification_vibro_off(); diff --git a/applications/services/notification/notification_app.h b/applications/services/notification/notification_app.h index 7ee421b31..0d4404fa5 100644 --- a/applications/services/notification/notification_app.h +++ b/applications/services/notification/notification_app.h @@ -34,7 +34,7 @@ typedef struct { Light light; } NotificationLedLayer; -#define NOTIFICATION_SETTINGS_VERSION 0x02 +#define NOTIFICATION_SETTINGS_VERSION 0x03 #define NOTIFICATION_SETTINGS_PATH INT_PATH(NOTIFICATION_SETTINGS_FILE_NAME) typedef struct { @@ -61,6 +61,11 @@ struct NotificationApp { NotificationSettings settings; RGBBacklightApp* rgb_srv; + + FuriTimer* night_shift_timer; + float current_night_shift; }; void notification_message_save_settings(NotificationApp* app); +void night_shift_timer_start(NotificationApp* app); +void night_shift_timer_stop(NotificationApp* app); diff --git a/applications/services/rgb_backlight/rgb_backlight.c b/applications/services/rgb_backlight/rgb_backlight.c index 9dc60a5ec..8862ac154 100644 --- a/applications/services/rgb_backlight/rgb_backlight.c +++ b/applications/services/rgb_backlight/rgb_backlight.c @@ -137,9 +137,9 @@ void rgb_backlight_update(float brightness) { if(app->settings->rgb_backlight_installed) { for(uint8_t i = 0; i < SK6805_get_led_count(); i++) { - uint8_t r = current_led[i].red * (brightness * 1.0f); - uint8_t g = current_led[i].green * (brightness * 1.0f); - uint8_t b = current_led[i].blue * (brightness * 1.0f); + uint8_t r = current_led[i].red * brightness; + uint8_t g = current_led[i].green * brightness; + uint8_t b = current_led[i].blue * brightness; SK6805_set_led_color(i, r, g, b); } SK6805_update(); @@ -154,7 +154,7 @@ void rainbow_timer_start(RGBBacklightApp* app) { // stop furi timer for rainbow void rainbow_timer_stop(RGBBacklightApp* app) { - if (furi_timer_is_running (app->rainbow_timer)){ + if(furi_timer_is_running(app->rainbow_timer)) { furi_timer_stop(app->rainbow_timer); } } @@ -214,7 +214,7 @@ static void rainbow_timer_callback(void* context) { break; } - rgb_backlight_update(app->settings->brightness); + rgb_backlight_update(app->settings->brightness * app->current_night_shift); } } @@ -233,6 +233,7 @@ int32_t rgb_backlight_srv(void* p) { rgb_backlight_settings_load(app->settings); app->rainbow_hue = 1; + app->current_night_shift = 1.0f; furi_record_create(RECORD_RGB_BACKLIGHT, app); @@ -244,7 +245,7 @@ int32_t rgb_backlight_srv(void* p) { rgb_backlight_set_led_static_color(2, app->settings->led_2_color_index); rgb_backlight_set_led_static_color(1, app->settings->led_1_color_index); rgb_backlight_set_led_static_color(0, app->settings->led_0_color_index); - rgb_backlight_update(app->settings->brightness); + rgb_backlight_update(app->settings->brightness * app->current_night_shift); } // if rgb_backlight not installed then set default static orange color(index=0) to all leds (0-2) and force light on } else { diff --git a/applications/services/rgb_backlight/rgb_backlight.h b/applications/services/rgb_backlight/rgb_backlight.h index d6f8b5fce..1be252dc1 100644 --- a/applications/services/rgb_backlight/rgb_backlight.h +++ b/applications/services/rgb_backlight/rgb_backlight.h @@ -33,6 +33,9 @@ typedef struct { uint8_t rainbow_green; uint8_t rainbow_blue; RGBBacklightSettings* settings; + // night_shift multiplicator for leds brightnes coming from Notificatoin app. + float current_night_shift; + } RGBBacklightApp; #define RECORD_RGB_BACKLIGHT "rgb_backlight" diff --git a/applications/services/rgb_backlight/rgb_backlight_settings.c b/applications/services/rgb_backlight/rgb_backlight_settings.c index b76c6598a..17f6bb271 100644 --- a/applications/services/rgb_backlight/rgb_backlight_settings.c +++ b/applications/services/rgb_backlight/rgb_backlight_settings.c @@ -30,6 +30,7 @@ typedef struct { uint16_t rainbow_step; uint8_t rainbow_saturation; uint8_t rainbow_wide; + } RGBBacklightSettingsPrevious; void rgb_backlight_settings_load(RGBBacklightSettings* settings) { diff --git a/applications/services/rgb_backlight/rgb_backlight_settings.h b/applications/services/rgb_backlight/rgb_backlight_settings.h index 3f3af005f..b1e4a4a01 100644 --- a/applications/services/rgb_backlight/rgb_backlight_settings.h +++ b/applications/services/rgb_backlight/rgb_backlight_settings.h @@ -20,7 +20,7 @@ typedef struct { uint16_t rainbow_step; uint8_t rainbow_saturation; uint8_t rainbow_wide; - + } RGBBacklightSettings; #ifdef __cplusplus diff --git a/applications/settings/notification_settings/notification_settings_app.c b/applications/settings/notification_settings/notification_settings_app.c index 435a0087c..86176c8e5 100644 --- a/applications/settings/notification_settings/notification_settings_app.c +++ b/applications/settings/notification_settings/notification_settings_app.c @@ -182,65 +182,93 @@ typedef enum { // --- RGB BACKLIGHT END --- - // --- NIGHT SHIFT --- - #define NIGHT_SHIFT_COUNT 7 - const char* const night_shift_text[NIGHT_SHIFT_COUNT] = { - "OFF", - "5%", - "10%" - "15%", - "20%", - "25%", - "30%" +// --- NIGHT SHIFT --- +#define NIGHT_SHIFT_COUNT 7 +const char* const night_shift_text[NIGHT_SHIFT_COUNT] = + {"OFF", "-10%", "-20%", "-30%", "-40%", "-50%", "-60%" - }; - const float night_shift_value[NIGHT_SHIFT_COUNT] = { - 0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, - }; +}; +const float night_shift_value[NIGHT_SHIFT_COUNT] = { + 1.0f, + 0.9f, + 0.8f, + 0.7f, + 0.6f, + 0.5f, + 0.4f, +}; - #define NIGHT_SHIFT_START_COUNT 14 - const char* const night_shift_start_text[NIGHT_SHIFT_START_COUNT] = { - "17:00", - "17:30", - "18:00" - "18:30", - "19:00", - "19:30", - "20:00", - "20:30", - "21:00", - "21:30", - "22:00", - "22:30", - "23:00", - "23:30", - }; - const uint32_t night_shift_start_value[NIGHT_SHIFT_START_COUNT] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - }; +#define NIGHT_SHIFT_START_COUNT 14 +const char* const night_shift_start_text[NIGHT_SHIFT_START_COUNT] = { + "17:00", + "17:30", + "18:00", + "18:30", + "19:00", + "19:30", + "20:00", + "20:30", + "21:00", + "21:30", + "22:00", + "22:30", + "23:00", + "23:30", +}; +// values in minutes like 23:30 = 23*60+30=1410 +const uint32_t night_shift_start_value[NIGHT_SHIFT_START_COUNT] = { + 1020, + 1050, + 1080, + 1110, + 1140, + 1170, + 1200, + 1230, + 1260, + 1290, + 1320, + 1350, + 1380, + 1410, +}; - #define NIGHT_SHIFT_END_COUNT 14 - const char* const night_shift_end_text[NIGHT_SHIFT_END_COUNT] = { - "05:00", - "05:30", - "06:00" - "06:30", - "07:00", - "07:30", - "08:00", - "08:30", - "09:00", - "09:30", - "10:00", - "10:30", - "11:00", - "11:30", - }; - const uint32_t night_shift_end_value[NIGHT_SHIFT_END_COUNT] = { - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - }; +#define NIGHT_SHIFT_END_COUNT 14 +const char* const night_shift_end_text[NIGHT_SHIFT_END_COUNT] = { + "05:00", + "05:30", + "06:00", + "06:30", + "07:00", + "07:30", + "08:00", + "08:30", + "09:00", + "09:30", + "10:00", + "10:30", + "11:00", + "11:30", +}; +// values in minutes like 6:30 = 6*60+30=390 +const uint32_t night_shift_end_value[NIGHT_SHIFT_END_COUNT] = { + 300, + 330, + 360, + 390, + 410, + 440, + 470, + 500, + 530, + 560, + 590, + 620, + 650, + 680, +}; - // --- NIGHT SHIFT END --- +// --- NIGHT SHIFT END --- static void contrast_changed(VariableItem* item) { NotificationAppSettings* app = variable_item_get_context(item); @@ -345,7 +373,9 @@ static void rgb_backlight_installed_changed(VariableItem* item) { 1, app->notification->rgb_srv->settings->led_1_color_index); rgb_backlight_set_led_static_color( 0, app->notification->rgb_srv->settings->led_0_color_index); - rgb_backlight_update(app->notification->settings.display_brightness); + rgb_backlight_update( + app->notification->settings.display_brightness * + app->notification->current_night_shift); } } @@ -376,7 +406,9 @@ static void led_2_color_changed(VariableItem* item) { // dont update screen color if rainbow timer working if(!furi_timer_is_running(app->notification->rgb_srv->rainbow_timer)) { rgb_backlight_set_led_static_color(2, index); - rgb_backlight_update(app->notification->rgb_srv->settings->brightness); + rgb_backlight_update( + app->notification->rgb_srv->settings->brightness * + app->notification->current_night_shift); } rgb_backlight_settings_save(app->notification->rgb_srv->settings); @@ -392,7 +424,9 @@ static void led_1_color_changed(VariableItem* item) { // dont update screen color if rainbow timer working if(!furi_timer_is_running(app->notification->rgb_srv->rainbow_timer)) { rgb_backlight_set_led_static_color(1, index); - rgb_backlight_update(app->notification->rgb_srv->settings->brightness); + rgb_backlight_update( + app->notification->rgb_srv->settings->brightness * + app->notification->current_night_shift); } rgb_backlight_settings_save(app->notification->rgb_srv->settings); @@ -408,7 +442,9 @@ static void led_0_color_changed(VariableItem* item) { // dont update screen color if rainbow timer working if(!furi_timer_is_running(app->notification->rgb_srv->rainbow_timer)) { rgb_backlight_set_led_static_color(0, index); - rgb_backlight_update(app->notification->rgb_srv->settings->brightness); + rgb_backlight_update( + app->notification->rgb_srv->settings->brightness * + app->notification->current_night_shift); } rgb_backlight_settings_save(app->notification->rgb_srv->settings); @@ -429,7 +465,9 @@ static void rgb_backlight_rainbow_changed(VariableItem* item) { 1, app->notification->rgb_srv->settings->led_1_color_index); rgb_backlight_set_led_static_color( 0, app->notification->rgb_srv->settings->led_0_color_index); - rgb_backlight_update(app->notification->rgb_srv->settings->brightness); + rgb_backlight_update( + app->notification->rgb_srv->settings->brightness * + app->notification->current_night_shift); rainbow_timer_stop(app->notification->rgb_srv); } else { rainbow_timer_starter(app->notification->rgb_srv); @@ -505,6 +543,60 @@ static uint32_t notification_app_rgb_settings_exit(void* context) { // --- NIGHT SHIFT --- +static void night_shift_changed(VariableItem* item) { + NotificationAppSettings* app = variable_item_get_context(item); + uint8_t index = variable_item_get_current_value_index(item); + + variable_item_set_current_value_text(item, night_shift_text[index]); + app->notification->settings.night_shift = night_shift_value[index]; + app->notification->current_night_shift = night_shift_value[index]; + app->notification->rgb_srv->current_night_shift = night_shift_value[index]; + + // force demo night_shift brightness ot rgb backlight and stock backlight + notification_message(app->notification, &sequence_display_backlight_on); + + int slide = 0; + if(furi_hal_rtc_is_flag_set(FuriHalRtcFlagDebug) || + (app->notification->rgb_srv->settings->rgb_backlight_installed)) { + slide = 1; + } + for(int i = 4 + slide; i < (6 + slide); i++) { + VariableItem* t_item = variable_item_list_get(app->variable_item_list, i); + if(index == 0) { + variable_item_set_locked(t_item, true, "Night shift\nOFF!"); + } else { + variable_item_set_locked(t_item, false, "Night shift\nOFF!"); + } + } + + if(night_shift_value[index] != 1) { + night_shift_timer_start(app->notification); + } else { + night_shift_timer_stop(app->notification); + } + + notification_message_save_settings(app->notification); +} + +static void night_shift_start_changed(VariableItem* item) { + NotificationAppSettings* app = variable_item_get_context(item); + uint8_t index = variable_item_get_current_value_index(item); + + variable_item_set_current_value_text(item, night_shift_start_text[index]); + app->notification->settings.night_shift_start = night_shift_start_value[index]; + + notification_message_save_settings(app->notification); +} + +static void night_shift_end_changed(VariableItem* item) { + NotificationAppSettings* app = variable_item_get_context(item); + uint8_t index = variable_item_get_current_value_index(item); + + variable_item_set_current_value_text(item, night_shift_end_text[index]); + app->notification->settings.night_shift_end = night_shift_end_value[index]; + + notification_message_save_settings(app->notification); +} // --- NIGHT SHIFT END --- @@ -562,28 +654,37 @@ static NotificationAppSettings* alloc_settings(void) { // --- NIGHT SHIFT --- item = variable_item_list_add( - app->variable_item_list, "Night Shift", NIGHT_SHIFT_COUNT, night_shift_changed, app); + app->variable_item_list, "Night shift", NIGHT_SHIFT_COUNT, night_shift_changed, app); value_index = value_index_float( app->notification->settings.night_shift, night_shift_value, NIGHT_SHIFT_COUNT); variable_item_set_current_value_index(item, value_index); variable_item_set_current_value_text(item, night_shift_text[value_index]); item = variable_item_list_add( - app->variable_item_list, " . Start", NIGHT_SHIFT_START_COUNT, night_shift_start_changed, app); + app->variable_item_list, + " . Start", + NIGHT_SHIFT_START_COUNT, + night_shift_start_changed, + app); value_index = value_index_uint32( - app->notification->settings.night_shift_start, night_shift_start_value, NIGHT_SHIFT_START_COUNT); + app->notification->settings.night_shift_start, + night_shift_start_value, + NIGHT_SHIFT_START_COUNT); variable_item_set_current_value_index(item, value_index); variable_item_set_current_value_text(item, night_shift_start_text[value_index]); - + variable_item_set_locked( + item, (app->notification->settings.night_shift == 1), "Night shift \nOFF!"); + item = variable_item_list_add( app->variable_item_list, " . End", NIGHT_SHIFT_END_COUNT, night_shift_end_changed, app); value_index = value_index_uint32( app->notification->settings.night_shift_end, night_shift_end_value, NIGHT_SHIFT_END_COUNT); variable_item_set_current_value_index(item, value_index); variable_item_set_current_value_text(item, night_shift_end_text[value_index]); - - // --- NIGHT SHIFT END--- + variable_item_set_locked( + item, (app->notification->settings.night_shift == 1), "Night shift \nOFF!"); + // --- NIGHT SHIFT END--- item = variable_item_list_add( app->variable_item_list, "LED Brightness", BACKLIGHT_COUNT, led_changed, app);