-
Notifications
You must be signed in to change notification settings - Fork 0
/
dlparduino.ino
219 lines (205 loc) · 5.39 KB
/
dlparduino.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
dlparduino : DPL-IO8 Compatible firmware/sketch for Arduino-like boards.
Copyright 2016 Romain Rossi <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
const int ledPin = 13; // the pin that the LED is attached to
char in; //incomming byte
struct channel_s
{
int pin;
int analog;
};
// Array to have association between channel number and pin number
// current pinout is for arduino nano : uses A0-A7 pins + 2 and 3
// A6 and A7 pins are input-only (on arduino nano board) so should be externally connected to 3 and 2 repectively to provides digital in/out function
const struct channel_s channels[8] =
{
{
.pin = A0,
.analog = A0
}
, {
.pin = A1,
.analog = A1
}
, {
.pin = A2,
.analog = A2
}
, {
.pin = A3,
.analog = A3
}
, {
.pin = A4,
.analog = A4
}
, {
.pin = A5,
.analog = A5
}
, {
.pin = 3,
.analog = A6
}
, {
.pin = 2,
.analog = A7
}
};
// Configure the pin as a digital output and set the ping level
void digitalout( const struct channel_s chan, const char val )
{
pinMode( chan.pin, OUTPUT );
digitalWrite( chan.pin, val );
}
// Configure the pin as digital input, read the level and send it to the uart
void digitalin( const struct channel_s chan )
{
pinMode( chan.pin, INPUT );
int val = digitalRead( chan.pin );
Serial.print( val );
}
// Configure the pin as analog input, read the value and sent it to the uart
void analogin( const struct channel_s chan )
{
pinMode( chan.pin, INPUT );
int ana_in = analogRead( chan.analog );
// map it to centivolts:
int ana_in_v = map( ana_in, 0, 1023, 0, 500 );
Serial.print( ana_in_v );
}
void setup()
{
Serial.begin( 115200 );
// initialize the LED pin as an output:
//pinMode( ledPin, OUTPUT );
analogReference( DEFAULT );
}
void loop()
{
if( Serial.available() > 0 )
{
int channel = -1;
// read the oldest byte in the serial buffer:
in = Serial.read();
// which channel [1 to 8]?
switch( in )
{
case '1':
case 'Q':
case 'A':
case 'Z':
channel = 1;
break;
case '2':
case 'W':
case 'S':
case 'X':
channel = 2;
break;
case '3':
case 'E':
case 'D':
case 'C':
channel = 3;
break;
case '4':
case 'R':
case 'F':
case 'V':
channel = 4;
break;
case '5':
case 'T':
case 'G':
case 'B':
channel = 5;
break;
case '6':
case 'Y':
case 'H':
case 'N':
channel = 6;
break;
case '7':
case 'U':
case 'J':
case 'M':
channel = 7;
break;
case '8':
case 'I':
case 'K':
case ',':
channel = 8;
break;
default:
channel = -1;
break;
}
// which operation ?
if( channel > 0 )
{
switch( in )
{
// DigitalOut HIGH
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
digitalout( channels[channel - 1], HIGH );
break;
// DigitalOut LOW
case 'Q':
case 'W':
case 'E':
case 'R':
case 'T':
case 'Y':
case 'U':
case 'I':
digitalout( channels[channel - 1], LOW );
break;
// Digital IN
case 'A':
case 'S':
case 'D':
case 'F':
case 'G':
case 'H':
case 'J':
case 'K':
digitalin( channels[channel - 1] );
break;
// Analog IN
case 'Z':
case 'X':
case 'C':
case 'V':
case 'B':
case 'N':
case 'M':
case ',':
analogin( channels[channel - 1] );
break;
default:
break;
}
}
}
}