Make update rate a variable

This commit is contained in:
Admar Schoonen 2023-01-15 11:58:53 +01:00
parent 8d38e81a95
commit 7c350359b7
1 changed files with 23 additions and 23 deletions

View File

@ -226,7 +226,7 @@ class AlarmSettings():
# sound_source = "https://icecast.omroep.nl/radio1-bb-mp3"
sound_selected = "Birds"
sound_source = "Woodpecker Chirps - QuickSounds.com.mp3"
seconds_to_sunrise = 30 * 60
seconds_to_sunrise = 30 * 60 # 30 minutes
volume = 15
wake_up_brightness = 20
@ -237,7 +237,7 @@ class MyClockWidget(FloatLayout):
grabbed = ""
face_numbers = []
# if not grabbed, set_alarm_timeout_counter is incremented at every update call (ie: 60 Hz);
# if not grabbed, set_alarm_timeout_counter is incremented at every update call;
# 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
@ -291,11 +291,6 @@ class MyClockWidget(FloatLayout):
))
self.ids["face"].add_widget(self.face_numbers[i - 1])
def draw_settings(self):
Color(1, 1, 1)
# self.ids["settings_menu"].add_widget(Rectangle(pos=(10, 10), size=(500, 500)))
return
def update_face(self):
alarm_settings = App.get_running_app().alarm_settings
@ -315,7 +310,6 @@ class MyClockWidget(FloatLayout):
def on_parent(self, myclock, parent):
self.draw_face()
self.draw_settings()
def position_on_clock(self, fraction, length):
"""
@ -329,8 +323,7 @@ class MyClockWidget(FloatLayout):
)
def update_set_alarm_button(self):
app = App.get_running_app()
alarm_settings = app.alarm_settings
alarm_settings = App.get_running_app().alarm_settings
if (self.view == "set_alarm") or alarm_settings.alarm_activated:
source = 'alarm_on.png'
@ -346,8 +339,7 @@ class MyClockWidget(FloatLayout):
Rectangle(size=set_alarm_button.size, pos=set_alarm_button.pos, source=source)
def update_settings_button(self):
app = App.get_running_app()
alarm_settings = app.alarm_settings
alarm_settings = App.get_running_app().alarm_settings
if (self.view.startswith("settings_menu")):
source = 'settings_visible.png'
@ -422,10 +414,8 @@ class MyClockWidget(FloatLayout):
def update_clock(self):
self.hide_widget(self.ids["face"], False)
self.hide_widget(self.ids["hands"], False)
"""
Redraw clock hands
"""
alarm_settings = App.get_running_app().alarm_settings
if self.view == "set_alarm":
time = alarm_settings.alarm_time
else:
@ -439,10 +429,11 @@ class MyClockWidget(FloatLayout):
self.update_face()
hands.canvas.clear()
update_rate = App.get_running_app().update_rate
with hands.canvas:
if self.view == "set_alarm":
if self.grabbed != "" or self.set_alarm_timeout_counter < 1 * 60 or \
self.set_alarm_timeout_counter % 60 <= 30 or alarm_settings.alarm_modified == False:
if self.grabbed != "" or self.set_alarm_timeout_counter < 1 * update_rate or \
self.set_alarm_timeout_counter % update_rate <= update_rate / 2 or alarm_settings.alarm_modified == False:
Color(0.9, 0.0, 0.0)
Line(points=[hands.center_x, hands.center_y, hours_hand.x, hours_hand.y], width=3, cap="round")
Color(0.8, 0.0, 0.0)
@ -451,7 +442,7 @@ class MyClockWidget(FloatLayout):
if self.grabbed == "":
self.set_alarm_timeout_counter += 1
if self.set_alarm_timeout_counter >= 4 * 60 + 30:
if self.set_alarm_timeout_counter >= 4.5 * update_rate:
self.view = "clock"
self.set_alarm_timeout_counter = 0
@ -491,6 +482,7 @@ class MyClockWidget(FloatLayout):
def settings_menu_wake_up_sound_select_button_cb(self):
self.settings_menu_wake_up_sound_select_button_cb_hack = True
alarm_settings = App.get_running_app().alarm_settings
self.ids["settings_menu_wake_up_sound_Birds"].active = False
self.ids["settings_menu_wake_up_sound_NpoRadio1"].active = False
print("sound selected: " + alarm_settings.sound_selected)
@ -503,6 +495,7 @@ class MyClockWidget(FloatLayout):
def settings_menu_wake_up_sound_Ok_button_cb(self):
alarm_settings = App.get_running_app().alarm_settings
self.ids["settings_menu_wake_up_sound_select_button"].text = alarm_settings.sound_selected
self.view = "settings_menu"
@ -530,6 +523,7 @@ class MyClockWidget(FloatLayout):
def volume_slider_value(self, *args):
alarm_settings = App.get_running_app().alarm_settings
alarm_settings.volume = int(args[1])
print("Volume changed to " + str(alarm_settings.volume))
@ -545,21 +539,25 @@ class MyClockWidget(FloatLayout):
def wake_up_brightness_slider_value(self, *args):
alarm_settings = App.get_running_app().alarm_settings
alarm_settings.wake_up_brightness = int(args[1])
print("Wake up brightness changed to " + str(alarm_settings.wake_up_brightness))
def reading_light_brightness_slider_value(self, *args):
alarm_settings = App.get_running_app().alarm_settings
alarm_settings.reading_light_brightness = int(args[1])
print("Reading light brightness changed to " + str(alarm_settings.reading_light_brightness))
def display_brightness_slider_value(self, *args):
alarm_settings = App.get_running_app().alarm_settings
alarm_settings.display_brightness = int(args[1])
print("Display brightness changed to " + str(alarm_settings.display_brightness))
def on_alarm_button_pressed(self):
alarm_settings = App.get_running_app().alarm_settings
alarm_settings.alarm_modified = False
self.set_alarm_timeout_counter = 0
@ -582,6 +580,7 @@ class MyClockWidget(FloatLayout):
self.grabbed = ""
alarm_settings = App.get_running_app().alarm_settings
if (self.view == "set_alarm") and (self.grabbed == "hour" or self.grabbed == "minute"):
self.set_alarm_timeout_counter = 0
@ -589,6 +588,7 @@ class MyClockWidget(FloatLayout):
def on_touch_move(self, touch):
alarm_settings = App.get_running_app().alarm_settings
self.alarm_set_timeout = 0
x = touch.pos[0] - self.size[0]/2
y = touch.pos[1] - self.size[1]/2
@ -650,14 +650,13 @@ class MyClockWidget(FloatLayout):
global sound_process
alarm_settings = App.get_running_app().alarm_settings
time = alarm_settings.alarm_time
hands = self.ids["hands"]
minutes_hand = self.position_on_clock(time.minute/60+time.second/3600, length=0.40*hands.size[0])
hours_hand = self.position_on_clock(time.hour/12 + time.minute/720, length=0.35*hands.size[0])
self.grabbed = ""
app = App.get_running_app()
alarm_settings = app.alarm_settings
if (0.85 <= touch.spos[0] <= 0.95) and (0.05 <= touch.spos[1] <= 0.15):
self.on_alarm_button_pressed()
@ -682,6 +681,7 @@ class MyClockWidget(FloatLayout):
class MyApp(App):
alarm_settings = AlarmSettings()
update_rate = 60.0
# apply volume setting
with pulsectl.Pulse('volume-increaser') as pulse:
@ -694,10 +694,11 @@ class MyApp(App):
def build(self):
clock_widget = MyClockWidget()
update_rate = App.get_running_app().update_rate
# update initially, just after construction of the widget is complete
Clock.schedule_once(clock_widget.update_display, 0)
# then update 60 times per second
Clock.schedule_interval(clock_widget.update_display, 1.0/60.0)
# then update at update_rate times per second
Clock.schedule_interval(clock_widget.update_display, 1.0/update_rate)
return clock_widget
def kill_sound_process():
@ -712,4 +713,3 @@ def except_hook(type, value, tb):
if __name__ == '__main__':
# sys.excepthook = except_hook
MyApp().run()