import processing.core.*; import processing.serial.*; import java.applet.*; import java.awt.*; import java.awt.image.*; import java.awt.event.*; import java.io.*; import java.net.*; import java.text.*; import java.util.*; import java.util.zip.*; public class uartToSevenSeg extends PApplet { 	

// Example by Tom Igoe



Serial myPort;  // The serial port
int data;
int numChars = 26;
int[] colors = new int[numChars];
int keyIndex;
float keyScale;
int rectWidth;

public void setup() 
{ 
     
  println(Serial.list());
  // I know that the first port in the serial list on my mac
  // is always my  Keyspan adaptor, so I open Serial.list()[0].
  // Open whatever port is the one you're using.
  myPort = new Serial(this, Serial.list()[1], 9600);
  myPort.bufferUntil(0xFF);

  size(200, 200);
  noStroke();
  background(0);
  keyScale = 200/numChars-1.0f;
  rectWidth = width/4;

  // Send a capital A out the serial port
  //myPort.write(65);

  println("polling incoming uart data every 250 ms...");
} 

/*void serialEvent(Serial myPort) {
  data = myPort.last();
}*/

public void draw()
{ 
  if(keyPressed) {
    if(key >= 'A' && key <= 'z') {
      if(key <= 'Z') {
        keyIndex = key-'A';
      } else {
        keyIndex = key-'a';
      }
      
      // dump ascii code for keypress to serial device
      myPort.write(key);
      println("wrote " + key + " to serial port");
      
      fill(millis()%255);
      float beginRect = rectWidth/2 + keyIndex*keyScale-rectWidth/2;
      rect(beginRect, 0.0f, rectWidth, height);
    }
  }
}

  static public void main(String args[]) {     PApplet.main(new String[] { "uartToSevenSeg" });  }}