Rev 49 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 49 | Rev 50 | ||
---|---|---|---|
Line 1... | Line 1... | ||
1 | #include "stm32l1xx_hal.h" |
1 | #include "stm32l1xx_hal.h" |
2 | 2 | ||
3 | #include "switches.h" |
3 | #include "switches.h" |
- | 4 | #include "main.h" |
|
4 | 5 | ||
5 | extern TIM_HandleTypeDef htim3; |
- | |
6 | extern TIM_HandleTypeDef htim9; |
6 | typedef enum |
7 | 7 | { |
|
8 | - | ||
9 | typedef enum { |
- | |
10 | PH_00 = 0, PH_10 = 2, PH_11 = 3, PH_01 = 1, |
8 | PH_00 = 0, PH_10 = 2, PH_11 = 3, PH_01 = 1, |
11 | } eGreyCode; |
9 | } eGreyCode; |
12 | 10 | ||
13 | typedef struct { |
11 | typedef struct |
- | 12 | { |
|
14 | GPIO_TypeDef * GPIO; |
13 | GPIO_TypeDef *GPIO; |
15 | uint16_t Pin; |
14 | uint16_t Pin; |
16 | uint16_t Debounce; |
15 | uint16_t Debounce; |
17 | } sGPIOPin; |
16 | } sGPIOPin; |
18 | 17 | ||
19 | - | ||
20 | volatile int push_pos[MAX_PUSHBUTTONS]; |
18 | volatile int push_pos[MAX_PUSHBUTTONS]; |
21 | volatile int sw_count[MAX_PUSHBUTTONS]; //< debounced switch state |
19 | volatile int sw_count[MAX_PUSHBUTTONS]; //< debounced switch state |
22 | volatile int sw_newstate[MAX_PUSHBUTTONS]; //< debounced switch state |
20 | volatile int sw_newstate[MAX_PUSHBUTTONS]; //< debounced switch state |
23 | 21 | ||
24 | volatile int dial_count[MAX_DIALS]; |
22 | volatile int dial_count[MAX_DIALS]; |
25 | volatile int dial_pos[MAX_DIALS]; |
23 | volatile int dial_pos[MAX_DIALS]; |
26 | volatile int dial_last[MAX_DIALS]; // previous debounced switch position |
24 | volatile int dial_last[MAX_DIALS]; // previous debounced switch position |
27 | 25 | ||
28 | static const sGPIOPin sw_arr[MAX_PUSHBUTTONS] = |
26 | static const sGPIOPin sw_arr[MAX_PUSHBUTTONS] = |
29 | { |
27 | { |
30 | { SW1_PUSH_GPIO_Port, SW1_PUSH_Pin, 10 }, // two debounced switches |
28 | { SW1_PUSH_GPIO_Port, SW1_PUSH_Pin, 10 }, // two debounced switches |
31 | { SW2_PUSH_GPIO_Port, SW2_PUSH_Pin, 10 }, |
29 | { SW2_PUSH_GPIO_Port, SW2_PUSH_Pin, 10 }, }; |
32 | }; |
- | |
33 | 30 | ||
34 | // call this to setup switch states |
31 | // call this to setup switch states |
- | 32 | void |
|
35 | void InitSwitches(void) { |
33 | InitSwitches (void) |
- | 34 | { |
|
36 | int i; |
35 | int i; |
37 | 36 | ||
38 | for (i = 0; i < MAX_PUSHBUTTONS; i++) { |
37 | for (i = 0; i < MAX_PUSHBUTTONS; i++) |
- | 38 | { |
|
39 | sw_newstate[i] = 0; |
39 | sw_newstate[i] = 0; |
40 | sw_count[i] = 0; |
40 | sw_count[i] = 0; |
41 | push_pos[i] = 0; |
41 | push_pos[i] = 0; |
42 | } |
42 | } |
43 | // reset the dial positions |
43 | // reset the dial positions |
44 | for(i=0; i< MAX_DIALS; i++) |
44 | for (i = 0; i < MAX_DIALS; i++) |
45 | { |
45 | { |
46 | dial_pos[i] = 0; |
46 | dial_pos[i] = 0; |
- | 47 | } |
|
- | 48 | dial_count[0] = __HAL_TIM_GET_COUNTER(&htim9) >> 2; |
|
- | 49 | dial_count[1] = __HAL_TIM_GET_COUNTER(&htim3) >> 2; |
|
47 | } |
50 | } |
48 | dial_count[0] = __HAL_TIM_GET_COUNTER(&htim9) >> 2; |
- | |
49 | dial_count[1] = __HAL_TIM_GET_COUNTER(&htim3) >> 2; |
- | |
50 | } |
- | |
51 | - | ||
52 | 51 | ||
53 | // this is the interrupt handler , dealling with the switches |
52 | // this is the interrupt handler , dealling with the switches |
- | 53 | void |
|
54 | void HandleSwitches(void) |
54 | HandleSwitches (void) |
55 | { |
55 | { |
56 | 56 | ||
57 | int i; |
57 | int i; |
58 | 58 | ||
59 | for (i = 0; i < MAX_PUSHBUTTONS; i++) |
59 | for (i = 0; i < MAX_PUSHBUTTONS; i++) |
60 | { |
60 | { |
61 | 61 | ||
62 | int sw = HAL_GPIO_ReadPin(sw_arr[i].GPIO, sw_arr[i].Pin) == GPIO_PIN_RESET; |
62 | int sw = HAL_GPIO_ReadPin (sw_arr[i].GPIO, sw_arr[i].Pin) |
- | 63 | == GPIO_PIN_RESET; |
|
63 | 64 | ||
64 | // bouncing, reset counter |
65 | // bouncing, reset counter |
65 | if (sw == sw_newstate[i]) |
66 | if (sw == sw_newstate[i]) |
66 | { |
67 | { |
67 | sw_count[i] = 0; |
68 | sw_count[i] = 0; |
68 | } |
69 | } |
69 | else |
70 | else |
70 | { |
71 | { |
71 | // count up to debounce time |
72 | // count up to debounce time |
72 | if (sw_count[i] < sw_arr[i].Debounce) |
73 | if (sw_count[i] < sw_arr[i].Debounce) |
73 | { |
74 | { |
74 | sw_count[i]++; |
75 | sw_count[i]++; |
75 | 76 | ||
76 | if (sw_count[i] == sw_arr[i].Debounce) |
77 | if (sw_count[i] == sw_arr[i].Debounce) |
77 | { |
78 | { |
78 | sw_newstate[i] = sw; |
79 | sw_newstate[i] = sw; |
79 | } |
80 | } |
- | 81 | } |
|
80 | } |
82 | } |
81 | } |
83 | } |
82 | } |
- | |
83 | push_pos[0] =sw_newstate[0]; |
84 | push_pos[0] = sw_newstate[0]; |
84 | push_pos[1] =sw_newstate[1]; |
85 | push_pos[1] = sw_newstate[1]; |
85 | 86 | ||
86 | int cnt0 = __HAL_TIM_GET_COUNTER(&htim9) >> 2; // counts 4 per step |
87 | int cnt0 = __HAL_TIM_GET_COUNTER(&htim9) >> 2; // counts 4 per step |
87 | int cnt1 = __HAL_TIM_GET_COUNTER(&htim3) >> 2; // counts 4 per step |
88 | int cnt1 = __HAL_TIM_GET_COUNTER(&htim3) >> 2; // counts 4 per step |
88 | 89 | ||
89 | // always count up, or count down to zero but dont wrap |
90 | // always count up, or count down to zero but dont wrap |
90 | 91 | ||
91 | if ((dial_pos[0] != 0) || (cnt0 > dial_count[0])) |
92 | if ((dial_pos[0] != 0) || (cnt0 > dial_count[0])) |
92 | dial_pos[0] += cnt0 - dial_count[0]; |
93 | dial_pos[0] += cnt0 - dial_count[0]; |
93 | dial_count[0] = cnt0; |
94 | dial_count[0] = cnt0; |
94 | 95 | ||
95 | if ((dial_pos[1] != 0) || (cnt1 > dial_count[1])) |
96 | if ((dial_pos[1] != 0) || (cnt1 > dial_count[1])) |
96 | dial_pos[1] += cnt1 - dial_count[1]; |
97 | dial_pos[1] += cnt1 - dial_count[1]; |
97 | dial_count[1] = cnt1; |
98 | dial_count[1] = cnt1; |
98 | 99 | ||
99 | } |
100 | } |
100 | 101 | ||
101 | - |