From 3509dad5b3364ea23ea8108a33aa88a853626104 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Wed, 24 Jun 2020 11:04:05 +0200 Subject: [PATCH] gnss: Remove start and stop --- ports/cxd56/common-hal/gnss/GNSS.c | 64 +++++---------- shared-bindings/gnss/GNSS.c | 105 ++++++++----------------- shared-bindings/gnss/GNSS.h | 6 +- shared-bindings/gnss/SatelliteSystem.h | 12 +-- 4 files changed, 58 insertions(+), 129 deletions(-) diff --git a/ports/cxd56/common-hal/gnss/GNSS.c b/ports/cxd56/common-hal/gnss/GNSS.c index 0e5655534b..401d880d92 100644 --- a/ports/cxd56/common-hal/gnss/GNSS.c +++ b/ports/cxd56/common-hal/gnss/GNSS.c @@ -40,24 +40,6 @@ typedef struct { STATIC gnss_dev_t gnss_dev = {"/dev/gps", -1}; -static unsigned long satellitesystem_type_to_bit_fields(gnss_satellitesystem_t system) { - switch (system) { - case SATELLITESYSTEM_GPS: - return CXD56_GNSS_SAT_GPS; - case SATELLITESYSTEM_GLONASS: - return CXD56_GNSS_SAT_GLONASS; - case SATELLITESYSTEM_SBAS: - return CXD56_GNSS_SAT_SBAS; - case SATELLITESYSTEM_QZSS_L1CA: - return CXD56_GNSS_SAT_QZ_L1CA; - case SATELLITESYSTEM_QZSS_L1S: - return CXD56_GNSS_SAT_QZ_L1S; - case SATELLITESYSTEM_NONE: - default: - return CXD56_GNSS_SAT_NONE; - } -} - static gnss_positionfix_t fix_to_positionfix_type(uint8_t fix) { switch (fix) { case CXD56_GNSS_PVT_POSFIX_2D: @@ -70,7 +52,7 @@ static gnss_positionfix_t fix_to_positionfix_type(uint8_t fix) { } } -void common_hal_gnss_construct(gnss_obj_t *self) { +void common_hal_gnss_construct(gnss_obj_t *self, unsigned long selection) { if (gnss_dev.fd < 0) { gnss_dev.fd = open(gnss_dev.devpath, O_RDONLY); if (gnss_dev.fd < 0) { @@ -80,6 +62,23 @@ void common_hal_gnss_construct(gnss_obj_t *self) { self->satellite_system = 0; self->fix = POSITIONFIX_INVALID; + + unsigned long sel = 0; + + if (selection & SATELLITESYSTEM_GPS) { + sel |= CXD56_GNSS_SAT_GPS; + } else if (selection & SATELLITESYSTEM_GLONASS) { + sel |= CXD56_GNSS_SAT_GLONASS; + } else if (selection & SATELLITESYSTEM_SBAS) { + sel |= CXD56_GNSS_SAT_SBAS; + } else if (selection & SATELLITESYSTEM_QZSS_L1CA) { + sel |= CXD56_GNSS_SAT_QZ_L1CA; + } else if (selection & SATELLITESYSTEM_QZSS_L1S) { + sel |= CXD56_GNSS_SAT_QZ_L1S; + } + + ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_SELECT_SATELLITE_SYSTEM, sel); + ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_START, CXD56_GNSS_STMOD_COLD); } void common_hal_gnss_deinit(gnss_obj_t *self) { @@ -95,33 +94,6 @@ bool common_hal_gnss_deinited(gnss_obj_t *self) { return gnss_dev.fd < 0; } -void common_hal_gnss_select(gnss_obj_t *self, gnss_satellitesystem_t system) { - unsigned long selection = self->satellite_system | satellitesystem_type_to_bit_fields(system); - - ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_SELECT_SATELLITE_SYSTEM, selection); - - self->satellite_system = selection; -} - -void common_hal_gnss_deselect(gnss_obj_t *self, gnss_satellitesystem_t system) { - unsigned long selection = self->satellite_system & ~satellitesystem_type_to_bit_fields(system); - - ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_SELECT_SATELLITE_SYSTEM, selection); - - self->satellite_system = selection; -} - -void common_hal_gnss_start(gnss_obj_t *self) { - if (self->satellite_system == 0) { - mp_raise_ValueError(translate("Cannot start without selecting at least one satellite system.")); - } - ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_START, CXD56_GNSS_STMOD_COLD); -} - -void common_hal_gnss_stop(gnss_obj_t *self) { - ioctl(gnss_dev.fd, CXD56_GNSS_IOCTL_STOP, 0); -} - void common_hal_gnss_update(gnss_obj_t *self) { struct cxd56_gnss_positiondata_s positiondata; diff --git a/shared-bindings/gnss/GNSS.c b/shared-bindings/gnss/GNSS.c index e7c2234506..a7bb964a66 100644 --- a/shared-bindings/gnss/GNSS.c +++ b/shared-bindings/gnss/GNSS.c @@ -38,31 +38,54 @@ //| import gnss //| import time //| -//| gps = gnss.GNSS() -//| gps.select(gnss.SatelliteSystem.GPS) -//| gps.start() +//| nav = gnss.GNSS([gnss.SatelliteSystem.GPS, gnss.SatelliteSystem.GLONASS]) //| last_print = time.monotonic() //| while True: -//| gps.update() +//| nav.update() //| current = time.monotonic() //| if current - last_print >= 1.0: //| last_print = current -//| if gps.fix is gnss.PositionFix.INVALID: +//| if nav.fix is gnss.PositionFix.INVALID: //| print("Waiting for fix...") //| continue -//| print("Latitude: {0:.6f} degrees".format(gps.latitude)) -//| print("Longitude: {0:.6f} degrees".format(gps.longitude))""" +//| print("Latitude: {0:.6f} degrees".format(nav.latitude)) +//| print("Longitude: {0:.6f} degrees".format(nav.longitude))""" //| //| def __init__(self, ): -//| """Turn on the GNSS.""" +//| """Turn on the GNSS. +//| +//| :param gnss.SatelliteSystem system: satellite system to use""" //| ... //| 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) { gnss_obj_t *self = m_new_obj(gnss_obj_t); self->base.type = &gnss_type; + enum { ARG_system }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_system, MP_ARG_REQUIRED | MP_ARG_OBJ }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); - common_hal_gnss_construct(self); + unsigned long selection = 0; + if (MP_OBJ_IS_TYPE(args[ARG_system].u_obj, &gnss_satellitesystem_type)) { + selection |= gnss_satellitesystem_obj_to_type(args[ARG_system].u_obj); + } else if (MP_OBJ_IS_TYPE(args[ARG_system].u_obj, &mp_type_list)) { + size_t systems_size = 0; + mp_obj_t *systems; + mp_obj_list_get(args[ARG_system].u_obj, &systems_size, &systems); + for (size_t i = 0; i < systems_size; ++i) { + if (!MP_OBJ_IS_TYPE(systems[i], &gnss_satellitesystem_type)) { + mp_raise_TypeError(translate("System entry must be gnss.SatelliteSystem")); + } + selection |= gnss_satellitesystem_obj_to_type(systems[i]); + } + } else { + mp_raise_TypeError(translate("System entry must be gnss.SatelliteSystem")); + } + + common_hal_gnss_construct(self, selection); return MP_OBJ_FROM_PTR(self); } @@ -83,64 +106,6 @@ STATIC void check_for_deinit(gnss_obj_t *self) { } } -//| def select(self, system: gnss.SatelliteSystem) -> Any: -//| """Add specified satellite system to selection for positioning. -//| -//| :param gnss.SatelliteSystem system: satellite system to use""" -//| ... -//| -STATIC mp_obj_t gnss_obj_select(mp_obj_t self_in, mp_obj_t system_obj) { - gnss_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - - gnss_satellitesystem_t system = gnss_satellitesystem_obj_to_type(system_obj); - common_hal_gnss_select(self, system); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(gnss_select_obj, gnss_obj_select); - -//| def deselect(self, system: gnss.SatelliteSystem) -> Any: -//| """Remove specified satellite system from selection for positioning. -//| -//| :param gnss.SatelliteSystem system: satellite system to remove""" -//| ... -//| -STATIC mp_obj_t gnss_obj_deselect(mp_obj_t self_in, mp_obj_t system_obj) { - gnss_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - - gnss_satellitesystem_t system = gnss_satellitesystem_obj_to_type(system_obj); - common_hal_gnss_deselect(self, system); - return mp_const_none; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_2(gnss_deselect_obj, gnss_obj_deselect); - -//| def start(self, ) -> Any: -//| """Start positioning.""" -//| ... -//| -STATIC mp_obj_t gnss_obj_start(mp_obj_t self_in) { - gnss_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - - common_hal_gnss_start(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(gnss_start_obj, gnss_obj_start); - -//| def stop(self, ) -> Any: -//| """Stop positioning.""" -//| ... -//| -STATIC mp_obj_t gnss_obj_stop(mp_obj_t self_in) { - gnss_obj_t *self = MP_OBJ_TO_PTR(self_in); - check_for_deinit(self); - - common_hal_gnss_stop(self); - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_1(gnss_stop_obj, gnss_obj_stop); - //| def update(self, ) -> Any: //| """Update GNSS positioning information.""" //| ... @@ -189,7 +154,7 @@ const mp_obj_property_t gnss_longitude_obj = { }; //| altitude: Any = ... -//| """Altitude of current position in degrees (float).""" +//| """Altitude of current position in meters (float).""" //| STATIC mp_obj_t gnss_obj_get_altitude(mp_obj_t self_in) { gnss_obj_t *self = MP_OBJ_TO_PTR(self_in); @@ -224,10 +189,6 @@ const mp_obj_property_t gnss_fix_obj = { STATIC const mp_rom_map_elem_t gnss_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gnss_deinit_obj) }, - { MP_ROM_QSTR(MP_QSTR_select), MP_ROM_PTR(&gnss_select_obj) }, - { MP_ROM_QSTR(MP_QSTR_deselect), MP_ROM_PTR(&gnss_deselect_obj) }, - { MP_ROM_QSTR(MP_QSTR_start), MP_ROM_PTR(&gnss_start_obj) }, - { MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&gnss_stop_obj) }, { MP_ROM_QSTR(MP_QSTR_update), MP_ROM_PTR(&gnss_update_obj) }, { MP_ROM_QSTR(MP_QSTR_latitude), MP_ROM_PTR(&gnss_latitude_obj) }, diff --git a/shared-bindings/gnss/GNSS.h b/shared-bindings/gnss/GNSS.h index 953eb9ce31..c7332dc0e4 100644 --- a/shared-bindings/gnss/GNSS.h +++ b/shared-bindings/gnss/GNSS.h @@ -33,13 +33,9 @@ extern const mp_obj_type_t gnss_type; -void common_hal_gnss_construct(gnss_obj_t *self); +void common_hal_gnss_construct(gnss_obj_t *self, unsigned long selection); void common_hal_gnss_deinit(gnss_obj_t *self); bool common_hal_gnss_deinited(gnss_obj_t *self); -void common_hal_gnss_select(gnss_obj_t *self, gnss_satellitesystem_t system); -void common_hal_gnss_deselect(gnss_obj_t *self, gnss_satellitesystem_t system); -void common_hal_gnss_start(gnss_obj_t *self); -void common_hal_gnss_stop(gnss_obj_t *self); void common_hal_gnss_update(gnss_obj_t *self); mp_float_t common_hal_gnss_get_latitude(gnss_obj_t *self); diff --git a/shared-bindings/gnss/SatelliteSystem.h b/shared-bindings/gnss/SatelliteSystem.h index 210cd0bb8b..484e861abd 100644 --- a/shared-bindings/gnss/SatelliteSystem.h +++ b/shared-bindings/gnss/SatelliteSystem.h @@ -30,12 +30,12 @@ #include "py/obj.h" typedef enum { - SATELLITESYSTEM_NONE, - SATELLITESYSTEM_GPS, - SATELLITESYSTEM_GLONASS, - SATELLITESYSTEM_SBAS, - SATELLITESYSTEM_QZSS_L1CA, - SATELLITESYSTEM_QZSS_L1S, + SATELLITESYSTEM_NONE = 0, + SATELLITESYSTEM_GPS = (1U << 0), + SATELLITESYSTEM_GLONASS = (1U << 1), + SATELLITESYSTEM_SBAS = (1U << 2), + SATELLITESYSTEM_QZSS_L1CA = (1U << 3), + SATELLITESYSTEM_QZSS_L1S = (1U << 4), } gnss_satellitesystem_t; const mp_obj_type_t gnss_satellitesystem_type;