2017-04-10 16:32:19 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2013, 2014 Damien P. George
|
2017-04-10 16:32:19 -04:00
|
|
|
*
|
|
|
|
* 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 <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
|
|
|
|
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/objtype.h"
|
|
|
|
#include "py/objproperty.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
|
|
|
#include "shared-bindings/digitalio/DigitalInOut.h"
|
2017-06-16 16:47:01 -04:00
|
|
|
#include "shared-bindings/digitalio/Direction.h"
|
|
|
|
#include "shared-bindings/digitalio/DriveMode.h"
|
|
|
|
#include "shared-bindings/digitalio/Pull.h"
|
2017-10-02 20:49:40 -04:00
|
|
|
#include "shared-bindings/util.h"
|
2018-07-31 19:53:54 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| class DigitalInOut:
|
2020-05-12 20:15:28 -04:00
|
|
|
//| """Digital input and output
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| A DigitalInOut is used to digitally control I/O pins. For analog control of
|
|
|
|
//| a pin, see the :py:class:`analogio.AnalogIn` and
|
|
|
|
//| :py:class:`analogio.AnalogOut` classes."""
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-07-03 14:23:34 -04:00
|
|
|
//| def __init__(self, pin: microcontroller.Pin) -> None:
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """Create a new DigitalInOut object associated with the pin. Defaults to input
|
|
|
|
//| with no pull. Use :py:meth:`switch_to_input` and
|
|
|
|
//| :py:meth:`switch_to_output` to change the direction.
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| :param ~microcontroller.Pin pin: The pin to control"""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_make_new(const mp_obj_type_t *type,
|
2021-03-15 09:57:36 -04:00
|
|
|
mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
2019-01-14 20:26:36 -05:00
|
|
|
mp_arg_check_num(n_args, kw_args, 1, 1, false);
|
2017-04-10 16:32:19 -04:00
|
|
|
|
|
|
|
digitalio_digitalinout_obj_t *self = m_new_obj(digitalio_digitalinout_obj_t);
|
|
|
|
self->base.type = &digitalio_digitalinout_type;
|
|
|
|
|
2020-03-05 16:35:31 -05:00
|
|
|
mcu_pin_obj_t *pin = validate_obj_is_free_pin(args[0]);
|
2017-04-10 16:32:19 -04:00
|
|
|
common_hal_digitalio_digitalinout_construct(self, pin);
|
|
|
|
|
2020-02-28 23:32:24 -05:00
|
|
|
return MP_OBJ_FROM_PTR(self);
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
|
2020-05-16 15:47:49 -04:00
|
|
|
//| def deinit(self) -> None:
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """Turn off the DigitalInOut and release the pin for other use."""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_deinit(mp_obj_t self_in) {
|
2017-06-07 17:39:12 -04:00
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
|
|
|
common_hal_digitalio_digitalinout_deinit(self);
|
|
|
|
return mp_const_none;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_deinit_obj, digitalio_digitalinout_obj_deinit);
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __enter__(self) -> DigitalInOut:
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """No-op used by Context Managers."""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
// Provided by context manager helper.
|
|
|
|
|
2020-07-02 13:28:36 -04:00
|
|
|
//| def __exit__(self) -> None:
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """Automatically deinitializes the hardware when exiting a context. See
|
|
|
|
//| :ref:`lifetime-and-contextmanagers` for more info."""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj___exit__(size_t n_args, const mp_obj_t *args) {
|
|
|
|
(void)n_args;
|
|
|
|
common_hal_digitalio_digitalinout_deinit(args[0]);
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(digitalio_digitalinout_obj___exit___obj, 4, 4, digitalio_digitalinout_obj___exit__);
|
|
|
|
|
2019-06-12 04:00:59 -04:00
|
|
|
STATIC void check_for_deinit(digitalio_digitalinout_obj_t *self) {
|
|
|
|
if (common_hal_digitalio_digitalinout_deinited(self)) {
|
|
|
|
raise_deinited_error();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| def switch_to_output(self, value: bool = False, drive_mode: DriveMode = DriveMode.PUSH_PULL) -> None:
|
2020-07-23 14:22:41 -04:00
|
|
|
//| """Set the drive mode and value and then switch to writing out digital
|
|
|
|
//| values.
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-07-23 14:22:41 -04:00
|
|
|
//| :param bool value: default value to set upon switching
|
|
|
|
//| :param ~digitalio.DriveMode drive_mode: drive mode for the output
|
|
|
|
//| """
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_switch_to_output(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2017-06-07 17:39:12 -04:00
|
|
|
enum { ARG_value, ARG_drive_mode };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_value, MP_ARG_BOOL, {.u_bool = false} },
|
2017-06-16 16:47:01 -04:00
|
|
|
{ MP_QSTR_drive_mode, MP_ARG_OBJ, {.u_rom_obj = &digitalio_drive_mode_push_pull_obj} },
|
2017-06-07 17:39:12 -04:00
|
|
|
};
|
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-07 17:39:12 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2017-04-10 16:32:19 -04:00
|
|
|
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_drive_mode_t drive_mode = DRIVE_MODE_PUSH_PULL;
|
2017-06-16 16:47:01 -04:00
|
|
|
if (args[ARG_drive_mode].u_rom_obj == &digitalio_drive_mode_open_drain_obj) {
|
2017-06-07 17:39:12 -04:00
|
|
|
drive_mode = DRIVE_MODE_OPEN_DRAIN;
|
|
|
|
}
|
|
|
|
// do the transfer
|
2020-05-19 20:46:29 -04:00
|
|
|
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(self, args[ARG_value].u_bool, drive_mode);
|
|
|
|
if (result == DIGITALINOUT_INPUT_ONLY) {
|
|
|
|
mp_raise_NotImplementedError(translate("Pin is input only"));
|
|
|
|
}
|
2017-06-07 17:39:12 -04:00
|
|
|
return mp_const_none;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_output_obj, 1, digitalio_digitalinout_switch_to_output);
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| def switch_to_input(self, pull: Optional[Pull] = None) -> None:
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """Set the pull and then switch to read in digital values.
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| :param Pull pull: pull configuration for the input
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| Example usage::
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| import digitalio
|
|
|
|
//| import board
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| switch = digitalio.DigitalInOut(board.SLIDE_SWITCH)
|
|
|
|
//| switch.switch_to_input(pull=digitalio.Pull.UP)
|
|
|
|
//| # Or, after switch_to_input
|
|
|
|
//| switch.pull = digitalio.Pull.UP
|
|
|
|
//| print(switch.value)"""
|
|
|
|
//| ...
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_switch_to_input(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
|
2017-06-07 17:39:12 -04:00
|
|
|
enum { ARG_pull };
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
{ MP_QSTR_pull, MP_ARG_OBJ, {.u_rom_obj = mp_const_none} },
|
|
|
|
};
|
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-07 17:39:12 -04:00
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
2020-05-16 16:14:50 -04:00
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_pull_t pull = PULL_NONE;
|
2017-06-16 16:47:01 -04:00
|
|
|
if (args[ARG_pull].u_rom_obj == &digitalio_pull_up_obj) {
|
2017-06-07 17:39:12 -04:00
|
|
|
pull = PULL_UP;
|
2021-03-15 09:57:36 -04:00
|
|
|
} else if (args[ARG_pull].u_rom_obj == &digitalio_pull_down_obj) {
|
2017-06-07 17:39:12 -04:00
|
|
|
pull = PULL_DOWN;
|
|
|
|
}
|
|
|
|
// do the transfer
|
|
|
|
common_hal_digitalio_digitalinout_switch_to_input(self, pull);
|
|
|
|
return mp_const_none;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_input_obj, 1, digitalio_digitalinout_switch_to_input);
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| direction: Direction
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """The direction of the pin.
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2017-06-07 17:39:12 -04:00
|
|
|
//| Setting this will use the defaults from the corresponding
|
|
|
|
//| :py:meth:`switch_to_input` or :py:meth:`switch_to_output` method. If
|
|
|
|
//| you want to set pull, value or drive mode prior to switching, then use
|
2020-05-07 10:54:09 -04:00
|
|
|
//| those methods instead."""
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
typedef struct {
|
|
|
|
mp_obj_base_t base;
|
2017-06-16 16:47:01 -04:00
|
|
|
} digitalio_digitalio_direction_obj_t;
|
|
|
|
extern const digitalio_digitalio_direction_obj_t digitalio_digitalio_direction_in_obj;
|
|
|
|
extern const digitalio_digitalio_direction_obj_t digitalio_digitalio_direction_out_obj;
|
2017-04-10 16:32:19 -04:00
|
|
|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_get_direction(mp_obj_t self_in) {
|
2017-06-07 17:39:12 -04:00
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_direction_t direction = common_hal_digitalio_digitalinout_get_direction(self);
|
2017-06-16 16:47:01 -04:00
|
|
|
if (direction == DIRECTION_INPUT) {
|
|
|
|
return (mp_obj_t)&digitalio_direction_input_obj;
|
2017-06-07 17:39:12 -04:00
|
|
|
}
|
2017-06-16 16:47:01 -04:00
|
|
|
return (mp_obj_t)&digitalio_direction_output_obj;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_direction_obj, digitalio_digitalinout_obj_get_direction);
|
|
|
|
|
2017-06-06 20:45:39 -04:00
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_obj_t value) {
|
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-16 16:47:01 -04:00
|
|
|
if (value == &digitalio_direction_input_obj) {
|
2017-06-06 20:45:39 -04:00
|
|
|
common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE);
|
2017-06-16 16:47:01 -04:00
|
|
|
} else if (value == &digitalio_direction_output_obj) {
|
2020-05-19 20:46:29 -04:00
|
|
|
digitalinout_result_t result = common_hal_digitalio_digitalinout_switch_to_output(self, false, DRIVE_MODE_PUSH_PULL);
|
|
|
|
if (result == DIGITALINOUT_INPUT_ONLY) {
|
|
|
|
mp_raise_NotImplementedError(translate("Pin is input only"));
|
|
|
|
}
|
2017-06-06 20:45:39 -04:00
|
|
|
} else {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("Invalid direction."));
|
2017-06-06 20:45:39 -04:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_direction_obj, digitalio_digitalinout_obj_set_direction);
|
|
|
|
|
2017-06-16 16:47:01 -04:00
|
|
|
const mp_obj_property_t digitalio_digitalio_direction_obj = {
|
2017-04-10 16:32:19 -04:00
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_direction_obj,
|
2017-06-06 20:45:39 -04:00
|
|
|
(mp_obj_t)&digitalio_digitalinout_set_direction_obj,
|
2017-04-10 16:32:19 -04:00
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| value: bool
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """The digital logic level of the pin."""
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_get_value(mp_obj_t self_in) {
|
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-04-10 16:32:19 -04:00
|
|
|
bool value = common_hal_digitalio_digitalinout_get_value(self);
|
|
|
|
return mp_obj_new_bool(value);
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_value_obj, digitalio_digitalinout_obj_get_value);
|
|
|
|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_set_value(mp_obj_t self_in, mp_obj_t value) {
|
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-16 16:47:01 -04:00
|
|
|
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_AttributeError(translate("Cannot set value when direction is input."));
|
2017-04-10 16:32:19 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
common_hal_digitalio_digitalinout_set_value(self, mp_obj_is_true(value));
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_value_obj, digitalio_digitalinout_obj_set_value);
|
|
|
|
|
|
|
|
const mp_obj_property_t digitalio_digitalinout_value_obj = {
|
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_value_obj,
|
|
|
|
(mp_obj_t)&digitalio_digitalinout_set_value_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| drive_mode: DriveMode
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """The pin drive mode. One of:
|
2018-02-07 16:08:42 -05:00
|
|
|
//|
|
|
|
|
//| - `digitalio.DriveMode.PUSH_PULL`
|
2020-05-07 10:54:09 -04:00
|
|
|
//| - `digitalio.DriveMode.OPEN_DRAIN`"""
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_get_drive_mode(mp_obj_t self_in) {
|
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-16 16:47:01 -04:00
|
|
|
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_AttributeError(translate("Drive mode not used when direction is input."));
|
2017-04-10 16:32:19 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_drive_mode_t drive_mode = common_hal_digitalio_digitalinout_get_drive_mode(self);
|
2017-04-10 16:32:19 -04:00
|
|
|
if (drive_mode == DRIVE_MODE_PUSH_PULL) {
|
2017-06-16 16:47:01 -04:00
|
|
|
return (mp_obj_t)&digitalio_drive_mode_push_pull_obj;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
2017-06-16 16:47:01 -04:00
|
|
|
return (mp_obj_t)&digitalio_drive_mode_open_drain_obj;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_drive_mode_obj, digitalio_digitalinout_obj_get_drive_mode);
|
|
|
|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_set_drive_mode(mp_obj_t self_in, mp_obj_t drive_mode) {
|
2017-06-07 17:39:12 -04:00
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-16 16:47:01 -04:00
|
|
|
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_INPUT) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_AttributeError(translate("Drive mode not used when direction is input."));
|
2017-06-07 17:39:12 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_drive_mode_t c_drive_mode = DRIVE_MODE_PUSH_PULL;
|
2017-06-16 16:47:01 -04:00
|
|
|
if (drive_mode == &digitalio_drive_mode_open_drain_obj) {
|
2017-06-07 17:39:12 -04:00
|
|
|
c_drive_mode = DRIVE_MODE_OPEN_DRAIN;
|
|
|
|
}
|
|
|
|
common_hal_digitalio_digitalinout_set_drive_mode(self, c_drive_mode);
|
|
|
|
return mp_const_none;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_drive_mode_obj, digitalio_digitalinout_obj_set_drive_mode);
|
|
|
|
|
2017-06-16 16:47:01 -04:00
|
|
|
const mp_obj_property_t digitalio_digitalio_drive_mode_obj = {
|
2017-04-10 16:32:19 -04:00
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_drive_mode_obj,
|
|
|
|
(mp_obj_t)&digitalio_digitalinout_set_drive_mode_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
2020-07-25 04:58:37 -04:00
|
|
|
//| pull: Optional[Pull]
|
2020-05-07 10:54:09 -04:00
|
|
|
//| """The pin pull direction. One of:
|
2018-02-07 16:08:42 -05:00
|
|
|
//|
|
|
|
|
//| - `digitalio.Pull.UP`
|
|
|
|
//| - `digitalio.Pull.DOWN`
|
|
|
|
//| - `None`
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
2020-05-07 10:54:09 -04:00
|
|
|
//| :raises AttributeError: if `direction` is :py:data:`~digitalio.Direction.OUTPUT`."""
|
2017-04-10 16:32:19 -04:00
|
|
|
//|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_get_pull(mp_obj_t self_in) {
|
2017-06-07 17:39:12 -04:00
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-16 16:47:01 -04:00
|
|
|
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_AttributeError(translate("Pull not used when direction is output."));
|
2017-06-07 17:39:12 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_pull_t pull = common_hal_digitalio_digitalinout_get_pull(self);
|
2017-06-07 17:39:12 -04:00
|
|
|
if (pull == PULL_UP) {
|
2017-06-16 16:47:01 -04:00
|
|
|
return (mp_obj_t)&digitalio_pull_up_obj;
|
2017-06-07 17:39:12 -04:00
|
|
|
} else if (pull == PULL_DOWN) {
|
2017-06-16 16:47:01 -04:00
|
|
|
return (mp_obj_t)&digitalio_pull_down_obj;
|
2017-06-07 17:39:12 -04:00
|
|
|
}
|
|
|
|
return (mp_obj_t)&mp_const_none_obj;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_pull_obj, digitalio_digitalinout_obj_get_pull);
|
|
|
|
|
|
|
|
STATIC mp_obj_t digitalio_digitalinout_obj_set_pull(mp_obj_t self_in, mp_obj_t pull_obj) {
|
2017-06-07 17:39:12 -04:00
|
|
|
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(self);
|
2017-06-16 16:47:01 -04:00
|
|
|
if (common_hal_digitalio_digitalinout_get_direction(self) == DIRECTION_OUTPUT) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_AttributeError(translate("Pull not used when direction is output."));
|
2017-06-07 17:39:12 -04:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2018-01-02 21:25:41 -05:00
|
|
|
digitalio_pull_t pull = PULL_NONE;
|
2017-06-16 16:47:01 -04:00
|
|
|
if (pull_obj == &digitalio_pull_up_obj) {
|
2017-06-07 17:39:12 -04:00
|
|
|
pull = PULL_UP;
|
2017-06-16 16:47:01 -04:00
|
|
|
} else if (pull_obj == &digitalio_pull_down_obj) {
|
2017-06-07 17:39:12 -04:00
|
|
|
pull = PULL_DOWN;
|
2017-08-22 15:41:04 -04:00
|
|
|
} else if (pull_obj != mp_const_none) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_ValueError(translate("Unsupported pull value."));
|
2017-06-07 17:39:12 -04:00
|
|
|
}
|
|
|
|
common_hal_digitalio_digitalinout_set_pull(self, pull);
|
|
|
|
return mp_const_none;
|
2017-04-10 16:32:19 -04:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_pull_obj, digitalio_digitalinout_obj_set_pull);
|
|
|
|
|
2017-06-16 16:47:01 -04:00
|
|
|
const mp_obj_property_t digitalio_digitalio_pull_obj = {
|
2017-04-10 16:32:19 -04:00
|
|
|
.base.type = &mp_type_property,
|
|
|
|
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_pull_obj,
|
|
|
|
(mp_obj_t)&digitalio_digitalinout_set_pull_obj,
|
|
|
|
(mp_obj_t)&mp_const_none_obj},
|
|
|
|
};
|
|
|
|
|
|
|
|
STATIC const mp_rom_map_elem_t digitalio_digitalinout_locals_dict_table[] = {
|
|
|
|
// instance methods
|
2017-08-27 15:02:50 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&digitalio_digitalinout_deinit_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&digitalio_digitalinout_obj___exit___obj) },
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_switch_to_output), MP_ROM_PTR(&digitalio_digitalinout_switch_to_output_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_switch_to_input), MP_ROM_PTR(&digitalio_digitalinout_switch_to_input_obj) },
|
|
|
|
|
|
|
|
// Properties
|
2017-06-16 16:47:01 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_direction), MP_ROM_PTR(&digitalio_digitalio_direction_obj) },
|
2017-04-10 16:32:19 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&digitalio_digitalinout_value_obj) },
|
2017-06-16 16:47:01 -04:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_drive_mode), MP_ROM_PTR(&digitalio_digitalio_drive_mode_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_pull), MP_ROM_PTR(&digitalio_digitalio_pull_obj) },
|
2017-04-10 16:32:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(digitalio_digitalinout_locals_dict, digitalio_digitalinout_locals_dict_table);
|
|
|
|
|
|
|
|
const mp_obj_type_t digitalio_digitalinout_type = {
|
|
|
|
{ &mp_type_type },
|
|
|
|
.name = MP_QSTR_DigitalInOut,
|
|
|
|
.make_new = digitalio_digitalinout_make_new,
|
|
|
|
.locals_dict = (mp_obj_t)&digitalio_digitalinout_locals_dict,
|
|
|
|
};
|
2019-04-16 13:11:54 -04:00
|
|
|
|
|
|
|
// Helper for validating digitalio.DigitalInOut arguments
|
|
|
|
digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj) {
|
2021-04-22 20:55:39 -04:00
|
|
|
if (!mp_obj_is_type(obj, &digitalio_digitalinout_type)) {
|
2019-04-16 13:11:54 -04:00
|
|
|
mp_raise_TypeError(translate("argument num/types mismatch"));
|
|
|
|
}
|
|
|
|
digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(obj);
|
2019-06-12 04:00:59 -04:00
|
|
|
check_for_deinit(pin);
|
2019-04-16 13:11:54 -04:00
|
|
|
return pin;
|
|
|
|
}
|