-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrot.c
100 lines (82 loc) · 1.87 KB
/
rot.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include "rot.h"
#include "config.h"
#include "serial.h"
typedef enum {
SX00,
SL01,
SL11,
SL10,
SR10,
SR11,
SR01
} t_state;
typedef struct t_stateChange {
t_state srcState;
t_state dstState;
uint8_t input;
int step;
} t_stateChange;
t_stateChange stateTab[] = {
{SX00, SL01, 0b01, 0},
{SL01, SL11, 0b11, 0},
{SL11, SR10, 0b10, 1},
{SL10, SX00, 0b00, 0},
{SL01, SX00, 0b00, 0},
{SL11, SX00, 0b00, 0},
{SX00, SR10, 0b10, 0},
{SR10, SR11, 0b11, 0},
{SR11, SL01, 0b01, -1},
{SR01, SX00, 0b00, 0},
{SR10, SX00, 0b00, 0},
{SR11, SX00, 0b00, 0}
};
t_state state = SX00;
volatile int c = 0;
volatile int encoder_position = 0;
char b[20];
volatile uint8_t laststate;
t_state stateMachine(uint8_t input) {
uint8_t p;
for (p = 0; p < sizeof(stateTab) / sizeof(stateTab[0]); p++) {
if ((stateTab[p].srcState == state) && (stateTab[p].input == input)) {
state = stateTab[p].dstState;
encoder_position += stateTab[p].step;
break;
}
}
}
void doStateChange() {
stateMachine((!(ROTA_PINS & _BV(ROTA_PIN)) << 1) | !(ROTB_PINS & _BV(ROTB_PIN)));
}
ISR(ENCODER_INTA_VECT) {
c += 1;
doStateChange();
}
ISR(ENCODER_INTB_VECT) {
c += 1;
doStateChange();
}
void rot_init() {
serial_init();
ROTA_DDR &= ~_BV(ROTA_PIN);
ROTA_PORT |= _BV(ROTA_PIN);
ROTB_DDR &= ~_BV(ROTB_PIN);
ROTB_PORT |= _BV(ROTB_PIN);
serial_println("Hello");
ENCODER_INTA_SETUP();
ENCODER_INTB_SETUP();
serial_println(itoa(sizeof(stateTab) / sizeof(stateTab[0]), b, 10));
}
void rot_run() {
serial_print(itoa(!(ROTA_PINS & _BV(ROTA_PIN)), b, 10));
serial_print(itoa(!(ROTB_PINS & _BV(ROTB_PIN)), b, 10));
serial_print(" ");
serial_print(itoa(c, b, 10));
serial_print(" ");
serial_print(itoa(encoder_position, b, 10));
serial_println(" ");
_delay_ms(200);
}