This commit is contained in:
Dan Halbert 2020-11-20 23:33:39 -05:00
parent 39e1f52e28
commit e4c66990e2
15 changed files with 98 additions and 73 deletions

View File

@ -26,8 +26,8 @@
*/
#include "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm_io/__init__.h"
#include "shared-bindings/alarm_time/__init__.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
#include "esp_sleep.h"
@ -35,41 +35,47 @@ void common_hal_alarm_disable_all(void) {
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
}
mp_obj_t common_hal_alarm_get_reset_reason(void) {
alarm_reset_reason_t common_hal_alarm_get_reset_reason(void) {
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_TIMER:
return RESET_REASON_DEEP_SLEEP_ALARM;
case ESP_SLEEP_WAKEUP_EXT0:
return RESET_REASON_DEEP_SLEEP_ALARM;
case ESP_SLEEP_WAKEUP_TOUCHPAD:
//TODO: implement TouchIO
case ESP_SLEEP_WAKEUP_UNDEFINED:
default:
return mp_const_none;
break;
return RESET_REASON_INVALID;
}
}
mp_obj_t common_hal_alarm_get_wake_alarm(void) {
switch (esp_sleep_get_wakeup_cause()) {
case ESP_SLEEP_WAKEUP_TIMER: ;
//Wake up from timer.
alarm_time_obj_t *timer = m_new_obj(alarm_time_obj_t);
timer->base.type = &alarm_time_type;
case ESP_SLEEP_WAKEUP_TIMER: {
// Wake up from timer.
alarm_time_duration_alarm_obj_t *timer = m_new_obj(alarm_time_duration_alarm_obj_t);
timer->base.type = &alarm_time_duration_alarm_type;
return timer;
case ESP_SLEEP_WAKEUP_EXT0: ;
//Wake up from GPIO
alarm_io_obj_t *ext0 = m_new_obj(alarm_io_obj_t);
ext0->base.type = &alarm_io_type;
}
case ESP_SLEEP_WAKEUP_EXT0: {
// Wake up from GPIO
alarm_pin_pin_alarm_obj_t *ext0 = m_new_obj(alarm_pin_pin_alarm_obj_t);
ext0->base.type = &alarm_pin_pin_alarm_type;
return ext0;
}
case ESP_SLEEP_WAKEUP_TOUCHPAD:
//TODO: implement TouchIO
//Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status()
// TODO: implement TouchIO
// Wake up from touch on pad, esp_sleep_get_touchpad_wakeup_status()
break;
case ESP_SLEEP_WAKEUP_UNDEFINED:
default:
//Not a deep sleep reset
// Not a deep sleep reset.
break;
}
return mp_const_none;

View File

@ -27,15 +27,17 @@
#include "esp_sleep.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/microcontroller/Pin.h"
void common_hal_alarm_pin_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, mcu_pin_obj_t *pin, bool level, bool edge, bool pull) {
void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, const mcu_pin_obj_t *pin, bool level, bool edge, bool pull) {
self->pin = pin;
self->level = level;
self->edge = edge;
self->pull = pull;
}
mcu_pin_obj_t *common_hal_alarm_pin_pin_alarm_get_pin(alarm_pin_pin_alarm_obj_t *self) {
const mcu_pin_obj_t *common_hal_alarm_pin_pin_alarm_get_pin(alarm_pin_pin_alarm_obj_t *self) {
return self->pin;
}

View File

@ -29,7 +29,7 @@
typedef struct {
mp_obj_base_t base;
mcu_pin_obj_t *pin;
const mcu_pin_obj_t *pin;
bool level;
bool edge;
bool pull;

View File

@ -27,6 +27,8 @@
#include "esp_sleep.h"
#include "py/runtime.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
void common_hal_alarm_time_duration_alarm_construct(alarm_time_duration_alarm_obj_t *self, mp_float_t duration) {
@ -36,7 +38,8 @@ void common_hal_alarm_time_duration_alarm_construct(alarm_time_duration_alarm_ob
mp_float_t common_hal_alarm_time_duration_alarm_get_duration(alarm_time_duration_alarm_obj_t *self) {
return self->duration;
}
void common_hal_alarm_time_duration_alarm_enable(alarm_time_duration_alarm_obj_t *self)
void common_hal_alarm_time_duration_alarm_enable(alarm_time_duration_alarm_obj_t *self) {
if (esp_sleep_enable_timer_wakeup((uint64_t) (self->duration * 1000000)) == ESP_ERR_INVALID_ARG) {
mp_raise_ValueError(translate("duration out of range"));
}

View File

@ -359,8 +359,6 @@ SRC_COMMON_HAL_ALL = \
rtc/__init__.c \
sdioio/SDCard.c \
sdioio/__init__.c \
sleepio/__init__.c \
sleepio/ResetReason.c \
socketpool/__init__.c \
socketpool/SocketPool.c \
socketpool/Socket.c \
@ -395,9 +393,10 @@ $(filter $(SRC_PATTERNS), \
_bleio/Address.c \
_bleio/Attribute.c \
_bleio/ScanEntry.c \
canio/Match.c \
_eve/__init__.c \
alarm/ResetReason.c \
camera/ImageFormat.c \
canio/Match.c \
digitalio/Direction.c \
digitalio/DriveMode.c \
digitalio/Pull.c \
@ -414,9 +413,6 @@ SRC_SHARED_MODULE_ALL = \
_bleio/Attribute.c \
_bleio/ScanEntry.c \
_bleio/ScanResults.c \
canio/Match.c \
canio/Message.c \
canio/RemoteTransmissionRequest.c \
_eve/__init__.c \
_pixelbuf/PixelBuf.c \
_pixelbuf/__init__.c \
@ -441,6 +437,9 @@ SRC_SHARED_MODULE_ALL = \
bitbangio/__init__.c \
board/__init__.c \
busio/OneWire.c \
canio/Match.c \
canio/Message.c \
canio/RemoteTransmissionRequest.c \
displayio/Bitmap.c \
displayio/ColorConverter.c \
displayio/Display.c \

View File

@ -35,12 +35,12 @@ typedef struct {
} cp_enum_obj_t;
#define MAKE_ENUM_VALUE(type, prefix, name, value) \
STATIC const cp_enum_obj_t prefix ## _ ## name ## _obj = { \
const cp_enum_obj_t prefix ## _ ## name ## _obj = { \
{ &type }, value, MP_QSTR_ ## name, \
}
#define MAKE_ENUM_MAP(name) \
STATIC const mp_rom_map_elem_t name ## _locals_table[] =
const mp_rom_map_elem_t name ## _locals_table[] =
#define MAKE_ENUM_MAP_ENTRY(prefix, name) \
{ MP_ROM_QSTR(MP_QSTR_ ## name), MP_ROM_PTR(&prefix ## _ ## name ## _obj) }

View File

@ -47,7 +47,9 @@ def preprocess(command, output_dir, fn):
print(e, file=sys.stderr)
def maybe_preprocess(command, output_dir, fn):
if subprocess.call(["grep", "-lqE", "(MP_QSTR|translate)", fn]) == 0:
# Preprocess the source file if it contains "MP_QSTR", "translate",
# or if it uses enum.h (which generates "MP_QSTR" strings.
if subprocess.call(["grep", "-lqE", r"(MP_QSTR|translate|enum\.h)", fn]) == 0:
preprocess(command, output_dir, fn)
if __name__ == '__main__':

View File

@ -24,18 +24,25 @@
* THE SOFTWARE.
*/
#include "py/obj.h"
#include "py/enum.h"
#include "shared-bindings/alarm/ResetReason.h"
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, POWER_VALID, RESET_REASON_POWER_ON);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, INVALID, RESET_REASON_INVALID);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, POWER_ON, RESET_REASON_POWER_ON);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, BROWNOUT, RESET_REASON_BROWNOUT);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, SOFTWARE, RESET_REASON_SOFTWARE);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, DEEP_SLEEP_ALARM, RESET_REASON_DEEP_SLEEP_ALARM);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, EXTERNAL, RESET_REASON_EXTERNAL);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, RESET_PIN, RESET_REASON_RESET_PIN);
MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, WATCHDOG, RESET_REASON_WATCHDOG);
//| class ResetReason:
//| """The reason the chip was last reset"""
//|
//| INVALID: object
//| """Invalid reason: indicates an internal error."""
//|
//| POWER_ON: object
//| """The chip was started from power off."""
//|
@ -55,6 +62,7 @@ MAKE_ENUM_VALUE(alarm_reset_reason_type, reset_reason, EXTERNAL, RESET_REASON_EX
//| """The chip was reset by its watchdog timer."""
//|
MAKE_ENUM_MAP(alarm_reset_reason) {
MAKE_ENUM_MAP_ENTRY(reset_reason, INVALID),
MAKE_ENUM_MAP_ENTRY(reset_reason, POWER_ON),
MAKE_ENUM_MAP_ENTRY(reset_reason, BROWNOUT),
MAKE_ENUM_MAP_ENTRY(reset_reason, SOFTWARE),

View File

@ -27,7 +27,11 @@
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_ALARM__RESET_REASON__H
#define MICROPY_INCLUDED_SHARED_BINDINGS_ALARM__RESET_REASON__H
#include "py/obj.h"
#include "py/enum.h"
typedef enum {
RESET_REASON_INVALID,
RESET_REASON_POWER_ON,
RESET_REASON_BROWNOUT,
RESET_REASON_SOFTWARE,
@ -36,8 +40,8 @@ typedef enum {
RESET_REASON_WATCHDOG,
} alarm_reset_reason_t;
extern const cp_enum_obj_t reset_reason_INVALID_obj;
extern const mp_obj_type_t alarm_reset_reason_type;
extern alarm_reset_reason_t common_hal_alarm_get_reset_reason(void);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_ALARM__RESET_REASON__H

View File

@ -34,6 +34,11 @@
#include "py/obj.h"
#include "py/runtime.h"
#include "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm/ResetReason.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
STATIC mp_obj_t alarm_sleep_until_alarm(size_t n_args, const mp_obj_t *args) {
// TODO
return mp_const_none;
@ -56,47 +61,47 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(alarm_restart_on_alarm_obj, 1, MP_OBJ_FUN_AR
//| """The `alarm.pin` module contains alarm attributes and classes related to pins
//| """
//|
mp_map_elem_t alarm_pin_globals_table[] = {
STATIC const mp_map_elem_t alarm_pin_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_pin) },
{ MP_ROM_QSTR(MP_QSTR_PinAlarm), MP_ROM_PTR(&alarm_pin_pin_alarm_type) },
{ MP_ROM_QSTR(MP_QSTR_PinAlarm), MP_OBJ_FROM_PTR(&alarm_pin_pin_alarm_type) },
};
STATIC MP_DEFINE_CONST_DICT(alarm_pin_globals, alarm_pin_globals_table);
const mp_obj_module_t alarm_pin_module = {
STATIC const mp_obj_module_t alarm_pin_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&alarm_pinn_globals,
.globals = (mp_obj_dict_t*)&alarm_pin_globals,
};
//| """The `alarm.time` module contains alarm attributes and classes related to time-keeping.
//| """
//|
mp_map_elem_t alarm_time_globals_table[] = {
STATIC const mp_map_elem_t alarm_time_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
{ MP_ROM_QSTR(MP_QSTR_DurationAlarm), MP_ROM_PTR(&alarm_time_duration_alarm_type) },
{ MP_ROM_QSTR(MP_QSTR_DurationAlarm), MP_OBJ_FROM_PTR(&alarm_time_duration_alarm_type) },
};
STATIC MP_DEFINE_CONST_DICT(alarm_time_globals, alarm_time_globals_table);
const mp_obj_module_t alarm_time_module = {
STATIC const mp_obj_module_t alarm_time_module = {
.base = { &mp_type_module },
.globals = (mp_obj_dict_t*)&alarm_time_globals,
};
mp_map_elem_t alarm_module_globals_table[] = {
STATIC mp_map_elem_t alarm_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_alarm) },
// wake_alarm and reset_reason are mutable attributes.
{ MP_ROM_QSTR(MP_QSTR_wake_alarm), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_reset_reason), mp_const_none },
{ MP_ROM_QSTR(MP_QSTR_reset_reason), MP_OBJ_FROM_PTR(&reset_reason_INVALID_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_until_alarm), MP_ROM_PTR(&alarm_sleep_until_alarm_obj) },
{ MP_ROM_QSTR(MP_QSTR_restart_on_alarm), MP_ROM_PTR(&alarm_restart_on_alarm_obj) },
{ MP_ROM_QSTR(MP_QSTR_sleep_until_alarm), MP_OBJ_FROM_PTR(&alarm_sleep_until_alarm_obj) },
{ MP_ROM_QSTR(MP_QSTR_restart_on_alarm), MP_OBJ_FROM_PTR(&alarm_restart_on_alarm_obj) },
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&alarm_pin_module) },
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&alarm_time_module) }
{ MP_ROM_QSTR(MP_QSTR_pin), MP_OBJ_FROM_PTR(&alarm_pin_module) },
{ MP_ROM_QSTR(MP_QSTR_time), MP_OBJ_FROM_PTR(&alarm_time_module) }
};
STATIC MP_DEFINE_MUTABLE_DICT(alarm_module_globals, alarm_module_globals_table);
@ -105,27 +110,17 @@ void common_hal_alarm_set_wake_alarm(mp_obj_t alarm) {
// Equivalent of:
// alarm.wake_alarm = alarm
mp_map_elem_t *elem =
mp_map_lookup(&alarm_module_globals_table, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP);
mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_wake_alarm), MP_MAP_LOOKUP);
if (elem) {
elem->value = alarm;
}
}
alarm_reset_reason_t common_hal_alarm_get_reset_reason(void) {
mp_map_elem_t *elem =
mp_map_lookup(&alarm_module_globals_table, MP_ROM_QSTR(MP_QSTR_reset_reason), MP_MAP_LOOKUP);
if (elem) {
return elem->value;
} else {
return mp_const_none;
}
}
void common_hal_alarm_set_reset_reason(mp_obj_t reset_reason) {
// Equivalent of:
// alarm.reset_reason = reset_reason
mp_map_elem_t *elem =
mp_map_lookup(&alarm_module_globals_table, MP_ROM_QSTR(MP_QSTR_reset_reason), MP_MAP_LOOKUP);
mp_map_lookup(&alarm_module_globals.map, MP_ROM_QSTR(MP_QSTR_reset_reason), MP_MAP_LOOKUP);
if (elem) {
elem->value = reset_reason;
}

View File

@ -27,6 +27,7 @@
#include "shared-bindings/board/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "py/nlr.h"
#include "py/obj.h"
@ -58,8 +59,7 @@
//| """
//| ...
//|
STATIC mp_obj_t alarm_pin_pin_alarm_make_new(const mp_obj_type_t *type,
mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
STATIC mp_obj_t alarm_pin_pin_alarm_make_new(const mp_obj_type_t *type, mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
alarm_pin_pin_alarm_obj_t *self = m_new_obj(alarm_pin_pin_alarm_obj_t);
self->base.type = &alarm_pin_pin_alarm_type;
enum { ARG_pin, ARG_level, ARG_edge, ARG_pull };
@ -74,7 +74,7 @@ STATIC mp_obj_t alarm_pin_pin_alarm_make_new(const mp_obj_type_t *type,
const mcu_pin_obj_t* pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
common_hal_alarm_pin_pin_pin_alarm_construct(
common_hal_alarm_pin_pin_alarm_construct(
self, pin, args[ARG_level].u_bool, args[ARG_edge].u_bool, args[ARG_pull].u_bool);
return MP_OBJ_FROM_PTR(self);
@ -110,11 +110,12 @@ STATIC mp_obj_t alarm_pin_pin_alarm_binary_op(mp_binary_op_t op, mp_obj_t lhs_in
STATIC const mp_rom_map_elem_t alarm_pin_pin_alarm_locals_dict_table[] = {
};
STATIC MP_DEFINE_CONST_DICT(alarm_pin_pin_alarm_locals, alarm_pin_pin_alarm_locals_dict);
STATIC MP_DEFINE_CONST_DICT(alarm_pin_pin_alarm_locals_dict, alarm_pin_pin_alarm_locals_dict_table);
const mp_obj_type_t alarm_pin_pin_alarm_type = {
{ &mp_type_type },
.name = MP_QSTR_PinAlarm,
.make_new = alarm_pin_pin_alarm_make_new,
.locals_dict = (mp_obj_t)&alarm_pin_pin_alarm_locals,
.binary_op = alarm_pin_pin_alarm_binary_op,
.locals_dict = (mp_obj_t)&alarm_pin_pin_alarm_locals_dict,
};

View File

@ -29,11 +29,12 @@
#include "py/obj.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/alarm/pin/PinAlarm.h"
extern const mp_obj_type_t alarm_pin_pin_alarm_type;
extern void common_hal_alarm_pin_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, const mcu_pin_obj_t *pin, bool level, bool edge, bool pull);
extern mcu_pin_obj_t *common_hal_alarm_pin_pin_alarm_get_pin(alarm_pin_pin_alarm_obj_t *self);
extern void common_hal_alarm_pin_pin_alarm_construct(alarm_pin_pin_alarm_obj_t *self, const mcu_pin_obj_t *pin, bool level, bool edge, bool pull);
extern const mcu_pin_obj_t *common_hal_alarm_pin_pin_alarm_get_pin(alarm_pin_pin_alarm_obj_t *self);
extern bool common_hal_alarm_pin_pin_alarm_get_level(alarm_pin_pin_alarm_obj_t *self);
extern bool common_hal_alarm_pin_pin_alarm_get_edge(alarm_pin_pin_alarm_obj_t *self);
extern bool common_hal_alarm_pin_pin_alarm_get_pull(alarm_pin_pin_alarm_obj_t *self);

View File

@ -26,7 +26,7 @@
#include "shared-bindings/board/__init__.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/alarm/time/DurationAlarm.h"
#include "py/nlr.h"
#include "py/obj.h"
@ -49,8 +49,8 @@ STATIC mp_obj_t alarm_time_duration_alarm_make_new(const mp_obj_type_t *type,
mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
mp_arg_check_num(n_args, kw_args, 1, 1, false);
alarm_pin_duration_alarm_obj_t *self = m_new_obj(alarm_pin_duration_alarm_obj_t);
self->base.type = &alarm_pin_pin_alarm_type;
alarm_time_duration_alarm_obj_t *self = m_new_obj(alarm_time_duration_alarm_obj_t);
self->base.type = &alarm_time_duration_alarm_type;
mp_float_t secs = mp_obj_get_float(args[0]);
@ -60,7 +60,7 @@ STATIC mp_obj_t alarm_time_duration_alarm_make_new(const mp_obj_type_t *type,
}
//| def __eq__(self, other: object) -> bool:
//| """Two DurationAlarm objects are equal if their durations are the same."""
//| """Two DurationAlarm objects are equal if their durations differ by less than a millisecond."""
//| ...
//|
STATIC mp_obj_t alarm_time_duration_alarm_binary_op(mp_binary_op_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
@ -68,8 +68,8 @@ STATIC mp_obj_t alarm_time_duration_alarm_binary_op(mp_binary_op_t op, mp_obj_t
case MP_BINARY_OP_EQUAL:
if (MP_OBJ_IS_TYPE(rhs_in, &alarm_time_duration_alarm_type)) {
return mp_obj_new_bool(
common_hal_alarm_time_duration_alarm_get_duration(lhs_in) ==
common_hal_alarm_time_duration_alarm_get_duration(rhs_in));
abs(common_hal_alarm_time_duration_alarm_get_duration(lhs_in) -
common_hal_alarm_time_duration_alarm_get_duration(rhs_in)) < 0.001f);
}
return mp_const_false;
@ -87,5 +87,6 @@ const mp_obj_type_t alarm_time_duration_alarm_type = {
{ &mp_type_type },
.name = MP_QSTR_DurationAlarm,
.make_new = alarm_time_duration_alarm_make_new,
.locals_dict = (mp_obj_t)&alarm_time_duration_alarm_locals,
.binary_op = alarm_time_duration_alarm_binary_op,
.locals_dict = (mp_obj_t)&alarm_time_duration_alarm_locals_dict,
};

View File

@ -29,6 +29,8 @@
#include "py/obj.h"
#include "common-hal/alarm/time/DurationAlarm.h"
extern const mp_obj_type_t alarm_time_duration_alarm_type;
extern void common_hal_alarm_time_duration_alarm_construct(alarm_time_duration_alarm_obj_t *self, mp_float_t duration);

View File

@ -30,6 +30,7 @@
#include "shared-bindings/digitalio/DigitalInOut.h"
#if CIRCUITPY_ALARM
#include "shared-bindings/alarm/__init__.h"
#include "shared-bindings/alarm/ResetReason.h"
#endif