function setup() {
console.log('setup() started.');
createCanvas(windowWidth, windowHeight); // Create a responsive canvas
// Create the video capture from the webcam
video = createCapture(VIDEO, videoReady); // Add a callback for video readiness
video.hide(); // Hide the HTML video element as we'll draw it on the canvas
console.log('Video capture object created in setup().');
// Initialize custom ready flag to false
video.ready = false;
// Initial display message asking for model URL
textSize(32);
textAlign(CENTER, CENTER);
fill(0);
text('Please enter your Teachable Machine model URL below', width / 2, height / 2);
// --- Model URL Input Setup ---
modelURLInput = createInput();
modelURLInput.attribute('placeholder', 'Enter Teachable Machine model URL here');
modelURLInput.style('font-size', '16px');
modelURLInput.style('padding', '8px');
// FIX: videomargin is now defined, so this line will work.
modelURLInput.style('width', 'calc(100% - 100px - 2 * ' + videoMargin + 'px)'); // Adjust width
modelURLInput.style('border', '1px solid #ccc');
modelURLInput.style('border-radius', '4px');
modelURLLoadButton = createButton('OK');
modelURLLoadButton.mousePressed(loadModelFromInput);
modelURLLoadButton.style('font-size', '16px');
modelURLLoadButton.style('padding', '10px 15px');
modelURLLoadButton.style('background-color', '#007BFF');
modelURLLoadButton.style('color', 'white');
modelURLLoadButton.style('border', 'none');
modelURLLoadButton.style('border-radius', '5px');
modelURLLoadButton.style('cursor', 'pointer');
// --- Web Serial Setup ---
// Create p5 DOM elements for serial status and received data
serialStatusP = createP('Web Serial: Not connected.');
serialStatusP.style('font-size', '18px');
serialStatusP.style('color', '#333');
serialStatusP.style('font-family', 'sans-serif'); // Add a standard font for consistency
receivedSerialDataP = createP('Received: '); // Renamed for clarity
receivedSerialDataP.style('font-size', '18px');
receivedSerialDataP.style('color', '#333');
receivedSerialDataP.style('font-family', 'sans-serif'); // Add a standard font for consistency
// Check if Web Serial API is supported
if ('serial' in navigator) {
// Create the "Connect Serial Port" button
serialConnectButton = createButton('Connect Serial Port');
serialConnectButton.mousePressed(connectSerial); // Call connectSerial() when button is pressed
serialConnectButton.style('font-size', '16px');
serialConnectButton.style('padding', '10px 15px');
serialConnectButton.style('background-color', '#4CAF50');
serialConnectButton.style('color', 'white');
serialConnectButton.style('border', 'none');
serialConnectButton.style('border-radius', '5px');
serialConnectButton.style('cursor', 'pointer');
// Add event listener for port disconnection (e.g., USB cable unplugged)
navigator.serial.addEventListener('disconnect', portDisconnected);
} else {
// Display an error message if Web Serial is not supported
serialStatusP.html('Web Serial API not supported in this browser. Try Chrome/Edge.');
serialStatusP.style('color', 'red');
}
// Set initial visibility for UI elements
setClassifierUIVisibility(false); // Hide classifier elements initially
// Call windowResized once to set initial positions correctly
windowResized();
console.log('setup() finished. appState:', appState);
}