@@ -60,10 +60,17 @@ STATIC void incrementalencoder_interrupt_handler(void *self_in);
60
60
void common_hal_rotaryio_incrementalencoder_construct (rotaryio_incrementalencoder_obj_t * self ,
61
61
const mcu_pin_obj_t * pin_a , const mcu_pin_obj_t * pin_b ) {
62
62
mp_obj_t pins [] = {MP_OBJ_FROM_PTR (pin_a ), MP_OBJ_FROM_PTR (pin_b )};
63
+ bool swap = false;
63
64
if (!common_hal_rp2pio_pins_are_sequential (2 , pins )) {
64
- mp_raise_RuntimeError (translate ("Pins must be sequential" ));
65
+ pins [0 ] = MP_OBJ_FROM_PTR (pin_b );
66
+ pins [1 ] = MP_OBJ_FROM_PTR (pin_a );
67
+ swap = true;
68
+ if (!common_hal_rp2pio_pins_are_sequential (2 , pins )) {
69
+ mp_raise_RuntimeError (translate ("Pins must be sequential" ));
70
+ }
65
71
}
66
72
73
+ self -> swap = swap ;
67
74
self -> position = 0 ;
68
75
self -> quarter_count = 0 ;
69
76
@@ -72,7 +79,7 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
72
79
1000000 ,
73
80
encoder_init , MP_ARRAY_SIZE (encoder_init ), // init
74
81
NULL , 0 , 0 , 0 , // out pin
75
- pin_a , 2 , // in pins
82
+ pins [ 0 ] , 2 , // in pins
76
83
3 , 0 , // in pulls
77
84
NULL , 0 , 0 , 0x1f , // set pins
78
85
NULL , 0 , 0 , 0x1f , // sideset pins
@@ -106,12 +113,12 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
106
113
}
107
114
108
115
mp_int_t common_hal_rotaryio_incrementalencoder_get_position (rotaryio_incrementalencoder_obj_t * self ) {
109
- return self -> position ;
116
+ return self -> swap ? - self -> position : self -> position ;
110
117
}
111
118
112
119
void common_hal_rotaryio_incrementalencoder_set_position (rotaryio_incrementalencoder_obj_t * self ,
113
120
mp_int_t new_position ) {
114
- self -> position = new_position ;
121
+ self -> position = self -> swap ? - new_position : new_position ;
115
122
}
116
123
117
124
STATIC void incrementalencoder_interrupt_handler (void * self_in ) {
0 commit comments