Difference between revisions of "WingScreen"

From Noisebridge
Jump to: navigation, search
(StripHSV now w/Serial)
m (Desktop Emulator)
Line 176: Line 176:
 
== Desktop Emulator ==
 
== Desktop Emulator ==
 
[[File:HSV.jpg|960px]]
 
[[File:HSV.jpg|960px]]
 +
 +
See also: [[FlashDevelop]]
 +
  
 
=== Main ===
 
=== Main ===

Revision as of 22:25, 12 September 2014

A side wing for teh screen

                                               /---------\
                                               |         |
                                  RGB LEDs --  *         |
                                           \   |         |
            .__                             -  *         |
    __  _  _|__| ____    ____                  |         |
    \ \/ \/ /  |/    \  / ___\                 *         |
     \     /|  |   |  \/ /_/  >                |         |
      \/\_/ |__|___|  /\___  /                 *         |
                    \//_____/                  |         |
   ('''' .|'', '||''| .|''|, .|''|, `||''|,    *         |
    `'') ||     ||    ||..|| ||..||  ||  ||    |         |
   `...' `|..' .||.   `|...  `|...  .||  ||.   *         |
                                               |         |
                                               *         |
                                               |         |
                                               *         |
                                               |         |
                                               *         |
                                               |         |
                      /---------\              *         |
                      |  micro  |              |         |
   USB-----|~|--------|  puter  |              *         |
                      \---------/              |         |
                             \                 *         |
                              \----------------|         |
                                               \---------/

StripSerial

Rainbeau test sketch for strip of addressable RGB LEDs.

Requires http://fastled.io/ library.

Updated with optional remote serial control from PC. Six26 (talk) 22:19, 12 September 2014 (UTC)

/*
*  Serial control interface for LED Strip
*    Command Summary:
*      MODE0 == Auto (Rainbeau)
*      MODE1 == Off
*      MODE2 == Remote (PC Control)
*      HH..H == Sequence of Hue values to apply to LEDs
*        Note: Set "MODE2" before sending hues
*/

#include "FastLED.h"

// How many leds in your strip?
#define NUM_LEDS 12

// Spacing between pixel/color
#define HSV_PAD 22

// Delay between cycles
#define LOOP_DELAY 20

#define DATA_PIN 11

// Define the array of leds
CRGB leds[NUM_LEDS];

byte count = 0;

byte mode = 0;
byte hues[NUM_LEDS];

String inputString = "";

void setup() { 
  
  for (int i = 0; i < NUM_LEDS; i++)
  {
    hues[i] = 0;
  }
  
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  
  FastLED.setBrightness(64);
  
  Serial.begin(9600);
  Serial.setTimeout(50);
  
  delay(200);
}

void loop() {
  int i;
  
  if (mode == 0)
  {
    // Rainbeau
    for (i = 0; i < NUM_LEDS; i++)
    {
      leds[i] = CHSV(count + (i * HSV_PAD), 255, 255);
    }
    FastLED.show();
    count++;
    
    delay(LOOP_DELAY);
  }
  else if (mode == 1)
  {
    // Off
    for (i = 0; i < NUM_LEDS; i++)
    {
      leds[i] = CRGB(0, 0, 0);
    }
    FastLED.show();
    
    delay(LOOP_DELAY);
  }
  else if (mode == 2)
  {
    // Remote
  }
}

void updateHues() {
  for (int i = 0; i < NUM_LEDS; i++)
  {
    leds[i] = CHSV(hues[i], 255, 255);
  }
  FastLED.show();
}

void serialEvent() {
  byte count = 0;
  byte data;
  
  inputString = "";
  
  while(Serial.available())
  {
    data = Serial.read();
    inputString += (char)data;
    if (count < NUM_LEDS)
    {
      hues[count++] = data;
    }
    
    // Catch pending bits
    if (!Serial.available())
      delay(20);
  }
  
  Serial.println(inputString);
  
  if (inputString == "MODE0")
  {
    mode = 0;
  }
  else if (inputString == "MODE1")
  {
    mode = 1;
  }
  else if (inputString == "MODE2")
  {
    mode = 2;
  }
  else if (inputString.length() == NUM_LEDS)
  {
    updateHues();
  }
  else
  {
    // WTF?
  }
}

Desktop Emulator

HSV.jpg

See also: FlashDevelop


Main

package 
{
	import flash.display.Screen;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.geom.Rectangle;
	
	/**
	 * ...
	 * @author thex
	 */
	public class Main extends Sprite 
	{
		private var wing:Wing;
		
		public function Main():void 
		{
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			initUI();
		}
		
		private function initUI():void
		{			
			var appBounds:Rectangle = stage.nativeWindow.bounds;
			var screen:Screen = Screen.getScreensForRectangle(appBounds)[0];
			var screenSize:Rectangle = screen.bounds;
			
			/* Set in compile config
			  stage.nativeWindow.x = 0;
			  stage.nativeWindow.y = 0;
			*/
			  
			stage.nativeWindow.width = screenSize.width;
			
			wing = new Wing(12, screenSize.width, stage.stageHeight);
			addChild(wing);
		}
		
	}
	
}

Wing

package  
{
	import flash.display.GradientType;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author thex
	 */
	public class Wing extends Sprite 
	{
		private var count:int = 0;
		private var dots:Array = new Array();
		
		public function Wing(dotCount:int, wingWidth:int, wingHeight:int ) 
		{
			trace("Wing: " + wingWidth.toString() + ", " + wingHeight.toString());
			
			var matrix:Matrix = new Matrix();
			matrix.createGradientBox(wingWidth, wingHeight, 1.571, 0, 0);			
			
			this.graphics.beginGradientFill(GradientType.LINEAR, [0x3f3f3f, 0x000000], [1, 1], [0, 220], matrix);
			this.graphics.drawRect(0, 0, wingWidth, wingHeight);
			
			count = dotCount;
			
			initDots();
			
			this.addEventListener(Event.ENTER_FRAME, frameHandler);
		}
		
		private function initDots():void
		{
			var dot:Dot;
			var xOff:Number = (width - (count * Dot.WIDTH)) / count;
			
			for (var i:int = 0; i < count; i++)
			{
				dot = new Dot();
				dot.x = i * (xOff + Dot.WIDTH) + (xOff/2);
				dot.y = height - Dot.HEIGHT;
				
				dot.draw(HSVColor.HSV2RGB(i * 30));
				
				addChild(dot);
				
				dots.push(dot);
			}
		}
		
		private function update():void
		{
			var dot:Dot;
			var hue:Number;
			
			var phase:Number = (getTimer() / 20);
			
			for (var i:int = 0; i < count; i++)
			{
				dot = dots[i];
				
				// Magiq time Rainbeau
				hue = i * 30 + phase;
				hue %= 360;
				
				dot.draw(HSVColor.HSV2RGB(hue));
			}
		}
		
		private function frameHandler(e:Event):void
		{
			update();
		}
		
	}

}

Dot

package  
{
	import flash.display.Sprite;
	import flash.filters.GlowFilter;
	
	/**
	 * ...
	 * @author thex
	 */
	public class Dot extends Sprite 
	{
		public static var WIDTH:int = 64;
		public static var HEIGHT:int = 16;
		
		private static var OFF_COLOR:int = 0xcfcfcf;
		
		private var glowFilter:GlowFilter;
		
		public function Dot() 
		{			
			draw(OFF_COLOR);
		}
		
		public function draw(color:int):void
		{
			this.graphics.beginFill(color);
			this.graphics.drawRect(0, 0, WIDTH, HEIGHT);
			
			glowFilter = new GlowFilter(color, 1, 96, 256, 24, 1);
			filters = [glowFilter];
		}
		
	}

}

HSVColor

package  
{
	/**
	 * ...
	 * @author thex
	 */
	public class HSVColor 
	{
		
		public function HSVColor() 
		{
			
		}
		
		public static function HSV2RGB(h:Number, s:Number = 1.0, v:Number = 1.0):int
		{
			/*
			 * 
			 *  Ported from:
			 *   http://www.cs.rit.edu/~ncs/color/t_convert.html
			 * 
			 *  h = 0-360
			 *  s = 0-1, v = 0-1
			 */
			
			var r:Number;
			var g:Number;
			var b:Number;
			
			var i:int;
			
			var f:Number;
			var p:Number;
			var q:Number;
			var t:Number;

			if( s == 0 ) {
				// achromatic (grey)
				r = g = b = v;
				return 256 * 256 * r + 256 * g + b;
			}

			h /= 60.0;			// sector 0 to 5
			i = Math.floor( h );
			f = h - i;			// factorial part of h
			p = v * ( 1 - s );
			q = v * ( 1 - s * f );
			t = v * ( 1 - s * ( 1 - f ) );

			switch( i ) {
				case 0:
					r = v;
					g = t;
					b = p;
					break;
				case 1:
					r = q;
					g = v;
					b = p;
					break;
				case 2:
					r = p;
					g = v;
					b = t;
					break;
				case 3:
					r = p;
					g = q;
					b = v;
					break;
				case 4:
					r = t;
					g = p;
					b = v;
					break;
				default:		// case 5:
					r = v;
					g = p;
					b = q;
					break;
			}
			
			r = Math.round(r * 255);
			g = Math.round(g * 255);
			b = Math.round(b * 255);
			
			return 256 * 256 * r + 256 * g + b;
		}
		
	}

}