-
Notifications
You must be signed in to change notification settings - Fork 0
/
m5wm.py
118 lines (99 loc) · 3.1 KB
/
m5wm.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
from m5stack import lcd, axp, btnA, btnB, speaker
import machine, utime, ujson, uos, ntptime, wifiCfg
will_remove_cache = False
def show_startup_screen():
STARTUP_SCREEN_TIME = 5
lcd.clear(lcd.WHITE)
lcd.circle(68, 120, 46, color=lcd.BLACK, fillcolor=lcd.BLACK)
lcd.triangle(29, 131, 72, 94, 76, 122, color=lcd.YELLOW, fillcolor=lcd.YELLOW)
lcd.triangle(107, 108, 63, 145, 58, 118, color=lcd.YELLOW, fillcolor=lcd.YELLOW)
def btnA_was_double_press():
"""
キャッシュファイルを(後で)削除する
"""
global will_remove_cache
will_remove_cache = True
speaker.sing(440)
btnA.wasDoublePress(btnA_was_double_press)
utime.sleep(STARTUP_SCREEN_TIME)
btnA.restart()
def main():
import wattmeter
global will_remove_cache
TASK_LOOP_WAIT_SEC = 3
ERROR_DISPLAY_TIME = 60
REBOOT_WAIT_SEC = 10
logging = wattmeter.Logger
logging.basicConfig(
level=logging.INFO
)
wm = wattmeter.M5Wattmeter(
vlcd=wattmeter.VirtualLCD(lcd=lcd, axp=axp),
client=wattmeter.BP35A1Client(uart=machine.UART, utime=utime, logging=logging),
config=wattmeter.WMConfig(ujson=ujson, uos=uos, logging=logging),
logging=logging,
wifiCfg=wifiCfg,
utime=utime,
ntptime=ntptime,
speaker=speaker
)
if will_remove_cache:
"""
キャッシュファイルを削除後に再起動する
再起動しないとwm.prepare()内のキャッシュファイル生成時にパニックを起こす
"""
logging.info("Cache file will be removed.")
wm.config.removeCache()
logging.info("Restart")
machine.reset()
def btnA_was_released():
"""
画面の点灯/消灯を切り替える
"""
nonlocal wm
wm.toggleSleep()
def btnB_was_released():
"""
FACEを切り替える
"""
nonlocal wm
wm.switchFace()
def btnB_press_for():
"""
画面の上下を切り替える
"""
nonlocal wm
wm.toggleFlip()
btnA.wasReleased(btnA_was_released)
btnB.wasReleased(btnB_was_released)
btnB.pressFor(2, btnB_press_for)
prepared = False
try:
wm.prepare()
except Exception as e:
logging.exception(e)
wm.vlcd.showError(str(e), e.__class__.__name__)
else:
prepared = True
while prepared:
try:
while wm.execLaunchableTask():
pass
except Exception as e:
logging.exception(e)
wm.vlcd.showError(str(e), e.__class__.__name__)
break
utime.sleep(TASK_LOOP_WAIT_SEC)
if wm.config.config is None:
return
if wm.config.config.wattmeter.auto_reboot:
"""
auto_reboot = True の場合はエラーを一定時間表示してから再起動
"""
utime.sleep(ERROR_DISPLAY_TIME)
wm.vlcd.showError("Shutting down...", "Reboot")
utime.sleep(REBOOT_WAIT_SEC)
machine.reset()
if __name__ == "__main__":
show_startup_screen()
main()