-
Notifications
You must be signed in to change notification settings - Fork 466
/
inky_helper.py
154 lines (113 loc) · 3.81 KB
/
inky_helper.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
from pimoroni_i2c import PimoroniI2C
from pcf85063a import PCF85063A
import math
from machine import Pin, PWM, Timer
import time
import inky_frame
import json
import network
import os
# Pin setup for VSYS_HOLD needed to sleep and wake.
HOLD_VSYS_EN_PIN = 2
hold_vsys_en_pin = Pin(HOLD_VSYS_EN_PIN, Pin.OUT)
# intialise the pcf85063a real time clock chip
I2C_SDA_PIN = 4
I2C_SCL_PIN = 5
i2c = PimoroniI2C(I2C_SDA_PIN, I2C_SCL_PIN, 100000)
rtc = PCF85063A(i2c)
led_warn = Pin(6, Pin.OUT)
# set up for the network LED
network_led_pwm = PWM(Pin(7))
network_led_pwm.freq(1000)
network_led_pwm.duty_u16(0)
# set the brightness of the network led
def network_led(brightness):
brightness = max(0, min(100, brightness)) # clamp to range
# gamma correct the brightness (gamma 2.8)
value = int(pow(brightness / 100.0, 2.8) * 65535.0 + 0.5)
network_led_pwm.duty_u16(value)
network_led_timer = Timer(-1)
network_led_pulse_speed_hz = 1
def network_led_callback(t):
# updates the network led brightness based on a sinusoid seeded by the current time
brightness = (math.sin(time.ticks_ms() * math.pi * 2 / (1000 / network_led_pulse_speed_hz)) * 40) + 60
value = int(pow(brightness / 100.0, 2.8) * 65535.0 + 0.5)
network_led_pwm.duty_u16(value)
# set the network led into pulsing mode
def pulse_network_led(speed_hz=1):
global network_led_timer, network_led_pulse_speed_hz
network_led_pulse_speed_hz = speed_hz
network_led_timer.deinit()
network_led_timer.init(period=50, mode=Timer.PERIODIC, callback=network_led_callback)
# turn off the network led and disable any pulsing animation that's running
def stop_network_led():
global network_led_timer
network_led_timer.deinit()
network_led_pwm.duty_u16(0)
def sleep(t):
# Time to have a little nap until the next update
rtc.clear_timer_flag()
rtc.set_timer(t, ttp=rtc.TIMER_TICK_1_OVER_60HZ)
rtc.enable_timer_interrupt(True)
# Set the HOLD VSYS pin to an input
# this allows the device to go into sleep mode when on battery power.
hold_vsys_en_pin.init(Pin.IN)
# Regular time.sleep for those powering from USB
time.sleep(60 * t)
# Turns off the button LEDs
def clear_button_leds():
inky_frame.button_a.led_off()
inky_frame.button_b.led_off()
inky_frame.button_c.led_off()
inky_frame.button_d.led_off()
inky_frame.button_e.led_off()
def network_connect(SSID, PSK):
# Enable the Wireless
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
# Number of attempts to make before timeout
max_wait = 10
# Sets the Wireless LED pulsing and attempts to connect to your local network.
pulse_network_led()
wlan.config(pm=0xa11140) # Turn WiFi power saving off for some slow APs
wlan.connect(SSID, PSK)
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print('waiting for connection...')
time.sleep(1)
stop_network_led()
network_led_pwm.duty_u16(30000)
# Handle connection error. Switches the Warn LED on.
if wlan.status() != 3:
stop_network_led()
led_warn.on()
state = {"run": None}
app = None
def file_exists(filename):
try:
return (os.stat(filename)[0] & 0x4000) == 0
except OSError:
return False
def clear_state():
if file_exists("state.json"):
os.remove("state.json")
def save_state(data):
with open("/state.json", "w") as f:
f.write(json.dumps(data))
f.flush()
def load_state():
global state
data = json.loads(open("/state.json", "r").read())
if type(data) is dict:
state = data
def update_state(running):
global state
state['run'] = running
save_state(state)
def launch_app(app_name):
global app
app = __import__(app_name)
print(app)
update_state(app_name)