Source code for the Curilights Controller. See http://www.saccade.com/writing/projects/CuriController/ for details.
Dependencies: FatFileSystem mbed
This is the source code for the Curilights controller. This lets you interactively control a string of Curilights. It provides a simple click-wheel user interface for changing colors, brightness and behavior. It responds to movement and lighting.
Finished Controller
System Block Diagram
Diff: LightString.h
- Revision:
- 0:6da5625a6946
- Child:
- 2:965388eecf95
- Child:
- 3:0ac64c4ca40f
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LightString.h Thu Dec 29 01:59:53 2011 +0000 @@ -0,0 +1,95 @@ +// +// Tools for talking to the CuriLight string +// + +#ifndef __LIGHTSTRING__ +#define __LIGHTSTRING__ + +#ifndef MBED_H +#include "mbed.h" +#endif + +#ifndef __LIGHTSNOOP__ +#include "LightSnoop.h" +#endif + +#include <vector> + +class LightString +{ +public: + + // Note: We rely on the default serial configuration of 9600 8N1, + // this matches the CuriLights serial parameters. + LightString( PinName pin, int numLights ) + : fPort( pin, NC ), fUSBPort( NC, USBRX ), fSnoop( numLights ) + { + fNumLights = numLights; + } + + // This sends an initialization to the string and configures it. + // If numLights is non-zero, the length of the light string is reset + void InitLights( int numLights = 0 ); + + int GetNumLights() const { return fNumLights; } + + // The light code uses a "decimal RGB" notation, where + // the color is specified as a three digit number, with each + // digit from 0..7. The first (hundreds) digit specifies + // red, the second (tens) digit specifies green, and + // the last (ones) blue. So, for example, "700" specifies + // red, and "755" is pink. + + void SetAllLights( int color ); + + // Set a single light (id == the Nth light in the string, starting from zero) + void SetOneColor( int color, uint8_t id ); + + // Set each light to the value in the corresponding array. + void SetColors( const vector<int>& colorList ); + + void Off() { SetAllLights( 0 ); } + + void Red() { SetAllLights( 700 ); } + + void Green() { SetAllLights( 70 ); } + + void Blue() { SetAllLights( 7 ); } + + void Ouch() { SetAllLights( 777 ); } + + // Attaches the USB port to the light port, so + // data is passed straight through from the host to the lights. + void AttachUSBSerial(); + + LightSnoop * Snoop() { return &fSnoop; } + +private: + void sendCommand1( uint8_t ch ); + void sendCommand2( uint8_t ch1, uint8_t ch2 ); + void sendCommand3( uint8_t ch1, uint8_t ch2, uint8_t ch3 ); + + void HandleUSBToLights(); + + // Note the top red bit is stripped off to fit in the byte, other + // code must manually add it back to the top bit if the light ID + uint8_t colorByte( uint8_t r, uint8_t g, uint8_t b ) + { return ((r & 3) << 6) | (g << 3) | b; } + + uint8_t colorByte( int rgb, uint8_t& redBit ) + { redBit = ((rgb / 100) >> 2) << 7; + return colorByte( rgb/100, (rgb % 100) / 10, rgb % 10 ); + } + +/* uint8_t color( unsigned char * rgb, uint8_t& redBit ) + { + redBit = ((rgb[0] - '0') >> 2) << 7; + return color( rgb[0] - '0', rgb[1] - '0', rgb[2] - '0' ); + } +*/ + Serial fPort; + Serial fUSBPort; + int fNumLights; + LightSnoop fSnoop; +}; +#endif