@@ -81,7 +81,7 @@ STATIC mp_obj_t machine_pin_obj_init_helper(machine_pin_obj_t *self, mp_uint_t n
8181}
8282
8383// constructor(drv_name, pin, ...)
84- 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 ) {
84+ 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 ) {
8585 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
8686
8787 // get the wanted port
@@ -154,6 +154,24 @@ STATIC mp_obj_t machine_pin_high(mp_obj_t self_in) {
154154}
155155STATIC MP_DEFINE_CONST_FUN_OBJ_1 (machine_pin_high_obj , machine_pin_high );
156156
157+ STATIC mp_uint_t machine_pin_ioctl (mp_obj_t self_in , mp_uint_t request , uintptr_t arg , int * errcode ) {
158+ (void )errcode ;
159+ machine_pin_obj_t * self = self_in ;
160+
161+ switch (request ) {
162+ case MP_PIN_READ : {
163+ uint32_t pin_val ;
164+ gpio_pin_read (self -> port , self -> pin , & pin_val );
165+ return pin_val ;
166+ }
167+ case MP_PIN_WRITE : {
168+ gpio_pin_write (self -> port , self -> pin , arg );
169+ return 0 ;
170+ }
171+ }
172+ return -1 ;
173+ }
174+
157175STATIC const mp_map_elem_t machine_pin_locals_dict_table [] = {
158176 // instance methods
159177 { MP_OBJ_NEW_QSTR (MP_QSTR_init ), (mp_obj_t )& machine_pin_init_obj },
@@ -170,11 +188,16 @@ STATIC const mp_map_elem_t machine_pin_locals_dict_table[] = {
170188
171189STATIC MP_DEFINE_CONST_DICT (machine_pin_locals_dict , machine_pin_locals_dict_table );
172190
191+ STATIC const mp_pin_p_t machine_pin_pin_p = {
192+ .ioctl = machine_pin_ioctl ,
193+ };
194+
173195const mp_obj_type_t machine_pin_type = {
174196 { & mp_type_type },
175197 .name = MP_QSTR_Pin ,
176198 .print = machine_pin_print ,
177- .make_new = machine_pin_make_new ,
199+ .make_new = mp_pin_make_new ,
178200 .call = machine_pin_call ,
201+ .protocol = & machine_pin_pin_p ,
179202 .locals_dict = (mp_obj_t )& machine_pin_locals_dict ,
180203};
0 commit comments