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