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,32 @@ 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
+ 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
+
59
79
self -> pin_a = pin_a -> number ;
60
80
self -> pin_b = pin_b -> number ;
61
81
self -> unit = (pcnt_unit_t )unit ;
@@ -77,21 +97,20 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
77
97
mp_int_t common_hal_rotaryio_incrementalencoder_get_position (rotaryio_incrementalencoder_obj_t * self ) {
78
98
int16_t count ;
79
99
pcnt_get_counter_value (self -> unit , & count );
80
- return (count / 2 ) + self -> position ;
100
+
101
+ return (count + self -> position ) / self -> divisor ;
81
102
}
82
103
83
104
void common_hal_rotaryio_incrementalencoder_set_position (rotaryio_incrementalencoder_obj_t * self ,
84
105
mp_int_t new_position ) {
85
- self -> position = new_position ;
106
+ self -> position = new_position * self -> divisor ;
86
107
pcnt_counter_clear (self -> unit );
87
108
}
88
109
89
110
mp_int_t common_hal_rotaryio_incrementalencoder_get_divisor (rotaryio_incrementalencoder_obj_t * self ) {
90
- return 4 ;
111
+ return self -> divisor ;
91
112
}
92
113
93
114
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
- }
115
+ self -> divisor = divisor ;
97
116
}
0 commit comments