import processing.serial.*;

Serial myPort;

void setup() {
  size(400, 300);

  // Show available serial ports in the Processing console
  println(Serial.list());

  // XIAO ESP32-C6 serial port
  myPort = new Serial(this, "/dev/cu.usbmodem1101", 115200);
}

void draw() {
  background(240);

  // LED D3 title
  fill(0);
  textSize(16);
  text("LED D3", 80, 50);

  // LED D3 ON button
  fill(0, 200, 0);
  rect(50, 70, 100, 40);
  fill(255);
  text("ON", 80, 95);

  // LED D3 OFF button
  fill(200, 0, 0);
  rect(200, 70, 100, 40);
  fill(255);
  text("OFF", 230, 95);

  // LED D5 title
  fill(0);
  text("LED D5", 80, 150);

  // LED D5 ON button
  fill(0, 200, 0);
  rect(50, 170, 100, 40);
  fill(255);
  text("ON", 80, 195);

  // LED D5 OFF button
  fill(200, 0, 0);
  rect(200, 170, 100, 40);
  fill(255);
  text("OFF", 230, 195);
}

void mousePressed() {

  // LED D3 ON
  if (mouseX > 50 && mouseX < 150 && mouseY > 70 && mouseY < 110) {
    myPort.write('A');
    println("Sent A - LED D3 ON");
  }

  // LED D3 OFF
  if (mouseX > 200 && mouseX < 300 && mouseY > 70 && mouseY < 110) {
    myPort.write('B');
    println("Sent B - LED D3 OFF");
  }

  // LED D5 ON
  if (mouseX > 50 && mouseX < 150 && mouseY > 170 && mouseY < 210) {
    myPort.write('C');
    println("Sent C - LED D5 ON");
  }

  // LED D5 OFF
  if (mouseX > 200 && mouseX < 300 && mouseY > 170 && mouseY < 210) {
    myPort.write('D');
    println("Sent D - LED D5 OFF");
  }
}
