From ab4c09cea7d93478f9e305bfdf4cfe4be6204186 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Wed, 24 Jun 2020 11:06:25 +0200 Subject: [PATCH] gnss: Add timestamp --- ports/cxd56/common-hal/gnss/GNSS.c | 13 ++++++++++++- ports/cxd56/common-hal/gnss/GNSS.h | 4 ++++ shared-bindings/gnss/GNSS.c | 21 +++++++++++++++++++++ shared-bindings/gnss/GNSS.h | 3 +++ 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/ports/cxd56/common-hal/gnss/GNSS.c b/ports/cxd56/common-hal/gnss/GNSS.c index 401d880d92..c0aaa3cfbd 100644 --- a/ports/cxd56/common-hal/gnss/GNSS.c +++ b/ports/cxd56/common-hal/gnss/GNSS.c @@ -26,8 +26,8 @@ #include #include +#include #include -#include #include "py/runtime.h" @@ -104,6 +104,8 @@ void common_hal_gnss_update(gnss_obj_t *self) { self->latitude = positiondata.receiver.latitude; self->longitude = positiondata.receiver.longitude; self->altitude = positiondata.receiver.altitude; + memcpy(&self->date, &positiondata.receiver.date, sizeof(struct cxd56_gnss_date_s)); + memcpy(&self->time, &positiondata.receiver.time, sizeof(struct cxd56_gnss_time_s)); } } @@ -119,6 +121,15 @@ mp_float_t common_hal_gnss_get_altitude(gnss_obj_t *self) { return (mp_float_t) self->altitude; } +void common_hal_gnss_get_timestamp(gnss_obj_t *self, timeutils_struct_time_t *tm) { + tm->tm_year = self->date.year; + tm->tm_mon = self->date.month; + tm->tm_mday = self->date.day; + tm->tm_hour = self->time.hour; + tm->tm_min = self->time.minute; + tm->tm_sec = self->time.sec; +} + gnss_positionfix_t common_hal_gnss_get_fix(gnss_obj_t *self) { return fix_to_positionfix_type(self->fix); } diff --git a/ports/cxd56/common-hal/gnss/GNSS.h b/ports/cxd56/common-hal/gnss/GNSS.h index fd44bcb5cd..e226104492 100644 --- a/ports/cxd56/common-hal/gnss/GNSS.h +++ b/ports/cxd56/common-hal/gnss/GNSS.h @@ -27,6 +27,8 @@ #ifndef MICROPY_INCLUDED_CXD56_COMMON_HAL_GNSS_GNSS_H #define MICROPY_INCLUDED_CXD56_COMMON_HAL_GNSS_GNSS_H +#include + #include "py/obj.h" typedef struct { @@ -36,6 +38,8 @@ typedef struct { double latitude; double longitude; double altitude; + struct cxd56_gnss_date_s date; + struct cxd56_gnss_time_s time; } gnss_obj_t; #endif // MICROPY_INCLUDED_CXD56_COMMON_HAL_GNSS_GNSS_H diff --git a/shared-bindings/gnss/GNSS.c b/shared-bindings/gnss/GNSS.c index a7bb964a66..929e02ab81 100644 --- a/shared-bindings/gnss/GNSS.c +++ b/shared-bindings/gnss/GNSS.c @@ -25,6 +25,7 @@ */ #include "shared-bindings/gnss/GNSS.h" +#include "shared-bindings/time/__init__.h" #include "shared-bindings/util.h" #include "py/objproperty.h" @@ -170,6 +171,25 @@ const mp_obj_property_t gnss_altitude_obj = { (mp_obj_t)&mp_const_none_obj}, }; +//| timestamp: Any = ... +//| """Time when the position data was updated.""" +//| +STATIC mp_obj_t gnss_obj_get_timestamp(mp_obj_t self_in) { + gnss_obj_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); + timeutils_struct_time_t tm; + common_hal_gnss_get_timestamp(self, &tm); + return struct_time_from_tm(&tm); +} +MP_DEFINE_CONST_FUN_OBJ_1(gnss_get_timestamp_obj, gnss_obj_get_timestamp); + +const mp_obj_property_t gnss_timestamp_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&gnss_get_timestamp_obj, + (mp_obj_t)&mp_const_none_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + //| fix: Any = ... //| """Fix mode.""" //| @@ -194,6 +214,7 @@ STATIC const mp_rom_map_elem_t gnss_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_latitude), MP_ROM_PTR(&gnss_latitude_obj) }, { MP_ROM_QSTR(MP_QSTR_longitude), MP_ROM_PTR(&gnss_longitude_obj) }, { MP_ROM_QSTR(MP_QSTR_altitude), MP_ROM_PTR(&gnss_altitude_obj) }, + { MP_ROM_QSTR(MP_QSTR_timestamp), MP_ROM_PTR(&gnss_timestamp_obj) }, { MP_ROM_QSTR(MP_QSTR_fix), MP_ROM_PTR(&gnss_fix_obj) } }; STATIC MP_DEFINE_CONST_DICT(gnss_locals_dict, gnss_locals_dict_table); diff --git a/shared-bindings/gnss/GNSS.h b/shared-bindings/gnss/GNSS.h index c7332dc0e4..61ae35b12a 100644 --- a/shared-bindings/gnss/GNSS.h +++ b/shared-bindings/gnss/GNSS.h @@ -31,6 +31,8 @@ #include "shared-bindings/gnss/SatelliteSystem.h" #include "shared-bindings/gnss/PositionFix.h" +#include "lib/timeutils/timeutils.h" + extern const mp_obj_type_t gnss_type; void common_hal_gnss_construct(gnss_obj_t *self, unsigned long selection); @@ -41,6 +43,7 @@ void common_hal_gnss_update(gnss_obj_t *self); mp_float_t common_hal_gnss_get_latitude(gnss_obj_t *self); mp_float_t common_hal_gnss_get_longitude(gnss_obj_t *self); mp_float_t common_hal_gnss_get_altitude(gnss_obj_t *self); +void common_hal_gnss_get_timestamp(gnss_obj_t *self, timeutils_struct_time_t *tm); gnss_positionfix_t common_hal_gnss_get_fix(gnss_obj_t *self); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_GNSS_GNSS_H