Added sunrise

This commit is contained in:
Admar Schoonen 2023-04-09 22:34:59 +02:00
parent 07ce2b4157
commit 32685b777f
1 changed files with 65 additions and 16 deletions

View File

@ -6,6 +6,8 @@ import sys
import traceback
import copy
import os
from rgbw_colorspace_converter.colors.converters import HSV, RGB
from hsluv import hsluv_to_rgb
from kivy.config import Config
def is_arm():
@ -23,10 +25,24 @@ if is_arm():
#Config.set('graphics', 'height', '480')
Config.set('graphics', 'width', '667')
Config.set('graphics', 'height', '400')
import board
import neopixel
pixel_pin = board.D10
num_pixels = 144
ORDER = neopixel.GRBW
pixels = neopixel.NeoPixel(
pixel_pin, num_pixels, brightness=0.2, auto_write=False, pixel_order=ORDER
)
pixels.fill((0, 0, 0, 0))
pixels.show()
else:
Config.set('graphics', 'width', '1200')
Config.set('graphics', 'height', '720')
pixels = None
Config.set('graphics', 'maxfps', '60')
from kivy.app import App
@ -215,12 +231,14 @@ class AlarmSettings():
alarm_playing = False
alarm_modified = False
led_color = [0, 0, 0]
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
seconds_to_sunrise = 1 * 60 # 30 minutes
sunrise_state = "off" # "off", "active" or "completed"
volume = 15
wake_up_brightness = 20
@ -259,7 +277,8 @@ class MyClockWidget(FloatLayout):
# this is used to blink the hands at 1 Hz when setting the alarm and releasing the hand
set_alarm_timeout_counter = 0
seconds_to_next_alarm = 0
led_color = [0, 0, 0]
led_color_rgbw = HSV(0, 0, 0).rgbw
# view can be one of the following strings:
# - "clock"
@ -445,20 +464,37 @@ class MyClockWidget(FloatLayout):
# Rectangle(size=settings_button.size, pos=settings_button.pos, source=source)
self.draw_list_curr_frame.append(["Rectangle", settings_button.canvas, settings_button.size, settings_button.pos, source])
def sun_rise(self):
alarm_settings = App.get_running_app().alarm_settings
# to do: calculate brightness and color according to sun rise instead of linear increment
intensity = math.floor((1.0 - self.seconds_to_next_alarm / alarm_settings.seconds_to_sunrise) * 256.0)
def intensity_to_rgbw(self, intensity):
if intensity < 0:
intensity = 0
elif intensity > 1:
intensity = 1
led_color = [0, 0, 0]
for i in range(3):
led_color[i] = intensity
h = max(0, min(75, (75 - 12) * intensity + 12))
s = max(0, min(100, 250 - 250 * intensity))
# s = max(0, min(100, 100 - 100 * intensity))
l = 100 * intensity
if self.led_color != led_color:
self.led_color = led_color
rgb = hsluv_to_rgb([h, s, l])
led_color = RGB(rgb[0] * 255, rgb[1] * 255, rgb[2] * 255)
return led_color
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)
led_color = self.intensity_to_rgbw(intensity)
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()
def check_sun_rise(self):
alarm_settings = App.get_running_app().alarm_settings
@ -467,7 +503,13 @@ class MyClockWidget(FloatLayout):
return
if self.seconds_to_next_alarm < alarm_settings.seconds_to_sunrise:
self.sun_rise()
self.sunrise()
else:
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()
def check_play_sound(self):
alarm_settings = App.get_running_app().alarm_settings
@ -678,7 +720,11 @@ class MyClockWidget(FloatLayout):
self.set_backlight(alarm_settings.display_brightness)
def on_light_button_pressed(self):
pass
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()
def on_play_button_pressed(self):
alarm_settings = App.get_running_app().alarm_settings
@ -879,8 +925,11 @@ class MyApp(App):
return clock_widget
def except_hook(type, value, tb):
if is_arm():
pixels.fill((0, 0, 0, 0))
pixels.show()
return
if __name__ == '__main__':
# sys.excepthook = except_hook
#sys.excepthook = except_hook
MyApp().run()