From de8b585ab7436db3acac3a6a32b50a594729fea9 Mon Sep 17 00:00:00 2001 From: Damien George Date: Mon, 22 Jun 2015 23:03:17 +0100 Subject: [PATCH] esp8266: Make pyb.RTC a type, and pyb.RTC() constructs an RTC object. This is the standard way of doing things, one should construct a peripheral object (even if it's a singleton). See issue #1330. --- esp8266/modpyb.c | 2 +- esp8266/modpyb.h | 2 +- esp8266/modpybrtc.c | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/esp8266/modpyb.c b/esp8266/modpyb.c index d72ee79f05..aaa359a93d 100644 --- a/esp8266/modpyb.c +++ b/esp8266/modpyb.c @@ -172,7 +172,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type }, { MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type }, - { MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_obj }, + { MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type }, }; STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table); diff --git a/esp8266/modpyb.h b/esp8266/modpyb.h index 641c8fc762..56159de24f 100644 --- a/esp8266/modpyb.h +++ b/esp8266/modpyb.h @@ -1,3 +1,3 @@ extern const mp_obj_type_t pyb_pin_type; extern const mp_obj_type_t pyb_adc_type; -extern const mp_obj_base_t pyb_rtc_obj; +extern const mp_obj_type_t pyb_rtc_type; diff --git a/esp8266/modpybrtc.c b/esp8266/modpybrtc.c index 7bf65e821c..260894881d 100644 --- a/esp8266/modpybrtc.c +++ b/esp8266/modpybrtc.c @@ -33,6 +33,7 @@ #include MICROPY_HAL_H #include "timeutils.h" #include "user_interface.h" +#include "modpyb.h" typedef struct _pyb_rtc_obj_t { mp_obj_base_t base; @@ -46,6 +47,17 @@ typedef struct _pyb_rtc_obj_t { #define MEM_USER_DATA_ADDR (MEM_USER_LEN_ADDR + 1) #define MEM_USER_MAXLEN (512 - (MEM_USER_DATA_ADDR - MEM_DELTA_ADDR) * 4) +// singleton RTC object +STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}}; + +STATIC mp_obj_t pyb_rtc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) { + // check arguments + mp_arg_check_num(n_args, n_kw, 0, 0, false); + + // return constant object + return (mp_obj_t)&pyb_rtc_obj; +} + STATIC uint64_t pyb_rtc_raw_us(uint64_t cal) { return system_get_rtc_time() * ((cal >> 12) * 1000 + (cal & 0xfff) / 4) / 1000; }; @@ -158,10 +170,9 @@ STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = { }; STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table); -STATIC const mp_obj_type_t pyb_rtc_type = { +const mp_obj_type_t pyb_rtc_type = { { &mp_type_type }, .name = MP_QSTR_RTC, + .make_new = pyb_rtc_make_new, .locals_dict = (mp_obj_t)&pyb_rtc_locals_dict, }; - -const mp_obj_base_t pyb_rtc_obj = {&pyb_rtc_type};