29
29
#include "common-hal/microcontroller/Pin.h"
30
30
31
31
#include "py/runtime.h"
32
- #include "supervisor/shared/translate.h"
33
32
34
33
void common_hal_rotaryio_incrementalencoder_construct (rotaryio_incrementalencoder_obj_t * self ,
35
34
const mcu_pin_obj_t * pin_a , const mcu_pin_obj_t * pin_b ) {
36
35
claim_pin (pin_a );
37
36
claim_pin (pin_b );
38
37
39
38
// Prepare configuration for the PCNT unit
40
- const pcnt_config_t pcnt_config = {
39
+ pcnt_config_t pcnt_config = {
41
40
// Set PCNT input signal and control GPIOs
42
41
.pulse_gpio_num = pin_a -> number ,
43
42
.ctrl_gpio_num = pin_b -> number ,
@@ -51,11 +50,46 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
51
50
};
52
51
53
52
// Initialize PCNT unit
54
- const int8_t unit = peripherals_pcnt_init (pcnt_config );
53
+ const int8_t unit = peripherals_pcnt_get_unit (pcnt_config );
55
54
if (unit == -1 ) {
56
55
mp_raise_RuntimeError (translate ("All PCNT units in use" ));
57
56
}
58
57
58
+ pcnt_unit_config (& pcnt_config );
59
+
60
+ if ((self -> divisor == 2 ) || (self -> divisor == 1 )) {
61
+ // Setup channel 1 for divisor=2 or divisor=1
62
+ pcnt_config .pulse_gpio_num = pin_b -> number ; // What was control is now signal
63
+ pcnt_config .ctrl_gpio_num = pin_a -> number ; // What was signal is now control
64
+ pcnt_config .channel = PCNT_CHANNEL_1 ;
65
+ // What to do on the positive / negative edge of pulse input?
66
+ pcnt_config .pos_mode = PCNT_COUNT_DEC ; // Count up on the positive edge
67
+ pcnt_config .neg_mode = PCNT_COUNT_INC ; // Keep the counter value on the negative edge
68
+ // What to do when control input is low or high?
69
+ pcnt_config .lctrl_mode = PCNT_MODE_KEEP ; // Keep the primary counter mode if low
70
+ pcnt_config .hctrl_mode = PCNT_MODE_REVERSE ; // Reverse counting direction if high
71
+ } else {
72
+ // Ensure channel 1 is disabled for divisor=4
73
+ pcnt_config .pulse_gpio_num = pin_b -> number ; // What was control is now signal
74
+ pcnt_config .ctrl_gpio_num = pin_a -> number ; // What was signal is now control
75
+ pcnt_config .channel = PCNT_CHANNEL_1 ;
76
+ // What to do on the positive / negative edge of pulse input?
77
+ pcnt_config .pos_mode = PCNT_COUNT_DIS ; // Disabled
78
+ pcnt_config .neg_mode = PCNT_COUNT_DIS ; // Disabled
79
+ // What to do when control input is low or high?
80
+ pcnt_config .lctrl_mode = PCNT_MODE_DISABLE ; // Disabled
81
+ pcnt_config .hctrl_mode = PCNT_MODE_DISABLE ; // Disabled
82
+ }
83
+
84
+ pcnt_unit_config (& pcnt_config );
85
+
86
+ // Initialize PCNT's counter
87
+ pcnt_counter_pause (pcnt_config .unit );
88
+ pcnt_counter_clear (pcnt_config .unit );
89
+
90
+ // Everything is set up, now go to counting
91
+ pcnt_counter_resume (pcnt_config .unit );
92
+
59
93
self -> pin_a = pin_a -> number ;
60
94
self -> pin_b = pin_b -> number ;
61
95
self -> unit = (pcnt_unit_t )unit ;
@@ -77,7 +111,12 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
77
111
mp_int_t common_hal_rotaryio_incrementalencoder_get_position (rotaryio_incrementalencoder_obj_t * self ) {
78
112
int16_t count ;
79
113
pcnt_get_counter_value (self -> unit , & count );
80
- return (count / 2 ) + self -> position ;
114
+
115
+ if ((self -> divisor == 4 ) || (self -> divisor == 2 )) {
116
+ return (count / 2 ) + self -> position ;
117
+ } else {
118
+ return (count ) + self -> position ;
119
+ }
81
120
}
82
121
83
122
void common_hal_rotaryio_incrementalencoder_set_position (rotaryio_incrementalencoder_obj_t * self ,
@@ -87,11 +126,9 @@ void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalenc
87
126
}
88
127
89
128
mp_int_t common_hal_rotaryio_incrementalencoder_get_divisor (rotaryio_incrementalencoder_obj_t * self ) {
90
- return 4 ;
129
+ return self -> divisor ;
91
130
}
92
131
93
132
void 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
- }
133
+ self -> divisor = divisor ;
97
134
}
0 commit comments