// Copyright (c) 2007-2008, ThingM Corporation /** * BlinkMComm -- Simple Processing 'library' to talk to BlinkM * (via an Arduino programmed with BlinkMCommunicator) * * This is almost 100% Java with few Processing dependencies, * so with a bit of work can be usable in other Java environments * * 2007-2008, Tod E. Kurt, ThingM, http://thingm.com/ * * * NOTE: you should NOT have a "serialEvent()" method in your sketch, * else the BlinkM reading functions will not work. For debugging * a "serialEvent()" method is useful, but be sure to comment it out * if you want to read anything back from a BlinkM. */ import processing.serial.*; import javax.swing.*; // for connect dialog public class BlinkMComm { public final boolean debug = true; public final boolean fakeIt = false; boolean isConnected = false; public byte blinkm_addr = 0x09; public String portName = null; public final int portSpeed = 19200; PApplet papplet; // our owner, owns the GUI window Serial port; // mapping of duration to ticks (must be same length as 'durations') public byte[] durTicks = { (byte) 1, (byte) 18, (byte) 72 }; // mapping of duration to fadespeeds (must be same length as 'durations') public byte[] fadeSpeeds = { (byte) 100, (byte) 25, (byte) 5 }; Color cBlk = new Color(0,0,0); Color lastColor; // Return a list of potential ports // they should be ordered by best to worst (but are not right now) // this can't be static as a .pde, sigh. public String[] listPorts() { String[] a = Serial.list(); String osname = System.getProperty("os.name"); if( osname.toLowerCase().startsWith("windows") ) { // reverse list because Arduino is almost always highest COM port for(int i=0;i 0 ) { byte[] resp = new byte[respcnt]; int j = 100; while( port.available() < respcnt ) { pause(1); if( j-- == 0 ) { debug("sendCommand couldn't receive"); return null; } } for( int i=0; i 0)) { disconnect(); // just in case try { connect( s ); } catch( Exception e ) { JOptionPane.showMessageDialog( null, "Could not connect\n"+e, "Connect error", JOptionPane.ERROR_MESSAGE); return false; } return true; } // otherwise, we failed return false; } // --------------------- /** * A simple delay */ public void pause( int millis ) { try { Thread.sleep(millis); } catch(Exception e) { } } public void debug( String s ) { if(debug) println(s); } } // Java data struct representation of a BlinkM script line // also includes string rendering public class BlinkMScriptLine { int dur; char cmd; int arg1,arg2,arg3; String comment; public BlinkMScriptLine() { } public BlinkMScriptLine( int d, char c, int a1, int a2, int a3 ) { dur = d; cmd = c; arg1 = a1; arg2 = a2; arg3 = a3; } // "construct" from a byte array. could also do other error checking here public boolean fromByteArray(byte[] ba) { if( ba==null || ba.length != 5 ) return false; dur = ba[0] & 0xff; cmd = (char)(ba[1] & 0xff); arg1 = ba[2] & 0xff; arg2 = ba[3] & 0xff; arg3 = ba[4] & 0xff; // because byte is stupidly signed return true; } public void addComment(String s) { comment = s; } public String toStringSimple() { return "{"+dur+", {'"+cmd+"',"+arg1+","+arg2+","+arg3+"}},"; } public String toFormattedString() { return toString(); } // this seems pretty inefficient with all the string cats public String toString() { String s="{"+dur+", {'"+cmd+"',"; if( cmd=='n'||cmd=='c'||cmd=='C'||cmd=='h'||cmd=='H' ) { s += makeGoodHexString(arg1) +","+ makeGoodHexString(arg2) +","+ makeGoodHexString(arg3) +"}},"; } else s += arg1+","+arg2+","+arg3+"}},"; if( comment!=null ) s += "\t// "+comment; return s; } // convert a byte properly to a hex string // why does Java number formatting still suck? public String makeGoodHexString(int b) { String s = Integer.toHexString(b); if( s.length() == 1 ) return "0x0"+s; return "0x"+s; } }