# emerytura.py -rw-r--r-- 3.5 KiB View raw
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import badger2040
from badger2040 import WIDTH
import jpegdec
import time
import machine
import sys

COUNTDOWN_UNTIL = time.mktime((2024, 8, 29, 0, 0, 0, 0, 0))


def is_clock_set() -> bool:
    badger2040.pcf_to_pico_rtc()
    now = time.localtime()
    return now[0] >= 2023


def get_voltage_raw() -> float:
    wl_cs = machine.Pin(25) # WiFi chip SDIO_DATA3 / gate on FET between VSYS divider (FET drain) and GPIO29 (FET source)
    wl_cs.init(mode=machine.Pin.OUT, value=1)
    pin = machine.ADC(29)
    adc_reading  = pin.read_u16()
    adc_voltage  = (adc_reading * 3.3) / 65535
    vsys_voltage = adc_voltage * 3
    #wl_cs.init(mode=machine.Pin.ALT, pull=machine.Pin.PULL_DOWN, alt=31)#try to restore initial WL_CS state
    return vsys_voltage


def get_voltage() -> str:
    return f"{get_voltage_raw():.2f}v"


def is_usb_connected() -> bool:
    return get_voltage_raw() > 4.0


def config_rtc(display: badger2040.Badger2040) -> None:
    if badger2040.is_wireless() and not is_clock_set() and False:
        import ntptime
        try:
            display.connect()
            if display.isconnected():
                ntptime.settime()
        except (RuntimeError, OSError) as e:
            print(f"Wireless Error: {e.value}")
    try:
        badger2040.pcf_to_pico_rtc()
    except RuntimeError:
        pass


def get_diffstr() -> Optional[str]:
    now = time.localtime()
    tdelta = COUNTDOWN_UNTIL - time.mktime(now)
    if tdelta < 0:
        return None
    years, months, days, _, _, _, _, _ = time.localtime(tdelta)
    years -= 1970
    months -= 1
    retval: str = ""
    if years != 0:
        retval += f"{years} lat, "
    if months != 0:
        retval += f"{months} mies. i "
    if days == 1:
        return f"{retval}{days} dzien"
    return f"{retval}{days} dni"


def display_background(display: badger2040.Badger2040, filepath: str) -> None:
    jpeg = jpegdec.JPEG(display.display)
    jpeg.open_file(filepath)
    jpeg.decode(0, 0)


def display_countdown(display: boadger2040.Badger2040, countdown_str: str) -> None:
    display.set_font("sans")
    display.set_pen(0)
    display.set_thickness(3)
    display.text(countdown_str, 8, 60, WIDTH, 1)


def display_voltage(display: badger2040.Badger2040, x: int = 185) -> None:
    display.set_font("bitmap8")
    display.set_pen(0)
    display.text(get_voltage(), x, 115, WIDTH, 1)


def main() -> None:
    display = badger2040.Badger2040()
    display.led(8)
    display.set_update_speed(badger2040.UPDATE_FAST)
    config_rtc(display)
    
    if is_clock_set():
        countdown_str = get_diffstr()
        if countdown_str:
            display_background(display, "/emerytura/emerytura.jpg")
            display_countdown(display, countdown_str)
        else:
            display_background(display, "/emerytura/juz_na_emeryturze.jpg")
        display_voltage(display)
    else:
        display_background(display, "/emerytura/bad_clock.jpg")
        display_voltage(display, 260)

    display.update()

    badger2040.pico_rtc_to_pcf()
    badger2040.sleep_for(180)


try:
    while True:
        main()
except Exception as e:
    import io
    import sys
    display = badger2040.Badger2040()
    display.set_font("bitmap8")
    display.set_pen(15)
    display.text(f"Exception: {e}", 5, 5, WIDTH, 1)
    tb = io.StringIO()
    sys.print_exception(e, tb)
    line_pos=15
    for tb_line in tb.getvalue().split('\n'):
        display.text(tb_line, 5, line_pos, WIDTH, 1)
        line_pos = line_pos+10
    display.update()
    while True:
        display.keepalive()
        display.halt()