Skip to content

Commit 84f424f

Browse files
committed
gnss: Implement new library for GNSS
1 parent 741e1d9 commit 84f424f

File tree

10 files changed

+724
-0
lines changed

10 files changed

+724
-0
lines changed

py/circuitpy_defns.mk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ endif
162162
ifeq ($(CIRCUITPY_GAMEPADSHIFT),1)
163163
SRC_PATTERNS += gamepadshift/%
164164
endif
165+
ifeq ($(CIRCUITPY_GNSS),1)
166+
SRC_PATTERNS += gnss/%
167+
endif
165168
ifeq ($(CIRCUITPY_I2CSLAVE),1)
166169
SRC_PATTERNS += i2cslave/%
167170
endif
@@ -283,6 +286,10 @@ SRC_COMMON_HAL_ALL = \
283286
displayio/ParallelBus.c \
284287
frequencyio/FrequencyIn.c \
285288
frequencyio/__init__.c \
289+
gnss/__init__.c \
290+
gnss/GNSS.c \
291+
gnss/PositionFix.c \
292+
gnss/SatelliteSystem.c \
286293
i2cslave/I2CSlave.c \
287294
i2cslave/__init__.c \
288295
microcontroller/Pin.c \

py/circuitpy_mpconfig.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,13 @@ extern const struct _mp_obj_module_t gamepadshift_module;
401401
#define GAMEPAD_ROOT_POINTERS
402402
#endif
403403

404+
#if CIRCUITPY_GNSS
405+
extern const struct _mp_obj_module_t gnss_module;
406+
#define GNSS_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_gnss), (mp_obj_t)&gnss_module },
407+
#else
408+
#define GNSS_MODULE
409+
#endif
410+
404411
#if CIRCUITPY_I2CSLAVE
405412
extern const struct _mp_obj_module_t i2cslave_module;
406413
#define I2CSLAVE_MODULE { MP_OBJ_NEW_QSTR(MP_QSTR_i2cslave), (mp_obj_t)&i2cslave_module },
@@ -682,6 +689,7 @@ extern const struct _mp_obj_module_t watchdog_module;
682689
FREQUENCYIO_MODULE \
683690
GAMEPAD_MODULE \
684691
GAMEPADSHIFT_MODULE \
692+
GNSS_MODULE \
685693
I2CSLAVE_MODULE \
686694
JSON_MODULE \
687695
MATH_MODULE \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ CFLAGS += -DCIRCUITPY_GAMEPAD=$(CIRCUITPY_GAMEPAD)
109109
CIRCUITPY_GAMEPADSHIFT ?= 0
110110
CFLAGS += -DCIRCUITPY_GAMEPADSHIFT=$(CIRCUITPY_GAMEPADSHIFT)
111111

112+
CIRCUITPY_GNSS ?= 0
113+
CFLAGS += -DCIRCUITPY_GNSS=$(CIRCUITPY_GNSS)
114+
112115
CIRCUITPY_I2CSLAVE ?= $(CIRCUITPY_FULL_BUILD)
113116
CFLAGS += -DCIRCUITPY_I2CSLAVE=$(CIRCUITPY_I2CSLAVE)
114117

shared-bindings/gnss/GNSS.c

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright 2020 Sony Semiconductor Solutions Corporation
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "shared-bindings/gnss/GNSS.h"
28+
#include "shared-bindings/util.h"
29+
30+
#include "py/objproperty.h"
31+
#include "py/runtime.h"
32+
33+
//| class GNSS:
34+
//| """Get updated positioning information from GNSS
35+
//|
36+
//| Usage::
37+
//|
38+
//| import gnss
39+
//| import time
40+
//|
41+
//| gps = gnss.GNSS()
42+
//| gps.select(gnss.SatelliteSystem.GPS)
43+
//| gps.start()
44+
//| last_print = time.monotonic()
45+
//| while True:
46+
//| gps.update()
47+
//| current = time.monotonic()
48+
//| if current - last_print >= 1.0:
49+
//| last_print = current
50+
//| if gps.fix is gnss.PositionFix.INVALID:
51+
//| print("Waiting for fix...")
52+
//| continue
53+
//| print("Latitude: {0:.6f} degrees".format(gps.latitude))
54+
//| print("Longitude: {0:.6f} degrees".format(gps.longitude))"""
55+
//|
56+
57+
//| def __init__(self, ):
58+
//| """Turn on the GNSS."""
59+
//| ...
60+
//|
61+
STATIC mp_obj_t gnss_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
62+
gnss_obj_t *self = m_new_obj(gnss_obj_t);
63+
self->base.type = &gnss_type;
64+
65+
common_hal_gnss_construct(self);
66+
return MP_OBJ_FROM_PTR(self);
67+
}
68+
69+
//| def deinit(self, ) -> Any:
70+
//| """Turn off the GNSS."""
71+
//| ...
72+
//|
73+
STATIC mp_obj_t gnss_obj_deinit(mp_obj_t self_in) {
74+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
75+
common_hal_gnss_deinit(self);
76+
return mp_const_none;
77+
}
78+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_deinit_obj, gnss_obj_deinit);
79+
80+
STATIC void check_for_deinit(gnss_obj_t *self) {
81+
if (common_hal_gnss_deinited(self)) {
82+
raise_deinited_error();
83+
}
84+
}
85+
86+
//| def select(self, system: gnss.SatelliteSystem) -> Any:
87+
//| """Add specified satellite system to selection for positioning.
88+
//|
89+
//| :param gnss.SatelliteSystem system: satellite system to use"""
90+
//| ...
91+
//|
92+
STATIC mp_obj_t gnss_obj_select(mp_obj_t self_in, mp_obj_t system_obj) {
93+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
94+
check_for_deinit(self);
95+
96+
gnss_satellitesystem_t system = gnss_satellitesystem_obj_to_type(system_obj);
97+
common_hal_gnss_select(self, system);
98+
return mp_const_none;
99+
}
100+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gnss_select_obj, gnss_obj_select);
101+
102+
//| def deselect(self, system: gnss.SatelliteSystem) -> Any:
103+
//| """Remove specified satellite system from selection for positioning.
104+
//|
105+
//| :param gnss.SatelliteSystem system: satellite system to remove"""
106+
//| ...
107+
//|
108+
STATIC mp_obj_t gnss_obj_deselect(mp_obj_t self_in, mp_obj_t system_obj) {
109+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
110+
check_for_deinit(self);
111+
112+
gnss_satellitesystem_t system = gnss_satellitesystem_obj_to_type(system_obj);
113+
common_hal_gnss_deselect(self, system);
114+
return mp_const_none;
115+
}
116+
STATIC MP_DEFINE_CONST_FUN_OBJ_2(gnss_deselect_obj, gnss_obj_deselect);
117+
118+
//| def start(self, ) -> Any:
119+
//| """Start positioning."""
120+
//| ...
121+
//|
122+
STATIC mp_obj_t gnss_obj_start(mp_obj_t self_in) {
123+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
124+
check_for_deinit(self);
125+
126+
common_hal_gnss_start(self);
127+
return mp_const_none;
128+
}
129+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_start_obj, gnss_obj_start);
130+
131+
//| def stop(self, ) -> Any:
132+
//| """Stop positioning."""
133+
//| ...
134+
//|
135+
STATIC mp_obj_t gnss_obj_stop(mp_obj_t self_in) {
136+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
137+
check_for_deinit(self);
138+
139+
common_hal_gnss_stop(self);
140+
return mp_const_none;
141+
}
142+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_stop_obj, gnss_obj_stop);
143+
144+
//| def update(self, ) -> Any:
145+
//| """Update GNSS positioning information."""
146+
//| ...
147+
//|
148+
STATIC mp_obj_t gnss_obj_update(mp_obj_t self_in) {
149+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
150+
check_for_deinit(self);
151+
152+
common_hal_gnss_update(self);
153+
return mp_const_none;
154+
}
155+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_update_obj, gnss_obj_update);
156+
157+
//| latitude: Any = ...
158+
//| """Latitude of current position."""
159+
//|
160+
STATIC mp_obj_t gnss_obj_get_latitude(mp_obj_t self_in) {
161+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
162+
check_for_deinit(self);
163+
return mp_obj_new_float(common_hal_gnss_get_latitude(self));
164+
}
165+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_get_latitude_obj, gnss_obj_get_latitude);
166+
167+
const mp_obj_property_t gnss_latitude_obj = {
168+
.base.type = &mp_type_property,
169+
.proxy = {(mp_obj_t)&gnss_get_latitude_obj,
170+
(mp_obj_t)&mp_const_none_obj,
171+
(mp_obj_t)&mp_const_none_obj},
172+
};
173+
174+
//| longitude: Any = ...
175+
//| """Longitude of current position."""
176+
//|
177+
STATIC mp_obj_t gnss_obj_get_longitude(mp_obj_t self_in) {
178+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
179+
check_for_deinit(self);
180+
return mp_obj_new_float(common_hal_gnss_get_longitude(self));
181+
}
182+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_get_longitude_obj, gnss_obj_get_longitude);
183+
184+
const mp_obj_property_t gnss_longitude_obj = {
185+
.base.type = &mp_type_property,
186+
.proxy = {(mp_obj_t)&gnss_get_longitude_obj,
187+
(mp_obj_t)&mp_const_none_obj,
188+
(mp_obj_t)&mp_const_none_obj},
189+
};
190+
191+
//| altitude: Any = ...
192+
//| """Altitude of current position."""
193+
//|
194+
STATIC mp_obj_t gnss_obj_get_altitude(mp_obj_t self_in) {
195+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
196+
check_for_deinit(self);
197+
return mp_obj_new_float(common_hal_gnss_get_altitude(self));
198+
}
199+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_get_altitude_obj, gnss_obj_get_altitude);
200+
201+
const mp_obj_property_t gnss_altitude_obj = {
202+
.base.type = &mp_type_property,
203+
.proxy = {(mp_obj_t)&gnss_get_altitude_obj,
204+
(mp_obj_t)&mp_const_none_obj,
205+
(mp_obj_t)&mp_const_none_obj},
206+
};
207+
208+
//| fix: Any = ...
209+
//| """Fix mode."""
210+
//|
211+
STATIC mp_obj_t gnss_obj_get_fix(mp_obj_t self_in) {
212+
gnss_obj_t *self = MP_OBJ_TO_PTR(self_in);
213+
check_for_deinit(self);
214+
return gnss_positionfix_type_to_obj(common_hal_gnss_get_fix(self));
215+
}
216+
MP_DEFINE_CONST_FUN_OBJ_1(gnss_get_fix_obj, gnss_obj_get_fix);
217+
218+
const mp_obj_property_t gnss_fix_obj = {
219+
.base.type = &mp_type_property,
220+
.proxy = {(mp_obj_t)&gnss_get_fix_obj,
221+
(mp_obj_t)&mp_const_none_obj,
222+
(mp_obj_t)&mp_const_none_obj},
223+
};
224+
225+
STATIC const mp_rom_map_elem_t gnss_locals_dict_table[] = {
226+
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gnss_deinit_obj) },
227+
{ MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&gnss_select_obj) },
228+
{ MP_ROM_QSTR(MP_QSTR_deselect), MP_ROM_PTR(&gnss_deselect_obj) },
229+
{ MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&gnss_start_obj) },
230+
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&gnss_stop_obj) },
231+
{ MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&gnss_update_obj) },
232+
233+
{ MP_ROM_QSTR(MP_QSTR_latitude), MP_ROM_PTR(&gnss_latitude_obj) },
234+
{ MP_ROM_QSTR(MP_QSTR_longitude), MP_ROM_PTR(&gnss_longitude_obj) },
235+
{ MP_ROM_QSTR(MP_QSTR_altitude), MP_ROM_PTR(&gnss_altitude_obj) },
236+
{ MP_ROM_QSTR(MP_QSTR_fix), MP_ROM_PTR(&gnss_fix_obj) }
237+
};
238+
STATIC MP_DEFINE_CONST_DICT(gnss_locals_dict, gnss_locals_dict_table);
239+
240+
const mp_obj_type_t gnss_type = {
241+
{ &mp_type_type },
242+
.name = MP_QSTR_GNSS,
243+
.make_new = gnss_make_new,
244+
.locals_dict = (mp_obj_dict_t*)&gnss_locals_dict,
245+
};

shared-bindings/gnss/GNSS.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright 2020 Sony Semiconductor Solutions Corporation
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_GNSS_GNSS_H
28+
#define MICROPY_INCLUDED_SHARED_BINDINGS_GNSS_GNSS_H
29+
30+
#include "common-hal/gnss/GNSS.h"
31+
#include "shared-bindings/gnss/SatelliteSystem.h"
32+
#include "shared-bindings/gnss/PositionFix.h"
33+
34+
extern const mp_obj_type_t gnss_type;
35+
36+
void common_hal_gnss_construct(gnss_obj_t *self);
37+
void common_hal_gnss_deinit(gnss_obj_t *self);
38+
bool common_hal_gnss_deinited(gnss_obj_t *self);
39+
void common_hal_gnss_select(gnss_obj_t *self, gnss_satellitesystem_t system);
40+
void common_hal_gnss_deselect(gnss_obj_t *self, gnss_satellitesystem_t system);
41+
void common_hal_gnss_start(gnss_obj_t *self);
42+
void common_hal_gnss_stop(gnss_obj_t *self);
43+
void common_hal_gnss_update(gnss_obj_t *self);
44+
45+
mp_float_t common_hal_gnss_get_latitude(gnss_obj_t *self);
46+
mp_float_t common_hal_gnss_get_longitude(gnss_obj_t *self);
47+
mp_float_t common_hal_gnss_get_altitude(gnss_obj_t *self);
48+
gnss_positionfix_t common_hal_gnss_get_fix(gnss_obj_t *self);
49+
50+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_GNSS_GNSS_H

0 commit comments

Comments
 (0)