Skip to content
Snippets Groups Projects
Commit 010f7ac4 authored by schneider's avatar schneider
Browse files

Merge branch 'clock-colors' into 'master'

Clock colors

See merge request card10/firmware!199
parents 1dbf1bb3 3150c8b3
No related branches found
No related tags found
No related merge requests found
...@@ -4,6 +4,12 @@ from utime import sleep ...@@ -4,6 +4,12 @@ from utime import sleep
import utime import utime
import math import math
import leds import leds
import buttons
import ujson
import os
CONFIG_NAME = "clock.json"
class Time: class Time:
def __init__(self, start=0): def __init__(self, start=0):
...@@ -26,10 +32,21 @@ class Time: ...@@ -26,10 +32,21 @@ class Time:
def hour(self): def hour(self):
return (self.time / 3600) % 24 return (self.time / 3600) % 24
class Clock: class Clock:
def __init__(self, sizex = 80, sizey = 80, radius = 38, offsetx = 30, def __init__(
hour_hand = True, minute_hand = True, second_hand = True, console_out = False, self,
run_once = False, update_interval = 0): sizex=80,
sizey=80,
radius=38,
offsetx=30,
hour_hand=True,
minute_hand=True,
second_hand=True,
console_out=False,
run_once=False,
update_interval=0,
):
self.sizex = sizex self.sizex = sizex
self.sizey = sizey self.sizey = sizey
self.radius = radius self.radius = radius
...@@ -38,26 +55,152 @@ class Clock: ...@@ -38,26 +55,152 @@ class Clock:
self.minute_hand = minute_hand self.minute_hand = minute_hand
self.second_hand = second_hand self.second_hand = second_hand
self.console_out = console_out self.console_out = console_out
self.update_interval = update_interval if update_interval != 0 else (1 if self.second_hand else 30) self.update_interval = (
update_interval if update_interval != 0 else (1 if self.second_hand else 30)
)
self.run_once = run_once self.run_once = run_once
self.offsetx = offsetx self.offsetx = offsetx
self.time = Time() self.time = Time()
white = (255, 255, 255) self.theme = 0
self.center_col = white self.default_themes = [
self.m1_col = white {
self.m5_col = white "background": [0, 0, 0],
self.hour_hand_col = white "center": [255, 255, 255],
self.minute_hand_col = white "m1": [255, 255, 255],
self.second_hand_col = white "m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [130, 30, 70],
"center": [255, 255, 255],
"m1": [255, 255, 255],
"m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [0, 80, 0],
"center": [255, 255, 255],
"m1": [255, 255, 255],
"m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [0, 80, 80],
"center": [255, 255, 255],
"m1": [255, 255, 255],
"m5": [255, 255, 255],
"hour_hand": [255, 255, 255],
"minute_hand": [255, 255, 255],
"second_hand": [255, 255, 255],
},
{
"background": [255, 255, 255],
"center": [0, 0, 0],
"m1": [0, 0, 0],
"m5": [0, 0, 0],
"hour_hand": [0, 0, 0],
"minute_hand": [0, 0, 0],
"second_hand": [0, 0, 0],
},
]
self.themes = self.default_themes
# check for config file
if CONFIG_NAME in os.listdir("."):
self.readConfig()
else:
self.writeConfig()
# load colors
self.setTheme(self.theme)
def readConfig(self):
with open(CONFIG_NAME, "r") as f:
try:
c = ujson.loads(f.read())
if (
"themes" in c
and len(c["themes"]) > 0
and isinstance(c["themes"], list)
):
self.themes = c["themes"]
if "theme" and isinstance(c["theme"], int):
self.theme = c["theme"]
except ValueError:
print("parsing %s failed" % CONFIG_NAME)
def writeConfig(self):
with open(CONFIG_NAME, "w") as f:
f.write(ujson.dumps({"theme": self.theme, "themes": self.themes}))
def setTheme(self, theme):
self.theme = theme % len(self.themes)
self.background_col = (
self.themes[self.theme]["background"]
if "background" in self.themes[self.theme]
else self.default_themes[0]["background"]
)
self.center_col = (
self.themes[self.theme]["center"]
if "center" in self.themes[self.theme]
else self.default_themes[0]["center"]
)
self.m1_col = (
self.themes[self.theme]["m1"]
if "m1" in self.themes[self.theme]
else self.default_themes[0]["m1"]
)
self.m5_col = (
self.themes[self.theme]["m5"]
if "m5" in self.themes[self.theme]
else self.default_themes[0]["m5"]
)
self.hour_hand_col = (
self.themes[self.theme]["hour_hand"]
if "hour_hand" in self.themes[self.theme]
else self.default_themes[0]["hour_hand"]
)
self.minute_hand_col = (
self.themes[self.theme]["minute_hand"]
if "minute_hand" in self.themes[self.theme]
else self.default_themes[0]["minute_hand"]
)
self.second_hand_col = (
self.themes[self.theme]["second_hand"]
if "second_hand" in self.themes[self.theme]
else self.default_themes[0]["second_hand"]
)
def loop(self): def loop(self):
colored = False colored = False
try: try:
with display.open() as disp: with display.open() as disp:
button_pressed = False
while True: while True:
self.updateClock(disp) self.updateClock(disp)
if self.run_once: if self.run_once:
break break
# check for button presses
v = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT)
if v == 0:
button_pressed = False
if not button_pressed and v & buttons.BOTTOM_LEFT != 0:
button_pressed = True
self.setTheme(self.theme - 1)
self.writeConfig()
elif not button_pressed and v & buttons.BOTTOM_RIGHT != 0:
button_pressed = True
self.setTheme(self.theme + 1)
self.writeConfig()
except KeyboardInterrupt: except KeyboardInterrupt:
for i in range(11): for i in range(11):
leds.set(i, (0, 0, 0)) leds.set(i, (0, 0, 0))
...@@ -68,15 +211,25 @@ class Clock: ...@@ -68,15 +211,25 @@ class Clock:
d.clear() d.clear()
for x in range(len(image)): for x in range(len(image)):
for y in range(len(image[x])): for y in range(len(image[x])):
d.pixel(x + self.offsetx, y, col = (255, 255, 255) if image[x][y] else (0, 0, 0)) d.pixel(
x + self.offsetx,
y,
col=(255, 255, 255) if image[x][y] else (0, 0, 0),
)
d.update() d.update()
def updateClock(self, disp): def updateClock(self, disp):
disp.clear() disp.clear(self.background_col)
localtime = utime.localtime() localtime = utime.localtime()
disp.pixel(self.center[0] + self.offsetx, self.center[1], col=self.center_col) disp.pixel(self.center[0] + self.offsetx, self.center[1], col=self.center_col)
hour_coords = self.circlePoint(math.radians((((localtime[3]%12)/12.) if localtime[3] else 0)*360 + 270 + (localtime[4]/2))) hour_coords = self.circlePoint(
math.radians(
(((localtime[3] % 12) / 12.0) if localtime[3] else 0) * 360
+ 270
+ (localtime[4] / 2)
)
)
minute_coords = self.circlePoint(math.radians(localtime[4] * 6 + 270)) minute_coords = self.circlePoint(math.radians(localtime[4] * 6 + 270))
second_coords = self.circlePoint(math.radians(localtime[5] * 6 + 270)) second_coords = self.circlePoint(math.radians(localtime[5] * 6 + 270))
...@@ -91,30 +244,65 @@ class Clock: ...@@ -91,30 +244,65 @@ class Clock:
self.addLine(disp, coords, self.center, 1, col=self.m1_col) self.addLine(disp, coords, self.center, 1, col=self.m1_col)
if self.hour_hand: if self.hour_hand:
self.addLine(disp, self.center, hour_coords, int(self.radius / 3), 1, col = self.hour_hand_col) self.addLine(
disp,
self.center,
hour_coords,
int(self.radius / 3),
1,
col=self.hour_hand_col,
)
if self.minute_hand: if self.minute_hand:
self.addLine(disp, self.center, minute_coords, int(self.radius / 2), col = self.minute_hand_col) self.addLine(
disp,
self.center,
minute_coords,
int(self.radius / 2),
col=self.minute_hand_col,
)
if self.second_hand: if self.second_hand:
self.addLine(disp, self.center, second_coords, self.radius - int(self.radius/8.), col = self.second_hand_col) self.addLine(
disp,
self.center,
second_coords,
self.radius - int(self.radius / 8.0),
col=self.second_hand_col,
)
if self.console_out: if self.console_out:
for y in range(self.radius * 2): for y in range(self.radius * 2):
line = "" line = ""
for x in range(self.radius * 2): for x in range(self.radius * 2):
line = line + ("." if image[(self.center[1]-self.radius)+y][(self.center[0]-self.radius)+x] else " ") line = line + (
"."
if image[(self.center[1] - self.radius) + y][
(self.center[0] - self.radius) + x
]
else " "
)
print(line) print(line)
disp.update() disp.update()
def circlePoint(self, t): def circlePoint(self, t):
return (int(round(self.radius*math.cos(t))) + self.center[0], int(round(self.radius*math.sin(t))) + self.center[1]) return (
int(round(self.radius * math.cos(t))) + self.center[0],
int(round(self.radius * math.sin(t))) + self.center[1],
)
def addLine(self, disp, source, aim, length, thickness=1, col=(255, 255, 255)): def addLine(self, disp, source, aim, length, thickness=1, col=(255, 255, 255)):
vector = self.subVector(aim, source) vector = self.subVector(aim, source)
vector = self.normVector(vector) vector = self.normVector(vector)
destination = self.addVector(source, self.multiplyVector(vector, length)) destination = self.addVector(source, self.multiplyVector(vector, length))
disp.line(round(source[0]) + self.offsetx, round(source[1]), round(destination[0]) + self.offsetx, round(destination[1]), col=col, size=thickness) disp.line(
round(source[0]) + self.offsetx,
round(source[1]),
round(destination[0]) + self.offsetx,
round(destination[1]),
col=col,
size=thickness,
)
def normVector(self, v): def normVector(self, v):
length = math.sqrt(sum([i ** 2 for i in v])) length = math.sqrt(sum([i ** 2 for i in v]))
...@@ -138,5 +326,6 @@ class Clock: ...@@ -138,5 +326,6 @@ class Clock:
def multiplyVector(self, v, multiplier): def multiplyVector(self, v, multiplier):
return tuple([i * multiplier for i in v]) return tuple([i * multiplier for i in v])
clock = Clock() clock = Clock()
clock.loop() clock.loop()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment