Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

win app: apparently got color pattern play timer working

  • Loading branch information...
commit 826a3c2493e764614d076737d36681f3683aa08d 1 parent 76bf48f
@todbot authored
View
66 windows/Blink1Control/Blink1Control/Blink1Pattern.cs
@@ -2,11 +2,13 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
+using System.Threading;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing;
using System.Globalization;
+using Blink1Lib;
namespace Blink1Control
@@ -20,6 +22,7 @@ class Blink1Pattern
List<Color> colors;
List<float> times;
+ Timer timer;
/// <summary>
/// number of repeats for this pattern (0=play forever)
@@ -33,20 +36,31 @@ class Blink1Pattern
/// <summary>
/// Is this pattern playing
/// </summary>
- public Boolean playing { get; set; }
+ public Boolean playing { get; set; } // FIXME: how to make this read-only except in this class?
+ /// <summary>
+ /// String representation of pattern repeats, colors & times
+ /// </summary>
public string pattern
{
get
{
List<string> sa = new List<string>();
for (int i = 0; i < colors.Count; i++) {
- sa.Add(System.Drawing.ColorTranslator.ToHtml(colors[i]));
+ sa.Add(ColorTranslator.ToHtml(colors[i]));
sa.Add(times[i].ToString("F2", CultureInfo.InvariantCulture));
}
return String.Join(",", sa);
}
}
+
+ public Blink1Server blink1Server { get; set; } //
+
+ /// <summary>
+ /// Constructor
+ /// </summary>
+ /// <param name="aName"></param>
+ /// <param name="patternstr"></param>
public Blink1Pattern(string aName, string patternstr)
{
name = aName;
@@ -79,7 +93,7 @@ public Boolean parsePatternStr(string patternstr)
try {
string rgbstr = values[1 + i + 0];
string secstr = values[1 + i + 1];
- colr = System.Drawing.ColorTranslator.FromHtml(rgbstr);
+ colr = ColorTranslator.FromHtml(rgbstr);
secs = float.Parse(secstr, CultureInfo.InvariantCulture);
}
catch (Exception e) { Console.WriteLine(e.ToString()); }
@@ -96,16 +110,60 @@ public Boolean parsePatternStr(string patternstr)
/// </summary>
public void play()
{
+ playpos = 0;
+ playcount = 0;
+ playing = true;
+
+ Color colr = colors[playpos];
+ float nextTime = times[playpos];
+
+ float fadeTime = nextTime / 2;
+ blink1Server.fadeToRGB(fadeTime, colr);
+ timer = new Timer( update, null, (int)(nextTime*1000), 0);
}
+
+ /// <summary>
+ /// Called whenever a new color needs to be played, via timer
+ /// </summary>
+ public void update(Object stateInfo)
+ {
+ if (!playing) return;
+ Console.WriteLine("update! "+name);
+
+ Boolean scheduleNext = true;
+ playpos++;
+ if( playpos == times.Count() ) {
+ playpos = 0;
+ if( repeats != 0 ) { // infinite
+ playcount++;
+ if( playcount == repeats ) {
+ scheduleNext = false;
+ }
+ }
+ }
+
+ if( scheduleNext ) {
+ float nextTime = times[playpos];
+ Color color = colors[playpos];
+ Console.WriteLine("update: scheduleNext: "+nextTime);
+ //DLog(@"%@ updt p:%d c:%d %@ nextTime:%f",name,playpos,playcount,[Blink1 hexStringFromColor:color],nextTime);
+ blink1Server.fadeToRGB(nextTime/2, color);
+ timer = new Timer( update, null, (int)(nextTime*1000), 0);
+ } else {
+ playing = false;
+ }
+ }
+
/// <summary>
/// Stop a pattern playing
/// </summary>
public void stop()
{
-
+ playing = false;
}
+
public override string ToString()
{
return "{name:"+name+",pattern:"+pattern+",repeats:"+repeats+",playing:"+playing+"}";
View
106 windows/Blink1Control/Blink1Control/blink1ControlServer.cs
@@ -84,11 +84,28 @@ public Blink1Server()
pattadd.GetStringResponse = blink1PatternAdd;
blink1dir.AddFile(pattadd);
+ Blink1JSONFile pattdel = new Blink1JSONFile("patterndel", blink1dir, this);
+ pattdel.GetStringResponse = blink1PatternDel;
+ blink1dir.AddFile(pattdel);
+
+ Blink1JSONFile pattdelall = new Blink1JSONFile("patterndelall", blink1dir, this);
+ pattdelall.GetStringResponse = blink1PatternDelAll;
+ blink1dir.AddFile(pattdelall);
+
+ Blink1JSONFile pattplay = new Blink1JSONFile("patternplay", blink1dir, this);
+ pattplay.GetStringResponse = blink1PatternPlay;
+ blink1dir.AddFile(pattplay);
+
+ Blink1JSONFile pattstop = new Blink1JSONFile("patternstop", blink1dir, this);
+ pattstop.GetStringResponse = blink1PatternStop;
+ blink1dir.AddFile(pattstop);
+
+
/*
- Blink1JSONFile id2 = new Blink1JSONFile("id2", blink1dir, blink1);
- id2.GetStringResponse = blink1Id2;
- blink1dir.AddFile(id2); //add a virtual file for each json method
- */
+ Blink1JSONFile id2 = new Blink1JSONFile("id2", blink1dir, blink1);
+ id2.GetStringResponse = blink1Id2;
+ blink1dir.AddFile(id2); //add a virtual file for each json method
+ */
root.AddDirectory(blink1dir);
@@ -101,6 +118,18 @@ public Blink1Server()
}
}
+ /// <summary>
+ /// Same as Blink1.fadeToRGB, but does an open/close around it
+ /// </summary>
+ public void fadeToRGB(double secs, Color c)
+ {
+ Console.WriteLine("fadeToRGB: rgb:" + ColorTranslator.ToHtml(c) + " secs:" + secs);
+ blink1.open();
+ blink1.fadeToRGB((int)(secs * 1000), c.R, c.G, c.B);
+ blink1.close();
+ }
+
+ // why do we need this?
public delegate string GetJSONStringResponse(HttpRequest request, Blink1Server aBlink1Server);
// /blink1/id -- Display blink1_id and blink1 serial numbers (if any)
@@ -166,7 +195,6 @@ static string blink1RegenerateBlink1Id(HttpRequest request, Blink1Server blink1S
// /blink1/fadeToRGB -- Send fadeToRGB command to blink(1) with hex color & fade time
static string blink1FadeToRGB(HttpRequest request, Blink1Server blink1Server)
{
- Blink1 blink1 = blink1Server.blink1;
// FIXME: stop pattern player
//NameValueCollection query = request.Query;
string rgbstr = request.Query.Get("rgb");
@@ -176,7 +204,7 @@ static string blink1FadeToRGB(HttpRequest request, Blink1Server blink1Server)
Color colr = System.Drawing.ColorTranslator.FromHtml(rgbstr);
float secs = float.Parse(timestr, CultureInfo.InvariantCulture);
- blink1.simpleFadeToRGB(secs, colr);
+ blink1Server.fadeToRGB(secs, colr);
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", "fadeToRGB");
@@ -188,8 +216,7 @@ static string blink1FadeToRGB(HttpRequest request, Blink1Server blink1Server)
// /blink1/on -- Stop pattern playback and send fadeToRGB command to blink(1) with #FFFFFF & 0.1 sec fade time
static string blink1On(HttpRequest request, Blink1Server blink1Server)
{
- Blink1 blink1 = blink1Server.blink1;
- blink1.simpleFadeToRGB(0.1, Color.White);
+ blink1Server.fadeToRGB(0.1, Color.White);
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", "on");
@@ -199,8 +226,7 @@ static string blink1On(HttpRequest request, Blink1Server blink1Server)
// /blink1/off -- Stop pattern playback and send fadeToRGB command to blink(1) with #000000 & 0.1 sec fade time
static string blink1Off(HttpRequest request, Blink1Server blink1Server)
{
- Blink1 blink1 = blink1Server.blink1;
- blink1.simpleFadeToRGB(0.1, Color.Black);
+ blink1Server.fadeToRGB(0.1, Color.Black);
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", "off");
@@ -250,10 +276,57 @@ static string blink1PatternDel(HttpRequest request, Blink1Server blink1Server)
Dictionary<string, object> result = new Dictionary<string, object>();
result.Add("status", statusstr);
- result.Add("pattern", patt.ToString());
return JsonConvert.SerializeObject(result, Formatting.Indented);
}
+ // /blink1/pattern/delall -- Remove all color patterns from color pattern list
+ static string blink1PatternDelAll(HttpRequest request, Blink1Server blink1Server)
+ {
+ foreach (KeyValuePair<string, Blink1Pattern> kvp in blink1Server.patterns) {
+ kvp.Value.stop();
+ }
+ blink1Server.patterns.Clear();
+ Dictionary<string, object> result = new Dictionary<string, object>();
+ result.Add("status", "all patterns removed");
+ return JsonConvert.SerializeObject(result, Formatting.Indented);
+ }
+
+ // /blink1/pattern/play -- Play/test a specific color pattern
+ static string blink1PatternPlay(HttpRequest request, Blink1Server blink1Server)
+ {
+ string pname = request.Query.Get("pname");
+ string statusstr = "no pattern by that name";
+ if (pname != null) {
+ Blink1Pattern patt = null ;
+ if( blink1Server.patterns.TryGetValue(pname, out patt)) {
+ patt.blink1Server = blink1Server; // justin case
+ patt.play();
+ statusstr = "pattern '" + pname + "' playing";
+ }
+ }
+ Dictionary<string, object> result = new Dictionary<string, object>();
+ result.Add("status", statusstr);
+ return JsonConvert.SerializeObject(result, Formatting.Indented);
+ }
+
+ // /blink1/pattern/stop -- Stop a pattern playback, for a given pattern or all patterns
+ static string blink1PatternStop(HttpRequest request, Blink1Server blink1Server)
+ {
+ string pname = request.Query.Get("pname");
+ string statusstr = "no pattern by that name";
+ if (pname != null) {
+ Blink1Pattern patt = null ;
+ if( blink1Server.patterns.TryGetValue(pname, out patt) ) {
+ patt.stop();
+ statusstr = "pattern '" + pname + "' stopped";
+ }
+ }
+ Dictionary<string, object> result = new Dictionary<string, object>();
+ result.Add("status", statusstr);
+ return JsonConvert.SerializeObject(result, Formatting.Indented);
+ }
+
+
// /blink1/lastColor -- Return the last color command sent to blink(1)
//Input Selection //interface methods to patterns and watchers
@@ -281,13 +354,8 @@ static string blink1PatternDel(HttpRequest request, Blink1Server blink1Server)
-// /blink1/pattern/delall -- Remove all color patterns from color pattern list
-
-// /blink1/pattern/play -- Play/test a specific color pattern
-
-// /blink1/pattern/stop -- Stop a pattern playback, for a given pattern or all patterns
-
+ // testing
static string blink1Id2(HttpRequest request, Blink1 blink1)//example
{
Properties.Settings.Default["blink1Id"] = blink1.getBlink1Id();
@@ -297,7 +365,9 @@ static string blink1PatternDel(HttpRequest request, Blink1Server blink1Server)
return @"\n\n{ suck it }\n\n";
}
-
+ /// <summary>
+ /// Weirdass wrapper for binding urls to funcblocks, why do we need this?
+ /// </summary>
public class Blink1JSONFile : IFile
{
public Blink1Server blink1Server;
View
10 windows/Blink1Lib/Blink1Lib/Blink1.cs
@@ -121,16 +121,6 @@ public void fadeToRGB(int millis, int r, int g, int b)
}
/// <summary>
- /// Same as fadeToRGB, but does an open/close around it
- /// </summary>
- public void simpleFadeToRGB(double secs, Color c)
- {
- open();
- fadeToRGB((int)(secs*1000), c.R, c.G, c.B);
- close();
- }
-
- /// <summary>
/// FIXME: make more like Blink1.m enumerate
/// </summary>
/// <returns>number of blink1 devices</returns>
Please sign in to comment.
Something went wrong with that request. Please try again.