-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
302 lines (238 loc) · 8.94 KB
/
sketch.js
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
const serviceUuid = "555a0002-0000-467a-9538-01f0652c74e8";
const characteristicsUUID = {
prox: "555a0002-0017-467a-9538-01f0652c74e8",
gyro: "555a0002-0012-467a-9538-01f0652c74e8",
acce: "555a0002-0011-467a-9538-01f0652c74e8",
rgba: "555a0002-0018-467a-9538-01f0652c74e8",
};
let myBLE;
let gyroCharacteristic;
let acceCharacteristic;
let proxCharacteristic;
let rgbaCharacteristic;
let gyroValue = new Array(3);
let acceValue = new Array(3);
let proxValue = 0;
let rgbaValue = new Array(4);
let sensorArray = new Array(11);
let cnv;
let screenWidth;
let screenHeight;
let controlMenuWidth = 25 / 100;
let shapes;
let selected3D = "box";
let shapeSelection = document.querySelector("#dropdown-3d");
let xRotationSelection = document.querySelector("#dropdown-xRotation");
let yRotationSelection = document.querySelector("#dropdown-yRotation");
let zRotationSelection = document.querySelector("#dropdown-zRotation");
let xStretchSelection = document.querySelector("#dropdown-xStretch");
let yStretchSelection = document.querySelector("#dropdown-yStretch");
let zStretchSelection = document.querySelector("#dropdown-zStretch");
let xTranslationSelection = document.querySelector("#dropdown-xTranslation");
let yTranslationSelection = document.querySelector("#dropdown-yTranslation");
let zTranslationSelection = document.querySelector("#dropdown-zTranslation");
let xRotationIndex;
let yRotationIndex;
let zRotationIndex;
let xStretchIndex;
let yStretchIndex;
let zStretchIndex;
let xTranslationIndex;
let yTranslationIndex;
let zTranslationIndex;
shapeSelection.addEventListener("change", () => {
selected3D = shapeSelection.options[shapeSelection.selectedIndex].value;
});
xRotationSelection.addEventListener("change", () => {
xRotationIndex =
xRotationSelection.options[xRotationSelection.selectedIndex].value;
});
yRotationSelection.addEventListener("change", () => {
yRotationIndex =
yRotationSelection.options[yRotationSelection.selectedIndex].value;
});
zRotationSelection.addEventListener("change", () => {
zRotationIndex =
zRotationSelection.options[zRotationSelection.selectedIndex].value;
});
xStretchSelection.addEventListener("change", () => {
xStretchIndex = xStretchSelection.options[xStretchSelection.selectedIndex].value;
});
yStretchSelection.addEventListener("change", () => {
yStretchIndex = yStretchSelection.options[yStretchSelection.selectedIndex].value;
});
zStretchSelection.addEventListener("change", () => {
zStretchIndex = zStretchSelection.options[zStretchSelection.selectedIndex].value;
});
xTranslationSelection.addEventListener("change", () => {
xTranslationIndex = xTranslationSelection.options[xTranslationSelection.selectedIndex].value;
});
yTranslationSelection.addEventListener("change", () => {
yTranslationIndex = yTranslationSelection.options[yTranslationSelection.selectedIndex].value;
});
zTranslationSelection.addEventListener("change", () => {
zTranslationIndex = zTranslationSelection.options[zTranslationSelection.selectedIndex].value;
});
function handleCanvas() {
let controlMenuSize = windowWidth * controlMenuWidth;
cnv = createCanvas(windowWidth - controlMenuSize, windowHeight, WEBGL);
cnv.position(controlMenuSize, 0);
screenHeight= windowHeight;
screenWidth= windowWidth;
}
function setup() {
// Create a p5ble class
myBLE = new p5ble();
handleCanvas();
shapes = new Shapes();
background("#ffff");
}
function draw() {
background(255);
normalMaterial();
push();
translateShape();
rotateShape();
stretchShape();
shapes.draw3D();
pop();
}
function windowResized() {
handleCanvas();
}
function connectAndStartNotify() {
// Connect to a device by passing the service UUID
myBLE.connect(serviceUuid, gotCharacteristics);
}
// A function that will be called once got characteristics
function gotCharacteristics(error, characteristics) {
if (error) console.log("error: ", error);
console.log(characteristics[1].uuid);
for (let i = 0; i < characteristics.length; i++) {
if (characteristics[i].uuid == characteristicsUUID.prox) {
proxCharacteristic = characteristics[i];
myBLE.startNotifications(proxCharacteristic, handleProx);
} else if (characteristics[i].uuid == characteristicsUUID.gyro) {
gyroCharacteristic = characteristics[i];
myBLE.startNotifications(gyroCharacteristic, handleGyro, "string");
} else if (characteristics[i].uuid == characteristicsUUID.acce) {
acceCharacteristic = characteristics[i];
myBLE.startNotifications(acceCharacteristic, handleAcce, "string");
} else if (characteristics[i].uuid == characteristicsUUID.rgba) {
rgbaCharacteristic = characteristics[i];
myBLE.startNotifications(rgbaCharacteristic, handleRgba, "string");
} else {
console.log("nothing");
}
}
// Start notifications on the first characteristic by passing the characteristic
// And a callback function to handle notifications
}
function handleGyro(data) {
//console.log("GYRO: "+abs(gyroValue[0])+ " | "+ abs(gyroValue[1])+" | "+ abs(gyroValue[2]));
gyroValue = data.split("|");
sensorArray[0] = gyroValue[0];
sensorArray[1] = gyroValue[1];
sensorArray[2] = gyroValue[2];
}
function handleAcce(data) {
//console.log("ACCE: "+acceValue[0]+ " | "+ acceValue[1]+" | "+ acceValue[2]);
acceValue = data.split("|");
sensorArray[3] = acceValue[0];
sensorArray[4] = acceValue[1];
sensorArray[5] = acceValue[2];
}
function handleProx(data) {
//console.log('Prox: ', data);
proxValue = Number(data);
sensorArray[6] = proxValue;
}
function handleRgba(data) {
//console.log("color: "+rgbaValue[0]+ " | "+ rgbaValue[1]+" | "+ rgbaValue[2]+" | "+ rgbaValue[3]);
rgbaValue = data.split("|");
sensorArray[7] = rgbaValue[0];
sensorArray[8] = rgbaValue[1];
sensorArray[9] = rgbaValue[2];
sensorArray[10] = rgbaValue[3];
}
function mapMyData(selectedIndex, sensorData, defaultValue, min, max) {
let mappedData;
if (selectedIndex == "0" || selectedIndex == "1" || selectedIndex == "2") {
mappedData = map(sensorData, -2000, 2000, min, max);
} else if (
selectedIndex == "3" ||
selectedIndex == "4" ||
selectedIndex == "5"
) {
mappedData = map(sensorData, -4, 4, min, max);
} else if (selectedIndex == "6") {
mappedData = map(sensorData, 0, 255, min, max);
} else if (selectedIndex == "10") {
mappedData = map(sensorData, 0, 4097, min, max);
} else {
mappedData = defaultValue;
}
return mappedData;
}
class Shapes{
constructor(){
this.defaultSize = 100;
}
draw3D(){
if (selected3D === "box") {
box(this.defaultSize);
} else if (selected3D === "plane") {
plane(this.defaultSize);
} else if (selected3D === "sphere") {
sphere(this.defaultSize);
} else if (selected3D === "cone") {
cone(this.defaultSize, this.defaultSize);
} else if (selected3D === "cylinder") {
cylinder(this.defaultSize, this.defaultSize);
} else if (selected3D === "torus") {
torus(this.defaultSize, this.defaultSize / 4);
}
}
}
function translateShape() {
let defaultTranslation= 0;
if (myBLE.isConnected()) {
let xTranslation = sensorArray[xTranslationIndex];
let yTranslation = sensorArray[yTranslationIndex];
let zTranslation = sensorArray[zTranslationIndex];
let mappedXtranslation = mapMyData(xTranslationIndex, xTranslation, defaultTranslation, -((screenWidth/2)+controlMenuWidth) , (screenWidth/2));
let mappedYtranslation = mapMyData(yTranslationIndex, yTranslation, defaultTranslation, -(screenHeight/2) , (screenHeight/2));
let mappedZtranslation = mapMyData(zTranslationIndex, zTranslation, defaultTranslation, - (screenWidth*2), screenWidth*0.5);
translate(mappedXtranslation ,mappedYtranslation, mappedZtranslation);
}
}
function stretchShape() {
let defaultStretch= 1;
if (myBLE.isConnected()) {
let xStretch = sensorArray[xStretchIndex];
let yStretch = sensorArray[yStretchIndex];
let zStretch = sensorArray[zStretchIndex];
let mappedXstretch = mapMyData(xStretchIndex, xStretch, defaultStretch, 0.4, 5);
let mappedYstretch = mapMyData(yStretchIndex, yStretch, defaultStretch, 0.4, 5);
let mappedZstretch = mapMyData(zStretchIndex, zStretch, defaultStretch, 0.4, 5);
scale(mappedXstretch ,mappedYstretch, mappedZstretch);
}
}
function rotateShape() {
let defaultRotation = (frameCount * 0.01);
if (myBLE.isConnected()) {
let xRotation = sensorArray[xRotationIndex];
let yRotation = sensorArray[yRotationIndex];
let zRotation = sensorArray[zRotationIndex];
let mappedXrotation = mapMyData(xRotationIndex, xRotation, defaultRotation, 0, (2*PI));
let mappedYrotation = mapMyData(yRotationIndex, yRotation, defaultRotation, 0, (2*PI));
let mappedZrotation = mapMyData(zRotationIndex, zRotation, defaultRotation, 0, (2*PI));
rotateX(mappedXrotation);
rotateY(mappedYrotation);
rotateZ(mappedZrotation);
}else{
rotateX(defaultRotation);
rotateY(defaultRotation);
rotateZ(defaultRotation);
}
}