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
LightString.h@2:965388eecf95, 2012-01-16 (annotated)
- Committer:
- isonno
- Date:
- Mon Jan 16 06:33:34 2012 +0000
- Revision:
- 2:965388eecf95
- Parent:
- 0:6da5625a6946
Experiment with MODSERIAL - just hung....
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
isonno |
0:6da5625a6946 | 1 | // |
isonno |
0:6da5625a6946 | 2 | // Tools for talking to the CuriLight string |
isonno |
0:6da5625a6946 | 3 | // |
isonno |
0:6da5625a6946 | 4 | |
isonno |
0:6da5625a6946 | 5 | #ifndef __LIGHTSTRING__ |
isonno |
0:6da5625a6946 | 6 | #define __LIGHTSTRING__ |
isonno |
0:6da5625a6946 | 7 | |
isonno |
0:6da5625a6946 | 8 | #ifndef MBED_H |
isonno |
0:6da5625a6946 | 9 | #include "mbed.h" |
isonno |
0:6da5625a6946 | 10 | #endif |
isonno |
0:6da5625a6946 | 11 | |
isonno |
2:965388eecf95 | 12 | // Use ModSerial, Andy Kirkham's interrupt based serial library. |
isonno |
2:965388eecf95 | 13 | #ifndef MODSERIAL_H |
isonno |
2:965388eecf95 | 14 | // For a 128 light string, setting all colors requires 3*127=384 |
isonno |
2:965388eecf95 | 15 | #define MODSERIAL_DEFAULT_RX_BUFFER_SIZE 384 |
isonno |
2:965388eecf95 | 16 | #define MODSERIAL_DEFAULT_TX_BUFFER_SIZE 128 |
isonno |
2:965388eecf95 | 17 | #include <MODSERIAL.h> |
isonno |
2:965388eecf95 | 18 | #endif |
isonno |
2:965388eecf95 | 19 | |
isonno |
0:6da5625a6946 | 20 | #ifndef __LIGHTSNOOP__ |
isonno |
0:6da5625a6946 | 21 | #include "LightSnoop.h" |
isonno |
0:6da5625a6946 | 22 | #endif |
isonno |
0:6da5625a6946 | 23 | |
isonno |
0:6da5625a6946 | 24 | #include <vector> |
isonno |
0:6da5625a6946 | 25 | |
isonno |
0:6da5625a6946 | 26 | class LightString |
isonno |
0:6da5625a6946 | 27 | { |
isonno |
0:6da5625a6946 | 28 | public: |
isonno |
0:6da5625a6946 | 29 | |
isonno |
0:6da5625a6946 | 30 | // Note: We rely on the default serial configuration of 9600 8N1, |
isonno |
0:6da5625a6946 | 31 | // this matches the CuriLights serial parameters. |
isonno |
2:965388eecf95 | 32 | LightString( PinName pin, int numLights ); |
isonno |
0:6da5625a6946 | 33 | |
isonno |
0:6da5625a6946 | 34 | // This sends an initialization to the string and configures it. |
isonno |
0:6da5625a6946 | 35 | // If numLights is non-zero, the length of the light string is reset |
isonno |
0:6da5625a6946 | 36 | void InitLights( int numLights = 0 ); |
isonno |
0:6da5625a6946 | 37 | |
isonno |
0:6da5625a6946 | 38 | int GetNumLights() const { return fNumLights; } |
isonno |
0:6da5625a6946 | 39 | |
isonno |
0:6da5625a6946 | 40 | // The light code uses a "decimal RGB" notation, where |
isonno |
0:6da5625a6946 | 41 | // the color is specified as a three digit number, with each |
isonno |
0:6da5625a6946 | 42 | // digit from 0..7. The first (hundreds) digit specifies |
isonno |
0:6da5625a6946 | 43 | // red, the second (tens) digit specifies green, and |
isonno |
0:6da5625a6946 | 44 | // the last (ones) blue. So, for example, "700" specifies |
isonno |
0:6da5625a6946 | 45 | // red, and "755" is pink. |
isonno |
0:6da5625a6946 | 46 | |
isonno |
0:6da5625a6946 | 47 | void SetAllLights( int color ); |
isonno |
0:6da5625a6946 | 48 | |
isonno |
0:6da5625a6946 | 49 | // Set a single light (id == the Nth light in the string, starting from zero) |
isonno |
0:6da5625a6946 | 50 | void SetOneColor( int color, uint8_t id ); |
isonno |
0:6da5625a6946 | 51 | |
isonno |
0:6da5625a6946 | 52 | // Set each light to the value in the corresponding array. |
isonno |
0:6da5625a6946 | 53 | void SetColors( const vector<int>& colorList ); |
isonno |
0:6da5625a6946 | 54 | |
isonno |
0:6da5625a6946 | 55 | void Off() { SetAllLights( 0 ); } |
isonno |
0:6da5625a6946 | 56 | |
isonno |
0:6da5625a6946 | 57 | void Red() { SetAllLights( 700 ); } |
isonno |
0:6da5625a6946 | 58 | |
isonno |
0:6da5625a6946 | 59 | void Green() { SetAllLights( 70 ); } |
isonno |
0:6da5625a6946 | 60 | |
isonno |
0:6da5625a6946 | 61 | void Blue() { SetAllLights( 7 ); } |
isonno |
0:6da5625a6946 | 62 | |
isonno |
0:6da5625a6946 | 63 | void Ouch() { SetAllLights( 777 ); } |
isonno |
0:6da5625a6946 | 64 | |
isonno |
0:6da5625a6946 | 65 | // Attaches the USB port to the light port, so |
isonno |
0:6da5625a6946 | 66 | // data is passed straight through from the host to the lights. |
isonno |
0:6da5625a6946 | 67 | void AttachUSBSerial(); |
isonno |
0:6da5625a6946 | 68 | |
isonno |
0:6da5625a6946 | 69 | LightSnoop * Snoop() { return &fSnoop; } |
isonno |
0:6da5625a6946 | 70 | |
isonno |
0:6da5625a6946 | 71 | private: |
isonno |
0:6da5625a6946 | 72 | void sendCommand1( uint8_t ch ); |
isonno |
0:6da5625a6946 | 73 | void sendCommand2( uint8_t ch1, uint8_t ch2 ); |
isonno |
0:6da5625a6946 | 74 | void sendCommand3( uint8_t ch1, uint8_t ch2, uint8_t ch3 ); |
isonno |
0:6da5625a6946 | 75 | |
isonno |
2:965388eecf95 | 76 | void HandleUSBToLights(MODSERIAL_IRQ_INFO * serialInfo); |
isonno |
0:6da5625a6946 | 77 | |
isonno |
0:6da5625a6946 | 78 | // Note the top red bit is stripped off to fit in the byte, other |
isonno |
0:6da5625a6946 | 79 | // code must manually add it back to the top bit if the light ID |
isonno |
0:6da5625a6946 | 80 | uint8_t colorByte( uint8_t r, uint8_t g, uint8_t b ) |
isonno |
0:6da5625a6946 | 81 | { return ((r & 3) << 6) | (g << 3) | b; } |
isonno |
0:6da5625a6946 | 82 | |
isonno |
0:6da5625a6946 | 83 | uint8_t colorByte( int rgb, uint8_t& redBit ) |
isonno |
0:6da5625a6946 | 84 | { redBit = ((rgb / 100) >> 2) << 7; |
isonno |
0:6da5625a6946 | 85 | return colorByte( rgb/100, (rgb % 100) / 10, rgb % 10 ); |
isonno |
0:6da5625a6946 | 86 | } |
isonno |
0:6da5625a6946 | 87 | |
isonno |
0:6da5625a6946 | 88 | /* uint8_t color( unsigned char * rgb, uint8_t& redBit ) |
isonno |
0:6da5625a6946 | 89 | { |
isonno |
0:6da5625a6946 | 90 | redBit = ((rgb[0] - '0') >> 2) << 7; |
isonno |
0:6da5625a6946 | 91 | return color( rgb[0] - '0', rgb[1] - '0', rgb[2] - '0' ); |
isonno |
0:6da5625a6946 | 92 | } |
isonno |
0:6da5625a6946 | 93 | */ |
isonno |
2:965388eecf95 | 94 | MODSERIAL fPort; |
isonno |
2:965388eecf95 | 95 | MODSERIAL fUSBPort; |
isonno |
0:6da5625a6946 | 96 | int fNumLights; |
isonno |
0:6da5625a6946 | 97 | LightSnoop fSnoop; |
isonno |
0:6da5625a6946 | 98 | }; |
isonno |
0:6da5625a6946 | 99 | #endif |