Rev 4 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2 | mjames | 1 | #include "stm32f1xx_hal.h" |
2 | |||
3 | #include "switches.h" |
||
4 | |||
5 | |||
6 | typedef enum { |
||
7 | PH_00 = 0, PH_10 = 2, PH_11 = 3, PH_01 = 1, |
||
8 | } eGreyCode; |
||
9 | |||
10 | typedef struct { |
||
11 | GPIO_TypeDef * GPIO; |
||
12 | uint16_t Pin; |
||
13 | } sGPIOPin; |
||
14 | |||
15 | volatile int dial_pos[MAX_DIALS]; |
||
16 | volatile int dial_last[MAX_DIALS]; // previous debounced switch position |
||
17 | |||
18 | volatile int push_pos[MAX_PUSHBUTTONS]; |
||
19 | |||
20 | volatile static int sw_newstate[MAX_SWITCHES]; //< debounced switch state |
||
21 | volatile static int sw_count[MAX_SWITCHES]; //< debounce counter |
||
22 | |||
23 | static const sGPIOPin sw_arr[MAX_SWITCHES] = |
||
24 | { { SW1_I_GPIO_Port, SW1_I_Pin }, // these two pins used to encode SW1 phase |
||
25 | { SW1_Q_GPIO_Port, SW1_Q_Pin }, // " SW1 phase |
||
26 | { SW2_I_GPIO_Port, SW2_I_Pin }, // these two pins used to encode SW2 phase |
||
27 | { SW2_Q_GPIO_Port, SW2_Q_Pin }, // " SW2 phase |
||
28 | { SW1_PUSH_GPIO_Port, SW1_PUSH_Pin }, // two debounced switches |
||
29 | { SW2_PUSH_GPIO_Port, SW2_PUSH_Pin }, }; |
||
30 | |||
31 | // call this to setup switch states |
||
32 | void InitSwitches(void) { |
||
33 | int i; |
||
34 | |||
35 | for (i = 0; i < MAX_SWITCHES; i++) { |
||
36 | sw_newstate[i] = 0; |
||
37 | sw_count[i] = 0; |
||
38 | } |
||
39 | |||
40 | // reset the dial positions |
||
41 | dial_pos[0] = 0; |
||
42 | dial_pos[1] = 1; |
||
43 | } |
||
44 | |||
45 | // this gray encodes the dial . |
||
46 | void EncodeDial(int i) { |
||
47 | if (i < MAX_DIALS && i >= 0) { |
||
48 | int sw_index = i << 1; |
||
49 | // dial 0 uses switches 0 and 1 |
||
50 | // dial 1 uses switches 2 and 3 |
||
51 | int current = sw_newstate[sw_index] | (sw_newstate[sw_index + 1] << 1); |
||
52 | switch (dial_last[i]) { |
||
53 | case PH_00: |
||
54 | if (current == PH_10) |
||
55 | dial_pos[i]++; |
||
56 | if (current == PH_01) |
||
57 | dial_pos[i]--; |
||
58 | break; |
||
59 | case PH_10: |
||
60 | if (current == PH_11) |
||
61 | dial_pos[i]++; |
||
62 | if (current == PH_00) |
||
63 | dial_pos[i]--; |
||
64 | break; |
||
65 | case PH_11: |
||
66 | if (current == PH_01) |
||
67 | dial_pos[i]++; |
||
68 | if (current == PH_10) |
||
69 | dial_pos[i]--; |
||
70 | break; |
||
71 | case PH_01: |
||
72 | if (current == PH_00) |
||
73 | dial_pos[i]++; |
||
74 | if (current == PH_11) |
||
75 | dial_pos[i]--; |
||
76 | break; |
||
77 | default: |
||
78 | break; |
||
79 | } |
||
80 | dial_last[i] = current; |
||
81 | } |
||
82 | |||
83 | } |
||
84 | |||
85 | // this is the interrupt handler , dealling with the switches |
||
86 | void HandleSwitches(void) |
||
87 | { |
||
88 | |||
89 | int i; |
||
90 | |||
91 | for (i = 0; i < MAX_SWITCHES; i++) |
||
92 | { |
||
93 | |||
94 | int sw = HAL_GPIO_ReadPin(sw_arr[i].GPIO, sw_arr[i].Pin) == GPIO_PIN_RESET; |
||
95 | |||
96 | // bouncing, reset counter |
||
97 | if (sw != sw_newstate[i]) |
||
98 | { |
||
99 | sw_count[i] = 0; |
||
100 | } |
||
101 | else |
||
102 | { |
||
103 | // count up to debounce time |
||
104 | if (sw_count[i] < DEBOUNCE_TIME) |
||
105 | { |
||
106 | sw_count[i]++; |
||
107 | |||
108 | if (sw_count[i] == DEBOUNCE_TIME) |
||
109 | { |
||
110 | sw_newstate[i] = sw; |
||
111 | } |
||
112 | } |
||
113 | } |
||
114 | } |
||
115 | // we now have debounced switch states in sw_newstate . So we can go ahead and feed some of the |
||
116 | |||
117 | EncodeDial(0); |
||
118 | EncodeDial(1); |
||
119 | |||
120 | } |
||
121 | |||
122 | void zero_dial(int dial) { |
||
123 | if(dial>=0 && dial < MAX_DIALS) |
||
124 | dial_pos[dial]= 0; |
||
125 | } |
||
126 |