xxxxxxxxxx
96
//---------------
//Serial
let lastData;
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 gotData() {
let serialData = serial.readLine();
console.log(serialData);
goalDiam = map(serialData, 0, 1024, minDiam, maxDiam);
}
//---------------
//Rendering
let circDiam = 100;
let goalDiam = 400;
let minDiam = 20;
let maxDiam = 500;
const increment = 3;
function setup() {
createCanvas(windowWidth, windowHeight);
serial = new p5.SerialPort();
serial.list();
serial.open("/dev/tty.usbmodem144101");
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 keyPressed() {
if (keyCode === UP_ARROW) {
goalDiam += 50;
}
if (keyCode === DOWN_ARROW) {
goalDiam -= 50;
}
}
function handleCircleDiam() {
let diff = goalDiam - circDiam;
if (diff > increment) {
circDiam += increment;
} else if (diff < -increment) {
circDiam -= increment;
}
}
function draw() {
background('black');
handleCircleDiam();
noStroke();
rect(width/2 - 5, height/2, 10, height);
circle(width/2, height/2, circDiam);
}