xxxxxxxxxx
80
var serial; // variable to hold an instance of the serialport library
let sampleArray = [];
function setup() {
createCanvas(1280, 960);
//colorMode(HSB, 255);
serial = new p5.SerialPort();
serial.on("list", printList);
serial.on("data", serialEvent);
serial.on("open", openPort);
serial.list();
var options = { baudrate: 115200 };
serial.open("/dev/tty.usbmodem101");
background(color(255, 0, 255));
for (let i = 0; i < 8; i++) {
sampleArray.push(0);
}
}
function draw() {
background(color(0, 0, 0));
noStroke();
//console.log(sampleArray.length);
for(let i = 0; i < sampleArray.length; i++) {
let x = map(i, 0, sampleArray.length, width, 0);
let h = map(sampleArray[i], 0, 255, 0, height);
if(i % 2 == 1){
fill('#50E3CB');
}else{
fill('#FF802B');
}
rect(x, height-h, width/sampleArray.length, h);
}
}
function printList(portList) {
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
println(i + " " + portList[i]);
}
}
function openPort() {
serial.write("x");
}
function mousePressed() {
//enter full screen when mouse pressed
if (mouseX > 0 && mouseX < width && mouseY > 0 && mouseY < height) {
let fs = fullscreen();
fullscreen(!fs);
}
}
function mouseReleased() {
}
function serialEvent() {
var inString = serial.readLine();
if (inString.length > 0) {
inString = inString.trim();
//console.log(inString);
var values = split(inString, ",");
if(values.length > 7){
for(var i = 0; i < values.length; i++){
sampleArray[i]=Number(values[i]);
}
}
}
}