Skip to content

Commit d7442f2

Browse files
committed
Update my_cam/cam for uasyncio V3
1 parent f4a3727 commit d7442f2

File tree

1 file changed

+41
-23
lines changed

1 file changed

+41
-23
lines changed

my_cam/cam.py

+41-23
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
from machine import Pin, SPI, I2C, freq
88
from utime import ticks_ms, ticks_diff
99
import uasyncio as asyncio
10-
import micropython
10+
from micropython import const, mem_info
1111
import gc
1212

13-
from aswitch import Switch, Delay_ms
13+
from primitives.switch import Switch
14+
from primitives.delay_ms import Delay_ms
1415
from ssd1351_16bit import SSD1351 as SSD # STM Asm version
1516
from writer import CWriter
1617
import courier17 as font # Main text
@@ -20,17 +21,23 @@
2021
from interpolate_a import Interpolator # STM assembler version
2122

2223
freq(216_000_000) # In old version improved update rate 750ms -> 488ms
23-
loop = asyncio.get_event_loop()
2424

2525
eliza = lambda *_ : None
2626

27+
# Possible modes. Note there is no point in having a _HOLD mode as the text
28+
# fields are not updated so it would never show.
29+
_NORM = const(0)
30+
_AUTO = const(1)
31+
_HOG = const(2)
32+
2733
class Cam:
2834

2935
def __init__(self, txt_rf_ms, verbose):
36+
self.txt_rf_ms = txt_rf_ms
3037
self.verbose = verbose
3138
self.tmax = 30 # Initial temperature range
3239
self.tmin = 15
33-
self.auto_range = False
40+
self.mode = _NORM
3441
# Enable initial update
3542
self.rf_disp = True
3643
self.rf_txt = True
@@ -40,13 +47,13 @@ def __init__(self, txt_rf_ms, verbose):
4047

4148
# Instantiate switches
4249
self.timer = Delay_ms(duration=2000) # Long press delay
43-
# Release arg rarg is for future use. Enables calling switch to be identified.
50+
# Release arg rarg enables calling switch to be identified.
4451
for item in (('X4', self.chmax, 5, self.ar, 0), ('Y1', self.chmax, -5, self.ar, 1),
4552
('X5', self.chmin, 5, eliza, 2), ('X6', self.chmin, -5, eliza, 3)):
46-
sw, func, arg, rfunc, rarg = item
53+
sw, func, arg, long_func, rarg = item
4754
cs = Switch(Pin(sw, Pin.IN, Pin.PULL_UP))
4855
cs.close_func(self.press, (func, arg))
49-
cs.open_func(self.release, (rfunc, rarg))
56+
cs.open_func(self.release, (long_func, rarg))
5057

5158
# Instantiate display
5259
pdc = Pin('X1', Pin.OUT_PP, value=0)
@@ -60,20 +67,20 @@ def __init__(self, txt_rf_ms, verbose):
6067
ssd.fill(0)
6168
ssd.show()
6269

70+
self.avg = 0.0
6371
# Instantiate PIR temperature sensor
6472
i2c = I2C(2)
6573
pir = AMG88XX(i2c)
6674
pir.ma_mode(True) # Moving average mode
6775

6876
# Run the camera
69-
loop.create_task(self.run(pir, ssd))
70-
loop.create_task(self.refresh_txt(txt_rf_ms))
77+
asyncio.create_task(self.run(pir, ssd))
7178

7279
# A switch was pressed. Change temperature range.
7380
def press(self, func, arg):
7481
self.timer.trigger()
75-
if self.auto_range: # Short press clears auto range
76-
self.auto_range = False # Leave range unchanged
82+
self.mode = _NORM
83+
if self.mode == _AUTO: # Short press clears auto range, leaves range unchanged
7784
self.rf_disp = True
7885
self.rf_txt = True
7986
else:
@@ -92,13 +99,18 @@ def chmin(self, val):
9299

93100
def release(self, func, arg):
94101
if self.timer.running(): # Brief press: re-enable display
95-
self.rf_disp = True
96102
self.rf_txt = True # Show changed range
103+
self.rf_disp = True
97104
else:
98-
func(arg)
99-
100-
def ar(self, _):
101-
self.auto_range = True
105+
func(arg) # eliza will leave it with rf_disp False
106+
107+
def ar(self, sw): # Long press callback, top switch
108+
if sw: # Animal detection mode
109+
self.mode = _HOG
110+
self.tmin = self.avg
111+
self.tmax = self.avg + 5
112+
else: # Auto range
113+
self.mode = _AUTO
102114
self.rf_disp = True
103115
self.rf_txt = True # Show changed range
104116

@@ -112,9 +124,9 @@ def draw_scale(self, ssd):
112124
val -= dt
113125

114126
# Refreshing text is slow so do it periodically to maximise mean image framerate
115-
async def refresh_txt(self, tim):
127+
async def refresh_txt(self):
116128
while True:
117-
await asyncio.sleep_ms(tim)
129+
await asyncio.sleep_ms(self.txt_rf_ms)
118130
self.rf_txt = True
119131

120132
# Run the camera
@@ -151,15 +163,16 @@ async def run(self, pir, ssd):
151163
sum_t += val
152164
ssd.rect(col * 2, row * 2, 2, 2, ssd.rgb(*self.mapper(val)))
153165
await asyncio.sleep(0)
154-
if self.auto_range:
166+
self.avg = round(sum_t / 1024)
167+
if self.mode == _AUTO:
155168
self.tmin = round(min_t)
156169
self.tmax = round(max_t)
157170
if self.rf_disp:
158171
if self.rf_txt:
159172
wri_l.set_textpos(ssd, 66, 0)
160173
wri_l.printstring('Max:{:+4d}C\n'.format(int(max_t)))
161174
wri_l.printstring('Min:{:+4d}C\n'.format(int(min_t)))
162-
wri_l.printstring('Avg:{:+4d}C'.format(round(sum_t / 1024)))
175+
wri_l.printstring('Avg:{:+4d}C'.format(self.avg))
163176
wri_s.set_textpos(ssd, 128 - arial10.height(), 64)
164177
wri_s.setcolor(yellow, black)
165178
wri_s.printstring('Chip:{:5.1f}C'.format(pir.temperature()))
@@ -168,19 +181,24 @@ async def run(self, pir, ssd):
168181
wri_s.printstring('{:4d}C '.format(self.tmax))
169182
wri_s.set_textpos(ssd, 28, 95)
170183
wri_s.setcolor(green, black)
171-
wri_s.printstring('AR:{:s}'.format('on ' if self.auto_range else 'off'))
184+
if self.mode == _HOG:
185+
wri_s.printstring('Hog ')
186+
elif self.mode == _NORM:
187+
wri_s.printstring('Norm ')
188+
else:
189+
wri_s.printstring('Auto ')
172190
wri_s.set_textpos(ssd, 64 - arial10.height(), 90)
173191
wri_s.setcolor(blue, black)
174192
wri_s.printstring('{:4d}C '.format(self.tmin))
175193
self.rf_txt = False
176194
ssd.show()
177195
self.verbose and print(ticks_diff(ticks_ms(), t))
178196
gc.collect()
179-
# self.verbose and micropython.mem_info()
197+
# self.verbose and mem_info()
180198

181199
# stack: 1276 out of 15360
182200
# GC: total: 196672, used: 52128, free: 144544
183201
# No. of 1-blocks: 365, 2-blocks: 106, max blk sz: 1024, max free sz: 2545
184202

185203
cam = Cam(2000, False) # Refresh text every 2000ms. Verbose?
186-
loop.run_forever()
204+
asyncio.run(cam.refresh_txt())

0 commit comments

Comments
 (0)