/** * Simple Write. * * Check if the mouse is over a rectangle and writes the status to the serial port. * This example works with the Wiring / Arduino program that follows below. */ import processing.serial.*; Serial myPort; // Create object from Serial class int val, lastval; // Data received from the serial port static int RED = 3; static int YELLOW = 2; static int GREEN = 1; static byte[] codes = { 0x00, 0x01, 0x02, 0x04 }; static byte[] blinkcodes = { 0x00, 0x08, 0x10, 0x20 }; static color[] colors = { #000000, #00FF00, #FFFF00, #FF0000 }; int state = 0; int addr = 0; void setup() { size(200, 500); // I know that the first port in the serial list on my mac // is always my FTDI adaptor, so I open Serial.list()[0]. // On Windows machines, this generally opens COM1. // Open whatever port is the one you're using. String portName = Serial.list()[0]; println(portName); myPort = new Serial(this, portName, 9600); lastval=0; state=0; } void draw() { background(0); val = mouseOverRect(); if (val!=lastval) { // myPort.write(codes[val]); // state=state+codes[val]; // println(binary(state, 8)); myPort.write(state|codes[val]); lastval=val; } drawBoxes(); } void drawBoxes() { // noFill(); if (0x04==(state&0x04)) fill(colors[RED]); else noFill(); stroke(colors[RED]); rect(50, 25, 100, 100); // Draw a square if (0x02==(state&0x02)) fill(colors[YELLOW]); else noFill(); stroke(colors[YELLOW]); rect(50, 150, 100, 100); // Draw a square if (0x01==(state&0x01)) fill(colors[GREEN]); else noFill(); stroke(colors[GREEN]); rect(50, 275, 100, 100); // Draw a square } int mouseOverRect() { // Test if mouse is over square if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 25) && (mouseY <= 125)) return RED; // Red if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 150) && (mouseY <= 250)) return YELLOW; // Yellow if ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 275) && (mouseY <= 375)) return GREEN; // Green return 0; } void mousePressed() { // set state to whichever area was currently clicked val=mouseOverRect(); if (val==0) { state=state&0xC0; } else { if ((state&codes[val])>0) { state^=(blinkcodes[val]+codes[val]); } else { state^=codes[val]; // lock light on if not already on. } } println(binary(state, 8)+" ("+int(state)+")"); myPort.write(state); lastval=val; } void keyPressed() { switch(key) { case '0': addr=0; break; case '1': addr=1; break; case '2': addr=2; break; case '3': addr=3; break; default: break; }; println("addr: "+addr); state=(addr<<6)+(state & 0x3F); println("state: "+binary(state, 8)); }