|
34 | 34 | #include "py/runtime.h"
|
35 | 35 | #include "py/mphal.h"
|
36 | 36 | #include "modmachine.h"
|
| 37 | +#include "extmod/virtpin.h" |
37 | 38 |
|
38 | 39 | typedef struct _machine_pin_obj_t {
|
39 | 40 | mp_obj_base_t base;
|
@@ -162,7 +163,7 @@ STATIC mp_obj_t machine_pin_obj_init_helper(const machine_pin_obj_t *self, size_
|
162 | 163 | }
|
163 | 164 |
|
164 | 165 | // constructor(id, ...)
|
165 |
| -STATIC mp_obj_t machine_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 166 | +mp_obj_t mp_pin_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
166 | 167 | mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
167 | 168 |
|
168 | 169 | // get the wanted pin object
|
@@ -257,14 +258,35 @@ STATIC const mp_rom_map_elem_t machine_pin_locals_dict_table[] = {
|
257 | 258 | { MP_ROM_QSTR(MP_QSTR_IRQ_FALLING), MP_ROM_INT(GPIO_PIN_INTR_NEGEDGE) },
|
258 | 259 | };
|
259 | 260 |
|
| 261 | +STATIC mp_uint_t pin_ioctl(mp_obj_t self_in, mp_uint_t request, uintptr_t arg, int *errcode) { |
| 262 | + (void)errcode; |
| 263 | + machine_pin_obj_t *self = self_in; |
| 264 | + |
| 265 | + switch (request) { |
| 266 | + case MP_PIN_READ: { |
| 267 | + return gpio_get_level(self->id); |
| 268 | + } |
| 269 | + case MP_PIN_WRITE: { |
| 270 | + gpio_set_level(self->id, arg); |
| 271 | + return 0; |
| 272 | + } |
| 273 | + } |
| 274 | + return -1; |
| 275 | +} |
| 276 | + |
260 | 277 | STATIC MP_DEFINE_CONST_DICT(machine_pin_locals_dict, machine_pin_locals_dict_table);
|
261 | 278 |
|
| 279 | +STATIC const mp_pin_p_t pin_pin_p = { |
| 280 | + .ioctl = pin_ioctl, |
| 281 | +}; |
| 282 | + |
262 | 283 | const mp_obj_type_t machine_pin_type = {
|
263 | 284 | { &mp_type_type },
|
264 | 285 | .name = MP_QSTR_Pin,
|
265 | 286 | .print = machine_pin_print,
|
266 |
| - .make_new = machine_pin_make_new, |
| 287 | + .make_new = mp_pin_make_new, |
267 | 288 | .call = machine_pin_call,
|
| 289 | + .protocol = &pin_pin_p, |
268 | 290 | .locals_dict = (mp_obj_t)&machine_pin_locals_dict,
|
269 | 291 | };
|
270 | 292 |
|
|
0 commit comments