-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.cpp
73 lines (68 loc) · 1.66 KB
/
State.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
#include "State.h"
#include <Arduino.h>
State::State()
{
previousReturnValues[0] = 0;
previousReturnValues[1] = 0;
previousReturnValues[2] = 0;
previousReturnValues[3] = 0;
previousReturnValues[4] = 0;
previousReturnValues[5] = 0;
previousReturnValues[6] = 0;
previousReturnValues[7] = 0;
}
int State::updateValue(int valueIndex)
{
values[valueIndex] = analogRead(pinID);
if (binary[valueIndex])
{
int temp;
if(previousReturnValues[valueIndex] == 0) // button is in up-state and can detect down
temp = ((values[valueIndex] > 150) ? 127 : 0);
else
temp = ((values[valueIndex] < 50) ? 0 : 127);
if (temp != previousReturnValues[valueIndex])
{
previousReturnValues[valueIndex] = temp;
return temp;
}
else
return -1;
}else
{
if ((values[valueIndex] < (ccToLevel(previousReturnValues[valueIndex]) - 2)) || (values[valueIndex] > (ccToLevel(previousReturnValues[valueIndex] + 1) + 1)))
return previousReturnValues[valueIndex] = levelToCC(values[valueIndex]);
else
return -1;
}
}
int State::levelToCC(int level) // custom mapping of voltage levels 0-1023 to CC values 0-127
{
if (level < 3)
return 0;
else if (level > 1020)
return 127;
else
return (1 + (level - 3) / 8.095);
}
// turn this into getLowerEdge and getUpperEdge
int State::ccToLevel(int cc) // custom mapping of cc values 0-127 to voltage levels 0-1023 to improve the start and end point of the potentiometers
{
if (cc == 0)
return 0;
else if (cc == 127)
return 1020;
else
return ((cc - 1) * 8.095 + 3);
}
void State::setBinary(bool bin)
{
for (int i = 0; i < 8; i++)
{
binary[i] = bin;
}
}
void State::setBinary(bool bin, int index)
{
binary[index] = bin;
}