Subversion Repositories DashDisplay

Rev

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