Save RAM and improve volume changes

Save RAM by optimising imports and reducing class usage
Change volume by 8% each time, using saved volume value and only one query. Minimum volume is 1% and max is 100%. Queries are not run if decreasing volume from 1% or increasing from 100%
This commit is contained in:
2024-05-16 22:55:51 +01:00
parent 0c4cbfcbb1
commit ae8e42d68f
6 changed files with 214 additions and 231 deletions

170
api.py
View File

@@ -1,92 +1,94 @@
from requests import get, post
import gc
import time
from gc import collect
from time import mktime, localtime
from env import HASS_URL, TOKEN
class Api:
def __init__(self, base_url, access_token) -> None:
self.base_url = base_url
self.access_token = access_token
def getReq(endpoint) -> dict:
collect()
headers = {
"Authorization": "Bearer " + TOKEN,
"content-type": "application/json"
}
print("Starting request for " + endpoint)
try:
response = get(HASS_URL + endpoint, headers=headers)
return response.json()
except:
return {}
def __request(self, endpoint) -> dict:
gc.collect()
url = self.base_url + endpoint
headers = {
"Authorization": "Bearer " + self.access_token,
"content-type": "application/json"
def postReq(endpoint, d) -> bool:
collect()
headers = {
"Authorization": "Bearer " + TOKEN,
"content-type": "application/json"
}
print("Starting post request to " + endpoint)
try:
response = post(HASS_URL + endpoint, headers=headers, json=d)
return response.status_code == 200
except:
return False
def getLightData(entity_id: str) -> dict:
# TODO: error handling if can't access hass (e.g. no connection, invalid token)
response = getReq("/api/states/" + entity_id)
on = "state" in response and response["state"] == "on"
return {
"on": on,
"rgb_color": response["attributes"]["rgb_color"] if on else None,
"rgbw_color": response["attributes"]["rgbw_color"] if on else None,
"brightness": response["attributes"]["brightness"] if on else 0.0
}
def getMediaPlayerData(entity_id: str) -> dict:
response = getReq("/api/states/" + entity_id)
if (not "state" in response or not "attributes" in response):
response = {
"state": "",
"attributes": {}
}
print("Starting request for " + endpoint)
try:
response = get(url, headers=headers)
return response.json()
except:
return {}
e = "state" in response and "attributes" in response
if ("media_position" in (dict)(response["attributes"])):
p = response["attributes"]["media_position"]
else:
p = 0
if ("media_position_updated_at" in (dict)(response["attributes"])):
dts = response["attributes"]["media_position_updated_at"]
t = mktime((int(dts[0:4]), int(dts[5:7]), int(dts[8:10]), int(dts[11:13]) + int(dts[27:29]), int(dts[14:16]) + int(dts[30:31]), int(dts[17:19]), 0, 0))
p += mktime(localtime()) - t
return {
"playing": response["state"] in ["on", "playing", "buffering"],
"shuffle": response["attributes"]["shuffle"] if e and "shuffle" in response["attributes"] else False,
"repeat": response["attributes"]["repeat"] == "on" if e and "repeat" in response["attributes"] else False,
"volume_level": response["attributes"]["volume_level"] if e and "volume_level" in response["attributes"] else 0,
"entity_picture": response["attributes"]["entity_picture"] if e and "entity_picture" in (dict)(response["attributes"]) else None,
"media_duration": response["attributes"]["media_duration"] if e and "media_duration" in (dict)(response["attributes"]) else 0,
"media_position": p,
"media_title": response["attributes"]["media_title"] if e and "media_title" in (dict)(response["attributes"]) else "Nothing playing",
"media_artist": response["attributes"]["media_artist"] if e and "media_artist" in (dict)(response["attributes"]) else "",
"media_album_name": response["attributes"]["media_album_name"] if e and "media_album_name" in (dict)(response["attributes"]) else ""
}
def __post(self, endpoint, d) -> bool:
gc.collect()
url = self.base_url + endpoint
headers = {
"Authorization": "Bearer " + self.access_token,
"content-type": "application/json"
}
print("Starting post request to " + endpoint)
try:
response = post(url, headers=headers, json=d)
return response.status_code == 200
except:
return False
def getLightData(self, entity_id: str) -> dict:
# TODO: error handling if can't access hass (e.g. no connection, invalid token)
response = self.__request("/api/states/" + entity_id)
on = "state" in response and response["state"] == "on"
return {
"on": on,
"rgb_color": response["attributes"]["rgb_color"] if on else None,
"rgbw_color": response["attributes"]["rgbw_color"] if on else None,
"brightness": response["attributes"]["brightness"] if on else 0.0
}
def getMediaPlayerData(self, entity_id: str) -> dict:
response = self.__request("/api/states/" + entity_id)
e = "state" in response and "attributes" in response
if ("media_position" in (dict)(response["attributes"])):
p = response["attributes"]["media_position"]
else:
p = 0
if ("media_position_updated_at" in (dict)(response["attributes"])):
dts = response["attributes"]["media_position_updated_at"]
t = time.mktime((int(dts[0:4]), int(dts[5:7]), int(dts[8:10]), int(dts[11:13]) + int(dts[27:29]), int(dts[14:16]) + int(dts[30:31]), int(dts[17:19]), 0, 0))
p += time.mktime(time.localtime()) - t
return {
"playing": response["state"] in ["on", "playing", "buffering"],
"shuffle": response["attributes"]["shuffle"] if e and "shuffle" in response["attributes"] else False,
"repeat": response["attributes"]["repeat"] == "on" if e and "repeat" in response["attributes"] else False,
"volume_level": response["attributes"]["volume_level"] if e and "volume_level" in response["attributes"] else 0,
"entity_picture": response["attributes"]["entity_picture"] if e and "entity_picture" in (dict)(response["attributes"]) else None,
"media_duration": response["attributes"]["media_duration"] if e and "media_duration" in (dict)(response["attributes"]) else 0,
"media_position": p,
"media_title": response["attributes"]["media_title"] if e and "media_title" in (dict)(response["attributes"]) else "Nothing playing",
"media_artist": response["attributes"]["media_artist"] if e and "media_artist" in (dict)(response["attributes"]) else "",
"media_album_name": response["attributes"]["media_album_name"] if e and "media_album_name" in (dict)(response["attributes"]) else ""
}
def changeVolume(self, entity_id: str, up: bool = True) -> None:
if (up): dir = "up"
else: dir = "down"
self.__post(f"/api/services/media_player/volume_{dir}", {"entity_id": entity_id})
self.__post(f"/api/services/media_player/volume_{dir}", {"entity_id": entity_id})
def nextTrack(self, entity_id: str) -> None:
self.__post("/api/services/media_player/media_next_track", {"entity_id": entity_id})
def prevTrack(self, entity_id: str) -> None:
self.__post("/api/services/media_player/media_previous_track", {"entity_id": entity_id})
def changeVolume(entity_id: str, up: bool = True) -> None:
if (up): dir = "up"
else: dir = "down"
postReq(f"/api/services/media_player/volume_{dir}", {"entity_id": entity_id})
postReq(f"/api/services/media_player/volume_{dir}", {"entity_id": entity_id})
def playPause(self, entity_id: str) -> None:
self.__post("/api/services/media_player/media_play_pause", {"entity_id": entity_id})
def setVolume(entity_id: str, v: float) -> None:
postReq(f"/api/services/media_player/volume_set", {"entity_id": entity_id, "volume_level": v})
def toggleLight(self, entity_id: str) -> None:
self.__post("/api/services/light/toggle", {"entity_id": entity_id})
def nextTrack(entity_id: str) -> None:
postReq("/api/services/media_player/media_next_track", {"entity_id": entity_id})
def setBrightness(self, entity_id: str, v: int) -> None:
self.__post("/api/services/light/turn_on", {"entity_id": entity_id, "brightness": v})
def prevTrack(entity_id: str) -> None:
postReq("/api/services/media_player/media_previous_track", {"entity_id": entity_id})
def playPause(entity_id: str) -> None:
postReq("/api/services/media_player/media_play_pause", {"entity_id": entity_id})
def toggleLight(entity_id: str) -> None:
postReq("/api/services/light/toggle", {"entity_id": entity_id})
def setBrightness(entity_id: str, v: int) -> None:
postReq("/api/services/light/turn_on", {"entity_id": entity_id, "brightness": v})

68
app.py
View File

@@ -1,14 +1,12 @@
from api import Api
import machine
import network
from time import sleep
from machine import reset
from network import WLAN, STA_IF
from time import sleep, localtime
from lcd import LCD
from font import Font
import gc
from env import *
import _thread
import ntptime
import time
from font import cntr_st
from gc import collect, mem_free
from env import HOSTNAME, SSID, WIFI_PASSWORD, SCREENS
from _thread import start_new_thread
from ntptime import settime
from screens import *
class App:
@@ -18,10 +16,9 @@ class App:
self.scr_n = -1
else: self.scr_n = 0
self.lcd = LCD()
self.api = Api(HASS_URL, TOKEN)
def __connect(self) -> int:
wlan = network.WLAN(network.STA_IF)
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.config(hostname=HOSTNAME)
wlan.connect(SSID, WIFI_PASSWORD)
@@ -33,16 +30,15 @@ class App:
return self.ip
def __boot(self) -> None:
gc.collect()
collect()
print("Booting")
self.lcd.fill(self.lcd.black)
Font.cntr_st(self.lcd, self.lcd.width, "Booting...", 120, 2, 150, 150, 150)
self.lcd.fill(0x0000)
cntr_st(self.lcd, self.lcd.width, "Booting...", 120, 2, 150, 150, 150)
self.lcd.show()
self.__connect()
ntptime.host = "1.europe.pool.ntp.org"
try:
ntptime.settime()
print("Local time after synchronization: %s" %str(time.localtime()))
settime()
print("Local time after synchronization: %s" %str(localtime()))
except:
pass
@@ -109,7 +105,7 @@ class App:
print(f"Screen change: {c}")
return c
def handleButtons(self):
def handleButtons(self) -> bool:
up = self.lcd.up["v"]
down = self.lcd.down["v"]
left = self.lcd.left["v"]
@@ -119,27 +115,21 @@ class App:
keyB = self.lcd.keyB["v"]
keyX = self.lcd.keyX["v"]
keyY = self.lcd.keyY["v"]
# self.lcd.up["v"] = False
# self.lcd.down["v"] = False
# self.lcd.left["v"] = False
# self.lcd.right["v"] = False
# self.lcd.ctrl["v"] = False
# self.lcd.keyA["v"] = False
# self.lcd.keyB["v"] = False
# self.lcd.keyX["v"] = False
# self.lcd.keyY["v"] = False
self.__resetButtonStatuses()
if (ctrl):
machine.reset()
self.s.handleButtons(up, down, left, right, keyA, keyB, keyX, keyY, ctrl)
reset()
return self.s.handleButtons(up, down, left, right, keyA, keyB, keyX, keyY, ctrl)
def __manageScreen(self) -> None:
started = False
while (True):
gc.collect()
if (time.localtime()[3] == 0):
print("Mem free before and after collecting:")
print(mem_free())
collect()
print(mem_free())
if (localtime()[3] == 0):
try:
ntptime.settime()
settime()
except:
pass
changed = not started or self.__changeScreen()
@@ -147,13 +137,13 @@ class App:
# if the screen has changed, redraw the whole screen
if (changed):
self.__resetButtonStatuses()
gc.collect()
collect()
if (SCREENS[self.scr_n]["type"] == 0):
self.s = LightsScreen(self.api, SCREENS[self.scr_n]["name"], SCREENS[self.scr_n]["entities"])
self.s = LightsScreen(SCREENS[self.scr_n]["name"], SCREENS[self.scr_n]["entities"])
elif (SCREENS[self.scr_n]["type"] == 1):
self.s = MediaScreen(self.api, SCREENS[self.scr_n]["name"], SCREENS[self.scr_n]["entity"])
self.s = MediaScreen(SCREENS[self.scr_n]["name"], SCREENS[self.scr_n]["entity"])
else:
self.s = UnknownScreen(self.api, -1, "Unknown")
self.s = UnknownScreen("Unknown")
self.s.display(self.lcd)
# otherwise minimise the number of pixels being changed
else:
@@ -165,7 +155,7 @@ class App:
if (self.scr_n == None): return
self.__boot()
try:
_thread.start_new_thread(self.__manageButtons, ())
start_new_thread(self.__manageButtons, ())
self.__manageScreen()
except KeyboardInterrupt:
machine.reset()
reset()

83
font.py
View File

@@ -1,4 +1,4 @@
from utils import Utils
from utils import colour
# ===========Start of FONTS Section=========================
# Standard ASCII 5x8 font
@@ -264,51 +264,46 @@ FONT = bytes([
0x00, 0x00, 0x00, 0x00, 0x00 # 255 also a <space>
])
class Font:
@staticmethod
def character(lcd,asc,xt,yt,sz,r,g,b): # Single character sz is size: 1 or 2
cc = Utils.colour(r,g,b)
code = asc * 5 # 5 bytes per character
for ii in range(5):
line = FONT[code + ii]
for yy in range(8):
if (line >> yy) & 0x1:
lcd.pixel(ii*sz+xt,yy*sz+yt,cc)
if sz > 1:
lcd.pixel(ii*sz+xt+1,yy*sz+yt,cc)
lcd.pixel(ii*sz+xt,yy*sz+yt+1,cc)
lcd.pixel(ii*sz+xt+1,yy*sz+yt+1,cc)
if sz == 3:
lcd.pixel(ii*sz+xt, yy*sz+yt+2,cc)
lcd.pixel(ii*sz+xt+1,yy*sz+yt+2,cc)
lcd.pixel(ii*sz+xt+2,yy*sz+yt+2,cc)
lcd.pixel(ii*sz+xt+2,yy*sz+yt,cc)
lcd.pixel(ii*sz+xt+2,yy*sz+yt+1,cc)
def character(lcd,asc,xt,yt,sz,r,g,b): # Single character sz is size: 1 or 2
cc = colour(r,g,b)
code = asc * 5 # 5 bytes per character
for ii in range(5):
line = FONT[code + ii]
for yy in range(8):
if (line >> yy) & 0x1:
lcd.pixel(ii*sz+xt,yy*sz+yt,cc)
if sz > 1:
lcd.pixel(ii*sz+xt+1,yy*sz+yt,cc)
lcd.pixel(ii*sz+xt,yy*sz+yt+1,cc)
lcd.pixel(ii*sz+xt+1,yy*sz+yt+1,cc)
if sz == 3:
lcd.pixel(ii*sz+xt, yy*sz+yt+2,cc)
lcd.pixel(ii*sz+xt+1,yy*sz+yt+2,cc)
lcd.pixel(ii*sz+xt+2,yy*sz+yt+2,cc)
lcd.pixel(ii*sz+xt+2,yy*sz+yt,cc)
lcd.pixel(ii*sz+xt+2,yy*sz+yt+1,cc)
@staticmethod
def prnt_st(lcd,asci,xx,yy,sz,r,g,b): # Text string
if sz == 1: move = 6
if sz == 2: move = 11
if sz == 3: move = 17
for letter in(asci):
asci = ord(letter)
Font.character(lcd,asci,xx,yy,sz,r,g,b)
xx = xx + move
def prnt_st(lcd,asci,xx,yy,sz,r,g,b): # Text string
if sz == 1: move = 6
if sz == 2: move = 11
if sz == 3: move = 17
for letter in(asci):
asci = ord(letter)
character(lcd,asci,xx,yy,sz,r,g,b)
xx = xx + move
@staticmethod
def cntr_st(lcd,width,txt,y,size,r,g,b,o=0): # Centres text on line y, skipping first o pixels
if size == 1: w = 6
if size == 2: w = 11
if size == 3: w = 17
gap = (width - len(txt) * w)//2 + o
Font.prnt_st(lcd,txt,gap,y,size,r,g,b)
def cntr_st(lcd,width,txt,y,size,r,g,b,o=0): # Centres text on line y, skipping first o pixels
if size == 1: w = 6
if size == 2: w = 11
if size == 3: w = 17
gap = (width - len(txt) * w)//2 + o
prnt_st(lcd,txt,gap,y,size,r,g,b)
@staticmethod
def rght_st(lcd,asci,xx,yy,sz,r,g,b):
if sz == 1: w = 6
if sz == 2: w = 11
if sz == 3: w = 17
xo = xx - len(asci) * w
Font.prnt_st(lcd,asci,xo,yy,sz,r,g,b)
def rght_st(lcd,asci,xx,yy,sz,r,g,b):
if sz == 1: w = 6
if sz == 2: w = 11
if sz == 3: w = 17
xo = xx - len(asci) * w
prnt_st(lcd,asci,xo,yy,sz,r,g,b)
# =========== End of font support routines ===========

17
lcd.py
View File

@@ -1,7 +1,5 @@
from machine import Pin,SPI,PWM
from font import Font
from utils import Utils
import framebuf
from framebuf import FrameBuffer, RGB565
BL = 13
DC = 8
@@ -11,7 +9,7 @@ SCK = 10
CS = 9
# LCD driver
class LCD(framebuf.FrameBuffer):
class LCD(FrameBuffer):
def __init__(self):
pwm = PWM(Pin(BL))
pwm.freq(1000)
@@ -30,14 +28,8 @@ class LCD(framebuf.FrameBuffer):
self.dc = Pin(DC,Pin.OUT)
self.dc(1)
self.buffer = bytearray(self.height * self.width * 2)
super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
super().__init__(self.buffer, self.width, self.height, RGB565)
self.init_display()
self.red = 0x07E0
self.green = 0x001f
self.blue = 0xf800
self.white = 0xffff
self.black = 0x0000
self.keyA = {
"v": False, # value
@@ -194,6 +186,3 @@ class LCD(framebuf.FrameBuffer):
self.spi.write(self.buffer)
self.cs(1)
# END OF DRIVER
def clear(self, c):
self.fill(c)

View File

@@ -1,17 +1,17 @@
from font import Font
from utils import Utils
import bmp_file_reader as bmpr
from font import cntr_st, rght_st
from utils import colour
from bmp_file_reader import BMPFileReader
from api import getMediaPlayerData, getLightData, playPause, nextTrack, prevTrack, changeVolume, setVolume, toggleLight, setBrightness
class Screen():
def __init__(self, api, n: str) -> None:
self.api = api
def __init__(self, n: str) -> None:
self.name = n
self.d = {}
self.prev = {}
def display(self, lcd) -> None:
lcd.fill(lcd.black)
Font.cntr_st(lcd, lcd.width, self.name, 20, 2, 255, 255, 255)
lcd.fill(0x0000)
cntr_st(lcd, lcd.width, self.name, 20, 2, 255, 255, 255)
def update(self, lcd) -> None:
pass
@@ -20,15 +20,16 @@ class Screen():
return False
def _updateData(self) -> dict:
self.prev = self.d.copy()
return {}
def _invalidConfig(self, lcd) -> None:
Font.cntr_st(lcd, lcd.width, "Invalid config", lcd.height//2, 2, 255, 255, 255)
cntr_st(lcd, lcd.width, "Invalid config", lcd.height//2, 2, 255, 255, 255)
lcd.show()
class MediaScreen(Screen):
def __init__(self, api, n: str, e: str) -> None:
super().__init__(api, n)
def __init__(self, n: str, e: str) -> None:
super().__init__(n)
self.e = e
self.valid = e != None and e != ""
@@ -43,20 +44,20 @@ class MediaScreen(Screen):
if (self.d["media_duration"] != None):
mins = self.d["media_duration"] // 60
secs = self.d["media_duration"] % 60
Font.rght_st(lcd, f"{mins}:{secs}", lcd.width, lcd.height - 16, 1, 180, 180, 180)
Font.cntr_st(lcd, lcd.width, self.d["media_title"], lcd.height - 72, 3, 255, 255, 255)
Font.cntr_st(lcd, lcd.width, self.d["media_artist"], lcd.height - 96, 2, 255, 255, 255)
rght_st(lcd, f"{mins}:{secs}", lcd.width, lcd.height - 16, 1, 180, 180, 180)
cntr_st(lcd, lcd.width, self.d["media_title"], lcd.height - 72, 3, 255, 255, 255)
cntr_st(lcd, lcd.width, self.d["media_artist"], lcd.height - 96, 2, 255, 255, 255)
lcd.show()
def __updateMediaPositionBar(self, lcd, p: int, d: int):
if (d > 0):
for x in range (0, (lcd.width * p)//d):
lcd.pixel(x, lcd.height - 5, lcd.white)
lcd.pixel(x, lcd.height - 4, lcd.white)
lcd.pixel(x, lcd.height - 3, lcd.white)
lcd.pixel(x, lcd.height - 2, lcd.white)
lcd.pixel(x, lcd.height - 1, lcd.white)
lcd.pixel(x, lcd.height, lcd.white)
lcd.pixel(x, lcd.height - 5, 0xffff)
lcd.pixel(x, lcd.height - 4, 0xffff)
lcd.pixel(x, lcd.height - 3, 0xffff)
lcd.pixel(x, lcd.height - 2, 0xffff)
lcd.pixel(x, lcd.height - 1, 0xffff)
lcd.pixel(x, lcd.height, 0xffff)
def update(self, lcd):
if (not self.valid):
@@ -73,29 +74,34 @@ class MediaScreen(Screen):
self.display(lcd)
def _updateData(self) -> dict:
self.prev = self.d
self.d = self.api.getMediaPlayerData(self.e)
super()._updateData()
self.d = getMediaPlayerData(self.e)
return self.d
def handleButtons(self, up: bool, down: bool, left: bool, right: bool, keyA: bool, keyB: bool, keyX: bool, keyY: bool, ctrl: bool) -> bool:
a = False
if (up):
self.api.changeVolume(self.e)
v = self.d["volume_level"] if "volume_level" in self.d else None
if (v != None and up and v < 1):
vn = min(1.0, v + 0.08)
setVolume(self.e, vn)
self.d["volume_level"] = vn
a = True
elif (down):
self.api.changeVolume(self.e, False)
elif (v != None and down and v > 0.01):
vn = max(0.01, v - 0.08)
setVolume(self.e, vn)
self.d["volume_level"] = vn
a = True
if (keyX):
self.api.nextTrack(self.e)
nextTrack(self.e)
elif (keyY):
self.api.prevTrack(self.e)
prevTrack(self.e)
if (keyA):
self.api.playPause(self.e)
playPause(self.e)
return a
class LightsScreen(Screen):
def __init__(self, api, n: str, es: list) -> None:
super().__init__(api, n)
def __init__(self, n: str, es: list) -> None:
super().__init__(n)
self.es = es
self.valid = es != None and len(es) != 0
@@ -115,9 +121,9 @@ class LightsScreen(Screen):
def __displayLightEntity(self, lcd, i: int, w: int, h: int, xo: int, yo: int, n: str, d) -> None:
# if the light is turned on, display the filled-in lightbulb icon in the colour of the light, centrally in the light's grid square
if (d["on"]):
color = Utils.colour(d["rgb_color"][0], d["rgb_color"][1], d["rgb_color"][2])
color = colour(d["rgb_color"][0], d["rgb_color"][1], d["rgb_color"][2])
with open("images/lightbulb-on.bmp", "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
reader = BMPFileReader(file_handle)
img_height = reader.get_height()
x_offset = w//2 + xo - reader.get_width()//2
y_offset = h//2 + yo - reader.get_height()//2 - 4
@@ -128,12 +134,14 @@ class LightsScreen(Screen):
g = d["rgb_color"][1] if (color.green) != 0 else 0
b = d["rgb_color"][2] if (color.blue) != 0 else 0
if (color.red != 0 or color.green != 0 or color.blue != 0):
lcd.pixel(col_i + x_offset, row_i + y_offset, Utils.colour(r,g,b))
lcd.pixel(col_i + x_offset, row_i + y_offset, colour(r,g,b))
# otherwise display the outline lightbulb icon in grey, centrally in the light's grid square
else:
color = Utils.colour(80, 80, 80)
color = colour(80, 80, 80)
print("Drawing light off")
with open("images/lightbulb-off.bmp", "rb") as file_handle:
reader = bmpr.BMPFileReader(file_handle)
reader = BMPFileReader(file_handle)
print("Drawing light off - loaded file")
img_height = reader.get_height()
x_offset = w//2 + xo - reader.get_width()//2
y_offset = h//2 + yo - reader.get_height()//2 - 4
@@ -141,11 +149,11 @@ class LightsScreen(Screen):
row = reader.get_row(row_i)
for col_i, color in enumerate(row):
if (color.red != 0 or color.green != 0 or color.blue != 0):
lcd.pixel(col_i + x_offset, row_i + y_offset, Utils.colour(color.red, color.green, color.blue))
lcd.pixel(col_i + x_offset, row_i + y_offset, colour(color.red, color.green, color.blue))
else:
lcd.pixel(col_i + x_offset, row_i + y_offset, Utils.colour(0,0,0))
lcd.pixel(col_i + x_offset, row_i + y_offset, colour(0,0,0))
# display the name of the light 8px below the lightbulb icon
Font.cntr_st(lcd, w, n, y_offset + img_height + 8, 2, 220, 220, 220, xo)
cntr_st(lcd, w, n, y_offset + img_height + 8, 2, 220, 220, 220, xo)
def update(self, lcd):
if (not self.valid):
@@ -153,6 +161,7 @@ class LightsScreen(Screen):
self._invalidConfig(lcd)
return
self._updateData()
print("Updating lights")
# for each light to be displayed
for i in range(0, len(self.d)):
# if its settings have changed, re-draw them without clearing the display
@@ -161,9 +170,9 @@ class LightsScreen(Screen):
lcd.show()
def _updateData(self) -> dict:
self.prev = self.d
super()._updateData()
for i in range(0, min(len(self.es), 4)):
self.d[i] = self.api.getLightData(self.es[i]["id"])
self.d[i] = getLightData(self.es[i]["id"])
return self.d
def handleButtons(self, up: bool, down: bool, left: bool, right: bool, keyA: bool, keyB: bool, keyX: bool, keyY: bool, ctrl: bool) -> bool:
@@ -173,21 +182,21 @@ class LightsScreen(Screen):
for i in range(0, e):
# if button for light clicked, toggle the light
if (b[i]):
self.api.toggleLight(self.es[i]["id"])
toggleLight(self.es[i]["id"])
a = True
# if up/down clicked, adjust brightness for all lights that are turned on
pcfg = i in self.prev and "on" in self.prev[i] and self.prev[i]["on"] and "brightness" in self.prev[i]
if (up and pcfg):
self.api.setBrightness(self.es[i]["id"], min(255, self.prev[i]["brightness"] + 35))
setBrightness(self.es[i]["id"], min(255, self.prev[i]["brightness"] + 35))
a = True
elif (down and pcfg):
self.api.setBrightness(self.es[i]["id"], max(1, self.prev[i]["brightness"] - 35))
setBrightness(self.es[i]["id"], max(1, self.prev[i]["brightness"] - 35))
a = True
return a
class UnknownScreen(Screen):
def __init__(self, api, t: int, n: str) -> None:
super().__init__(api, t, n)
def __init__(self, n: str) -> None:
super().__init__(n)
def display(self, lcd) -> None:
super().display(lcd)

View File

@@ -1,5 +1,3 @@
class Utils:
# method from Tony Goodhew 21st April 2022, for thepihut.com
@staticmethod
def colour(R,G,B): # Convert RGB888 to RGB565
return (((G&0b00011100)<<3) +((B&0b11111000)>>3)<<8) + (R&0b11111000)+((G&0b11100000)>>5)
# method from Tony Goodhew 21st April 2022, for thepihut.com
def colour(R,G,B): # Convert RGB888 to RGB565
return (((G&0b00011100)<<3) +((B&0b11111000)>>3)<<8) + (R&0b11111000)+((G&0b11100000)>>5)