-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsympatique.pde
237 lines (197 loc) · 5.09 KB
/
sympatique.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Oscilloscope
* Gives a visual rendering of analog pin 0 in realtime.
*
* This project is part of Accrochages
* See http://accrochages.drone.ws
*
* (c) 2015 David Trimoulet ([email protected])
* Modified for Exia.Cesi school.
* v1.2 Patch for Processing 3.xx
*
* (c) 2013 Vincent Levorato ([email protected])
* Modified for Exia.Cesi school.
* v1.1
*
* (c) 2008 Sofian Audry ([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/>.
*/
import java.io.*;
import processing.serial.*;
Serial port; // Create object from Serial class
int touch;
int val; // Data received from the serial port
int[] values;
float zoom;
File f = new File ("var.txt");
int refresh_mean=0;
float cur_mean=0.0;
void settings(){
size(800, 600);
}
void setup()
{
// Open the port that the board is connected to and use the same speed
port = new Serial(this, "COM8", 115200);
values = new int[width];
zoom = 1.0f;
smooth();
rectMode(CORNER);
}
int getY(int val) {
return (int)(height - val / 1023.0f * (height - 1));
}
int getValue() {
int value = -1;
while (port.available() >= 3) {
if (port.read() == 0xff) {
value = (port.read() << 8) | (port.read());
}
}
return value;
}
void pushValue(int value) {
for (int i=0; i<width-1; i++)
values[i] = values[i+1];
values[width-1] = value;
}
void drawLines() {
stroke(255);
int displayWidth = (int) (width / zoom);
int k = values.length - displayWidth;
int x0 = 0;
int y0 = getY(values[k]);
for (int i=1; i<displayWidth; i++) {
k++;
int x1 = (int) (i * (width-1) / (displayWidth-1));
int y1 = getY(values[k]);
line(x0, y0, x1, y1);
x0 = x1;
y0 = y1;
}
}
void drawGrid() {
stroke(255, 0, 0);
line(30, height/2, width, height/2);
line(30, 1, width, 1);
line(30, height-1, width, height-1);
float mid=2.5,b=0.0;
fill(255,0,0);
b=mid;
text(str(b)+"V", 0, height/2 + 5);
b=mid*2;
text(str(b)+"V", 0, 10 );
b=0;
text(str(b)+"V", 0, height );
stroke(0,255,0);
line(40, height*1/4, width, height*1/4);
line(40, height*3/4, width, height*3/4);
stroke(0,128,0);
line(50, height*1/8, width, height*1/8);
line(50, height*3/8, width, height*3/8);
line(50, height*5/8, width, height*5/8);
line(50, height*7/8, width, height*7/8);
fill(0,255,0);
b=mid+mid/2;
text(str(b)+"V", 0, height*1/4 + 5);
b=mid/2;
text(str(b)+"V", 0, height*3/4 + 5);
fill(0,128,0);
b=mid+mid/2+mid/4;
text(str(b)+"V", 0, height*1/8 + 5);
b=mid+mid/4;
text(str(b)+"V", 0, height*3/8 + 5);
b=mid-mid/2+mid/4;
text(str(b)+"V", 0, height*5/8 + 5);
b=mid-mid/2-mid/4;
text(str(b)+"V", 0, height*7/8 + 5);
}
void drawValue(float v)
{
//draw rectangle background for value
fill(0);
rect(width-100, height-20,100, 20);
if(v>2.8){touch=0;}
if(v<2.8 && v>2.76 ){touch=1;}
if(v<2.759 && v>2.735 ){touch=2;}
if(v<2.734){touch=3;}
//print volt value
String s = str(v)+"V";
fill(0,255,0);
text(s, width-100, height);
try{
PrintWriter writer = new PrintWriter(new FileOutputStream(new File("var.txt")));
writer.println(touch);
writer.close();
}catch(Exception E){
E.printStackTrace();
}
}
void drawMean(boolean r)
{
if(r)
{
float mean=0;
for (int i=0; i<width-1; i++)
mean=mean+values[i];
mean=mean/width;
cur_mean=mean/1023.0f*5; //scale
//2-digits precision
cur_mean=cur_mean*100000;
cur_mean=round(cur_mean);
cur_mean=cur_mean/100000;
}
//draw rectangle background for mean value
fill(0);
rect(width-100, height-40,100, 20);
//print mean volt value
String s = "Mean:"+str(cur_mean)+"V";
fill(255,255,0);
text(s, width-100, height-20);
}
void keyReleased() {
switch (key) {
case '+':
zoom *= 2.0f;
println(zoom);
if ( (int) (width / zoom) <= 1 )
zoom /= 2.0f;
break;
case '-':
zoom /= 2.0f;
if (zoom < 1.0f)
zoom *= 2.0f;
break;
}
}
void draw()
{
background(0);
drawLines();
drawGrid();
val = getValue();
if (val != -1) {
pushValue(val);
drawValue(val/1023.0f*5);
if(refresh_mean % 100 == 0)
{
refresh_mean=0;
drawMean(true);
}
else
drawMean(false);
}
refresh_mean++;
}