xxxxxxxxxx
52
let gQRModule;
const CELL_SIZE = 10;
function setup() {
createCanvas(windowWidth, windowHeight);
background('white');
fill( 'black' );
frameRate(2);
}
const connectedCubeArray = [];
function draw() {
const cube = connectedCubeArray[0];
const name = cube?.cube?.device?.name;
if( name ){
drawQR( name );
}
}
function mouseClicked() {
P5tCube.connectNewP5tCube().then(cube => {
connectedCubeArray.push(cube);
cube?.turnLightOn( 'white' );
});
}
// Draw QR code
const drawQR = ( textData ) => {
gQRModule = QRCode.create( textData ).modules;
const size = gQRModule.size;
const dataArray = gQRModule.data;
const offsetW = width / 2 - CELL_SIZE * size / 2;
const offsetH = height / 2 - CELL_SIZE * size / 2;
translate( offsetW, offsetH );
for( let j = 0; j < size; j++ ){
for( let i = 0; i < size; i++ ){
if( dataArray[ size * j + i ] ){
square( CELL_SIZE * i, CELL_SIZE * j, CELL_SIZE )
}
}
}
}