Fix joystick power switch

This commit is contained in:
Edgar P. Burkhart 2024-11-11 18:06:48 +01:00
parent 5b7f4c29e1
commit bd12590460
Signed by: edpibu
GPG key ID: 9833D3C5A25BD227

View file

@ -2,7 +2,7 @@ import json
from threading import Timer from threading import Timer
import bdfparser import bdfparser
from sense_hat import ACTION_RELEASED, SenseHat from sense_hat import ACTION_HELD, ACTION_RELEASED, SenseHat
class Display: class Display:
@ -73,6 +73,7 @@ class Light:
self._values = [""] * n self._values = [""] * n
self._n = n self._n = n
self._i = 0 self._i = 0
self._toggled = False
self.font = bdfparser.Font("src/tom-thumb.bdf") self.font = bdfparser.Font("src/tom-thumb.bdf")
self.timer = Timer(0, self.__init__) self.timer = Timer(0, self.__init__)
@ -95,7 +96,7 @@ class Light:
"icon": "mdi:dots-grid", "icon": "mdi:dots-grid",
"name": f"{self.name} {i}", "name": f"{self.name} {i}",
"on_command_type": "brightness", "on_command_type": "brightness",
"rgb_command_topic": self.command_topic(i, "command"), "rgb_command_topic": self.command_topic(i, "rgb"),
"rgb_state_topic": self.state_topic, "rgb_state_topic": self.state_topic,
"rgb_value_template": "{{" + f"value_json.rgb[{i}]" + "}}", "rgb_value_template": "{{" + f"value_json.rgb[{i}]" + "}}",
"retain": True, "retain": True,
@ -119,14 +120,13 @@ class Light:
def on_message(self, client, userdata, message): def on_message(self, client, userdata, message):
data = message.payload.decode() data = message.payload.decode()
print(message.topic, data)
match message.topic.rsplit("/", maxsplit=2): match message.topic.rsplit("/", maxsplit=2):
case [self.base_topic, i, "command"]: case [self.base_topic, i, "command"]:
match data.split(","): self.switch = False
case ["OFF"]: case [self.base_topic, i, "rgb"]:
self.switch = False self.set_color(int(i), list(map(int, data.split(","))))
case [*rgb]:
self.set_color(int(i), list(map(int, rgb)))
case [self.base_topic, i, "effect"]: case [self.base_topic, i, "effect"]:
self.low_light = data == "Low Light" self.low_light = data == "Low Light"
case [self.base_topic, i, "value"]: case [self.base_topic, i, "value"]:
@ -251,7 +251,7 @@ class Light:
if text: if text:
return [ return [
self.color[self._i] if x else [0, 0, 0] self.color[self._i] if x else [0, 0, 0]
for x in self.font.draw(text).crop(8, 8, yoff=-2).todata(3) for x in self.font.draw(text).crop(8, 8).todata(3)
] ]
return [self.color[self._i]] * 64 return [self.color[self._i]] * 64
@ -267,11 +267,12 @@ class Light:
self.switch = True self.switch = True
def toggle(self, event): def toggle(self, event):
if event.action == ACTION_HELD:
if not self._toggled:
self.low_light = not self.low_light
self._toggled = True
if event.action == ACTION_RELEASED: if event.action == ACTION_RELEASED:
if not self.switch: if self._toggled:
self.low_light = False self._toggled = False
self.switch = True
elif self.low_light:
self.switch = False
else: else:
self.low_light = True self.switch = not self.switch