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