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