xxxxxxxxxx
219
// let serial; // Serial communication object
// let serialActive = false; // Tracks if the serial port is active
// let serialConnected = false; // Tracks if the serial port is selected
let currentStep = 0;
let canvasState = "menu"; // "menu", "starter", "main", "dessert", "video", "finalDish"
let currentCourse = "";
let currentVideo = null;
let choices = []; // Stores the user's choices (1 or 2)
// Videos and final GIFs for each course
let assets = {
starter: {
base: { 1: "lettuce.mp4", 2: "kale.mp4" },
veggies: { 1: "carrot.mp4", 2: "tomatoes.mp4" },
dressing: { 1: "olive_oil.mp4", 2: "caesar_dressing.mp4" },
finalGIFs: {
"111": "s_111.gif",
"112": "s_112.gif",
"121": "s_121.gif",
"122": "s_122.gif",
"211": "s_211.gif",
"212": "s_212.gif",
"221": "s_221.gif",
"222": "s_222.gif",
},
},
main: {
base: { 1: "rice.mp4", 2: "pasta.mp4" },
protein: { 1: "shrimp.mp4", 2: "beef.mp4" },
veggies: { 1: "tomat.mp4", 2: "peppers.mp4" },
finalGIFs: {
"111": "m_111.gif",
"112": "m_112.gif",
"121": "m_121.gif",
"122": "m_122.gif",
"211": "m_211.gif",
"212": "m_212.gif",
"221": "m_221.gif",
"222": "m_222.gif",
},
},
dessert: {
base: { 1: "cake.mp4", 2: "pastry.mp4" },
fruit: { 1: "strawberry.mp4", 2: "mango.mp4" },
garnish: { 1: "coconut_flakes.mp4", 2: "chocolate_shavings.mp4" },
finalGIFs: {
"111": "d_111.gif",
"112": "d_112.gif",
"121": "d_121.gif",
"122": "d_122.gif",
"211": "d_211.gif",
"212": "d_212.gif",
"221": "d_221.gif",
"222": "d_222.gif",
},
},
};
function preload() {
for (let course in assets) {
for (let category in assets[course]) {
if (category !== "finalGIFs") {
for (let option in assets[course][category]) {
let videoPath = assets[course][category][option];
assets[course][category][option] = createVideo(videoPath);
assets[course][category][option].hide();
}
} else {
for (let combo in assets[course][category]) {
let gifPath = assets[course][category][combo];
assets[course][category][combo] = loadImage(gifPath);
}
}
}
}
}
function setup() {
createCanvas(800, 600);
textSize(18);
serial = new p5.SerialPort();
serial.on("data", readSerial); // Listen for serial data
serial.on("open", serialOpened); // Callback for serial connection
}
function draw() {
background(255);
if (!serialConnected) {
textAlign(CENTER, CENTER);
fill(0);
text("Press Space Bar to select Serial Port", width / 2, height / 2);
return;
} else if (!serialActive) {
textAlign(CENTER, CENTER);
fill(0);
text("Connecting to Serial Port...", width / 2, height / 2);
return;
}
if (canvasState === "menu") {
displayMenu();
} else if (canvasState === "starter" || canvasState === "main" || canvasState === "dessert") {
displayChoices();
} else if (canvasState === "video") {
playCurrentVideo();
} else if (canvasState === "finalDish") {
displayFinalDish();
}
}
function keyPressed() {
if (key === " ") {
setUpSerial();
serialConnected = true;
}
}
function setUpSerial() {
serial.list();
serial.open("/dev/tty.usbmodem1421"); // Adjust for your port
}
function serialOpened() {
serialActive = true;
}
function readSerial(data) {
if (data != null) {
let trimmedData = data.trim();
if (trimmedData === "reset") {
resetSystem();
} else {
handleChoice(parseInt(trimmedData));
}
}
}
function sendToArduino(value) {
if (serialActive) {
serial.write(value + "\n");
}
}
function displayMenu() {
textSize(32);
textAlign(CENTER, CENTER);
text("Choose a Course:\nStarter, Main, Dessert", width / 2, height / 2);
if (mouseIsPressed) {
if (mouseX < width / 3) {
currentCourse = "starter";
canvasState = "starter";
} else if (mouseX < (2 * width) / 3) {
currentCourse = "main";
canvasState = "main";
} else {
currentCourse = "dessert";
canvasState = "dessert";
}
}
}
function displayChoices() {
textSize(24);
textAlign(CENTER, CENTER);
text(`Step ${currentStep + 1} of 3\nPress a Button to Choose`, width / 2, height / 2);
}
function playCurrentVideo() {
if (currentVideo && !currentVideo.isPlaying()) {
currentVideo.play();
}
if (currentVideo) {
image(currentVideo, 0, 0, width, height);
currentVideo.onended(() => {
if (currentStep < 3) {
canvasState = currentCourse;
} else {
canvasState = "finalDish";
}
});
}
}
function displayFinalDish() {
let choiceString = choices.join("");
let finalGIF = assets[currentCourse].finalGIFs[choiceString];
image(finalGIF, 0, 0, width, height);
textSize(24);
textAlign(CENTER, CENTER);
text("Your Final Dish! Press Reset to Start Over", width / 2, height - 50);
}
function resetSystem() {
currentStep = 0;
currentCourse = "";
currentVideo = null;
choices = [];
canvasState = "menu";
}
function handleChoice(choice) {
let categories = ["base", "veggies", "dressing"];
if (currentCourse === "main") categories = ["base", "protein", "veggies"];
if (currentCourse === "dessert") categories = ["base", "fruit", "garnish"];
if (currentStep < 3) {
let category = categories[currentStep];
currentVideo = assets[currentCourse][category][choice];
choices.push(choice);
currentStep++;
canvasState = "video";
}
}