Add support for moving light button to set light to wake up level

This commit is contained in:
Admar Schoonen 2023-04-10 15:59:15 +02:00
parent 6863e89977
commit deae16aa61
1 changed files with 36 additions and 3 deletions

View File

@ -260,6 +260,7 @@ class Touch():
self.spos = []
self.is_empty = True
self.angle = 0
self.corrected = False
else:
self.pos = [i for i in x.pos]
self.spos = [i for i in x.spos]
@ -267,9 +268,10 @@ class Touch():
x = self.spos[0] - 0.5
y = self.spos[1] - 0.5
self.angle = math.atan2(y, x) / math.pi; # angle is between -1 and 1
self.corrected = False
def __repr__(self):
return ".is_empty: " + str(self.is_empty) + ", .pos: " + str(self.pos) + ", .spos: " + str(self.spos)
return ".is_empty: " + str(self.is_empty) + ", .pos: " + str(self.pos) + ", .spos: " + str(self.spos), ", .corrected: " + str(self.corrected)
def copy(self, x):
self = Touch(x)
@ -306,6 +308,7 @@ class MyClockWidget(FloatLayout):
draw_list_curr_frame = []
touch_prev = Touch()
light_button_move_init = []
vlc = vlc.Instance()
player = vlc.media_player_new()
@ -790,7 +793,7 @@ class MyClockWidget(FloatLayout):
self.light_state = "off"
elif self.light_state == "on":
self.light_state = "off"
self.light_state = "reading"
self.intensity = 0
if alarm_settings.alarm_playing:
self.stop_sound()
@ -825,6 +828,8 @@ class MyClockWidget(FloatLayout):
def on_touch_up(self, touch):
self.grabbed = ""
print("light button move init reset")
self.light_button_move_init = []
alarm_settings = App.get_running_app().alarm_settings
@ -847,9 +852,15 @@ class MyClockWidget(FloatLayout):
tol = 0.5 # 0.5 is equal to 90 degrees
inc = 1 # 1 is equal to 180 degrees
if (self.touch_prev.is_empty == False) and (touch_curr.angle - self.touch_prev.angle >= tol):
touch_curr.spos[0] = 1 - touch_curr.spos[0]
touch_curr.spos[1] = 1 - touch_curr.spos[1]
touch_curr.corrected = True
while touch_curr.angle - self.touch_prev.angle >= tol:
touch_curr.angle -= inc
elif (self.touch_prev.is_empty == False) and (touch_curr.angle - self.touch_prev.angle <= -tol):
touch_curr.spos[0] = 1 - touch_curr.spos[0]
touch_curr.spos[1] = 1 - touch_curr.spos[1]
touch_curr.corrected = True
while touch_curr.angle - self.touch_prev.angle <= -tol:
touch_curr.angle += inc
@ -931,6 +942,26 @@ class MyClockWidget(FloatLayout):
alarm_settings.alarm_time.month, alarm_settings.alarm_time.day, \
hour, alarm_settings.alarm_time.minute, alarm_settings.alarm_time.second, 0)
elif self.grabbed == "light_button":
if len(self.light_button_move_init) == 0:
self.light_button_move_init = touch_curr.spos
# Ugly workaround for issue with Kivy and Raspberry Pi 3 + touch screen: ignore corrected touched
if touch_curr.corrected == False:
d = touch_curr.spos[0] - self.light_button_move_init[0]
threshold = 0.05
# Ugly workaround for issue with Kivy and Raspberry Pi 3 + touch screen: mirror d if position is on other side
if self.light_button_move_init[0] > 0.5:
d = -d
if d > 0.05:
# move to the right: set light to wake up level
self.light_state = "on"
elif d < -0.05:
# move to the left: set light off
self.light_state = "off"
super(MyClockWidget, self).on_touch_move(touch)
def on_touch_down(self, touch):
@ -941,7 +972,9 @@ class MyClockWidget(FloatLayout):
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])
if (0.05 <= touch.spos[0] <= 0.15) and (0.85 <= touch.spos[1] <= 0.95):
if (0.05 <= touch.spos[0] <= 0.25) and (0.85 <= touch.spos[1] <= 0.95):
if self.grabbed == "":
self.grabbed = "light_button"
self.on_light_button_pressed()
elif (0.05 <= touch.spos[0] <= 0.15) and (0.05 <= touch.spos[1] <= 0.15):
self.on_play_button_pressed()