Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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

… for completion
  • Loading branch information...
commit 4d52090b7b28c351e305a64abea4c2ee324df813 1 parent 4476aba
@todbot authored
View
19 processing/Blink1Server/Blink1NanoHTTPD.java
@@ -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 );
View
65 processing/Blink1Server/Blink1Server.pde
@@ -3,7 +3,8 @@
//
import thingm.blink1.*;
-Blink1 blink1;
+int serverport = 8080;
+
PatternPlayer player;
int sketchWidth = 400;
@@ -27,6 +28,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()
{
size(sketchWidth, sketchHeight);
@@ -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);
View
104 processing/Blink1Server/Pattern.java
@@ -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);
+ */
}
/*
@@ -98,9 +122,25 @@ public boolean 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,",
View
97 processing/Blink1Server/PatternPlayer.java
@@ -2,6 +2,10 @@
import thingm.blink1.Blink1;
import java.util.*;
+import java.util.concurrent.*;
+import java.awt.Color;
+
+//import java.util.concurrent.TimeUnit.*;
/**
* PatternPlayer contains a list of Patterns to be play on one or more blink(1)s
@@ -11,7 +15,7 @@
*
*
*/
-public class PatternPlayer implements Runnable
+public class PatternPlayer //implements Runnable
{
public static Blink1 blink1;
@@ -23,6 +27,9 @@
Map<String,Pattern> patterns;
// maybe this should be a hashmap? biink1 -> pattern
+ ScheduledExecutorService executor;
+
+
//
public PatternPlayer()
{
@@ -39,40 +46,53 @@ public PatternPlayer()
}
blink1.close();
- runner = new Thread(this, "PatternPlayer");
- runner.start();
+ executor = Executors.newSingleThreadScheduledExecutor();
+ executor.scheduleAtFixedRate( new Runnable() {
+ public void run() {
+ updatePatterns( System.currentTimeMillis() );
+ }
+ },
+ 0,
+ 25,
+ //200,
+ TimeUnit.MILLISECONDS
+ );
+ }
+
+ // note: synchronized
+ public synchronized void updateBlink1( int colorMillis, Color c )
+ {
+ System.out.println("updateBlink1: "+colorMillis+":"+c);
+ blink1.open();
+ blink1.fadeToRGB( colorMillis, c.getRed(), c.getGreen(), c.getBlue() );
+ blink1.close();
+ }
+
+ // note: synchronized
+ public synchronized void pingBlink1()
+ {
+ blink1.open();
+ blink1.getFirmwareVersion();
+ blink1.close();
}
-
//
- public void run()
+ public void updatePlayer( long nowMillis )
{
- while( !done ) {
- long now = System.currentTimeMillis();
- if( now > nextTimeMillis ) {
- updatePlayer(now);
- //
+ //long nowMillis = System.currentTimeMillis();
+ if( nowMillis > nextTimeMillis ) {
+ updatePatterns( nowMillis );
+ nextTimeMillis = nowMillis + (5 * 100);
}
- try { //delay
- Thread.sleep( 5000 );
- } catch (InterruptedException e) {
- System.out.println("player interrupted");
- }
- }
-
}
//
- public void updatePlayer( long now )
+ public void updatePatterns(long nowMillis)
{
- System.out.println("updatePlayer");
for( String name : patterns.keySet() ) {
Pattern p = patterns.get(name);
- p.update(now);
- System.out.println(name+" : "+ p);
- }
-
- nextTimeMillis = now + (5 * 100);
-
+ //System.out.println("updatePatterns:"+name +":"+p.mode() );
+ p.update(nowMillis);
+ }
}
//
@@ -82,19 +102,40 @@ public void listPatterns()
System.out.println("listPatterns:");
//for( Pattern p : patterns ) { // Set
for( String name : patterns.keySet() ) {
- System.out.println(i+":"+name+" p:"+ patterns.get(name));
+ System.out.println("-- "+i+":"+name+" p:"+ patterns.get(name));
i++;
}
}
+ // FIXME: what about blink1id
+ public void addPattern(String blink1id, Pattern pattern)
+ {
+ patterns.put( pattern.name, pattern );
+ }
+
//
+ public boolean playPattern(String blink1id, String patternName )
+ {
+ Pattern pattern = patterns.get( patternName );
+ if( pattern == null )
+ return false;
+ pattern.start();
+ return true;
+ }
+
+
+ /*
+ // FIXME: what about blink1id
public void playPattern(String blink1id, Pattern pattern)
{
- patterns.put( pattern.name, pattern );
+ addPattern( blink1id, pattern );
+ pattern.start();
//updatePlayer( System.currentTimeMillis() );
- runner.interrupt();
+ //runner.interrupt();
}
+ */
+
//
public String getStatus()
View
9 processing/Blink1Server/data/html/bootstrap/blink1.html
@@ -54,6 +54,7 @@
<div class="tabbable"> <!-- Only required for left/right tabs -->
<ul class="nav nav-tabs">
<li class="active"><a href="#tab1" data-toggle="tab">Control</a></li>
+ <li><a href="#tab5" data-toggle="tab">Colorpicker</a></li>
<li><a href="#tab2" data-toggle="tab">Twitter</a></li>
<li><a href="#tab3" data-toggle="tab">Gmail</a></li>
<li><a href="#tab4" data-toggle="tab">Email</a></li>
@@ -110,6 +111,14 @@
</div> <!-- end of tab1 -->
+
+ <div class="tab-pane" id="tab5">
+ <h1> Colorpicker </h1>
+ <p> pick a color </p>
+
+
+ </div>
+
<div class="tab-pane" id="tab2">
<h1> Twitter </h1>
View
4 processing/Blink1Test1/Blink1Test1.pde
@@ -64,9 +64,9 @@ void updateBlink1() {
int gn = log2lin(g);
int bn = log2lin(b);
- println("r,g,b: (lin)"+r+","+g+","+b + " => (log)"+rn+","+gn+","+bn);
+ println("r,g,b: (lin)"+r+","+g+","+b + " ==> (log)"+rn+","+gn+","+bn);
//blink1.open();
- blink1.setRGB( rn, gn, bn );
+ blink1.setRGB( r, g, b );
//blink1.close();
}
Please sign in to comment.
Something went wrong with that request. Please try again.