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.
This commit is contained in:
Damien George 2015-06-22 23:03:17 +01:00
parent c4b592d379
commit de8b585ab7
3 changed files with 16 additions and 5 deletions

View File

@ -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);

View File

@ -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;

View File

@ -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};