xxxxxxxxxx
260
/*
ToF Imager VL53L5CX visualization by Mary Mark and Dror Margalit.
Ardunio code at the bottom
*/
const serial = new p5.WebSerial();
let portButton;
let inData = [];
let imageWidth = 8;
let imageHeight = 8;
function setup() {
createCanvas(600,600);
allSerialStuff();
background(255); // black background
}
function draw() {
background(0);
ellipseMode(CENTER);
rectMode(CENTER);
if (inData.length > 0) {
for (let y = 0; y < imageHeight; y += 1) {
for (let x = 0; x < imageWidth; x += 1) {
let index = 7 - x + y * imageWidth;
let r = map(inData[index], 0, 2000, 255, 0);
let b = map(inData[index], 0, 2000, 0, 255);
fill(r, 100, b);
let ellipseSize = map(inData[index], 2000, 0, 0, width/imageWidth, true);
ellipse(
(x * width) / imageWidth + width / imageWidth / 2,
(y * height) / imageHeight + height / imageHeight / 2,
ellipseSize
);
// rect(
// (x * width) / imageWidth + width / imageWidth / 2,
// (y * height) / imageHeight + height / imageHeight / 2,
// width / imageWidth
// );
}
}
}
// console.log(inData);
}
function serialEvent() {
// read a string from the serial port
// until you get carriage return and newline:
var inString = serial.readStringUntil("\r\n");
//check to see that there's actually a string there:
if (inString) {
if (inString !== "hello") {
// if you get hello, ignore it
// split the string on the commas:
inData = split(inString, ",");
for (i = 0; i < inData.length; i++) {
inData[i] = int(inData[i]);
}
let tempWidth = Math.sqrt(inData.length, 2);
if (tempWidth != imageWidth) imageWidth = int(tempWidth);
// send a byte back to prompt for more data:
// serial.print("x");
}
}
}
function allSerialStuff() {
if (!navigator.serial) {
alert("WebSerial is not supported in this browser. Try Chrome or MS Edge.");
}
// check for any ports that are available:
serial.getPorts();
// if there's no port chosen, choose one:
serial.on("noport", makePortButton);
// open whatever port is available:
serial.on("portavailable", openPort);
// handle serial errors:
serial.on("requesterror", portError);
// handle any incoming serial data:
serial.on("data", serialEvent);
serial.on("close", makePortButton);
// add serial connect/disconnect listeners:
navigator.serial.addEventListener("connect", portConnect);
navigator.serial.addEventListener("disconnect", portDisconnect);
}
// if there's no port selected,
// make a port select button appear:
function makePortButton() {
// create and position a port chooser button:
portButton = createButton("choose port");
portButton.position(10, 10);
// give the port button a mousepressed handler:
portButton.mousePressed(choosePort);
}
// make the port selector window appear:
function choosePort() {
if (portButton) portButton.show();
serial.requestPort();
}
// open the selected port, and make the port
// button invisible:
// open the selected port, and make the port
// button invisible:
function openPort() {
// wait for the serial.open promise to return,
// then call the initiateSerial function
serial.open().then(initiateSerial);
// once the port opens, let the user know:
function initiateSerial() {
console.log("port open");
serial.print("x");
}
// hide the port button once a port is chosen:
if (portButton) portButton.hide();
}
// pop up an alert if there's a port error:
function portError(err) {
alert("Serial port error: " + err);
}
// read any incoming data as a string
// (assumes a newline at the end of it):
// try to connect if a new serial port
// gets added (i.e. plugged in via USB):
function portConnect() {
console.log("port connected");
serial.getPorts();
}
// if a port is disconnected:
function portDisconnect() {
serial.close();
console.log("port disconnected");
}
function closePort() {
serial.close();
}
/*
// Ardunio code
// Read an 8x8 array of distances from the VL53L5CX
// By: Nathan Seidle
// SparkFun Electronics
// Date: October 26, 2021
// License: MIT. See license file for more information but you can
// basically do whatever you want with this code.
// This example shows how to setup the I2C bus to minimize the amount
// of time taken to init the sensor.
// At each power on reset, a staggering 86,000 bytes of firmware have to be sent to the sensor.
// At 100kHz, this can take ~9.4s. By increasing the clock speed, we can cut this time down to ~1.4s.
// Two parameters can be tweaked:
// Clock speed: The VL53L5CX has a max bus speed of 1MHz.
// Max transfer size: The majority of Arduino platforms default to 32 bytes. If you are using one
// with a larger buffer (ESP32 is 128 bytes for example), this can help decrease transfer times a bit.
// Measurements:
// Default 100kHz clock and 32 byte transfer: 9.4s
// 400kHz, 32 byte transfer: 2.8s
// 400kHz, 128 byte transfer: 2.5s
// 1MHz, 32 byte transfer: 1.65s
// 1MHz, 128 byte transfer: 1.4s
// Feel like supporting our work? Buy a board from SparkFun!
// https://www.sparkfun.com/products/18642
#include <Wire.h>
#include <SparkFun_VL53L5CX_Library.h> //http://librarymanager/All#SparkFun_VL53L5CX
SparkFun_VL53L5CX myImager;
VL53L5CX_ResultsData measurementData; // Result data class structure, 1356 byes of RAM
int imageResolution = 8 * 8; //Used to pretty print output
int imageWidth = 0; //Used to pretty print output
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("SparkFun VL53L5CX Imager Example");
Wire.begin(); //This resets I2C bus to 100kHz
Wire.setClock(1000000); //Sensor has max I2C freq of 1MHz
//myImager.setWireMaxPacketSize(128); //Increase default from 32 bytes to 128 - not supported on all platforms
Serial.println("Initializing sensor board. This can take up to 10s. Please wait.");
//Time how long it takes to transfer firmware to sensor
long startTime = millis();
bool startup = myImager.begin();
long stopTime = millis();
if (startup == false) {
Serial.println(F("Sensor not found - check your wiring. Freezing"));
while (1)
;
}
Serial.print("Firmware transfer time: ");
float timeTaken = (stopTime - startTime) / 1000.0;
Serial.print(timeTaken, 3);
Serial.println("s");
myImager.setResolution(imageResolution); //Enable all 64 pads
//myImager.setResolution(4*4); //Enable all 16 pads
//imageResolution = myImager.getResolution(); //Query sensor for current resolution - either 4x4 or 8x8
imageWidth = sqrt(imageResolution); //Calculate printing width
myImager.startRanging();
myImager.setRangingFrequency(15);
}
void loop() {
//Poll sensor for new data
if (myImager.isDataReady() == true) {
if (myImager.getRangingData(&measurementData)) //Read distance data into array
{
//The ST library returns the data transposed from zone mapping shown in datasheet
//Pretty-print data with increasing y, decreasing x to reflect reality
for (int y = 0; y < imageResolution; y ++) {
Serial.print(measurementData.distance_mm[y]);
if (y < imageResolution - 1) {
Serial.print(",");
}
}
Serial.println();
}
}
delay(5); //Small delay between polling
}
*/