2929#include  "common-hal/microcontroller/Pin.h" 
3030
3131#include  "py/runtime.h" 
32- #include  "supervisor/shared/translate.h" 
3332
3433void  common_hal_rotaryio_incrementalencoder_construct (rotaryio_incrementalencoder_obj_t  * self ,
3534    const  mcu_pin_obj_t  * pin_a , const  mcu_pin_obj_t  * pin_b ) {
3635    claim_pin (pin_a );
3736    claim_pin (pin_b );
3837
3938    // Prepare configuration for the PCNT unit 
40-     const   pcnt_config_t  pcnt_config  =  {
39+     pcnt_config_t  pcnt_config  =  {
4140        // Set PCNT input signal and control GPIOs 
4241        .pulse_gpio_num  =  pin_a -> number ,
4342        .ctrl_gpio_num  =  pin_b -> number ,
@@ -51,11 +50,32 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
5150    };
5251
5352    // Initialize PCNT unit 
54-     const  int8_t  unit  =  peripherals_pcnt_init (pcnt_config );
53+     const  int8_t  unit  =  peripherals_pcnt_get_unit (pcnt_config );
5554    if  (unit  ==  -1 ) {
5655        mp_raise_RuntimeError (translate ("All PCNT units in use" ));
5756    }
5857
58+     pcnt_unit_config (& pcnt_config );
59+ 
60+     pcnt_config .pulse_gpio_num  =  pin_b -> number ;     // What was control is now signal 
61+     pcnt_config .ctrl_gpio_num  =  pin_a -> number ;      // What was signal is now control 
62+     pcnt_config .channel  =  PCNT_CHANNEL_1 ;
63+     // What to do on the positive / negative edge of pulse input? 
64+     pcnt_config .pos_mode  =  PCNT_COUNT_DEC ;       // Count up on the positive edge 
65+     pcnt_config .neg_mode  =  PCNT_COUNT_INC ;       // Keep the counter value on the negative edge 
66+     // What to do when control input is low or high? 
67+     pcnt_config .lctrl_mode  =  PCNT_MODE_KEEP ;         // Keep the primary counter mode if low 
68+     pcnt_config .hctrl_mode  =  PCNT_MODE_REVERSE ;      // Reverse counting direction if high 
69+ 
70+     pcnt_unit_config (& pcnt_config );
71+ 
72+     // Initialize PCNT's counter 
73+     pcnt_counter_pause (pcnt_config .unit );
74+     pcnt_counter_clear (pcnt_config .unit );
75+ 
76+     // Everything is set up, now go to counting 
77+     pcnt_counter_resume (pcnt_config .unit );
78+ 
5979    self -> pin_a  =  pin_a -> number ;
6080    self -> pin_b  =  pin_b -> number ;
6181    self -> unit  =  (pcnt_unit_t )unit ;
@@ -77,21 +97,20 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
7797mp_int_t  common_hal_rotaryio_incrementalencoder_get_position (rotaryio_incrementalencoder_obj_t  * self ) {
7898    int16_t  count ;
7999    pcnt_get_counter_value (self -> unit , & count );
80-     return  (count  / 2 ) +  self -> position ;
100+ 
101+     return  (count  +  self -> position ) / self -> divisor ;
81102}
82103
83104void  common_hal_rotaryio_incrementalencoder_set_position (rotaryio_incrementalencoder_obj_t  * self ,
84105    mp_int_t  new_position ) {
85-     self -> position  =  new_position ;
106+     self -> position  =  new_position   *   self -> divisor ;
86107    pcnt_counter_clear (self -> unit );
87108}
88109
89110mp_int_t  common_hal_rotaryio_incrementalencoder_get_divisor (rotaryio_incrementalencoder_obj_t  * self ) {
90-     return  4 ;
111+     return  self -> divisor ;
91112}
92113
93114void  common_hal_rotaryio_incrementalencoder_set_divisor (rotaryio_incrementalencoder_obj_t  * self , mp_int_t  divisor ) {
94-     if  (divisor  !=  4 ) {
95-         mp_raise_ValueError (translate ("divisor must be 4" ));
96-     }
115+     self -> divisor  =  divisor ;
97116}
0 commit comments