Preliminary support for reading light

This commit is contained in:
Admar Schoonen 2023-04-09 23:53:37 +02:00
parent 59ef86575a
commit b05ccf3ed1
1 changed files with 53 additions and 38 deletions

View File

@ -232,15 +232,11 @@ class AlarmSettings():
alarm_modified = False
if is_arm():
led_color_rgbw = HSV(0, 0, 0).rgbw
# sound_selected = "NPO Radio 1"
# sound_source = "https://icecast.omroep.nl/radio1-bb-mp3"
sound_selected = "Birds"
sound_source = "Woodpecker Chirps - QuickSounds.com.mp3"
seconds_to_sunrise = 30 * 60 # 30 minutes
sunrise_state = "off" # "off", "active" or "completed"
volume = 15
wake_up_brightness = 20
@ -280,8 +276,9 @@ class MyClockWidget(FloatLayout):
set_alarm_timeout_counter = 0
seconds_to_next_alarm = 0
if is_arm():
led_color_rgbw = HSV(0, 0, 0).rgbw
light_state = "off" # "off", "reading", "sunrise" or "on"
intensity = 0
intensity_prev = None
# view can be one of the following strings:
# - "clock"
@ -468,9 +465,6 @@ class MyClockWidget(FloatLayout):
self.draw_list_curr_frame.append(["Rectangle", settings_button.canvas, settings_button.size, settings_button.pos, source])
def intensity_to_rgbw(self, intensity):
if self.is_arm == False:
return None
if intensity < 0:
intensity = 0
elif intensity > 1:
@ -486,42 +480,46 @@ class MyClockWidget(FloatLayout):
return led_color
def set_leds(self):
if self.intensity_prev != self.intensity:
self.intensity_prev = self.intensity
if self.is_arm:
led_color = self.intensity_to_rgbw(self.intensity)
print(self.light_state + ", t: " + str(self.seconds_to_next_alarm) + ", i: " + str(self.intensity) + ", rgbw: " + str(led_color.rgbw))
pixels.fill(led_color.rgbw)
pixels.show()
else:
print(self.light_state + ", t: " + str(self.seconds_to_next_alarm) + ", i: " + str(self.intensity))
def sunrise(self):
alarm_settings = App.get_running_app().alarm_settings
if self.seconds_to_next_alarm < 0.1:
App.get_running_app().alarm_settings.sunrise_state = "completed"
intensity = (1.0 - self.seconds_to_next_alarm / alarm_settings.seconds_to_sunrise)
if self.is_arm == False:
if self.view == "set_alarm":
# do not simulate sunrise when adjusting alarm time
return
led_color = self.intensity_to_rgbw(intensity)
if self.seconds_to_next_alarm < 0.1:
self.light_state = "on"
if self.led_color_rgbw != led_color.rgbw:
print("t: " + str(self.seconds_to_next_alarm) + ", i: " + str(intensity) + ", rgbw: " + str(led_color.rgbw))
self.led_color_rgbw = led_color.rgbw
pixels.fill(led_color.rgbw)
pixels.show()
intensity = (1.0 - self.seconds_to_next_alarm / alarm_settings.seconds_to_sunrise) * (alarm_settings.wake_up_brightness / 20.0)
def check_sun_rise(self):
# only adjust intensity if new intensity is higher than current intesity, to avoid dimming light when reading mode is on
if self.intensity < intensity:
self.intensity = intensity
self.light_state = "sunrise"
def process_led_state(self):
alarm_settings = App.get_running_app().alarm_settings
if alarm_settings.alarm_activated == False:
return
if self.seconds_to_next_alarm < alarm_settings.seconds_to_sunrise:
if (alarm_settings.alarm_activated) and (self.seconds_to_next_alarm < alarm_settings.seconds_to_sunrise):
self.sunrise()
else:
if self.is_arm == False:
return
led_color_off = RGB(0, 0, 0)
if (alarm_settings.sunrise_state == "off") and (self.led_color_rgbw != led_color_off.rgbw):
self.led_color_rgbw = led_color_off.rgbw
pixels.fill(led_color_off.rgbw)
pixels.show()
if (self.light_state == "off"):
self.intensity = 0
elif (self.light_state == "reading"):
self.intensity = alarm_settings.reading_light_brightness / 20.0
elif (self.light_state == "on"):
self.intensity = alarm_settings.wake_up_brightness / 20.0
def check_play_sound(self):
alarm_settings = App.get_running_app().alarm_settings
@ -550,7 +548,8 @@ class MyClockWidget(FloatLayout):
def check_alarm(self):
self.calc_seconds_to_next_alarm()
self.check_sun_rise()
self.process_led_state()
self.set_leds()
self.check_play_sound()
def update_clock(self):
@ -734,9 +733,25 @@ class MyClockWidget(FloatLayout):
def on_light_button_pressed(self):
alarm_settings = App.get_running_app().alarm_settings
App.get_running_app().alarm_settings.sunrise_state = "off"
if alarm_settings.alarm_playing:
self.stop_sound()
if self.light_state == "off":
self.light_state = "reading"
self.intensity = alarm_settings.reading_light_brightness / 20.0
elif self.light_state == "reading":
self.light_state = "off"
self.intensity = 0
elif self.light_state == "sunrise":
# allow enabling reading mode when sunrise has not yet reached that level
if self.intensity < alarm_settings.reading_light_brightness / 20.0:
self.light_state = "reading"
self.intensity = alarm_settings.reading_light_brightness / 20.0
else:
self.light_state = "off"
elif self.light_state == "on":
self.light_state = "off"
self.intensity = 0
if alarm_settings.alarm_playing:
self.stop_sound()
def on_play_button_pressed(self):
alarm_settings = App.get_running_app().alarm_settings