xxxxxxxxxx
82
// An example of reading comma separated value (CSV) values ended by newlines sent via the keyboard.
// This is a bit of a "hack" but is incredibly flexible. It allows us to treat keyboard input strings
// as raw data to parse things like Arduino's Keyboard.println()
//
//
// Example hardware sketches to use for testing
// This sketch is intended to work with the Arduino program entitled 'AnalogKeyboardPrint.ino'
// found here: https://github.com/jonfroehlich/arduino/blob/master/GameController/AnalogKeyboardPrint/AnalogKeyboardPrint.ino
//
// You can also use this CPX+MakeCode program: https://makecode.com/_AJXgxPcyR2Yy
//
// By Jon Froehlich
// @jonfroehlich
// http://makeabilitylab.io
//
let csvString = "";
let values;
const MAX_ANALOG_VALUE = 1023;
function setup() {
createCanvas(800, 400);
}
function draw() {
background(230);
if (values) {
let maxCircleSize = min(height, width);
let xCircle1 = width * 0.33;
let y = height / 2;
let circle1Size = map(values[0], 0, MAX_ANALOG_VALUE, 0, maxCircleSize);
fill(200, 0, 0, 150);
circle(xCircle1, y, circle1Size);
drawValue(values[0], xCircle1, y);
let xCircle2 = width * 0.66;
let circle2Size = map(values[1], 0, MAX_ANALOG_VALUE, 0, maxCircleSize);
fill(0, 0, 200, 150);
circle(xCircle2, y, circle2Size);
drawValue(values[1], xCircle2, y);
} else {
let noInputStr = "No input received yet!";
textSize(50);
let strX = width / 2 - textWidth(noInputStr) / 2;
let strY = height / 2 + 20;
noStroke();
textStyle(BOLD);
fill(128, 128, 128, 128);
text(noInputStr, strX, strY);
}
}
function drawValue(val, xCircle, yCircle) {
let lblSize = 20;
textSize(lblSize);
fill(15);
let circleLbl = "" + val;
let xCircleLbl = xCircle - textWidth(circleLbl) / 2;
let yCircleLbl = yCircle + lblSize / 2;
text(circleLbl, xCircleLbl, yCircleLbl);
}
function keyPressed() {
print(key);
if (keyCode == ENTER) {
// if the current key is enter, parse the data into actual values
let rowsOfData = CSVToArray(csvString);
if (rowsOfData.length > 0) {
values = rowsOfData[0];
}
csvString = "";
} else {
// if the current key is not ENTER, then add key to the current csvString
// for later parsing
csvString += key;
}
}