Permalink
Browse files

got java blink1server to a far state, moving to cocoa and objective-c…

… for completion
  • Loading branch information...
1 parent 4476aba commit 4d52090b7b28c351e305a64abea4c2ee324df813 @todbot committed Sep 4, 2012
@@ -131,18 +131,27 @@ else if( cmd.equalsIgnoreCase("servertickle") ||
blink1.serverdown( state, timems );
blink1.close();
}
- else if( cmd.equalsIgnoreCase("addPttern") ) {
+ else if( cmd.equalsIgnoreCase("addPattern") ) {
String patternstr = parms.getProperty("pattern");
- Pattern patt = new Pattern( patternstr );
+ Pattern patt = new Pattern(player, patternstr );
// add pattern to player
- player.playPattern( "",patt );
+ player.addPattern( "", patt );
- msg+= ": "+patt;
+ msg+= " added pattern: "+patt;
+
+ }
+ else if( cmd.equalsIgnoreCase("playPattern") ) {
+ String pattName = parms.getProperty("patternName");
+
+ // add pattern to player
+ boolean rc = player.playPattern( "", pattName );
+
+ msg+= " play pattern: "+pattName+ ":"+rc;
}
else if( cmd.equalsIgnoreCase("listPatterns") ) {
- player.listPatterns();
+ player.listPatterns(); // FIXME:
}
json.put( "cmd", cmd );
@@ -3,7 +3,8 @@
//
import thingm.blink1.*;
-Blink1 blink1;
+int serverport = 8080;
+
PatternPlayer player;
int sketchWidth = 400;
@@ -26,6 +27,18 @@ PFont font;
boolean doRefresh = true;
+//
+void updateBlink1()
+{
+ int r = int(red(previewColor));
+ int g = int(green(previewColor));
+ int b = int(blue(previewColor));
+
+ println("r,g,b: "+r+","+g+","+b );
+ java.awt.Color c = new java.awt.Color( r,g,b );
+ player.updateBlink1( 100, c );
+}
+
//
void setup()
{
@@ -38,17 +51,16 @@ void setup()
File docroot = new File(dataPath("html"));
player = new PatternPlayer();
- blink1 = player.blink1;
// http://elonen.iki.fi/code/nanohttpd/
Blink1NanoHTTPD server;
try {
- server = new Blink1NanoHTTPD( 8080, docroot);
+ server = new Blink1NanoHTTPD( serverport, docroot);
server.setPatternPlayer(player);
- } catch(Exception e) {
+ } catch(Exception e) {
+ println("Couldn't start server on port "+serverport);
}
-
pickX = colorPickerX + (colorPickerWidth/8)*7; // hack
pickY = colorPickerY + colorPickerHeight/2;
@@ -61,38 +73,24 @@ void setup()
updateBlink1();
}
-//
-void updateBlink1() {
- int r = int(red(previewColor));
- int g = int(green(previewColor));
- int b = int(blue(previewColor));
-
- int rn = blink1.log2lin(r);
- int gn = blink1.log2lin(g);
- int bn = blink1.log2lin(b);
-
- println("r,g,b: (lin)"+r+","+g+","+b + " => (log)"+rn+","+gn+","+bn);
- blink1.open();
- blink1.fadeToRGB( 100, r, g, b );
- blink1.close();
- //doRefresh = true;
-}
-
+long lastMillis;
//
void draw()
{
- //if( doRefresh ) {
- background( backColor );
- drawColorPicker();
-
- drawPreview( previewX, previewY, previewWidth, previewHeight);
-
- drawStatus();
- //doRefresh = false;
- //}
+ background( backColor );
+ drawColorPicker();
+
+ drawPreview( previewX, previewY, previewWidth, previewHeight);
+
+ drawStatus();
+
+ if( (millis() - lastMillis) > 1000 ) {
+ lastMillis = millis();
+ //println("getting version");
+ player.pingBlink1();
+ }
}
-
//
void mousePressed() {
// if in colorpicker, we're updating previewColor
@@ -110,7 +108,8 @@ void mouseDragged() {
mousePressed();
}
-void drawStatus() {
+void drawStatus()
+{
textFont(font, 12);
String statusText = player.getStatus();
text( statusText, colorPickerX+colorPickerWidth+5, height-13);
@@ -3,16 +3,20 @@
/**
*
- * format: "name,repeats,color1,color1time,color1holdtime,color2,c2time,c2hold"
+ * format: "name,repeats,color1,color1time,color2,c2time,..."
*
+ * maybe: "name,repeats,color1,color1time,color1holdtime,color2,c2time,c2hold"
*
*
*/
public class Pattern
{
- public static final int OFF = 0;
+ public static final int START = 0;
public static final int CHANGE = 1;
- public static final int HOLD = 2;
+ public static final int HOLD = 2;
+ public static final int STOP = 3;
+
+ PatternPlayer player;
String name; // name of pattern
int repeats; // number of repeats, 0 = inf
@@ -21,35 +25,35 @@
float[] timeshold; // list of hold times per color
int playpos = 0;
- int mode = 0; // 0=off, 1=changing, 2=holding
- int repsleft = 0;
+ int mode = STOP;
+ int playcount = 0;
- long lasttime = 0;
- long nexttime = 0;
+ long lastMillis = 0;
- public Pattern( String patternstr)
+ public Pattern(PatternPlayer parent, String patternstr)
{
+ player = parent;
+
String[] vals = patternstr.split(",");
name = vals[0];
repeats = Integer.valueOf( vals[1] ).intValue();
int p=0;
- int l = (vals.length-2) / 3 ;
+ int l = (vals.length-2) / 2 ;
System.out.println("l="+l+", "+vals.length);
colors = new Color[l];
timeschng = new float[l];
timeshold = new float[l];
- for( int i=2; i< vals.length; i+=3 ) {
+ for( int i=2; i< vals.length; i+=2 ) {
Color c = Color.BLACK;
float tc = (float)0.1;
- float th = (float)0.5;
- c = Color.decode( vals[i] ); // FIXME:
+ float th = (float)0.0;
+ try { c = Color.decode( vals[i] ); } catch(Exception e) { }
//int t = Integer.valueOf( vals[i+1] ).intValue();
- tc = Float.parseFloat( vals[i+1] );
- th = Float.parseFloat( vals[i+2] );
- //System.out.println("c:"+c+", tc:"+tc+", th:"+th);
+ try { tc = Float.parseFloat( vals[i+1] ); } catch(Exception e) { }
colors[p] = c;
timeschng[p] = tc;
timeshold[p] = th;
+ System.out.println("c:"+c+", tc:"+tc+", th:"+th);
p++;
}
}
@@ -58,37 +62,57 @@ public Pattern( String patternstr)
* actions:
* at beginning of new CHANGE or HOLD state, send comamnd to blink1
* algorithm
- * - if pattern is new (playpos=0, mode=OFF), start playing (OFF->CHANGE)
+ * - if pattern is new (playpos=0, mode=START), start playing (START->CHANGE)
* - if change time is up, move to holding (CHANGE->HOLD)
* - if hold time is up, move to next color (HOLD->CHANGE, playpos++)
* -
*/
- public void update(long now)
+ public void update(long nowMillis)
{
- lasttime = now;
float deltac=0, deltah=0;
- boolean modeChange = false;
+ boolean updateLed = false;
- deltac = 1000 * timeschng[ playpos ];
- deltah = 1000 * timeshold[ playpos ];
+ deltac = 1000 * timeschng[ playpos ]; // convert to millis
+ deltah = 1000 * timeshold[ playpos ]; // convert to millis
- if( mode == OFF ) {
- mode = CHANGE;
- modeChange = true;
+ if( mode == STOP ) {
+ return;
}
- else if( mode == CHANGE ) {
- if( now > (deltac + lasttime) ) { // time to hold
+ else if( mode == START ) { // we've just been started
+ mode = CHANGE; // next state is changing
+ updateLed = true; // and we need to act
+ player.updateBlink1( (int)deltac, colors[playpos] );
+ lastMillis = nowMillis;
+ }
+ else if( mode == CHANGE ) { // we've started changing,
+ if( (nowMillis - lastMillis) > deltac ) { // are we at the top?
mode = HOLD;
- modeChange = true;
+ updateLed = true;
+ lastMillis = nowMillis;
}
}
else if( mode == HOLD ) {
- if( now > (deltah + lasttime) ) {
- mode = CHANGE; // loop
- modeChange = true;
+ if( (nowMillis - lastMillis) > deltah ) {
+ mode = START; // loop
+ updateLed = true;
+ lastMillis = nowMillis;
+ // increment play pos, decrement play count
+ playpos++;
+ if( playpos == colors.length ) {
+ playpos = 0;
+ playcount++;
+ if( playcount == repeats ) {
+ mode = STOP;
+ playcount = 0;
+ }
+ }
}
}
- nexttime = now + (int)deltac;
+ /*
+ System.out.println(name+":"+playpos+":"+playcount+",mode:"+mode+
+ ",updateLed:"+updateLed+", dc:"+deltac+",dh:"+deltah+
+ ",last:"+lastMillis+",now:"+nowMillis);
+ */
}
/*
@@ -97,10 +121,26 @@ public boolean modeChange()
return modeChange;
}*/
+ //
+ public void start()
+ {
+ mode = START;
+ }
+ //
+ public void stop()
+ {
+ mode = STOP;
+ }
+
+ public int mode()
+ {
+ return mode;
+ }
+
//
public String toString()
{
- String str = "Pattern:"+name+","+repeats+",";
+ String str = "Pattern:"+name+":"+repeats+":"+mode+":"+playpos+":";
for( int i=0; i< colors.length; i++ ) {
Color c = colors[i];
String s = String.format( "#%02x%02x%02x,%.2f,%.2f,",
Oops, something went wrong.

0 comments on commit 4d52090

Please sign in to comment.