xxxxxxxxxx
119
// This is a basic web serial template for p5.js. Please see:
// https://makeabilitylab.github.io/physcomp/communication/p5js-serial
//
// By Jon E. Froehlich
// @jonfroehlich
// http://makeabilitylab.io/
//
let pHtmlMsg;
let serialOptions = { baudRate: 115200 };
let serial;
let handPoseModel;
let video;
let curHandPose = null;
let isHandPoseModelInitialized = false;
function setup() {
createCanvas(640, 480);
video = createCapture(VIDEO);
// Hide the video element, and just show the canvas
video.hide();
handPoseModel = ml5.handpose(video, onHandPoseModelReady);
// Call onNewHandPosePrediction every time a new handPose is predicted
handPoseModel.on("predict", onNewHandPosePrediction);
// Setup Web Serial using serial.js
serial = new Serial();
serial.on(SerialEvents.CONNECTION_OPENED, onSerialConnectionOpened);
serial.on(SerialEvents.CONNECTION_CLOSED, onSerialConnectionClosed);
serial.on(SerialEvents.DATA_RECEIVED, onSerialDataReceived);
serial.on(SerialEvents.ERROR_OCCURRED, onSerialErrorOccurred);
// If we have previously approved ports, attempt to connect with them
serial.autoConnectAndOpenPreviouslyApprovedPort(serialOptions);
// Add in a lil <p> element to provide messages. This is optional
pHtmlMsg = createP("Click anywhere on this page to open the serial connection dialog");
}
/**
* Callback function called by ml5.js HandPose when the HandPose model is ready
* Will be called once and only once
*/
function onHandPoseModelReady() {
console.log("HandPose model ready!");
isHandPoseModelInitialized = true;
}
/**
* Callback function called by ml5.js HandPose when a pose has been detected
*/
function onNewHandPosePrediction(predictions) {
if (predictions && predictions.length > 0) {
curHandPose = predictions[0];
console.log(curHandPose);
} else {
curHandPose = null;
}
}
function draw() {
background(100);
}
/**
* Callback function by serial.js when there is an error on web serial
*
* @param {} eventSender
*/
function onSerialErrorOccurred(eventSender, error) {
console.log("onSerialErrorOccurred", error);
pHtmlMsg.html(error);
}
/**
* Callback function by serial.js when web serial connection is opened
*
* @param {} eventSender
*/
function onSerialConnectionOpened(eventSender) {
console.log("onSerialConnectionOpened");
pHtmlMsg.html("Serial connection opened successfully");
}
/**
* Callback function by serial.js when web serial connection is closed
*
* @param {} eventSender
*/
function onSerialConnectionClosed(eventSender) {
console.log("onSerialConnectionClosed");
pHtmlMsg.html("onSerialConnectionClosed");
}
/**
* Callback function serial.js when new web serial data is received
*
* @param {*} eventSender
* @param {String} newData new data received over serial
*/
function onSerialDataReceived(eventSender, newData) {
console.log("onSerialDataReceived", newData);
pHtmlMsg.html("onSerialDataReceived: " + newData);
}
/**
* Called automatically by the browser through p5.js when mouse clicked
*/
function mouseClicked() {
if (!serial.isOpen()) {
serial.connectAndOpen(null, serialOptions);
}
}