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