-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.cpp
165 lines (149 loc) · 3.88 KB
/
Controller.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "Controller.h"
//****************************************************************************************
Mux::Mux(byte outpin_, byte numPins_, bool analog_)
{
outpin = outpin_;
//enablepin = enablepin_;
numPins = numPins_;
analog = analog_;
if (analog == false) pinMode(outpin, INPUT_PULLUP);
//pinMode(enablepin, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
if (numPins > 8) pinMode(5, OUTPUT);
}
//****************************************************************************************
//Button (Pin Number, Command, Note Number, Channel, Debounce Time)
Button::Button(byte pin, byte command, byte value, byte channel, byte debounce)
{
_pin = pin;
pinMode(_pin, INPUT_PULLUP);
_value = value;
_command = command;
_debounce = debounce;
_time = 0;
_busy = false;
_status = 0b00000010;
_last = 1;
Bcommand = command;
Bvalue = value;
Bchannel = channel;
Btoggle = 0;
LedPin = (pin + 4);
pinMode(LedPin, OUTPUT);
}
Button::Button(Mux mux, byte muxpin, byte command, byte value, byte channel, byte debounce)
{
_pin = mux.outpin;
_numMuxPins = mux.numPins;
_muxpin = muxpin;
_value = value;
_command = command;
_debounce = debounce;
_time = 0;
_busy = false;
_status = 0b00000010;
_last = 1;
Bcommand = command;
Bvalue = value;
Bchannel = channel;
Btoggle = 0;
}
void Button::muxUpdate()
{
byte temp = _muxpin;
temp = temp << 2;
if (_numMuxPins > 8) PORTD = PORTD & B11000011;
else PORTD = PORTD & B11100011;
PORTD = PORTD | temp;
}
byte Button::getValue()
{
// If BUSY bit not set - read button
if (bitRead(_status, 0) == false) { // If busy false
if (digitalRead(_pin) == _last) return 2; // If same as last state - exit
}
// If NEW Bit set - Key just pressed, record time
if (bitRead(_status, 1) == true) { // If new is true
bitSet(_status, 0); // Set busy TRUE
bitClear(_status, 1); // Set New FALSE
_time = millis();
return 255;
}
// Check if debounce time has passed - If no, exit
if (millis() - _time < _debounce) return 255;
// Debounce time has passed. Read pin to see if still set the same
// If it has changed back - assume false alarm
if (digitalRead(_pin) == _last) {
bitClear(_status, 0); // Set busy false
bitSet(_status, 1); // Set new true
return 255;
}
// If this point is reached, event is valid. return event type
else {
bitClear(_status, 0); // Set busy false
bitSet(_status, 1); // Set new true
_last = ((~_last) & 0b00000001); // invert _last
return _last;
}
}
void Button::newValue(byte command, byte value, byte channel)
{
Bvalue = value;
Bcommand = command;
Bchannel = channel;
}
//********************************************************************
Pot::Pot(byte pin, byte command, byte control, byte channel)
{
_pin = pin;
_control = control;
_value = analogRead(_pin);
_value = _value >> 3;
_oldValue = _value << 3;
_value = _value << 3;
Pcommand = command;
Pcontrol = control;
Pchannel = channel;
}
void Pot::muxUpdate()
{
byte temp = _muxpin;
temp = temp << 2;
if (_numMuxPins > 8) PORTD = PORTD & B11000011;
else PORTD = PORTD & B11100011;
//PORTD = PORTD & B11000011;
PORTD = PORTD | temp;
}
Pot::Pot(Mux mux, byte muxpin, byte command, byte control, byte channel)
{
_pin = mux.outpin;
_numMuxPins = mux.numPins;
_muxpin = muxpin;
_control = control;
muxUpdate();
_value = analogRead(_pin);
_value = _value >> 3;
_oldValue = _value << 3;
_value = _value << 3;
Pcommand = command;
Pcontrol = control;
Pchannel = channel;
}
byte Pot::getValue()
{
_value = analogRead(_pin);
int tmp = (_oldValue - _value);
if (tmp >= 8 || tmp <= -8) {
_oldValue = _value >> 3;
_oldValue = _oldValue << 3;
return _value >> 3;
}
return 255;
}
void Pot::newValue(byte command, byte value, byte channel) {
Pcommand = command;
Pcontrol = value;
Pchannel = channel;
}