Subversion Repositories DashDisplay

Rev

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