// Example by Tom Igoe import processing.serial.*; Serial myPort; // The serial port int data; int numChars = 26; // global variables byte[] font = new byte[128]; byte[] mapping = new byte[128]; color[] colors = new color[numChars]; int keyIndex; float keyScale; int rectWidth; int addr=0; 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.buffer(2); // graphics init size(200, 200); noStroke(); background(0); keyScale = 200/numChars-1.0; rectWidth = width/4; initCharacterArray(); println("Type a letter to send it to the serial device."); } /*void serialEvent(Serial myPort) { data = myPort.last(); }*/ void draw() { if(myPort.available() > 0) { print("received "); while (myPort.available() > 0) { int inByte = myPort.read(); print(hex(inByte,2) + " "); } } 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 sendToBoard(addr, key); // pretty graphics stuff fill(millis()%255); float beginRect = rectWidth/2 + keyIndex*keyScale-rectWidth/2; rect(beginRect, 0.0, rectWidth, height); delay(150); // to prevent multiple key-presses } if(key >= '1' && key <= '9') { // select a character to write too. addr = key-'1'; println("addressed changed to " + addr); delay(150); // to prevent multiple key-presses } if(key == '0') { // select a character to write too. addr = 9; println("addressed changed to " + addr); delay(150); // to prevent multiple key-presses } if(key == '-') { // select a character to write too. addr = 0x0A; println("addressed changed to " + addr); delay(150); // to prevent multiple key-presses } if(key == '=') { // select a character to write too. addr = 0x0B; println("addressed changed to " + addr); delay(150); // to prevent multiple key-presses } } } void sendToBoard(int addr, int data) { byte keytowrite; byte addrtowrite; int actualIndex; // prep addr for transmission addrtowrite = byte(addr); addrtowrite |= 0x80; // prep char data if(mapping[data]!=0) { actualIndex = mapping[data]; } else { actualIndex = data; } keytowrite = font[actualIndex]; keytowrite &= 0x7F; // send to board myPort.write(addrtowrite); myPort.write(keytowrite); println(); println("wrote " + hex(addrtowrite,2) + ":" + hex(keytowrite,2) + " to serial port"); } void initCharacterArray() { int i=0; // init character mapping for(i=0; i<128; i++) mapping[i]=0; // stores hex codes for ASCII font in char array. for(i=0; i<48; i++) font[i]=0x00; // blank font[34]=0x22; // "=HIGH_DOUBLE_VERTICAL font[38]=0x00; mapping[38]=43; // &->+ font[39]=0x02; // '=HIGH_RIGHT_VERTICAL //font[39]=0x01; // '=HIGH_BAR //font[46]=0x0C; // .->LOWER_RIGHT_ANGLE font[46]=0x00; mapping[46]=95; // .->_ font[58]=0x00; mapping[58]=95; // :->_ font[43]=0x46; // + font[44]=0x00; mapping[44]=95; // ,->_ font[45]=0x40; // - font[48]=0x3F; // 0 font[49]=0x06; // 1 font[50]=0x5B; // 2 font[51]=0x4F; // 3 font[52]=0x66; // 4 font[53]=0x6D; // 5 font[54]=0x7D; // 6 font[55]=0x07; // 7 font[56]=0x7F; // 8 font[57]=0x67; // 9 for(i=58; i<65; i++) font[i]=0x00; // blank // upper case letters. font[65]=0x77; // A font[66]=0x00; mapping[66] = 98;//font[66]=0x7F; // B font[67]=0x39; // C font[68]=0x00; mapping[68] = 100; // D->d //font[68]=0x3F; // D font[69]=0x79; // E font[70]=0x71; // F font[71]=0x00; mapping[71] = 103; //font[71]=0x3D; // G font[72]=0x76; // H font[73]=0x06; // I font[74]=0x0E; // J font[75]=0x00; mapping[75]=72; // K->H font[76]=0x38; // L font[77]=0x00; mapping[77]=78; // M->N font[78]=0x37; // N font[79]=0x3F; // O font[80]=0x73; // P font[81]=0x00; mapping[81]=113; // Q->q font[82]=0x00; mapping[82]=65; // R->A font[83]=0x6D; // S font[84]=0x07; // T font[85]=0x3E; // U font[86]=0x00; mapping[86]=85; // V->U font[87]=0x00; mapping[87]=85; // W->U font[88]=0x00; mapping[88]=72; // X->H` font[89]=0x66; // Y font[90]=0x5B; // Z for(i=91; i<95; i++) font[i]=0x00; // blank font[95]=0x08; // _ font[96]=0x00; // blank // lower case letters font[97]=0x00; mapping[97]=65;// a->A 5c; // a font[98]=0x7C; // b font[99]=0x58; // c font[100]=0x5E; // d font[101]=0x00; mapping[101]=69; // e->E font[102]=0x71; // f font[103]=0x6F; // g font[104]=0x74; // h font[105]=0x04; // i font[106]=0x0c; // j font[107]=0x00; mapping[107]=72; // k->H font[108]=0x06; // l font[109]=0x00; mapping[109]=110; // m->n font[110]=0x54; // n font[111]=0x5C; // o font[112]=0x73; // p font[113]=0x67; // q font[114]=0x50; // r font[115]=0x00; mapping[115]=83; // s->S font[116]=0x46; // t, alternately, could be 0x70 font[117]=0x1C; // u font[118]=0x00; mapping[118]=117; // v->u font[119]=0x00; mapping[119]=117; // w->u font[120]=0x00; mapping[120]=72; // x->H font[121]=0x6E; // y font[122]=0x00; mapping[122]=90; // z->Z for(i=123; i<128; i++) font[i]=0x00; // blank }