2020-11-18 20:44:34 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "shared-bindings/board/__init__.h"
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2020-11-20 23:33:39 -05:00
|
|
|
#include "shared-bindings/alarm/pin/PinAlarm.h"
|
2020-11-18 20:44:34 -05:00
|
|
|
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/obj.h"
|
2020-11-25 15:07:57 -05:00
|
|
|
#include "py/objproperty.h"
|
2020-11-18 20:44:34 -05:00
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
|
|
|
//| class PinAlarm:
|
2020-11-21 23:29:52 -05:00
|
|
|
//| """Trigger an alarm when a pin changes state."""
|
2020-11-18 20:44:34 -05:00
|
|
|
//|
|
2020-12-01 20:01:14 -05:00
|
|
|
//| def __init__(self, pin: microcontroller.Pin, value: bool, edge: bool = False, pull: bool = False) -> None:
|
2020-11-24 23:36:59 -05:00
|
|
|
//| """Create an alarm triggered by a `microcontroller.Pin` level. The alarm is not active
|
2020-12-01 20:13:46 -05:00
|
|
|
//| until it is passed to an `alarm`-enabling function, such as `alarm.light_sleep_until_alarms()` or
|
2020-11-26 22:06:37 -05:00
|
|
|
//| `alarm.exit_and_deep_sleep_until_alarms()`.
|
2020-11-21 23:29:52 -05:00
|
|
|
//|
|
2020-12-01 20:01:14 -05:00
|
|
|
//| :param microcontroller.Pin pin: The pin to monitor. On some ports, the choice of pin
|
2020-11-24 23:36:59 -05:00
|
|
|
//| may be limited due to hardware restrictions, particularly for deep-sleep alarms.
|
2020-11-22 19:10:09 -05:00
|
|
|
//| :param bool value: When active, trigger when the pin value is high (``True``) or low (``False``).
|
2020-11-24 23:36:59 -05:00
|
|
|
//| On some ports, multiple `PinAlarm` objects may need to have coordinated values
|
|
|
|
//| for deep-sleep alarms.
|
2020-11-18 20:44:34 -05:00
|
|
|
//| :param bool edge: If ``True``, trigger only when there is a transition to the specified
|
2020-11-24 23:36:59 -05:00
|
|
|
//| value of ``value``. If ``True``, if the alarm becomes active when the pin value already
|
|
|
|
//| matches ``value``, the alarm is not triggered: the pin must transition from ``not value``
|
|
|
|
//| to ``value`` to trigger the alarm. On some ports, edge-triggering may not be available,
|
|
|
|
//| particularly for deep-sleep alarms.
|
|
|
|
//| :param bool pull: Enable a pull-up or pull-down which pulls the pin to the level opposite
|
2020-11-27 16:02:17 -05:00
|
|
|
//| that of ``value``. For instance, if ``value`` is set to ``True``, setting ``pull``
|
2020-11-24 23:36:59 -05:00
|
|
|
//| to ``True`` will enable a pull-down, to hold the pin low normally until an outside signal
|
|
|
|
//| pulls it high.
|
2020-11-18 20:44:34 -05:00
|
|
|
//| """
|
|
|
|
//| ...
|
|
|
|
//|
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC mp_obj_t alarm_pin_pinalarm_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_pinalarm_obj_t *self = m_new_obj(alarm_pin_pinalarm_obj_t);
|
|
|
|
self->base.type = &alarm_pin_pinalarm_type;
|
2020-12-01 20:01:14 -05:00
|
|
|
enum { ARG_pin, ARG_value, ARG_edge, ARG_pull };
|
2020-11-18 20:44:34 -05:00
|
|
|
static const mp_arg_t allowed_args[] = {
|
2020-12-01 20:01:14 -05:00
|
|
|
{ MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
2020-11-22 19:10:09 -05:00
|
|
|
{ MP_QSTR_value, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_BOOL },
|
2020-11-18 20:44:34 -05:00
|
|
|
{ MP_QSTR_edge, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
{ MP_QSTR_pull, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
|
|
|
|
};
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
2020-11-22 19:10:09 -05:00
|
|
|
mp_arg_parse_all(0, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-11-18 20:44:34 -05:00
|
|
|
|
2020-12-01 20:01:14 -05:00
|
|
|
mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[ARG_pin].u_obj);
|
2020-11-18 20:44:34 -05:00
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
common_hal_alarm_pin_pinalarm_construct(self,
|
2020-12-01 20:01:14 -05:00
|
|
|
pin,
|
2020-11-22 19:10:09 -05:00
|
|
|
args[ARG_value].u_bool,
|
|
|
|
args[ARG_edge].u_bool,
|
|
|
|
args[ARG_pull].u_bool);
|
2020-11-18 20:44:34 -05:00
|
|
|
|
|
|
|
return MP_OBJ_FROM_PTR(self);
|
|
|
|
}
|
|
|
|
|
2020-12-01 20:01:14 -05:00
|
|
|
//| pin: microcontroller.Pin
|
|
|
|
//| """The trigger pin."""
|
2020-11-25 15:07:57 -05:00
|
|
|
//|
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC mp_obj_t alarm_pin_pinalarm_obj_get_pin(mp_obj_t self_in) {
|
|
|
|
alarm_pin_pinalarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2021-03-15 09:57:36 -04:00
|
|
|
mcu_pin_obj_t *pin = common_hal_alarm_pin_pinalarm_get_pin(self);
|
2020-12-08 20:13:00 -05:00
|
|
|
if (pin == NULL) {
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
return MP_OBJ_FROM_PTR(pin);
|
2020-11-25 15:07:57 -05:00
|
|
|
}
|
2020-12-22 19:13:02 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(alarm_pin_pinalarm_get_pin_obj, alarm_pin_pinalarm_obj_get_pin);
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
const mp_obj_property_t alarm_pin_pinalarm_pin_obj = {
|
2020-11-25 15:07:57 -05:00
|
|
|
.base.type = &mp_type_property,
|
2020-12-22 19:13:02 -05:00
|
|
|
.proxy = {(mp_obj_t)&alarm_pin_pinalarm_get_pin_obj,
|
2020-11-25 15:07:57 -05:00
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2020-12-01 20:01:14 -05:00
|
|
|
//| value: bool
|
2020-11-25 15:07:57 -05:00
|
|
|
//| """The value on which to trigger."""
|
|
|
|
//|
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC mp_obj_t alarm_pin_pinalarm_obj_get_value(mp_obj_t self_in) {
|
|
|
|
alarm_pin_pinalarm_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
return mp_obj_new_bool(common_hal_alarm_pin_pinalarm_get_value(self));
|
2020-11-25 15:07:57 -05:00
|
|
|
}
|
2020-12-22 19:13:02 -05:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(alarm_pin_pinalarm_get_value_obj, alarm_pin_pinalarm_obj_get_value);
|
2020-11-25 15:07:57 -05:00
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
const mp_obj_property_t alarm_pin_pinalarm_value_obj = {
|
2020-11-25 15:07:57 -05:00
|
|
|
.base.type = &mp_type_property,
|
2020-12-22 19:13:02 -05:00
|
|
|
.proxy = {(mp_obj_t)&alarm_pin_pinalarm_get_value_obj,
|
2020-11-25 15:07:57 -05:00
|
|
|
(mp_obj_t)&mp_const_none_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC const mp_rom_map_elem_t alarm_pin_pinalarm_locals_dict_table[] = {
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pin), MP_ROM_PTR(&alarm_pin_pinalarm_pin_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&alarm_pin_pinalarm_value_obj) },
|
2020-11-18 20:44:34 -05:00
|
|
|
};
|
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
STATIC MP_DEFINE_CONST_DICT(alarm_pin_pinalarm_locals_dict, alarm_pin_pinalarm_locals_dict_table);
|
2020-11-18 20:44:34 -05:00
|
|
|
|
2020-12-22 19:13:02 -05:00
|
|
|
const mp_obj_type_t alarm_pin_pinalarm_type = {
|
2020-11-18 20:44:34 -05:00
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_PinAlarm,
|
2020-12-22 19:13:02 -05:00
|
|
|
.make_new = alarm_pin_pinalarm_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&alarm_pin_pinalarm_locals_dict,
|
2020-11-18 20:44:34 -05:00
|
|
|
};
|