14
14
from os import uname
15
15
from rtc_time_cfg import enabled
16
16
if not enabled :
17
- print ('rtc_time module has not been enabled.' )
18
- sys .exit (0 )
17
+ raise ImportError ('rtc_time is not enabled.' )
18
+
19
+ # sleep_ms is defined to stop things breaking if someone imports uasyncio.core
20
+ # Power won't be saved if this is done.
21
+ sleep_ms = utime .sleep_ms
19
22
20
- _PERIOD = const (604800000 ) # ms in 7 days
21
- _PERIOD_2 = const (302400000 ) # half period
22
- _SS_TO_MS = 1000 / 256 # Subsecs to ms
23
23
d_series = uname ().machine [:5 ] == 'PYBD_'
24
24
use_utime = True # Assume the normal utime timebase
25
25
39
39
raise OSError ('rtc_time.py is Pyboard-specific.' )
40
40
41
41
# For lowest power consumption set unused pins as inputs with pullups.
42
- # Note the 4K7 I2C pullups on X9 X10 Y9 Y10.
42
+ # Note the 4K7 I2C pullups on X9 X10 Y9 Y10 (Pyboard 1.x) .
43
43
if d_series :
44
44
print ('Running on Pyboard D' ) # Investigate which pins we can do this to TODO
45
- #for pin in [p for p in dir(pyb.Pin.board) if p[0] in 'XYW']:
46
- #pin_x = pyb.Pin(pin, pyb.Pin.IN, pyb.Pin.PULL_UP)
45
+ # pinlist = [p for p in dir(pyb.Pin.board) if p.startswith('W') and p[1].isdigit() and p[-1].isdigit()]
46
+ # sorted(pinlist, key=lambda s: int(s[1:]))
47
+ #pinlist = ['W3', 'W5', 'W6', 'W7', 'W8', 'W9', 'W10', 'W11', 'W12', 'W14', 'W15',
48
+ #'W16', 'W17', 'W18', 'W19', 'W20', 'W22', 'W23', 'W24', 'W25',
49
+ #'W26', 'W27', 'W28', 'W29', 'W30', 'W32', 'W33', 'W34', 'W43', 'W45',
50
+ #'W46', 'W47', 'W49', 'W50', 'W51', 'W52', 'W53', 'W54', 'W55', 'W56',
51
+ #'W57', 'W58', 'W59', 'W60', 'W61', 'W62', 'W63', 'W64', 'W65', 'W66',
52
+ #'W67', 'W68', 'W70', 'W71', 'W72', 'W73', 'W74']
53
+ # sorted([p for p in dir(pyb.Pin.board) if p[0] in 'XY' and p[-1].isdigit()], key=lambda x: int(x[1:]) if x[0]=='X' else int(x[1:])+100)
54
+ pinlist = ['X1' , 'X2' , 'X3' , 'X4' , 'X5' , 'X6' , 'X7' , 'X8' , 'X9' , 'X10' , 'X11' , 'X12' ,
55
+ 'Y3' , 'Y4' , 'Y5' , 'Y6' , 'Y7' , 'Y8' , 'Y9' , 'Y10' , 'Y11' , 'Y12' ]
56
+ for pin in pinlist :
57
+ pin_x = pyb .Pin (pin , pyb .Pin .IN , pyb .Pin .PULL_UP )
58
+ pyb .Pin ('EN_3V3' ).off ()
47
59
else :
48
60
print ('Running on Pyboard 1.x' )
49
61
for pin in [p for p in dir (pyb .Pin .board ) if p [0 ] in 'XY' ]:
50
62
pin_x = pyb .Pin (pin , pyb .Pin .IN , pyb .Pin .PULL_UP )
51
63
# User code redefines any pins in use
52
64
53
- # sleep_ms is defined to stop things breaking if someone imports uasyncio.core
54
- # Power won't be saved if this is done.
55
- sleep_ms = utime .sleep_ms
56
- if use_utime : # Run utime: Pyboard connected to PC via USB or alien platform
65
+ if use_utime :
57
66
ticks_ms = utime .ticks_ms
58
67
ticks_add = utime .ticks_add
59
68
ticks_diff = utime .ticks_diff
60
- else :
69
+ else : # All conditions met for low power operation
70
+ _PERIOD = const (604800000 ) # ms in 7 days
71
+ _PERIOD_2 = const (302400000 ) # half period
72
+ _SS_TO_MS = 1000 / 256 # Subsecs to ms
61
73
rtc = pyb .RTC ()
62
74
# dt: (year, month, day, weekday, hours, minutes, seconds, subseconds)
63
75
# weekday is 1-7 for Monday through Sunday.
@@ -82,9 +94,6 @@ def ticks_diff(end, start):
82
94
83
95
import uasyncio as asyncio
84
96
85
- # Common version has a needless dict: https://www.python.org/dev/peps/pep-0318/#examples
86
- # https://stackoverflow.com/questions/6760685/creating-a-singleton-in-python
87
- # Resolved: https://forum.micropython.org/viewtopic.php?f=2&t=5033&p=28824#p28824
88
97
def singleton (cls ):
89
98
instance = None
90
99
def getinstance (* args , ** kwargs ):
@@ -124,6 +133,7 @@ def _run(self):
124
133
rtc .wakeup (None )
125
134
126
135
def value (self , val = None ):
127
- if val is not None and not use_utime :
136
+ v = self ._t_ms
137
+ if val is not None :
128
138
self ._t_ms = max (val , 0 )
129
- return self . _t_ms
139
+ return v
0 commit comments