-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotary.cpp
116 lines (85 loc) · 2.65 KB
/
rotary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <myDebug.h>
#include "rotary.h"
#include "defines.h"
#include "expSystem.h"
#define DEBUG_ROTARY 0
typedef struct
{
int pollA; // the last value polled for the A part of the switch
int pinA; // the pin for A polling
int pinB; // the pin for B polling
int min_range; // the minimum value
int max_range; // the maximum value
float inc_dec; // the amount to inc or dec per signal
float value; // the current value
} rotary_t;
rotary_t rotary[NUM_ROTARY] =
{
{0, ROTARY_1A, ROTARY_1B, 0, 127, DEFAULT_INC_DEC, 0.00},
{0, ROTARY_2A, ROTARY_2B, 0, 127, DEFAULT_INC_DEC, 0.00},
{0, ROTARY_3A, ROTARY_3B, 0, 127, DEFAULT_INC_DEC, 0.00},
{0, ROTARY_4A, ROTARY_4B, 0, 127, DEFAULT_INC_DEC, 0.00}
};
void setRotaryValue(int num, int value)
// note that the stored value is a float
// but the returned value is a truncated int
{
if (value > rotary[num].max_range)
value = rotary[num].max_range;
if (value < rotary[num].min_range)
value = rotary[num].min_range;
rotary[num].value = value;
}
void setRotary(int num, int min_range, int max_range, float inc_dec)
{
rotary[num].min_range = min_range;
rotary[num].max_range = max_range;
rotary[num].inc_dec = inc_dec;
}
void initRotary()
{
for (int i=0; i<4; i++)
{
pinMode(rotary[i].pinA,INPUT_PULLDOWN);
pinMode(rotary[i].pinB,INPUT_PULLDOWN);
// init to current state
rotary[i].pollA = digitalRead(rotary[i].pinA);
}
}
int getRotaryValue(int i)
{
return rotary[i].value;
}
bool _pollRotary(int i)
{
int aval = digitalRead(rotary[i].pinA);
if (rotary[i].pollA == aval)
return false;
// only do something if A has changed
rotary[i].pollA = aval;
int bval = digitalRead(rotary[i].pinB);
if (aval == bval)
{
if (rotary[i].value + rotary[i].inc_dec > rotary[i].max_range)
rotary[i].value = rotary[i].max_range;
else
rotary[i].value += rotary[i].inc_dec;
}
else
{
if (rotary[i].value - rotary[i].inc_dec < rotary[i].min_range)
rotary[i].value = rotary[i].min_range;
else
rotary[i].value -= rotary[i].inc_dec;
}
#if DEBUG_ROTARY
display(0,"rotary(%d) aval=%d bval=%d value=%d",i,aval,bval,rotary_vslue[i]);
#endif
return true;
}
void pollRotary()
{
for (int i=0; i<NUM_ROTARY; i++)
if (_pollRotary(i))
theSystem.rotaryEvent(i,rotary[i].value);
}