import processing.serial.*;

Serial myPort;
String status = "OFF";

void setup() {
  size(430, 500);
  smooth();
  textAlign(CENTER, CENTER);

  println(Serial.list());
  myPort = new Serial(this, "COM6", 115200);
  myPort.clear();
}

void draw() {
  background(245);

  fill(0);
  textSize(28);
  text("LED Control", width/2, 60);

  fill(120);
  textSize(14);
  text("ON / OFF / BLINK", width/2, 90);

  // STATUS
  fill(255);
  rect(50, 120, 330, 100, 20);

  fill(0);
  textSize(18);
  text("Status", width/2, 150);

  fill(status.equals("ON") ? color(0,200,100) :
       status.equals("BLINK") ? color(0,120,255) :
       color(150));

  textSize(28);
  text(status, width/2, 190);

  // BOTONES
  drawButton(60, 270, 90, 70, "ON");
  drawButton(170, 270, 90, 70, "OFF");
  drawButton(280, 270, 90, 70, "BLINK");
}

void drawButton(float x, float y, float w, float h, String label) {
  boolean hover = mouseX > x && mouseX < x + w && mouseY > y && mouseY < y + h;

  noStroke();
  fill(hover ? color(80) : color(50));
  rect(x, y, w, h, 15);

  fill(255);
  textSize(16);
  text(label, x + w/2, y + h/2);
}

void mousePressed() {

  if (mouseY > 270 && mouseY < 340) {

    if (mouseX > 60 && mouseX < 150) {
      send("ON");
      status = "ON";
    }

    if (mouseX > 170 && mouseX < 260) {
      send("OFF");
      status = "OFF";
    }

    if (mouseX > 280 && mouseX < 370) {
      send("BLINK");
      status = "BLINK";
    }
  }
}

void send(String cmd) {
  myPort.write(cmd + "\n");
  println("Sent: " + cmd);
}
