Subversion Repositories DashDisplay

Rev

Rev 66 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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