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.cpp@0:6da5625a6946, 2011-12-29 (annotated)
- Committer:
- isonno
- Date:
- Thu Dec 29 01:59:53 2011 +0000
- Revision:
- 0:6da5625a6946
- Child:
- 2:965388eecf95
- Child:
- 3:0ac64c4ca40f
Contest submission version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
isonno |
0:6da5625a6946 | 1 | // |
isonno |
0:6da5625a6946 | 2 | // Interface to the CuriLights |
isonno |
0:6da5625a6946 | 3 | // |
isonno |
0:6da5625a6946 | 4 | |
isonno |
0:6da5625a6946 | 5 | #include "LightString.h" |
isonno |
0:6da5625a6946 | 6 | |
isonno |
0:6da5625a6946 | 7 | #include "HoldInterrupts.h" |
isonno |
0:6da5625a6946 | 8 | |
isonno |
0:6da5625a6946 | 9 | void LightString::AttachUSBSerial() |
isonno |
0:6da5625a6946 | 10 | { |
isonno |
0:6da5625a6946 | 11 | fUSBPort.attach( this, &LightString::HandleUSBToLights ); |
isonno |
0:6da5625a6946 | 12 | } |
isonno |
0:6da5625a6946 | 13 | |
isonno |
0:6da5625a6946 | 14 | void LightString::HandleUSBToLights() |
isonno |
0:6da5625a6946 | 15 | { |
isonno |
0:6da5625a6946 | 16 | uint8_t buffer[512]; |
isonno |
0:6da5625a6946 | 17 | int i, count = 0; |
isonno |
0:6da5625a6946 | 18 | while (fUSBPort.readable() && (count < sizeof(buffer))) |
isonno |
0:6da5625a6946 | 19 | { |
isonno |
0:6da5625a6946 | 20 | buffer[count++] = fUSBPort.getc(); |
isonno |
0:6da5625a6946 | 21 | } |
isonno |
0:6da5625a6946 | 22 | for (i = 0; i < count; ++i) |
isonno |
0:6da5625a6946 | 23 | { |
isonno |
0:6da5625a6946 | 24 | while (! fPort.writeable()) {}; // Spin wait on the output |
isonno |
0:6da5625a6946 | 25 | fPort.putc( buffer[i] ); |
isonno |
0:6da5625a6946 | 26 | } |
isonno |
0:6da5625a6946 | 27 | } |
isonno |
0:6da5625a6946 | 28 | |
isonno |
0:6da5625a6946 | 29 | void LightString::sendCommand1( uint8_t ch ) |
isonno |
0:6da5625a6946 | 30 | { |
isonno |
0:6da5625a6946 | 31 | HoldInterrupts noint(); |
isonno |
0:6da5625a6946 | 32 | |
isonno |
0:6da5625a6946 | 33 | fPort.putc( ch ); |
isonno |
0:6da5625a6946 | 34 | } |
isonno |
0:6da5625a6946 | 35 | |
isonno |
0:6da5625a6946 | 36 | void LightString::sendCommand2( uint8_t ch1, uint8_t ch2 ) |
isonno |
0:6da5625a6946 | 37 | { |
isonno |
0:6da5625a6946 | 38 | HoldInterrupts noint(); |
isonno |
0:6da5625a6946 | 39 | fPort.putc( ch1 ); |
isonno |
0:6da5625a6946 | 40 | fPort.putc( ch2 ); |
isonno |
0:6da5625a6946 | 41 | } |
isonno |
0:6da5625a6946 | 42 | |
isonno |
0:6da5625a6946 | 43 | void LightString::sendCommand3( uint8_t ch1, uint8_t ch2, uint8_t ch3 ) |
isonno |
0:6da5625a6946 | 44 | { |
isonno |
0:6da5625a6946 | 45 | HoldInterrupts noint(); |
isonno |
0:6da5625a6946 | 46 | fPort.putc( ch1 ); |
isonno |
0:6da5625a6946 | 47 | fPort.putc( ch2 ); |
isonno |
0:6da5625a6946 | 48 | fPort.putc( ch3 ); |
isonno |
0:6da5625a6946 | 49 | } |
isonno |
0:6da5625a6946 | 50 | |
isonno |
0:6da5625a6946 | 51 | void LightString::InitLights( int numLights ) |
isonno |
0:6da5625a6946 | 52 | { |
isonno |
0:6da5625a6946 | 53 | if (numLights) |
isonno |
0:6da5625a6946 | 54 | fNumLights = numLights; |
isonno |
0:6da5625a6946 | 55 | |
isonno |
0:6da5625a6946 | 56 | printf("Init called\n\r"); |
isonno |
0:6da5625a6946 | 57 | sendCommand2( 'I', 0 ); |
isonno |
0:6da5625a6946 | 58 | sendCommand2( 'N', fNumLights ); |
isonno |
0:6da5625a6946 | 59 | } |
isonno |
0:6da5625a6946 | 60 | |
isonno |
0:6da5625a6946 | 61 | void LightString::SetOneColor( int color, uint8_t id ) |
isonno |
0:6da5625a6946 | 62 | { |
isonno |
0:6da5625a6946 | 63 | uint8_t redBit, c; |
isonno |
0:6da5625a6946 | 64 | |
isonno |
0:6da5625a6946 | 65 | c = colorByte( color, redBit ); |
isonno |
0:6da5625a6946 | 66 | |
isonno |
0:6da5625a6946 | 67 | sendCommand3( 'C', id | redBit, c ); |
isonno |
0:6da5625a6946 | 68 | } |
isonno |
0:6da5625a6946 | 69 | |
isonno |
0:6da5625a6946 | 70 | void LightString::SetAllLights( int color ) |
isonno |
0:6da5625a6946 | 71 | { |
isonno |
0:6da5625a6946 | 72 | int i; |
isonno |
0:6da5625a6946 | 73 | for (i = 0; i < fNumLights; ++i) |
isonno |
0:6da5625a6946 | 74 | SetOneColor( color, i ); |
isonno |
0:6da5625a6946 | 75 | } |
isonno |
0:6da5625a6946 | 76 | |
isonno |
0:6da5625a6946 | 77 | void LightString::SetColors( const vector<int>& colorList ) |
isonno |
0:6da5625a6946 | 78 | { |
isonno |
0:6da5625a6946 | 79 | int num = fNumLights < colorList.size() ? fNumLights : colorList.size(); |
isonno |
0:6da5625a6946 | 80 | |
isonno |
0:6da5625a6946 | 81 | for (int i = 0; i < num; ++i) |
isonno |
0:6da5625a6946 | 82 | SetOneColor( colorList[i], i ); |
isonno |
0:6da5625a6946 | 83 | } |