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@4:cfef06d8bb96, 2013-02-11 (annotated)
- Committer:
- isonno
- Date:
- Mon Feb 11 05:04:18 2013 +0000
- Revision:
- 4:cfef06d8bb96
- Parent:
- 3:0ac64c4ca40f
Minor changes to add backlight routines. Not hooked up yet, shouldn't affect build operation.
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 |
3:0ac64c4ca40f | 9 | #include "RGBLED.h" |
isonno |
3:0ac64c4ca40f | 10 | |
isonno |
3:0ac64c4ca40f | 11 | LightString::LightString( PinName pin, int numLights ) |
isonno |
3:0ac64c4ca40f | 12 | : fLightsPort( pin, NC ), fUSBPort( NC, USBRX ), fSnoop( numLights ) |
isonno |
0:6da5625a6946 | 13 | { |
isonno |
3:0ac64c4ca40f | 14 | fNumLights = numLights; |
isonno |
3:0ac64c4ca40f | 15 | fBufferInPos = 0; |
isonno |
3:0ac64c4ca40f | 16 | fBufferOutPos = 0; |
isonno |
3:0ac64c4ca40f | 17 | // Attaches the USB port to the light port, so |
isonno |
3:0ac64c4ca40f | 18 | // data is passed straight through from the host to the lights. |
isonno |
3:0ac64c4ca40f | 19 | fUSBPort.attach( this, &LightString::HandleIncomingData ); |
isonno |
3:0ac64c4ca40f | 20 | // fLightsPort.attach( this, &LightString::HandleOutgoingData ); |
isonno |
0:6da5625a6946 | 21 | } |
isonno |
0:6da5625a6946 | 22 | |
isonno |
3:0ac64c4ca40f | 23 | void LightString::HandleIncomingData() |
isonno |
0:6da5625a6946 | 24 | { |
isonno |
3:0ac64c4ca40f | 25 | while (fUSBPort.readable()) |
isonno |
0:6da5625a6946 | 26 | { |
isonno |
3:0ac64c4ca40f | 27 | fBuffer[fBufferInPos] = fUSBPort.getc(); |
isonno |
3:0ac64c4ca40f | 28 | fBufferInPos++; |
isonno |
3:0ac64c4ca40f | 29 | if (fBufferInPos >= sizeof( fBuffer )) |
isonno |
3:0ac64c4ca40f | 30 | fBufferInPos = 0; |
isonno |
3:0ac64c4ca40f | 31 | if (fBufferInPos == fBufferOutPos) |
isonno |
3:0ac64c4ca40f | 32 | printf("Buffer overflow\r\n"); |
isonno |
0:6da5625a6946 | 33 | } |
isonno |
3:0ac64c4ca40f | 34 | } |
isonno |
3:0ac64c4ca40f | 35 | |
isonno |
3:0ac64c4ca40f | 36 | void LightString::HandleOutgoingData() |
isonno |
3:0ac64c4ca40f | 37 | { |
isonno |
3:0ac64c4ca40f | 38 | gDebugLED.Set( 700 ); |
isonno |
3:0ac64c4ca40f | 39 | while ((fBufferOutPos != fBufferInPos) && fLightsPort.writeable()) |
isonno |
0:6da5625a6946 | 40 | { |
isonno |
3:0ac64c4ca40f | 41 | fLightsPort.putc( fBuffer[fBufferOutPos++] ); |
isonno |
3:0ac64c4ca40f | 42 | if (fBufferOutPos >= sizeof( fBuffer )) |
isonno |
3:0ac64c4ca40f | 43 | fBufferOutPos = 0; |
isonno |
0:6da5625a6946 | 44 | } |
isonno |
3:0ac64c4ca40f | 45 | gDebugLED.Set( (fBufferOutPos == fBufferInPos) ? 0 : 70 ); |
isonno |
0:6da5625a6946 | 46 | } |
isonno |
0:6da5625a6946 | 47 | |
isonno |
0:6da5625a6946 | 48 | void LightString::sendCommand1( uint8_t ch ) |
isonno |
0:6da5625a6946 | 49 | { |
isonno |
0:6da5625a6946 | 50 | HoldInterrupts noint(); |
isonno |
0:6da5625a6946 | 51 | |
isonno |
3:0ac64c4ca40f | 52 | fLightsPort.putc( ch ); |
isonno |
0:6da5625a6946 | 53 | } |
isonno |
0:6da5625a6946 | 54 | |
isonno |
0:6da5625a6946 | 55 | void LightString::sendCommand2( uint8_t ch1, uint8_t ch2 ) |
isonno |
0:6da5625a6946 | 56 | { |
isonno |
0:6da5625a6946 | 57 | HoldInterrupts noint(); |
isonno |
3:0ac64c4ca40f | 58 | fLightsPort.putc( ch1 ); |
isonno |
3:0ac64c4ca40f | 59 | fLightsPort.putc( ch2 ); |
isonno |
0:6da5625a6946 | 60 | } |
isonno |
0:6da5625a6946 | 61 | |
isonno |
0:6da5625a6946 | 62 | void LightString::sendCommand3( uint8_t ch1, uint8_t ch2, uint8_t ch3 ) |
isonno |
0:6da5625a6946 | 63 | { |
isonno |
0:6da5625a6946 | 64 | HoldInterrupts noint(); |
isonno |
3:0ac64c4ca40f | 65 | fLightsPort.putc( ch1 ); |
isonno |
3:0ac64c4ca40f | 66 | fLightsPort.putc( ch2 ); |
isonno |
3:0ac64c4ca40f | 67 | fLightsPort.putc( ch3 ); |
isonno |
0:6da5625a6946 | 68 | } |
isonno |
0:6da5625a6946 | 69 | |
isonno |
0:6da5625a6946 | 70 | void LightString::InitLights( int numLights ) |
isonno |
0:6da5625a6946 | 71 | { |
isonno |
0:6da5625a6946 | 72 | if (numLights) |
isonno |
0:6da5625a6946 | 73 | fNumLights = numLights; |
isonno |
0:6da5625a6946 | 74 | |
isonno |
0:6da5625a6946 | 75 | printf("Init called\n\r"); |
isonno |
0:6da5625a6946 | 76 | sendCommand2( 'I', 0 ); |
isonno |
0:6da5625a6946 | 77 | sendCommand2( 'N', fNumLights ); |
isonno |
0:6da5625a6946 | 78 | } |
isonno |
0:6da5625a6946 | 79 | |
isonno |
0:6da5625a6946 | 80 | void LightString::SetOneColor( int color, uint8_t id ) |
isonno |
0:6da5625a6946 | 81 | { |
isonno |
0:6da5625a6946 | 82 | uint8_t redBit, c; |
isonno |
0:6da5625a6946 | 83 | |
isonno |
0:6da5625a6946 | 84 | c = colorByte( color, redBit ); |
isonno |
0:6da5625a6946 | 85 | |
isonno |
0:6da5625a6946 | 86 | sendCommand3( 'C', id | redBit, c ); |
isonno |
0:6da5625a6946 | 87 | } |
isonno |
0:6da5625a6946 | 88 | |
isonno |
0:6da5625a6946 | 89 | void LightString::SetAllLights( int color ) |
isonno |
0:6da5625a6946 | 90 | { |
isonno |
0:6da5625a6946 | 91 | int i; |
isonno |
0:6da5625a6946 | 92 | for (i = 0; i < fNumLights; ++i) |
isonno |
0:6da5625a6946 | 93 | SetOneColor( color, i ); |
isonno |
0:6da5625a6946 | 94 | } |
isonno |
0:6da5625a6946 | 95 | |
isonno |
0:6da5625a6946 | 96 | void LightString::SetColors( const vector<int>& colorList ) |
isonno |
0:6da5625a6946 | 97 | { |
isonno |
0:6da5625a6946 | 98 | int num = fNumLights < colorList.size() ? fNumLights : colorList.size(); |
isonno |
0:6da5625a6946 | 99 | |
isonno |
0:6da5625a6946 | 100 | for (int i = 0; i < num; ++i) |
isonno |
0:6da5625a6946 | 101 | SetOneColor( colorList[i], i ); |
isonno |
0:6da5625a6946 | 102 | } |