Skip to content

Commit db6e4e3

Browse files
committed
GPS and HTU21D ported to V3
1 parent d184e6e commit db6e4e3

35 files changed

+872
-264
lines changed

v3/README.md

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1, Guide to uasyncio V3
1+
# 1 Guide to uasyncio V3
22

33
The new release of `uasyncio` is pre-installed in current daily firmware
44
builds. This complete rewrite of `uasyncio` supports CPython 3.8 syntax. A
@@ -7,11 +7,24 @@ design aim is that it should be be a compatible subset of `asyncio`.
77
These notes and the tutorial should be read in conjunction with
88
[the official docs](http://docs.micropython.org/en/latest/library/uasyncio.html)
99

10-
There is a new tutorial for V3.
10+
## 1.1 Resources for V3
1111

12-
#### [V3 Tutorial](./TUTORIAL.md)
12+
This repo contains the following:
1313

14-
# 2. Overview
14+
#### [V3 Tutorial](./docs/TUTORIAL.md)
15+
#### Test/demo scripts
16+
17+
Documented in the tutorial.
18+
19+
#### Synchronisation primitives
20+
21+
Documented in the tutorial.
22+
23+
#### Asynchronous device drivers
24+
The device drivers are in the process of being ported. Currently there is a
25+
[GPS driver](./docs/GPS.md).
26+
27+
# 2 V3 Overview
1528

1629
These notes are intended for users familiar with `asyncio` under CPython.
1730

@@ -34,7 +47,7 @@ The `uasyncio` library supports the following features:
3447
It supports millisecond level timing with the following:
3548
* `uasyncio.sleep_ms(time)`
3649

37-
It includes the followiing CPython compatible synchronisation primitives:
50+
It includes the following CPython compatible synchronisation primitives:
3851
* `Event`.
3952
* `Lock`.
4053
* `gather`.
@@ -45,7 +58,7 @@ supported.
4558
The `Future` class is not supported, nor are the `event_loop` methods
4659
`call_soon`, `call_later`, `call_at`.
4760

48-
# 3. Porting applications from V2
61+
# 3 Porting applications from V2
4962

5063
Many applications using the coding style advocated in the V2 tutorial will work
5164
unchanged. However there are changes, firstly to `uasyncio` syntax and secondly
@@ -65,12 +78,12 @@ related to modules in this repository.
6578

6679
It is possible to write an awaitable class with code portable between
6780
MicroPython and CPython 3.8. This is discussed
68-
[in the tutorial](./TUTORIAL.md#412-portable-code).
81+
[in the tutorial](./docs/TUTORIAL.md#412-portable-code).
6982

7083
## 3.2 Modules from this repository
7184

7285
Modules `asyn.py` and `aswitch.py` are deprecated for V3 applications. See
73-
[the tutorial](./TUTORIAL.md) for V3 replacements.
86+
[the tutorial](./docs/TUTORIAL.md#3-synchronisation) for V3 replacements.
7487

7588
### 3.2.1 Synchronisation primitives
7689

@@ -101,7 +114,7 @@ used with V3:
101114
* The nonstandard support for `gather` (now properly supported).
102115

103116
The `Event` class in `asyn.py` is now replaced by `Message` - this is discussed
104-
in [the tutorial](./TUTORIAL.md).
117+
in [the tutorial](./docs/TUTORIAL.md#36-message).
105118

106119
### 3.2.3 Switches, Pushbuttons and delays (old aswitch.py)
107120

@@ -114,7 +127,7 @@ New versions are provided in this repository. Classes:
114127
* `Switch` Debounced switch with close and open callbacks.
115128
* `Pushbutton` Pushbutton with double-click and long press callbacks.
116129

117-
# 4. Outstanding issues with V3
130+
# 4 Outstanding issues with V3
118131

119132
V3 is still a work in progress. The following is a list of issues which I hope
120133
will be addressed in due course.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

v3/as_drivers/as_GPS/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .as_GPS import *

v3/demos/gps/as_GPS.py renamed to v3/as_drivers/as_GPS/as_GPS.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ def time_since_fix(self): # ms since last valid fix
555555
return self._time_diff(self._get_time(), self._fix_time)
556556

557557
def compass_direction(self): # Return cardinal point as string.
558-
from as_GPS_utils import compass_direction
558+
from .as_GPS_utils import compass_direction
559559
return compass_direction(self)
560560

561561
def latitude_string(self, coord_format=DM):
@@ -611,5 +611,5 @@ def time_string(self, local=True):
611611
return '{:02d}:{:02d}:{:02d}'.format(hrs, mins, secs)
612612

613613
def date_string(self, formatting=MDY):
614-
from as_GPS_utils import date_string
614+
from .as_GPS_utils import date_string
615615
return date_string(self, formatting)

v3/demos/gps/as_GPS_time.py renamed to v3/as_drivers/as_GPS/as_GPS_time.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import pyb
1111
import utime
1212
import math
13-
import as_tGPS
13+
from .as_tGPS import GPS_Timer
1414
from primitives.message import Message
1515

1616
# Hardware assumptions. Change as required.
@@ -31,7 +31,7 @@ async def setup():
3131
uart = pyb.UART(UART_ID, 9600, read_buf_len=200)
3232
sreader = asyncio.StreamReader(uart)
3333
pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
34-
return as_tGPS.GPS_Timer(sreader, pps_pin, local_offset=1,
34+
return GPS_Timer(sreader, pps_pin, local_offset=1,
3535
fix_cb=lambda *_: red.toggle(),
3636
pps_cb=lambda *_: green.toggle())
3737

@@ -126,9 +126,9 @@ async def us_setup(tick):
126126
uart = pyb.UART(UART_ID, 9600, read_buf_len=200)
127127
sreader = asyncio.StreamReader(uart)
128128
pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
129-
return as_tGPS.GPS_Timer(sreader, pps_pin, local_offset=1,
130-
fix_cb=lambda *_: red.toggle(),
131-
pps_cb=us_cb, pps_cb_args=(tick, yellow))
129+
return GPS_Timer(sreader, pps_pin, local_offset=1,
130+
fix_cb=lambda *_: red.toggle(),
131+
pps_cb=us_cb, pps_cb_args=(tick, yellow))
132132

133133
async def do_usec(minutes):
134134
tick = Message()

v3/demos/gps/as_GPS_utils.py renamed to v3/as_drivers/as_GPS/as_GPS_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
# Copyright (c) 2018 Peter Hinch
66
# Released under the MIT License (MIT) - see LICENSE file
7-
from as_GPS import MDY, DMY, LONG
7+
from .as_GPS import MDY, DMY, LONG
88

99
_DIRECTIONS = ('N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW',
1010
'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW')

v3/demos/gps/as_rwGPS.py renamed to v3/as_drivers/as_GPS/as_rwGPS.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# Copyright (c) 2018 Peter Hinch
88
# Released under the MIT License (MIT) - see LICENSE file
99

10-
import as_GPS
10+
import as_drivers.as_GPS as as_GPS
1111
try:
1212
from micropython import const
1313
except ImportError:

v3/demos/gps/as_rwGPS_time.py renamed to v3/as_drivers/as_GPS/as_rwGPS_time.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import pyb
1919
import utime
2020
import math
21-
import as_tGPS
22-
import as_rwGPS
21+
from .as_tGPS import GPS_RWTimer
22+
from .as_rwGPS import FULL_COLD_START
2323

2424
# Hardware assumptions. Change as required.
2525
PPS_PIN = pyb.Pin.board.X3
@@ -46,7 +46,7 @@ async def shutdown():
4646
# session with ctrl-c.
4747
uart.init(BAUDRATE)
4848
await asyncio.sleep(0.5)
49-
await gps.command(as_rwGPS.FULL_COLD_START)
49+
await gps.command(FULL_COLD_START)
5050
print('Factory reset')
5151
gps.close() # Stop ISR
5252
#print('Restoring default baudrate (9600).')
@@ -67,7 +67,7 @@ async def setup():
6767
sreader = asyncio.StreamReader(uart)
6868
swriter = asyncio.StreamWriter(uart, {})
6969
pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
70-
gps = as_tGPS.GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
70+
gps = GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
7171
fix_cb=lambda *_: red.toggle(),
7272
pps_cb=lambda *_: blue.toggle())
7373
gps.FULL_CHECK = False
@@ -179,7 +179,7 @@ async def us_setup(tick):
179179
sreader = asyncio.StreamReader(uart)
180180
swriter = asyncio.StreamWriter(uart, {})
181181
pps_pin = pyb.Pin(PPS_PIN, pyb.Pin.IN)
182-
gps = as_tGPS.GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
182+
gps = GPS_RWTimer(sreader, swriter, pps_pin, local_offset=1,
183183
fix_cb=lambda *_: red.toggle(),
184184
pps_cb=us_cb, pps_cb_args=(tick, blue))
185185
gps.FULL_CHECK = False

v3/demos/gps/as_tGPS.py renamed to v3/as_drivers/as_GPS/as_tGPS.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This is STM-specific: requires pyb module.
33
# Hence not as RAM-critical as as_GPS
44

5-
# Copyright (c) 2018 Peter Hinch
5+
# Copyright (c) 2018-2020 Peter Hinch
66
# Released under the MIT License (MIT) - see LICENSE file
77
# TODO Test machine version. Replace LED with callback. Update tests and doc.
88

@@ -17,8 +17,8 @@
1717
import utime
1818
import micropython
1919
import gc
20-
import as_GPS
21-
import as_rwGPS
20+
from .as_GPS import RMC, AS_GPS
21+
from .as_rwGPS import GPS
2222

2323
micropython.alloc_emergency_exception_buf(100)
2424

@@ -31,17 +31,17 @@ def rtc_secs():
3131

3232
# Constructor for GPS_Timer class
3333
def gps_ro_t_init(self, sreader, pps_pin, local_offset=0,
34-
fix_cb=lambda *_ : None, cb_mask=as_GPS.RMC, fix_cb_args=(),
34+
fix_cb=lambda *_ : None, cb_mask=RMC, fix_cb_args=(),
3535
pps_cb=lambda *_ : None, pps_cb_args=()):
36-
as_GPS.AS_GPS.__init__(self, sreader, local_offset, fix_cb, cb_mask, fix_cb_args)
36+
AS_GPS.__init__(self, sreader, local_offset, fix_cb, cb_mask, fix_cb_args)
3737
self.setup(pps_pin, pps_cb, pps_cb_args)
3838

3939
# Constructor for GPS_RWTimer class
4040
def gps_rw_t_init(self, sreader, swriter, pps_pin, local_offset=0,
41-
fix_cb=lambda *_ : None, cb_mask=as_GPS.RMC, fix_cb_args=(),
41+
fix_cb=lambda *_ : None, cb_mask=RMC, fix_cb_args=(),
4242
msg_cb=lambda *_ : None, msg_cb_args=(),
4343
pps_cb=lambda *_ : None, pps_cb_args=()):
44-
as_rwGPS.GPS.__init__(self, sreader, swriter, local_offset, fix_cb, cb_mask, fix_cb_args,
44+
GPS.__init__(self, sreader, swriter, local_offset, fix_cb, cb_mask, fix_cb_args,
4545
msg_cb, msg_cb_args)
4646
self.setup(pps_pin, pps_cb, pps_cb_args)
4747

@@ -236,5 +236,5 @@ def get_t_split(self):
236236
self._time[3] = us + ims*1000
237237
return self._time
238238

239-
GPS_Timer = type('GPS_Timer', (GPS_Tbase, as_GPS.AS_GPS), {'__init__': gps_ro_t_init})
240-
GPS_RWTimer = type('GPS_RWTimer', (GPS_Tbase, as_rwGPS.GPS), {'__init__': gps_rw_t_init})
239+
GPS_Timer = type('GPS_Timer', (GPS_Tbase, AS_GPS), {'__init__': gps_ro_t_init})
240+
GPS_RWTimer = type('GPS_RWTimer', (GPS_Tbase, GPS), {'__init__': gps_rw_t_init})

v3/demos/gps/ast_pb.py renamed to v3/as_drivers/as_GPS/ast_pb.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pyb
99
import uasyncio as asyncio
1010
from primitives.delay_ms import Delay_ms
11-
import as_GPS
11+
from .as_GPS import DD, MPH, LONG, AS_GPS
1212

1313
red = pyb.LED(1)
1414
green = pyb.LED(2)
@@ -52,8 +52,8 @@ async def navigation(gps):
5252
await gps.data_received(position=True)
5353
print('***** NAVIGATION DATA *****')
5454
print('Data is Valid:', gps._valid)
55-
print('Longitude:', gps.longitude(as_GPS.DD))
56-
print('Latitude', gps.latitude(as_GPS.DD))
55+
print('Longitude:', gps.longitude(DD))
56+
print('Latitude', gps.latitude(DD))
5757
print()
5858

5959
async def course(gps):
@@ -62,7 +62,7 @@ async def course(gps):
6262
await gps.data_received(course=True)
6363
print('***** COURSE DATA *****')
6464
print('Data is Valid:', gps._valid)
65-
print('Speed:', gps.speed_string(as_GPS.MPH))
65+
print('Speed:', gps.speed_string(MPH))
6666
print('Course', gps.course)
6767
print('Compass Direction:', gps.compass_direction())
6868
print()
@@ -75,7 +75,7 @@ async def date(gps):
7575
print('Data is Valid:', gps._valid)
7676
print('UTC time:', gps.utc)
7777
print('Local time:', gps.local_time)
78-
print('Date:', gps.date_string(as_GPS.LONG))
78+
print('Date:', gps.date_string(LONG))
7979
print()
8080

8181
async def gps_test():
@@ -86,7 +86,7 @@ async def gps_test():
8686
sreader = asyncio.StreamReader(uart)
8787
timer = Delay_ms(timeout)
8888
sentence_count = 0
89-
gps = as_GPS.AS_GPS(sreader, local_offset=1, fix_cb=callback, fix_cb_args=(timer,))
89+
gps = AS_GPS(sreader, local_offset=1, fix_cb=callback, fix_cb_args=(timer,))
9090
print('awaiting first fix')
9191
asyncio.create_task(sat_test(gps))
9292
asyncio.create_task(stats(gps))

v3/demos/gps/ast_pbrw.py renamed to v3/as_drivers/as_GPS/ast_pbrw.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# ast_pb.py
1+
# ast_pbrw.py
22
# Basic test/demo of AS_GPS class (asynchronous GPS device driver)
33
# Runs on a Pyboard with GPS data on pin X2.
44
# Copyright (c) Peter Hinch 2018-2020
@@ -14,8 +14,8 @@
1414
import pyb
1515
import uasyncio as asyncio
1616
from primitives.delay_ms import Delay_ms
17-
import as_GPS
18-
import as_rwGPS
17+
from .as_GPS import DD, LONG, MPH
18+
from .as_rwGPS import *
1919

2020
# Avoid multiple baudrates. Tests use 9600 or 19200 only.
2121
BAUDRATE = 19200
@@ -69,8 +69,8 @@ async def navigation(gps):
6969
yellow.toggle()
7070
print('***** NAVIGATION DATA *****')
7171
print('Data is Valid:', hex(gps._valid))
72-
print('Longitude:', gps.longitude(as_GPS.DD))
73-
print('Latitude', gps.latitude(as_GPS.DD))
72+
print('Longitude:', gps.longitude(DD))
73+
print('Latitude', gps.latitude(DD))
7474
print()
7575

7676
async def course(gps):
@@ -79,7 +79,7 @@ async def course(gps):
7979
await gps.data_received(course=True)
8080
print('***** COURSE DATA *****')
8181
print('Data is Valid:', hex(gps._valid))
82-
print('Speed:', gps.speed_string(as_GPS.MPH))
82+
print('Speed:', gps.speed_string(MPH))
8383
print('Course', gps.course)
8484
print('Compass Direction:', gps.compass_direction())
8585
print()
@@ -92,7 +92,7 @@ async def date(gps):
9292
print('Data is Valid:', hex(gps._valid))
9393
print('UTC Time:', gps.utc)
9494
print('Local time:', gps.local_time)
95-
print('Date:', gps.date_string(as_GPS.LONG))
95+
print('Date:', gps.date_string(LONG))
9696
print()
9797

9898
async def change_status(gps, uart):
@@ -103,10 +103,10 @@ async def change_status(gps, uart):
103103
print('***** baudrate 19200 *****')
104104
await asyncio.sleep(5) # Ensure baudrate is sorted
105105
print('***** Query VERSION *****')
106-
await gps.command(as_rwGPS.VERSION)
106+
await gps.command(VERSION)
107107
await asyncio.sleep(10)
108108
print('***** Query ENABLE *****')
109-
await gps.command(as_rwGPS.ENABLE)
109+
await gps.command(ENABLE)
110110
await asyncio.sleep(10) # Allow time for 1st report
111111
await gps.update_interval(2000)
112112
print('***** Update interval 2s *****')
@@ -115,14 +115,14 @@ async def change_status(gps, uart):
115115
print('***** Disable satellite in view and channel messages *****')
116116
await asyncio.sleep(10)
117117
print('***** Query ENABLE *****')
118-
await gps.command(as_rwGPS.ENABLE)
118+
await gps.command(ENABLE)
119119

120120
# See README.md re antenna commands
121121
# await asyncio.sleep(10)
122-
# await gps.command(as_rwGPS.ANTENNA)
122+
# await gps.command(ANTENNA)
123123
# print('***** Antenna reports requested *****')
124124
# await asyncio.sleep(60)
125-
# await gps.command(as_rwGPS.NO_ANTENNA)
125+
# await gps.command(NO_ANTENNA)
126126
# print('***** Antenna reports turned off *****')
127127
# await asyncio.sleep(10)
128128

@@ -136,12 +136,12 @@ async def gps_test():
136136
swriter = asyncio.StreamWriter(uart, {})
137137
timer = Delay_ms(cb_timeout)
138138
sentence_count = 0
139-
gps = as_rwGPS.GPS(sreader, swriter, local_offset=1, fix_cb=callback,
139+
gps = GPS(sreader, swriter, local_offset=1, fix_cb=callback,
140140
fix_cb_args=(timer,), msg_cb = message_cb)
141141
await asyncio.sleep(2)
142-
await gps.command(as_rwGPS.DEFAULT_SENTENCES)
142+
await gps.command(DEFAULT_SENTENCES)
143143
print('Set sentence frequencies to default')
144-
#await gps.command(as_rwGPS.FULL_COLD_START)
144+
#await gps.command(FULL_COLD_START)
145145
#print('Performed FULL_COLD_START')
146146
print('awaiting first fix')
147147
asyncio.create_task(sat_test(gps))
@@ -158,7 +158,7 @@ async def shutdown():
158158
# session with ctrl-c.
159159
uart.init(BAUDRATE)
160160
await asyncio.sleep(1)
161-
await gps.command(as_rwGPS.FULL_COLD_START)
161+
await gps.command(FULL_COLD_START)
162162
print('Factory reset')
163163
#print('Restoring default baudrate.')
164164
#await gps.baudrate(9600)

0 commit comments

Comments
 (0)