Skip to content

Commit de8b585

Browse files
committed
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 adafruit#1330.
1 parent c4b592d commit de8b585

3 files changed

Lines changed: 16 additions & 5 deletions

File tree

esp8266/modpyb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ STATIC const mp_map_elem_t pyb_module_globals_table[] = {
172172

173173
{ MP_OBJ_NEW_QSTR(MP_QSTR_Pin), (mp_obj_t)&pyb_pin_type },
174174
{ MP_OBJ_NEW_QSTR(MP_QSTR_ADC), (mp_obj_t)&pyb_adc_type },
175-
{ MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_obj },
175+
{ MP_OBJ_NEW_QSTR(MP_QSTR_RTC), (mp_obj_t)&pyb_rtc_type },
176176
};
177177

178178
STATIC MP_DEFINE_CONST_DICT(pyb_module_globals, pyb_module_globals_table);

esp8266/modpyb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
extern const mp_obj_type_t pyb_pin_type;
22
extern const mp_obj_type_t pyb_adc_type;
3-
extern const mp_obj_base_t pyb_rtc_obj;
3+
extern const mp_obj_type_t pyb_rtc_type;

esp8266/modpybrtc.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include MICROPY_HAL_H
3434
#include "timeutils.h"
3535
#include "user_interface.h"
36+
#include "modpyb.h"
3637

3738
typedef struct _pyb_rtc_obj_t {
3839
mp_obj_base_t base;
@@ -46,6 +47,17 @@ typedef struct _pyb_rtc_obj_t {
4647
#define MEM_USER_DATA_ADDR (MEM_USER_LEN_ADDR + 1)
4748
#define MEM_USER_MAXLEN (512 - (MEM_USER_DATA_ADDR - MEM_DELTA_ADDR) * 4)
4849

50+
// singleton RTC object
51+
STATIC const pyb_rtc_obj_t pyb_rtc_obj = {{&pyb_rtc_type}};
52+
53+
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) {
54+
// check arguments
55+
mp_arg_check_num(n_args, n_kw, 0, 0, false);
56+
57+
// return constant object
58+
return (mp_obj_t)&pyb_rtc_obj;
59+
}
60+
4961
STATIC uint64_t pyb_rtc_raw_us(uint64_t cal) {
5062
return system_get_rtc_time() * ((cal >> 12) * 1000 + (cal & 0xfff) / 4) / 1000;
5163
};
@@ -158,10 +170,9 @@ STATIC const mp_map_elem_t pyb_rtc_locals_dict_table[] = {
158170
};
159171
STATIC MP_DEFINE_CONST_DICT(pyb_rtc_locals_dict, pyb_rtc_locals_dict_table);
160172

161-
STATIC const mp_obj_type_t pyb_rtc_type = {
173+
const mp_obj_type_t pyb_rtc_type = {
162174
{ &mp_type_type },
163175
.name = MP_QSTR_RTC,
176+
.make_new = pyb_rtc_make_new,
164177
.locals_dict = (mp_obj_t)&pyb_rtc_locals_dict,
165178
};
166-
167-
const mp_obj_base_t pyb_rtc_obj = {&pyb_rtc_type};

0 commit comments

Comments
 (0)