__init__.py 5.67 KB
Newer Older
schneider's avatar
schneider committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
"""
Improvement ideas
- animations
    - dvd
    - rainbow
    - led control
    - fade effekt
- led nick writing
"""
import utime
import display
import leds
import ledfx
import buttons
import light_sensor
import ujson
import os

FILENAME = 'nickname.txt'
FILENAME_ADV = 'nickname.json'
21
ANIM_TYPES = ['none', 'led', 'fade', 'gay']
schneider's avatar
schneider committed
22 23 24 25 26 27 28 29 30 31 32


def render_error(err1, err2):
    with display.open() as disp:
        disp.clear()
        disp.print(err1, posx=80 - round(len(err1) / 2 * 14), posy=18)
        disp.print(err2, posx=80 - round(len(err2) / 2 * 14), posy=42)
        disp.update()
        disp.close()


33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
def get_time():
    timestamp = ''
    if utime.localtime()[3] < 10:
        timestamp = timestamp + '0'
    timestamp = timestamp + str(utime.localtime()[3]) + ':'
    if utime.localtime()[4] < 10:
        timestamp = timestamp + '0'
    timestamp = timestamp + str(utime.localtime()[4]) + ':'
    if utime.localtime()[5] < 10:
        timestamp = timestamp + '0'
    timestamp = timestamp + str(utime.localtime()[5])
    return timestamp


def toggle_rockets(state):
    brightness = 15
    if not state:
        brightness = 0
    leds.set_rocket(0, brightness)
    leds.set_rocket(1, brightness)
    leds.set_rocket(2, brightness)


schneider's avatar
schneider committed
56
def render_nickname(title, sub, fg, bg, fg_sub, bg_sub, main_bg):
57
    anim = 0
schneider's avatar
schneider committed
58 59 60 61 62 63
    posy = 30
    if sub != '':
        posy = 18
    r = 255
    g = 0
    b = 0
64 65
    r_sub = sub
    last_btn_poll = utime.time() - 2
schneider's avatar
schneider committed
66
    while True:
67 68 69
        sleep = 0.5
        if sub == '#time':
            r_sub = get_time()
schneider's avatar
schneider committed
70
        dark = 0
71
        if light_sensor.get_reading() < 40:
schneider's avatar
schneider committed
72 73 74 75 76 77
            dark = 1
        r_fg_color = fg[dark]
        r_bg_color = bg[dark]
        r_fg_sub_color = fg_sub[dark]
        r_bg_sub_color = bg_sub[dark]
        r_bg = main_bg[dark]
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        # Button handling
        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:
                anim = anim + 1
                if anim >= len(ANIM_TYPES):
                    anim = 0
            if pressed & buttons.BOTTOM_LEFT != 0:
                anim = anim - 1
                if anim < 0:
                    anim = len(ANIM_TYPES) - 1
        # Animations
        if ANIM_TYPES[anim] == 'fade':
            sleep = 0.1
            leds.clear()
            toggle_rockets(False)
schneider's avatar
schneider committed
97 98 99 100 101 102 103 104 105 106
            if r > 0 and b == 0:
                r = r - 1
                g = g + 1
            if g > 0 and r == 0:
                g = g - 1
                b = b + 1
            if b > 0 and g == 0:
                r = r + 1
                b = b - 1
            r_bg = [r, g, b]
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
            r_bg_color = r_bg
            r_bg_sub_color = r_bg
        if ANIM_TYPES[anim] == 'led':
            if dark == 1:
                for i in range(0, 11):
                    leds.prep(i, r_bg)
                leds.update()
                leds.dim_top(4)
                toggle_rockets(True)
            else:
                leds.clear()
                toggle_rockets(False)
        if ANIM_TYPES[anim] == 'gay':
            toggle_rockets(False)
            leds.gay(0.4)
        if ANIM_TYPES[anim] == 'none':
schneider's avatar
schneider committed
123
            leds.clear()
124
            toggle_rockets(False)
schneider's avatar
schneider committed
125 126 127
        with display.open() as disp:
            disp.rect(0, 0, 160, 80, col=r_bg, filled=True)
            disp.print(title, fg=r_fg_color, bg=r_bg_color, posx=80 - round(len(title) / 2 * 14), posy=posy)
128 129
            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)
schneider's avatar
schneider committed
130 131
            disp.update()
            disp.close()
132
        utime.sleep(sleep)
schneider's avatar
schneider committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184


def get_key(json, key, default):
    try:
        return json[key]
    except KeyError:
        return default

leds.clear()
with display.open() as disp:
    disp.clear().update()
    disp.close()
if FILENAME_ADV in os.listdir("."):
    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', '')
        # 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)
        # 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)
        # 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))
    except ValueError:
        render_error('invalid', 'json')
else:
    if FILENAME not in os.listdir("."):
        render_error('file not', 'found')
    else:
        f = open(FILENAME, 'r')
        nick = f.read()
        f.close()
        if len(nick) > 11:
            render_error('name too', 'long')
        if len(nick) < 1:
            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]))