Skip to content

Commit f6152e3

Browse files
committed
lowpower Latency is functor.
1 parent f389189 commit f6152e3

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

lowpower/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# A low power usayncio adaptation
22

3-
Release 0.11 11th April 2019
3+
Release 0.12 15th April 2019
44

55
API changes: low power applications must now import `rtc_time_cfg` and set its
66
`enabled` flag.
7-
`Latency` class: Constructor requires event loop arg.
7+
`Latency` is now a functor rather than a class.
88

99
This module is specific to Pyboards including the D series.
1010

@@ -129,10 +129,10 @@ try:
129129
raise AttributeError
130130
except AttributeError:
131131
raise OSError('This requires fast_io fork of uasyncio.')
132-
import rtc_time
132+
from rtc_time import Latency
133133
# Instantiate event loop with any args before running code that uses it
134134
loop = asyncio.get_event_loop()
135-
rtc_time.Latency(100) # Define latency in ms
135+
Latency(100) # Define latency in ms
136136
```
137137

138138
The `Latency` class has a continuously running loop that executes `pyb.stop`
@@ -300,7 +300,7 @@ The class is a singleton consequently there is no need to pass an instance
300300
around or to make it global. Once instantiated, latency may be changed by
301301

302302
```python
303-
rtc_time.Latency(t)
303+
Latency(t)
304304
```
305305

306306
###### [Contents](./README.md#a-low-power-usayncio-adaptation)
@@ -321,10 +321,10 @@ try:
321321
except AttributeError:
322322
raise OSError('This requires fast_io fork of uasyncio.')
323323
# Do this import before configuring any pins or I/O:
324-
import rtc_time
324+
from rtc_time import Latency
325325
# Instantiate event loop with any args before running code that uses it:
326326
loop = asyncio.get_event_loop()
327-
lp = rtc_time.Latency(100) # Define latency in ms
327+
Latency(100) # Define latency in ms
328328
# Run application code
329329
```
330330

lowpower/lp_uart.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
raise AttributeError
1818
except AttributeError:
1919
raise OSError('This requires fast_io fork of uasyncio.')
20-
import rtc_time
20+
from rtc_time import Latency, use_utime
2121

2222
# Stop the test after a period
2323
async def killer(duration):
@@ -39,13 +39,13 @@ async def receiver(uart_in, uart_out):
3939
await swriter.awrite(res)
4040

4141
def test(duration):
42-
if rtc_time.use_utime: # Not running in low power mode
42+
if use_utime: # Not running in low power mode
4343
pyb.LED(3).on()
4444
uart2 = pyb.UART(1, 115200)
4545
uart4 = pyb.UART(4, 9600)
4646
# Instantiate event loop before using it in Latency class
4747
loop = asyncio.get_event_loop()
48-
lp = rtc_time.Latency(50) # ms
48+
lp = Latency(50) # ms
4949
loop.create_task(sender(uart4))
5050
loop.create_task(receiver(uart4, uart2))
5151
loop.run_until_complete(killer(duration))

lowpower/lpdemo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
raise AttributeError
1414
except AttributeError:
1515
raise OSError('This requires fast_io fork of uasyncio.')
16-
import rtc_time
16+
from rtc_time import Latency
1717

1818
class Button(aswitch.Switch):
1919
def __init__(self, pin):
@@ -35,7 +35,7 @@ def start(loop, leds, tims):
3535
running = True
3636
coros = []
3737
# Demo: assume app requires higher speed (not true in this instance)
38-
rtc_time.Latency().value(50)
38+
Latency(50)
3939
# Here you might apply power to external hardware
4040
for x, led in enumerate(leds): # Create a coroutine for each LED
4141
coros.append(toggle(led, tims[x]))
@@ -50,7 +50,7 @@ def stop(leds, coros):
5050
# Remove power from external hardware
5151
for led in leds:
5252
led.off()
53-
rtc_time.Latency().value(200) # Slow down scheduler to conserve power
53+
Latency(200) # Slow down scheduler to conserve power
5454

5555
async def monitor(loop, button):
5656
leds = [LED(x) for x in (1, 2, 3)] # Create list of LED's and times

lowpower/rtc_time.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,24 @@ def ticks_diff(end, start):
115115

116116
import uasyncio as asyncio
117117

118-
def singleton(cls):
118+
def functor(cls):
119119
instance = None
120120
def getinstance(*args, **kwargs):
121121
nonlocal instance
122122
if instance is None:
123123
instance = cls(*args, **kwargs)
124-
return instance
124+
return instance
125+
return instance(*args, **kwargs)
125126
return getinstance
126127

127-
@singleton
128-
class Latency():
128+
@functor
129+
class Latency:
129130
def __init__(self, t_ms=100):
130131
if use_utime: # Not in low power mode: t_ms stays zero
131132
self._t_ms = 0
132133
else:
133134
if asyncio.got_event_loop():
134-
self._t_ms = t_ms
135+
self._t_ms = max(t_ms, 0)
135136
loop = asyncio.get_event_loop()
136137
loop.create_task(self._run())
137138
else:
@@ -145,16 +146,17 @@ def _run(self):
145146
while True:
146147
if t_ms > 0:
147148
pyb.stop()
149+
# Pending tasks run once, may change self._t_ms
148150
yield
149-
if t_ms != self._t_ms:
151+
if t_ms != self._t_ms: # Has changed: update wakeup
150152
t_ms = self._t_ms
151153
if t_ms > 0:
152154
rtc.wakeup(t_ms)
153155
else:
154156
rtc.wakeup(None)
155157

156-
def value(self, val=None):
158+
def __call__(self, t_ms=None):
157159
v = self._t_ms
158-
if val is not None:
159-
self._t_ms = max(val, 0)
160+
if t_ms is not None:
161+
self._t_ms = max(t_ms, 0)
160162
return v

0 commit comments

Comments
 (0)