xxxxxxxxxx
141
var serial; // variable to hold an instance of the serialport library
var portName = '/dev/cu.usbmodem14301'; // fill in your serial port name here
var inData;
var xpot = 0;
var ypot = 0;
var red = 0;
var green = 0;
var blue = 0;
var data;
//list of line colors
var l = ["#00FF00",
"#FFFFFF",
"#FFFFFF"];
//list of background colors
var b = ["#000000",
"#000000",
"#0000FF"];
var w = window.innerWidth;
var h = window.innerHeight;
var options = {
baudrate: 9600
}; // change the data rate to whatever you wish
var x = 0;
var y = 0;
var prev_x = 0;
var prev_y = 0;
function setup() {
createCanvas(w, h);
background(0);
strokeWeight(2);
serial = new p5.SerialPort();
serial.on('list', gotList);
serial.on('data', gotData);
serial.on('error', gotError);
serial.on('open', gotOpen);
serial.list();
serial.open(portName);
}
function gotList(ports) {
for (var i = 0; i < ports.length; i++) {
if (ports[i].indexOf("cu.usb") != -1) {
portName = ports[i];
}
}
}
function gotOpen() {
console.log("Serial Port is open!");
}
function gotError(error) {
console.log(error);
}
function gotData() {
var currentString = trim(serial.readStringUntil("\r\n"));
if (!currentString) {
return;
}
// Trim white space and split data on commas
var data = split(trim(currentString), ',');
if (data.length < 3) {
return;
}
x = parseInt(map(data[0], 0, 1023, 0, width));
y = parseInt(map(data[1], 0, 1023, height, 0));
// if (data[2]) {
// return;
// mousePressed();
// }
}
function draw() {
// Turn on stroke
stroke(0,255,0);
// Draw a line from current to previous X,Y
line(x, y, prev_x, prev_y);
// Store X,Y position for next frame
prev_x = x;
prev_y = y;
}
function mousePressed() {
clear();
createCanvas(w, h);
background(0);
strokeWeight(2);
}
function pressRed() {
clear();
createCanvas(w, h);
background(0);
strokeWeight(2);
}
// function transparency() {
// fill(255, 255, 255, 2);
// noStroke();
// rect( 0, 0, width, height );
// }
// function colors() {
// clear();
// createCanvas(w, h);
// background(b);
// strokeWeight(2);
// stroke(l)
// }
function printList(portList) {
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}