This repository has been archived by the owner on Aug 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpectralSensorAS726X_Processing.pde
178 lines (139 loc) · 5.24 KB
/
SpectralSensorAS726X_Processing.pde
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
/*
SpectralSensorAS726 Processing
Copyright (C) 2018 Christian Rickert
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 2 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, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
// imports
import java.nio.*;
import processing.serial.*;
import controlP5.*;
// constants
ControlP5 cp5;
static final int cr = 13; // DEC integer value of the carriage return character
static final int lf = 10; // DEC integer value of the line feed character
static final int tb = 9; // DEC integer value of the tabulator character
static final int bf = 4; // number of bytes expected per data value
static final int nv = 7; // number of expected values per data transmission
static final int nb = bf * nv + 2; // number of bytes expected per data transmission (handshake + data)
// variables
byte[] offBuffer = new byte[4096]; // buffer for offset correction, maximum size
byte[] inBuffer = new byte[nb]; // buffer for data transmission, data transmission size
float value = 0.0; // single sensor reading
String[] Sliders = {" V [450 nm]", " B [500 nm]", " G [550 nm]", " Y [570 nm]", " O [600 nm]", " R [650 nm]", " T [°C]"};
String[] values = {}; // all sensor readings
// configure serial port
Serial serialPort = new Serial(this, "COM3", 115200);
// functions
void ONLINE() {
cp5.getController("ONLINE").setColorForeground(color(255, 0, 0));
dispose();
}
void RESET() {
cp5.getController("ONLINE").setColorForeground(color(100, 100, 100));
for (int s=0; s<(nv-1); s+=1)
cp5.getController(Sliders[s]).setMax(5.0);
}
void receiveSensorData() {
// wait for data block
while (serialPort.available() < nb)
delay(1);
// correct data offset, look for carriage return character
serialPort.readBytesUntil(cr, offBuffer);
// begin data processing, end with line feed character
serialPort.readBytesUntil(lf, inBuffer);
for (int b=0; b<(nb-2); b+=bf) {
value = ByteBuffer.wrap(inBuffer).order(ByteOrder.LITTLE_ENDIAN).getFloat(b);
if (value > cp5.getController(Sliders[b/bf]).getMax())
for (int s=0; s<(nv-1); s+=1)
cp5.getController(Sliders[s]).setMax(value);
cp5.getController(Sliders[b/bf]).setValue(value);
print(value); print(" ");
}
println();
// confirm data transmission, return tabulator character
cp5.getController("ONLINE").setColorForeground(color(0, 255, 0));
serialPort.write(tb);
}
void setup() {
while (serialPort.available() > 0) // clear (pull) read buffer
serialPort.read();
serialPort.clear(); // clear (push) write buffer
size(850, 600); // canvas size
background(color(64, 64, 64));
cp5 = new ControlP5(this); // constructor
cp5.addBang("ONLINE")
.setPosition(50, 50)
.setSize(50, 50)
.setColorForeground(color(100, 100, 100));
cp5.addBang("RESET")
.setPosition(50, 150)
.setSize(50, 50)
.setColorForeground(color(100, 100, 100));
cp5.addSlider(Sliders[0])
.setPosition(150, 50)
.setSize(50, 500)
.setRange(0, 5)
.setValue(1)
.setColorForeground(color(98, 0, 255))
.setColorBackground(color(128,128,128));
cp5.addSlider(Sliders[1])
.setPosition(250, 50)
.setSize(50, 500)
.setRange(0, 5)
.setValue(1)
.setColorForeground(color(0, 236, 167))
.setColorBackground(color(128,128,128));
cp5.addSlider(Sliders[2])
.setPosition(350, 50)
.setSize(50, 500)
.setRange(0, 5)
.setValue(1)
.setColorForeground(color(0, 255, 0))
.setColorBackground(color(128,128,128));
cp5.addSlider(Sliders[3])
.setPosition(450, 50)
.setSize(50, 500)
.setRange(0, 5)
.setValue(1)
.setColorForeground(color(237, 255, 0))
.setColorBackground(color(128,128,128));
cp5.addSlider(Sliders[4])
.setPosition(550, 50)
.setSize(50, 500)
.setRange(0, 5)
.setValue(1)
.setColorForeground(color(255, 114, 0))
.setColorBackground(color(128,128,128));
cp5.addSlider(Sliders[5])
.setPosition(650, 50)
.setSize(50, 500)
.setRange(0, 5)
.setValue(1)
.setColorForeground(color(221, 0, 0))
.setColorBackground(color(128,128,128));
cp5.addSlider(Sliders[6])
.setPosition(750, 50)
.setSize(50, 500)
.setRange(-40, 85)
.setValue(-27.5)
.setColorForeground(color(255, 255, 255))
.setColorBackground(color(128,128,128));
}
void draw() { // main loop
receiveSensorData();
}
void dispose() { // finally
print("Stopping ...");
serialPort.stop();
println(" done.");
}