-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPluino.ino
169 lines (150 loc) · 3.34 KB
/
Pluino.ino
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
165
166
167
168
169
#define PINS_COUNT 70
#define FIXED_TIME 5
#define IS_PWM_PIN(x) (((x) > 1) || ((x) < 13))
#define IS_NO_PIN(x) (((x) == 0) || ((x) == 1))
#define ANALOG_START 54
#define ANALOG_END 69
class PinState
{
public:
PinState()
{
m_isInput = false;
m_isOutput = false;
}
void setAsInput()
{
m_isInput = true;
m_isOutput = false;
}
void setAsOutput()
{
m_isOutput = true;
m_isInput = false;
}
void setAsNull()
{
m_isInput = false;
m_isOutput = false;
}
inline bool isOutput() const { return m_isOutput; }
inline bool isInput() const { return m_isInput; }
inline bool isNull() const { return !m_isInput && !m_isOutput; }
private:
bool m_isInput;
bool m_isOutput;
};
PinState pinsStates[PINS_COUNT];
unsigned long nowClk = 0;
unsigned long lastClk = 0;
unsigned long delta = 0;
unsigned long diff = 0;
byte readInstruction[3];
byte readCount = 0;
byte readData = 0;
unsigned short readVal = 0;
void setup() {
//Start Serial Communication
Serial.begin(9600);
}
void ProcessInstruction()
{
//Get Data from instruction
byte code = readInstruction[0];
byte arg1 = readInstruction[1];
byte arg2 = readInstruction[2];
//Check for pin ranges
if ((arg1 >= PINS_COUNT || IS_NO_PIN(arg1)) && code != 'c')
return;
//Perform Instruction
switch (code)
{
//Set pin as output
case 'o':
pinMode(arg1, OUTPUT);
pinsStates[arg1].setAsOutput();
break;
//Set pin as input
case 'i':
pinMode(arg1, (arg2 == 0 ? INPUT : INPUT_PULLUP));
pinsStates[arg1].setAsInput();
break;
//Set pin as null
case 'n':
pinMode(arg1, INPUT);
pinsStates[arg1].setAsNull();
break;
//Set pin output
case 'w':
if (pinsStates[arg1].isOutput())
{
if (IS_PWM_PIN(arg1))
analogWrite(arg1, arg2 > 255 ? 255 : arg2);
else
digitalWrite(arg1, arg2 > 127 ? 1 : 0);
}
break;
//Clear pins
//Set all pins to null
case 'c':
for (int i = 0; i < PINS_COUNT; i++)
{
if (!pinsStates[i].isNull())
{
digitalWrite(i, LOW);
pinMode(i, INPUT);
pinsStates[i].setAsNull();
}
}
break;
}
}
void loop() {
//Get delta value
nowClk = millis();
diff = nowClk - lastClk;
delta += diff;
//Read Data from Serial
while (Serial.available() > 0)
{
readData = Serial.read();
switch (readData)
{
//Start of command
case '-':
readCount = 0;
break;
//End of command
case ';':
if (readCount == 3)
ProcessInstruction();
break;
//Instruction data
default:
if (readCount == 3)
readCount = 0;
readInstruction[readCount++] = readData;
break;
}
}
//Compare delta to FIXED_TIME
if (delta >= FIXED_TIME)
{
//Get Input
for (int index = 0; index < PINS_COUNT; index++)
{
if (pinsStates[index].isInput())
{
readVal = ((index >= ANALOG_START && index <= ANALOG_END) ? analogRead(index - ANALOG_START) : (digitalRead(index) ? 1023 : 0));
Serial.write('-');
Serial.write(index);
Serial.write((byte)(readVal & 255));
Serial.write((byte)((readVal >> 8) & 3));
Serial.write(';');
}
}
delta = 0;
}
//Reset delta
lastClk = nowClk;
}