25
25
*/
26
26
27
27
#include "common-hal/rotaryio/IncrementalEncoder.h"
28
+ #include "nrfx_gpiote.h"
28
29
29
30
#include "py/runtime.h"
30
31
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
+
31
42
void common_hal_rotaryio_incrementalencoder_construct (rotaryio_incrementalencoder_obj_t * self ,
32
43
const mcu_pin_obj_t * pin_a , const mcu_pin_obj_t * pin_b ) {
33
44
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
+
34
63
claim_pin (pin_a );
35
64
claim_pin (pin_b );
36
65
}
@@ -43,6 +72,9 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
43
72
if (common_hal_rotaryio_incrementalencoder_deinited (self )) {
44
73
return ;
45
74
}
75
+ _objs [self -> pin_a ] = NULL ;
76
+ _objs [self -> pin_b ] = NULL ;
77
+
46
78
reset_pin_number (self -> pin_a );
47
79
self -> pin_a = NO_PIN ;
48
80
reset_pin_number (self -> pin_b );
0 commit comments