|
1 |
| -### serial-periodic-print-mcp9740 v1.1 |
2 |
| - |
3 |
| -### copy this file to BBC micro:bit V2 as main.py |
4 |
| - |
5 |
| -### MIT License |
6 |
| - |
7 |
| -### Copyright (c) 2025 Kevin J. Walters |
8 |
| - |
9 |
| -### Permission is hereby granted, free of charge, to any person obtaining a copy |
10 |
| -### of this software and associated documentation files (the "Software"), to deal |
11 |
| -### in the Software without restriction, including without limitation the rights |
12 |
| -### to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
13 |
| -### copies of the Software, and to permit persons to whom the Software is |
14 |
| -### furnished to do so, subject to the following conditions: |
15 |
| - |
16 |
| -### The above copyright notice and this permission notice shall be included in all |
17 |
| -### copies or substantial portions of the Software. |
18 |
| - |
19 |
| -### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
20 |
| -### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
21 |
| -### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
22 |
| -### AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
23 |
| -### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
24 |
| -### OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
25 |
| -### SOFTWARE. |
26 |
| - |
27 |
| - |
28 |
| -import gc |
29 |
| - |
30 |
| -import mcp7940 |
31 |
| - |
32 |
| -from microbit import i2c |
33 |
| -from utime import ticks_us, ticks_diff, ticks_add |
34 |
| - |
35 |
| -## PERIOD_US = 60 * 60 * 1000 * 1000 |
36 |
| -## PERIOD_US = 10 * 1000 * 1000 |
37 |
| -PERIOD_US = 1 * 1000 * 1000 |
38 |
| - |
39 |
| - |
40 |
| -class EnhancedI2C: |
41 |
| - def __init__(self, i2c): |
42 |
| - self._i2c = i2c |
43 |
| - |
44 |
| - def readfrom_mem(self, addr, memaddr, nbytes, *, addrsize=8): |
45 |
| - i2c.write(addr, bytes([memaddr])) |
46 |
| - return(i2c.read(addr, nbytes)) |
47 |
| - |
48 |
| - def readfrom_mem_into(self, addr, memaddr, buf, *, addrsize=8): |
49 |
| - raise NotImplementedError |
50 |
| - |
51 |
| - def writeto_mem(self, addr, memaddr, buf, *, addrsize=8): |
52 |
| - i2c.write(addr, bytes([memaddr]) + buf) |
53 |
| - |
54 |
| - |
55 |
| -ei2c = EnhancedI2C(i2c) |
56 |
| -mcp = mcp7940.MCP7940(ei2c) |
57 |
| -mcp.set_trim(0) |
58 |
| -mcp.time = (2025, 1, 1, 00, 00, 00, 2, 1) |
59 |
| -mcp.start() |
60 |
| - |
61 |
| -last_print_us = None |
62 |
| -timestamp_us = None |
63 |
| -uncollected = True |
64 |
| - |
65 |
| -gc.collect() |
66 |
| -last_print_us = ticks_us() |
67 |
| -while True: |
68 |
| - timestamp_us = ticks_us() |
69 |
| - if ticks_diff(timestamp_us, last_print_us) >= PERIOD_US: |
70 |
| - print(timestamp_us, ticks_us(), *mcp.time, sep=",") |
71 |
| - ### Don't assign timestamp_us here in case it has slipped a little |
72 |
| - last_print_us = ticks_add(last_print_us, PERIOD_US) |
73 |
| - uncollected = True |
74 |
| - elif uncollected: |
75 |
| - gc.collect() |
76 |
| - uncollected = False |
| 1 | +### serial-periodic-print-mcp9740 v1.2 |
| 2 | + |
| 3 | +### copy this file to BBC micro:bit V2 as main.py |
| 4 | + |
| 5 | +### MIT License |
| 6 | + |
| 7 | +### Copyright (c) 2025 Kevin J. Walters |
| 8 | + |
| 9 | +### Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +### of this software and associated documentation files (the "Software"), to deal |
| 11 | +### in the Software without restriction, including without limitation the rights |
| 12 | +### to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +### copies of the Software, and to permit persons to whom the Software is |
| 14 | +### furnished to do so, subject to the following conditions: |
| 15 | + |
| 16 | +### The above copyright notice and this permission notice shall be included in all |
| 17 | +### copies or substantial portions of the Software. |
| 18 | + |
| 19 | +### THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +### IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +### FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +### AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +### LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +### OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 | +### SOFTWARE. |
| 26 | + |
| 27 | + |
| 28 | +import gc |
| 29 | + |
| 30 | +import mcp7940 |
| 31 | + |
| 32 | +from microbit import i2c |
| 33 | +from utime import ticks_us, ticks_diff, ticks_add |
| 34 | + |
| 35 | +### This will work for up to 6 minutes with ticks_us/ticks_diff |
| 36 | +PERIOD_US = 6 * 60 * 1000 * 1000 |
| 37 | +## PERIOD_US = 10 * 1000 * 1000 |
| 38 | +##PERIOD_US = 1 * 1000 * 1000 |
| 39 | + |
| 40 | +class EnhancedI2C: |
| 41 | + def __init__(self, i2c_): |
| 42 | + self._i2c = i2c_ |
| 43 | + |
| 44 | + def readfrom_mem(self, addr, memaddr, nbytes, *, addrsize=8): |
| 45 | + self._i2c.write(addr, bytes([memaddr])) |
| 46 | + return self._i2c.read(addr, nbytes) |
| 47 | + |
| 48 | + def readfrom_mem_into(self, addr, memaddr, buf, *, addrsize=8): |
| 49 | + raise NotImplementedError |
| 50 | + |
| 51 | + def writeto_mem(self, addr, memaddr, buf, *, addrsize=8): |
| 52 | + self._i2c.write(addr, bytes([memaddr]) + buf) |
| 53 | + |
| 54 | + |
| 55 | +ei2c = EnhancedI2C(i2c) |
| 56 | +mcp = mcp7940.MCP7940(ei2c) |
| 57 | +mcp.set_trim(0) |
| 58 | +mcp.time = (2025, 1, 1, 00, 00, 00, 2, 1) |
| 59 | +mcp.start() |
| 60 | + |
| 61 | +last_print_us = None |
| 62 | +timestamp_us = None |
| 63 | +uncollected = False |
| 64 | + |
| 65 | +gc.collect() |
| 66 | +last_print_us = ticks_us() |
| 67 | +while True: |
| 68 | + timestamp_us = ticks_us() |
| 69 | + if ticks_diff(timestamp_us, last_print_us) >= PERIOD_US: |
| 70 | + print(timestamp_us, ticks_us(), *mcp.time, sep=",") |
| 71 | + ### Don't assign timestamp_us here in case it has slipped a little |
| 72 | + last_print_us = ticks_add(last_print_us, PERIOD_US) |
| 73 | + uncollected = True |
| 74 | + elif uncollected: |
| 75 | + gc.collect() |
| 76 | + uncollected = False |
0 commit comments