8000 shared-bindings: Allow for switching direction with the attribute. · sparkfun/circuitpython@c0e1f58 · GitHub
[go: up one dir, main page]

Skip to content

Commit c0e1f58

Browse files
committed
shared-bindings: Allow for switching direction with the attribute.
1 parent c8dc091 commit c0e1f58

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

shared-bindings/digitalio/DigitalInOut.c

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(digitalio_digitalinout_obj___exit___o
102102
//|
103103
//| .. method:: switch_to_output(value=False, drive_mode=DriveMode.PUSH_PULL)
104104
//|
105-
//| Switch to writing out digital values.
105+
//| Set the drive mode and value and then switch to writing out digital
106+
//| values.
106107
//|
107108
//| :param bool value: default value to set upon switching
108109
//| :param DriveMode drive_mode: drive mode for the output
@@ -135,7 +136,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_output_obj, 1, digit
135136

136137
//| .. method:: switch_to_input(pull=None)
137138
//|
138-
//| Switch to read in digital values.
139+
//| Set the pull and then switch to read in digital values.
139140
//|
140141
//| :param Pull pull: pull configuration for the input
141142
//|
@@ -179,9 +180,12 @@ MP_DEFINE_CONST_FUN_OBJ_KW(digitalio_digitalinout_switch_to_input_obj, 1, digita
179180

180181
//| .. attribute:: direction
181182
//|
182-
//| Get the direction of the pin.
183+
//| The direction of the pin.
183184
//|
184-
//| :raises AttributeError: when set. Use :py:meth:`switch_to_input` and :py:meth:`switch_to_output` to change the direction.
185+
//| Setting this will use the defaults from the corresponding
186+
//| :py:meth:`switch_to_input` or :py:meth:`switch_to_output` method. If
187+
//| you want to set pull, value or drive mode prior to switching, then use
188+
//| those methods instead.
185189
//|
186190
typedef struct {
187191
mp_obj_base_t base;
< E462 /td>
@@ -199,10 +203,23 @@ STATIC mp_obj_t digitalio_digitalinout_obj_get_direction(mp_obj_t self_in) {
199203
}
200204
MP_DEFINE_CONST_FUN_OBJ_1(digitalio_digitalinout_get_direction_obj, digitalio_digitalinout_obj_get_direction);
201205

206+
STATIC mp_obj_t digitalio_digitalinout_obj_set_direction(mp_obj_t self_in, mp_obj_t value) {
207+
digitalio_digitalinout_obj_t *self = MP_OBJ_TO_PTR(self_in);
208+
if (value == &digitalio_digitalinout_direction_in_obj) {
209+
common_hal_digitalio_digitalinout_switch_to_input(self, PULL_NONE);
210+
} else if (value == &digitalio_digitalinout_direction_out_obj) {
211+
common_hal_digitalio_digitalinout_switch_to_output(self, false, DRIVE_MODE_PUSH_PULL);
212+
} else {
213+
mp_raise_ValueError("Invalid direction.");
214+
}
215+
return mp_const_none;
216+
}
217+
MP_DEFINE_CONST_FUN_OBJ_2(digitalio_digitalinout_set_direction_obj, digitalio_digitalinout_obj_set_direction);
218+
202219
const mp_obj_property_t digitalio_digitalinout_direction_obj = {
203220
.base.type = &mp_type_property,
204221
.proxy = {(mp_obj_t)&digitalio_digitalinout_get_direction_obj,
205-
(mp_obj_t)&mp_const_none_obj,
222+
(mp_obj_t)&digitalio_digitalinout_set_direction_obj,
206223
(mp_obj_t)&mp_const_none_obj},
207224
};
208225

0 commit comments

Comments
 (0)
0