Skip to content

Commit 116b66b

Browse files
committed
Formatting!
1 parent 4cec9f1 commit 116b66b

9 files changed

Lines changed: 633 additions & 731 deletions

File tree

src/base_components/encoder.c

Lines changed: 73 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -8,112 +8,94 @@ void _encoder_gpio_callback(hal_gpio_pin_t pin, void *arg);
88
void _rotate_callback(hal_gpio_pin_t pin, void *arg);
99
void _pinSWChanged(uint8_t new_state, encoder_t *encoder);
1010

11-
void encoder_init(encoder_t *encoder)
12-
{
13-
printf("Encoder Init, with PinA: %d, PinB: %d, PinSW: %d\r\n", encoder->pin_a, encoder->pin_b, encoder->pin_sw);
11+
void encoder_init(encoder_t *encoder) {
12+
printf("Encoder Init, with PinA: %d, PinB: %d, PinSW: %d\r\n", encoder->pin_a, encoder->pin_b,
13+
encoder->pin_sw);
1414

15-
encoder->pin_sw_state = hal_gpio_read(encoder->pin_sw);
16-
encoder->pin_sw_last_change = hal_millis();
15+
encoder->pin_sw_state = hal_gpio_read(encoder->pin_sw);
16+
encoder->pin_sw_last_change = hal_millis();
1717

18-
encoder->old_AB = 0b00000011; // TODO, we are assuming pin a & b start high. We should read their actual values.
19-
encoder->encval = 0;
18+
encoder->old_AB = 0b00000011; // TODO, we are assuming pin a & b start high. We should read their actual values.
19+
encoder->encval = 0;
2020

21-
hal_gpio_callback(encoder->pin_a, _rotate_callback, encoder);
22-
hal_gpio_callback(encoder->pin_b, _rotate_callback, encoder);
23-
hal_gpio_callback(encoder->pin_sw, _encoder_gpio_callback, encoder);
21+
hal_gpio_callback(encoder->pin_a, _rotate_callback, encoder);
22+
hal_gpio_callback(encoder->pin_b, _rotate_callback, encoder);
23+
hal_gpio_callback(encoder->pin_sw, _encoder_gpio_callback, encoder);
2424

25-
encoder->rotate_since_pressed = false;
25+
encoder->rotate_since_pressed = false;
2626
}
2727

2828
// Based on https://youtube.com/watch?v=fgOfSHTYeio
2929
// NOTE: Using a half step encoder (detent on high and low), so step size (encval) is reduced from 4 to 2 per step
3030
void _rotate_callback(hal_gpio_pin_t pin, void *arg) {
31-
static const int8_t enc_states[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
32-
33-
encoder_t *encoder = (encoder_t *)arg;
34-
35-
encoder->old_AB <<= 2; // Push old pin values into into high positions IE 0b00000011 -> 0b00001100
31+
static const int8_t enc_states[] = { 0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0 };
32+
33+
encoder_t *encoder = (encoder_t *)arg;
34+
35+
encoder->old_AB <<= 2; // Push old pin values into into high positions IE 0b00000011 -> 0b00001100
36+
37+
if (hal_gpio_read(encoder->pin_a)) encoder->old_AB |= 0b00000010; // Read pin a state into position 2
38+
if (hal_gpio_read(encoder->pin_b)) encoder->old_AB |= 0b00000001; // Read pin b state into position 1
39+
40+
encoder->encval += enc_states[(encoder->old_AB & 0b00001111)]; // Use last four bits to lookup change in enc_state and apply to encval
41+
42+
if (encoder->encval > 1) {
43+
encoder->encval = 0;
44+
encoder->rotate_since_pressed = true;
45+
46+
if (encoder->pin_sw_state == 0) {
47+
printf("Rotating CW while Pressed\r\n");
48+
if (encoder->on_rotate_cw_while_pressed != NULL) {
49+
encoder->on_rotate_cw_while_pressed(encoder->callback_param);
50+
}
51+
}else{
52+
printf("Rotating CW\r\n");
53+
54+
if (encoder->on_rotate_cw != NULL) {
55+
encoder->on_rotate_cw(encoder->callback_param);
56+
}
57+
}
58+
}else if (encoder->encval < -1) {
59+
encoder->encval = 0;
60+
encoder->rotate_since_pressed = true;
61+
62+
if (encoder->pin_sw_state == 0) {
63+
printf("Rotating CCW while Pressed\r\n");
64+
if (encoder->on_rotate_ccw_while_pressed != NULL) {
65+
encoder->on_rotate_ccw_while_pressed(encoder->callback_param);
66+
}
67+
}else{
68+
printf("Rotating CCW\r\n");
69+
if (encoder->on_rotate_ccw != NULL) {
70+
encoder->on_rotate_ccw(encoder->callback_param);
71+
}
72+
}
73+
}
74+
}
3675

37-
if(hal_gpio_read(encoder->pin_a)) encoder->old_AB |= 0b00000010; // Read pin a state into position 2
38-
if(hal_gpio_read(encoder->pin_b)) encoder->old_AB |= 0b00000001; // Read pin b state into position 1
76+
void _encoder_gpio_callback(hal_gpio_pin_t pin, void *arg) {
77+
encoder_t *encoder = (encoder_t *)arg;
78+
uint8_t new_state = hal_gpio_read(pin);
79+
uint32_t now = hal_millis();
3980

40-
encoder->encval += enc_states[(encoder->old_AB & 0b00001111)]; // Use last four bits to lookup change in enc_state and apply to encval
81+
if (pin == encoder->pin_sw && new_state != encoder->pin_sw_state &&
82+
(now - encoder->pin_sw_last_change) > 10) {
83+
encoder->pin_sw_state = new_state;
84+
encoder->pin_sw_last_change = now;
4185

42-
if(encoder->encval > 1) {
43-
encoder->encval = 0;
44-
encoder->rotate_since_pressed = true;
45-
46-
if (encoder->pin_sw_state == 0)
47-
{
48-
printf("Rotating CW while Pressed\r\n");
49-
if (encoder->on_rotate_cw_while_pressed != NULL)
50-
{
51-
encoder->on_rotate_cw_while_pressed(encoder->callback_param);
52-
}
86+
_pinSWChanged(new_state, encoder);
5387
}
54-
else
55-
{
56-
printf("Rotating CW\r\n");
57-
58-
if (encoder->on_rotate_cw != NULL)
59-
{
60-
encoder->on_rotate_cw(encoder->callback_param);
61-
}
62-
}
63-
64-
}
65-
else if (encoder->encval < -1) {
66-
encoder->encval = 0;
67-
encoder->rotate_since_pressed = true;
68-
69-
if (encoder->pin_sw_state == 0)
70-
{
71-
printf("Rotating CCW while Pressed\r\n");
72-
if (encoder->on_rotate_ccw_while_pressed != NULL)
73-
{
74-
encoder->on_rotate_ccw_while_pressed(encoder->callback_param);
75-
}
76-
}
77-
else
78-
{
79-
printf("Rotating CCW\r\n");
80-
if (encoder->on_rotate_ccw != NULL)
81-
{
82-
encoder->on_rotate_ccw(encoder->callback_param);
83-
}
84-
}
85-
86-
}
8788
}
8889

89-
void _encoder_gpio_callback(hal_gpio_pin_t pin, void *arg)
90-
{
91-
encoder_t *encoder = (encoder_t *)arg;
92-
uint8_t new_state = hal_gpio_read(pin);
93-
uint32_t now = hal_millis();
90+
void _pinSWChanged(uint8_t new_state, encoder_t *encoder) {
91+
if (new_state == 0) {
92+
printf("Pressed\r\n");
9493

95-
if (pin == encoder->pin_sw && new_state != encoder->pin_sw_state && (now - encoder->pin_sw_last_change) > 10)
96-
{
97-
encoder->pin_sw_state = new_state;
98-
encoder->pin_sw_last_change = now;
94+
encoder->rotate_since_pressed = false;
95+
}else{
96+
printf("Released\r\n");
9997

100-
_pinSWChanged(new_state, encoder);
101-
}
98+
if (!encoder->rotate_since_pressed && encoder->on_press != NULL)
99+
encoder->on_press(encoder->callback_param);
100+
}
102101
}
103-
104-
void _pinSWChanged(uint8_t new_state, encoder_t *encoder)
105-
{
106-
if (new_state == 0)
107-
{
108-
printf("Pressed\r\n");
109-
110-
encoder->rotate_since_pressed = false;
111-
}
112-
else
113-
{
114-
printf("Released\r\n");
115-
116-
if (!encoder->rotate_since_pressed && encoder->on_press != NULL)
117-
encoder->on_press(encoder->callback_param);
118-
}
119-
}

src/base_components/encoder.h

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,26 @@
88

99
typedef void (*ev_encoder_callback_t)(void *);
1010

11-
typedef struct
12-
{
13-
hal_gpio_pin_t pin_a; // Also known as CLK
14-
hal_gpio_pin_t pin_b; // Also known as DT
15-
uint8_t old_AB;
16-
int8_t encval;
17-
18-
hal_gpio_pin_t pin_sw;
19-
uint8_t pin_sw_state;
20-
uint32_t pin_sw_last_change;
21-
22-
bool rotate_since_pressed;
23-
24-
ev_encoder_callback_t on_press;
25-
ev_encoder_callback_t on_rotate_ccw;
26-
ev_encoder_callback_t on_rotate_cw;
27-
ev_encoder_callback_t on_rotate_ccw_while_pressed;
28-
ev_encoder_callback_t on_rotate_cw_while_pressed;
29-
void *callback_param;
11+
typedef struct {
12+
hal_gpio_pin_t pin_a; // Also known as CLK
13+
hal_gpio_pin_t pin_b; // Also known as DT
14+
uint8_t old_AB;
15+
int8_t encval;
16+
17+
hal_gpio_pin_t pin_sw;
18+
uint8_t pin_sw_state;
19+
uint32_t pin_sw_last_change;
20+
21+
bool rotate_since_pressed;
22+
23+
ev_encoder_callback_t on_press;
24+
ev_encoder_callback_t on_rotate_ccw;
25+
ev_encoder_callback_t on_rotate_cw;
26+
ev_encoder_callback_t on_rotate_ccw_while_pressed;
27+
ev_encoder_callback_t on_rotate_cw_while_pressed;
28+
void * callback_param;
3029
} encoder_t;
3130

3231
void encoder_init(encoder_t *encoder);
3332

34-
#endif
33+
#endif

0 commit comments

Comments
 (0)