2525 */
2626
2727#include "common-hal/rotaryio/IncrementalEncoder.h"
28+ #include "nrfx_gpiote.h"
2829
2930#include "py/runtime.h"
3031
32+ // obj array to map pin -> self since nrfx hide the mapping
33+ static rotaryio_incrementalencoder_obj_t * _objs [NUMBER_OF_PINS ];
34+
35+ static void _intr_handler (nrfx_gpiote_pin_t pin , nrf_gpiote_polarity_t action ) {
36+ rotaryio_incrementalencoder_obj_t * self = _objs [pin ];
37+ if (!self ) return ;
38+
39+ self -> position ++ ;
40+ }
41+
3142void common_hal_rotaryio_incrementalencoder_construct (rotaryio_incrementalencoder_obj_t * self ,
3243 const mcu_pin_obj_t * pin_a , const mcu_pin_obj_t * pin_b ) {
3344
45+ self -> pin_a = pin_a -> number ;
46+ self -> pin_b = pin_b -> number ;
47+
48+ _objs [self -> pin_a ] = self ;
49+ _objs [self -> pin_b ] = self ;
50+
51+ nrfx_gpiote_in_config_t cfg = {
52+ .sense = NRF_GPIOTE_POLARITY_TOGGLE ,
53+ .pull = NRF_GPIO_PIN_NOPULL ,
54+ .is_watcher = false,
55+ .hi_accuracy = true,
56+ .skip_gpio_setup = false
57+ };
58+ nrfx_gpiote_in_init (self -> pin_a , & cfg , _intr_handler );
59+ nrfx_gpiote_in_init (self -> pin_b , & cfg , _intr_handler );
60+ nrfx_gpiote_in_event_enable (self -> pin_a , true);
61+ nrfx_gpiote_in_event_enable (self -> pin_b , true);
62+
3463 claim_pin (pin_a );
3564 claim_pin (pin_b );
3665}
@@ -43,6 +72,9 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
4372 if (common_hal_rotaryio_incrementalencoder_deinited (self )) {
4473 return ;
4574 }
75+ _objs [self -> pin_a ] = NULL ;
76+ _objs [self -> pin_b ] = NULL ;
77+
4678 reset_pin_number (self -> pin_a );
4779 self -> pin_a = NO_PIN ;
4880 reset_pin_number (self -> pin_b );
0 commit comments