xxxxxxxxxx
129
//---------------
//Serial
let portAddr = "/dev/tty.usbmodem144201";
let lastData;
let avgData = 200;
let lastAvg = 0;
//videos
let circleOne;
const boundaryOne = 200;
let circleTwo;
let circleThree;
let videoPlaying;
function serverConnected() {
print("Connected to Server");
}
function gotList(thelist) {
print("List of Serial Ports:");
for (let i = 0; i < thelist.length; i++) {
print(i + " " + thelist[i]);
}
}
function gotOpen() {
print("Serial Port is Open");
}
function gotClose() {
print("Serial Port is Closed");
latestData = "Serial Port is Closed";
}
function gotError(theerror) {
print(theerror);
}
function handleAvgData() {
if (lastAvg < boundaryOne && avgData > boundaryOne) {
videoPlaying = circleOne;
console.log("Play circ1");
} else if (lastAvg > boundaryOne && avgData < boundaryOne) {
videoPlaying = circleTwo;
console.log("Play circ2");
}
lastAvg = avgData;
}
function gotData() {
let serialData = serial.readLine();
if (serialData > 0) {
let diff = serialData - avgData;
avgData += diff * 0.08;
}
handleAvgData();
}
//---------------
//Rendering
function preload(){
circleOne = createVideo("1_2.mp4")
circleOne.hide();
circleOne.loop()
videoPlaying = circleOne;
circleTwo = createVideo("2_1.mp4")
circleTwo.hide();
circleTwo.loop()
circleThree = createVideo("3_2.mp4")
circleThree.hide();
circleThree.loop()
print("loaded")
}
function setup() {
createCanvas(windowWidth, windowHeight);
serial = new p5.SerialPort();
serial.list();
serial.open(portAddr);
serial.on("connected", serverConnected);
serial.on("list", gotList);
serial.on("data", gotData);
serial.on("error", gotError);
serial.on("open", gotOpen);
serial.on("close", gotClose);
}
function draw() {
image(videoPlaying, 0, 0, windowWidth, windowWidth)
}
function keyPressed() {
if (keyCode === LEFT_ARROW) {
videoPlaying = circleOne
console.log("ONE")
} else if (keyCode === UP_ARROW) {
videoPlaying = circleTwo
console.log("TWO")
}
else if (keyCode == RIGHT_ARROW) {
videoPlaying = circleThree
console.log("THREE")
}
}