Skip to content
Snippets Groups Projects
Verified Commit ca40b2db authored by rahix's avatar rahix
Browse files

chore: Fix formatting of preload apps


Signed-off-by: default avatarRahix <rahix@rahix.de>
parent b23fb212
No related tags found
No related merge requests found
...@@ -10,7 +10,7 @@ CONFIG_NAME = "at-timestamp.json" ...@@ -10,7 +10,7 @@ CONFIG_NAME = "at-timestamp.json"
def init(): def init():
if CONFIG_NAME not in os.listdir("."): if CONFIG_NAME not in os.listdir("."):
at_config = {"time_start": "unset"} at_config = {"time_start": "unset"}
f = open(CONFIG_NAME, 'w') f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(at_config)) f.write(ujson.dumps(at_config))
f.close() f.close()
...@@ -21,7 +21,7 @@ def init(): ...@@ -21,7 +21,7 @@ def init():
def is_timestamp_set(): def is_timestamp_set():
f = open(CONFIG_NAME, 'r') f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = ujson.loads(f.read())
f.close() f.close()
if c["time_start"] == "unset": if c["time_start"] == "unset":
...@@ -39,29 +39,29 @@ def triangle(disp, x, y, left): ...@@ -39,29 +39,29 @@ def triangle(disp, x, y, left):
def timestamp_reset(): def timestamp_reset():
f = open(CONFIG_NAME, 'r') f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = ujson.loads(f.read())
c["time_start"] = "unset" c["time_start"] = "unset"
f.close() f.close()
f = open(CONFIG_NAME, 'w') f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(c)) f.write(ujson.dumps(c))
f.close() f.close()
def timestamp_read(): def timestamp_read():
global time_start global time_start
f = open(CONFIG_NAME, 'r') f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = ujson.loads(f.read())
time_start = c["time_start"] time_start = c["time_start"]
f.close() f.close()
def timestamp_write(): def timestamp_write():
f = open(CONFIG_NAME, 'r') f = open(CONFIG_NAME, "r")
c = ujson.loads(f.read()) c = ujson.loads(f.read())
c["time_start"] = utime.time() c["time_start"] = utime.time()
f.close() f.close()
f = open(CONFIG_NAME, 'w') f = open(CONFIG_NAME, "w")
f.write(ujson.dumps(c)) f.write(ujson.dumps(c))
f.close() f.close()
......
...@@ -7,9 +7,9 @@ import light_sensor ...@@ -7,9 +7,9 @@ import light_sensor
import ujson import ujson
import os import os
FILENAME = 'nickname.txt' FILENAME = "nickname.txt"
FILENAME_ADV = 'nickname.json' FILENAME_ADV = "nickname.json"
ANIM_TYPES = ['none', 'led', 'fade', 'gay', 'rainbow', 'rnd_led'] ANIM_TYPES = ["none", "led", "fade", "gay", "rainbow", "rnd_led"]
def wheel(pos): def wheel(pos):
...@@ -40,7 +40,7 @@ def random_rgb(): ...@@ -40,7 +40,7 @@ def random_rgb():
""" """
rgb = [] rgb = []
for i in range(0, 3): for i in range(0, 3):
rand = int.from_bytes(os.urandom(1), 'little') rand = int.from_bytes(os.urandom(1), "little")
if rand > 255: if rand > 255:
rand = 255 rand = 255
rgb.append(rand) rgb.append(rand)
...@@ -112,15 +112,15 @@ def get_time(): ...@@ -112,15 +112,15 @@ def get_time():
Generates a nice timestamp in format hh:mm:ss from the devices localtime Generates a nice timestamp in format hh:mm:ss from the devices localtime
:return: timestamp :return: timestamp
""" """
timestamp = '' timestamp = ""
if utime.localtime()[3] < 10: if utime.localtime()[3] < 10:
timestamp = timestamp + '0' timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[3]) + ':' timestamp = timestamp + str(utime.localtime()[3]) + ":"
if utime.localtime()[4] < 10: if utime.localtime()[4] < 10:
timestamp = timestamp + '0' timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[4]) + ':' timestamp = timestamp + str(utime.localtime()[4]) + ":"
if utime.localtime()[5] < 10: if utime.localtime()[5] < 10:
timestamp = timestamp + '0' timestamp = timestamp + "0"
timestamp = timestamp + str(utime.localtime()[5]) timestamp = timestamp + str(utime.localtime()[5])
return timestamp return timestamp
...@@ -154,7 +154,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -154,7 +154,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
""" """
anim = mode anim = mode
posy = 30 posy = 30
if sub != '': if sub != "":
posy = 18 posy = 18
r = 255 r = 255
g = 0 g = 0
...@@ -164,7 +164,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -164,7 +164,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
last_btn_poll = utime.time() - 2 last_btn_poll = utime.time() - 2
while True: while True:
sleep = 0.5 sleep = 0.5
if sub == '#time': if sub == "#time":
r_sub = get_time() r_sub = get_time()
dark = 0 dark = 0
if light_sensor.get_reading() < 30: if light_sensor.get_reading() < 30:
...@@ -175,9 +175,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -175,9 +175,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
r_bg_sub_color = bg_sub[dark] r_bg_sub_color = bg_sub[dark]
r_bg = main_bg[dark] r_bg = main_bg[dark]
# Button handling # Button handling
pressed = buttons.read( pressed = buttons.read(buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT)
buttons.BOTTOM_LEFT | buttons.BOTTOM_RIGHT
)
if utime.time() - last_btn_poll >= 1: if utime.time() - last_btn_poll >= 1:
last_btn_poll = utime.time() last_btn_poll = utime.time()
if pressed & buttons.BOTTOM_RIGHT != 0: if pressed & buttons.BOTTOM_RIGHT != 0:
...@@ -191,7 +189,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -191,7 +189,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
anim = len(ANIM_TYPES) - 1 anim = len(ANIM_TYPES) - 1
blink_led(0) blink_led(0)
# Animations # Animations
if ANIM_TYPES[anim] == 'fade': if ANIM_TYPES[anim] == "fade":
sleep = 0.1 sleep = 0.1
leds.clear() leds.clear()
toggle_rockets(False) toggle_rockets(False)
...@@ -207,7 +205,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -207,7 +205,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
r_bg = [r, g, b] r_bg = [r, g, b]
r_bg_color = r_bg r_bg_color = r_bg
r_bg_sub_color = r_bg r_bg_sub_color = r_bg
if ANIM_TYPES[anim] == 'led': if ANIM_TYPES[anim] == "led":
if dark == 1: if dark == 1:
for i in range(0, 11): for i in range(0, 11):
leds.prep(i, r_bg) leds.prep(i, r_bg)
...@@ -217,7 +215,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -217,7 +215,7 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
else: else:
leds.clear() leds.clear()
toggle_rockets(False) toggle_rockets(False)
if ANIM_TYPES[anim] == 'rnd_led': if ANIM_TYPES[anim] == "rnd_led":
if dark == 1: if dark == 1:
for i in range(0, 11): for i in range(0, 11):
leds.prep(i, random_rgb()) leds.prep(i, random_rgb())
...@@ -227,10 +225,10 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -227,10 +225,10 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
else: else:
leds.clear() leds.clear()
toggle_rockets(False) toggle_rockets(False)
if ANIM_TYPES[anim] == 'gay': if ANIM_TYPES[anim] == "gay":
toggle_rockets(False) toggle_rockets(False)
leds.gay(0.4) leds.gay(0.4)
if ANIM_TYPES[anim] == 'rainbow': if ANIM_TYPES[anim] == "rainbow":
for i in range(0, 11): for i in range(0, 11):
lr, lg, lb = wheel(rainbow_led_pos + i * 3) lr, lg, lb = wheel(rainbow_led_pos + i * 3)
leds.prep(i, [lr, lg, lb]) leds.prep(i, [lr, lg, lb])
...@@ -240,16 +238,28 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat): ...@@ -240,16 +238,28 @@ def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg, mode, bat):
leds.update() leds.update()
leds.dim_top(3) leds.dim_top(3)
toggle_rockets(True) toggle_rockets(True)
if ANIM_TYPES[anim] == 'none': if ANIM_TYPES[anim] == "none":
leds.clear() leds.clear()
toggle_rockets(False) toggle_rockets(False)
with display.open() as disp: with display.open() as disp:
disp.rect(0, 0, 160, 80, col=r_bg, filled=True) disp.rect(0, 0, 160, 80, col=r_bg, filled=True)
if bat[0]: if bat[0]:
render_battery(disp, bat) render_battery(disp, bat)
disp.print(title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy) disp.print(
if r_sub != '': title,
disp.print(r_sub, fg=r_fg_sub_color, bg=r_bg_sub_color, posx=80 - round(len(r_sub) / 2 * 14), posy=42) fg=r_fg_color,
bg=r_bg_color,
posx=80 - round(len(title) / 2 * 14),
posy=posy,
)
if r_sub != "":
disp.print(
r_sub,
fg=r_fg_sub_color,
bg=r_bg_sub_color,
posx=80 - round(len(r_sub) / 2 * 14),
posy=42,
)
disp.update() disp.update()
disp.close() disp.close()
utime.sleep(sleep) utime.sleep(sleep)
...@@ -274,49 +284,63 @@ with display.open() as disp: ...@@ -274,49 +284,63 @@ with display.open() as disp:
disp.clear().update() disp.clear().update()
disp.close() disp.close()
if FILENAME_ADV in os.listdir("."): if FILENAME_ADV in os.listdir("."):
f = open(FILENAME_ADV, 'r') f = open(FILENAME_ADV, "r")
try: try:
c = ujson.loads(f.read()) c = ujson.loads(f.read())
f.close() f.close()
# parse config # parse config
nick = get_key(c, 'nickname', 'no nick') nick = get_key(c, "nickname", "no nick")
sub = get_key(c, 'subtitle', '') sub = get_key(c, "subtitle", "")
mode = get_key(c, 'mode', 0) mode = get_key(c, "mode", 0)
# battery # battery
battery_show = get_key(c, 'battery', True) battery_show = get_key(c, "battery", True)
battery_c_good = get_key(c, 'battery_color_good', [0, 230, 00]) battery_c_good = get_key(c, "battery_color_good", [0, 230, 00])
battery_c_ok = get_key(c, 'battery_color_ok', [255, 215, 0]) battery_c_ok = get_key(c, "battery_color_ok", [255, 215, 0])
battery_c_bad = get_key(c, 'battery_color_bad', [255, 0, 0]) battery_c_bad = get_key(c, "battery_color_bad", [255, 0, 0])
# daytime values # daytime values
background = get_key(c, 'background', [0, 0, 0]) background = get_key(c, "background", [0, 0, 0])
fg_color = get_key(c, 'fg_color', [255, 255, 255]) fg_color = get_key(c, "fg_color", [255, 255, 255])
bg_color = get_key(c, 'bg_color', background) bg_color = get_key(c, "bg_color", background)
fg_sub_color = get_key(c, 'fg_sub_color', [255, 255, 255]) fg_sub_color = get_key(c, "fg_sub_color", [255, 255, 255])
bg_sub_color = get_key(c, 'bg_sub_color', background) bg_sub_color = get_key(c, "bg_sub_color", background)
# nighttime values # nighttime values
background_night = get_key(c, 'background_night', [0, 0, 0]) background_night = get_key(c, "background_night", [0, 0, 0])
fg_color_night = get_key(c, 'fg_color_night', [255, 255, 255]) fg_color_night = get_key(c, "fg_color_night", [255, 255, 255])
bg_color_night = get_key(c, 'bg_color_night', background_night) bg_color_night = get_key(c, "bg_color_night", background_night)
fg_sub_color_night = get_key(c, 'fg_sub_color_night', [255, 255, 255]) fg_sub_color_night = get_key(c, "fg_sub_color_night", [255, 255, 255])
bg_sub_color_night = get_key(c, 'bg_sub_color_night', background_night) bg_sub_color_night = get_key(c, "bg_sub_color_night", background_night)
# render nickname # render nickname
render_nickname(nick, sub, (fg_color, fg_color_night), (bg_color, bg_color_night), render_nickname(
(fg_sub_color, fg_sub_color_night), (bg_sub_color, bg_sub_color_night), nick,
(background, background_night), mode, sub,
(battery_show, battery_c_good, battery_c_ok, battery_c_bad)) (fg_color, fg_color_night),
(bg_color, bg_color_night),
(fg_sub_color, fg_sub_color_night),
(bg_sub_color, bg_sub_color_night),
(background, background_night),
mode,
(battery_show, battery_c_good, battery_c_ok, battery_c_bad),
)
except ValueError: except ValueError:
render_error('invalid', 'json') render_error("invalid", "json")
else: else:
if FILENAME not in os.listdir("."): if FILENAME not in os.listdir("."):
render_error('file not', 'found') render_error("file not", "found")
else: else:
f = open(FILENAME, 'r') f = open(FILENAME, "r")
nick = f.read() nick = f.read()
f.close() f.close()
if len(nick) > 11: if len(nick) > 11:
render_error('name too', 'long') render_error("name too", "long")
if len(nick) < 1: if len(nick) < 1:
render_error('nick file', 'empty') render_error("nick file", "empty")
else: else:
render_nickname(nick, '', ([255, 255, 255], [255, 255, 255]), ([0, 0, 0], [0, 0, 0]), render_nickname(
([255, 255, 255], [255, 255, 255]), ([0, 0, 0], [0, 0, 0]), ([0, 0, 0], [0, 0, 0])) nick,
"",
([255, 255, 255], [255, 255, 255]),
([0, 0, 0], [0, 0, 0]),
([255, 255, 255], [255, 255, 255]),
([0, 0, 0], [0, 0, 0]),
([0, 0, 0], [0, 0, 0]),
)
...@@ -3,17 +3,20 @@ import leds ...@@ -3,17 +3,20 @@ import leds
import utime import utime
_rand = 123456789 _rand = 123456789
def rand(): def rand():
global _rand global _rand
_rand = (1103515245 * _rand + 12345) & 0xffffff _rand = (1103515245 * _rand + 12345) & 0xFFFFFF
return _rand return _rand
gs = 160 gs = 160
colors = [((i >> 2) * gs, (i >> 1 & 1) * gs, (i & 1) * gs) for i in range(1, 8)] colors = [((i >> 2) * gs, (i >> 1 & 1) * gs, (i & 1) * gs) for i in range(1, 8)]
nick = 'sample text' nick = "sample text"
try: try:
with open('/nickname.txt') as f: with open("/nickname.txt") as f:
nick = f.read() nick = f.read()
except: except:
pass pass
...@@ -29,7 +32,13 @@ while True: ...@@ -29,7 +32,13 @@ while True:
pass pass
fg = colors[rand() % len(colors)] fg = colors[rand() % len(colors)]
nx = 80 - round(len(nick) / 2 * 14) nx = 80 - round(len(nick) / 2 * 14)
d.print(nick, fg=fg, bg=[0xff-c for c in fg], posx=(nx-8)+rand()%16, posy=22+rand()%16) d.print(
nick,
fg=fg,
bg=[0xFF - c for c in fg],
posx=(nx - 8) + rand() % 16,
posy=22 + rand() % 16,
)
d.update() d.update()
d.close() d.close()
leds.set(rand() % 11, colors[rand() % len(colors)]) leds.set(rand() % 11, colors[rand() % len(colors)])
......
...@@ -16,6 +16,7 @@ SPECIAL_EXIT = "[ exit ]" ...@@ -16,6 +16,7 @@ SPECIAL_EXIT = "[ exit ]"
SPECIAL_EMPTY = "# empty file" SPECIAL_EMPTY = "# empty file"
BUTTON_TIMER_POPPED = -1 BUTTON_TIMER_POPPED = -1
def list_files(): def list_files():
"""Create a list of available text files.""" """Create a list of available text files."""
files = sorted(os.listdir("/")) files = sorted(os.listdir("/"))
...@@ -68,6 +69,7 @@ def button_events(timeout=0): ...@@ -68,6 +69,7 @@ def button_events(timeout=0):
COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE) COLOR1, COLOR2 = (color.CHAOSBLUE_DARK, color.CHAOSBLUE)
def file_len(filename): def file_len(filename):
i = -1 i = -1
with open(filename) as fh: with open(filename) as fh:
...@@ -82,11 +84,7 @@ def draw_filecontent(disp, filename, pos, linecount, lineoffset = 0): ...@@ -82,11 +84,7 @@ def draw_filecontent(disp, filename, pos, linecount, lineoffset = 0):
# stop if file is empty # stop if file is empty
if linecount <= 0: if linecount <= 0:
disp.print( disp.print(SPECIAL_EMPTY, posy=20, bg=color.BLACK)
SPECIAL_EMPTY,
posy=20,
bg=color.BLACK
)
return return
# calc start position # calc start position
...@@ -104,21 +102,26 @@ def draw_filecontent(disp, filename, pos, linecount, lineoffset = 0): ...@@ -104,21 +102,26 @@ def draw_filecontent(disp, filename, pos, linecount, lineoffset = 0):
break break
if i >= start: if i >= start:
disp.rect( disp.rect(
0, (i - start) * 20, 159, (i - start) * 20 + 20, 0,
col=COLOR1 if i == pos else COLOR2 (i - start) * 20,
159,
(i - start) * 20 + 20,
col=COLOR1 if i == pos else COLOR2,
) )
off = 0 off = 0
linelength = len(line) linelength = len(line)
if i == pos and linelength > 11 and lineoffset > 0: if i == pos and linelength > 11 and lineoffset > 0:
off = lineoffset if lineoffset + 11 < linelength else linelength - 11 off = (
lineoffset if lineoffset + 11 < linelength else linelength - 11
)
if lineoffset > linelength: if lineoffset > linelength:
off = 0 off = 0
disp.print( disp.print(
line[off : (off + 11)], line[off : (off + 11)],
posy=(i - start) * 20, posy=(i - start) * 20,
bg=COLOR1 if i == pos else COLOR2 bg=COLOR1 if i == pos else COLOR2,
) )
if linelength > 11 and off < linelength - 11: if linelength > 11 and off < linelength - 11:
triangle(disp, 153, (i - start) * 20 + 6, False) triangle(disp, 153, (i - start) * 20 + 6, False)
...@@ -144,8 +147,11 @@ def draw_filelist(disp, filelist, pos, filecount, lineoffset): ...@@ -144,8 +147,11 @@ def draw_filelist(disp, filelist, pos, filecount, lineoffset):
break break
if i >= start: if i >= start:
disp.rect( disp.rect(
0, (i - start) * 20, 159, (i - start) * 20 + 20, 0,
col=COLOR1 if i == pos else COLOR2 (i - start) * 20,
159,
(i - start) * 20 + 20,
col=COLOR1 if i == pos else COLOR2,
) )
off = 0 off = 0
...@@ -158,7 +164,7 @@ def draw_filelist(disp, filelist, pos, filecount, lineoffset): ...@@ -158,7 +164,7 @@ def draw_filelist(disp, filelist, pos, filecount, lineoffset):
disp.print( disp.print(
" " + line[off : (off + 10)], " " + line[off : (off + 10)],
posy=(i - start) * 20, posy=(i - start) * 20,
bg=COLOR1 if i == pos else COLOR2 bg=COLOR1 if i == pos else COLOR2,
) )
if i == pos: if i == pos:
disp.print(">", posy=(i - start) * 20, fg=color.COMMYELLOW, bg=COLOR1) disp.print(">", posy=(i - start) * 20, fg=color.COMMYELLOW, bg=COLOR1)
...@@ -214,7 +220,10 @@ def main(): ...@@ -214,7 +220,10 @@ def main():
elif ev == BUTTON_TIMER_POPPED: elif ev == BUTTON_TIMER_POPPED:
timercountpopped += 1 timercountpopped += 1
if timercountpopped >= timerstartscroll and (timercountpopped - timerstartscroll) % timerscrollspeed == 0: if (
timercountpopped >= timerstartscroll
and (timercountpopped - timerstartscroll) % timerscrollspeed == 0
):
lineoffset += 1 lineoffset += 1
elif ev == buttons.TOP_RIGHT: elif ev == buttons.TOP_RIGHT:
...@@ -267,7 +276,10 @@ def main(): ...@@ -267,7 +276,10 @@ def main():
elif ev == BUTTON_TIMER_POPPED: elif ev == BUTTON_TIMER_POPPED:
timercountpopped += 1 timercountpopped += 1
if timercountpopped >= timerstartscroll and (timercountpopped - timerstartscroll) % timerscrollspeed == 0: if (
timercountpopped >= timerstartscroll
and (timercountpopped - timerstartscroll) % timerscrollspeed == 0
):
lineoffset += 1 lineoffset += 1
elif ev == buttons.TOP_RIGHT: elif ev == buttons.TOP_RIGHT:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment