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

/media/uploads/isonno/nxp3872_controllerclose.jpg

System Block Diagram

/media/uploads/isonno/blockdiagram.png

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?

UserRevisionLine numberNew contents of line
isonno 3:0ac64c4ca40f 1 // SystemState.cpp - Keeps track of system state, so you can
isonno 3:0ac64c4ca40f 2 // power on back where you left off.
isonno 3:0ac64c4ca40f 3
isonno 3:0ac64c4ca40f 4 #include "SystemState.h"
isonno 3:0ac64c4ca40f 5 #include "HoldInterrupts.h"
isonno 3:0ac64c4ca40f 6 #include "SDFileSystem.h"
isonno 3:0ac64c4ca40f 7
isonno 3:0ac64c4ca40f 8 const char * kStateFilename = "/sd/SYSSTATE.TXT";
isonno 3:0ac64c4ca40f 9
isonno 3:0ac64c4ca40f 10 static SystemState static_SystemState;
isonno 3:0ac64c4ca40f 11 SystemState& gSystemState = static_SystemState;
isonno 3:0ac64c4ca40f 12
isonno 3:0ac64c4ca40f 13 SystemState::SystemState()
isonno 3:0ac64c4ca40f 14 {
isonno 3:0ac64c4ca40f 15
isonno 3:0ac64c4ca40f 16 fModeSelector = kWhiteSelector;
isonno 3:0ac64c4ca40f 17 fBrightLevel = 7;
isonno 3:0ac64c4ca40f 18 fSatColorIndex = 0; // This is an INDEX, not actual color
isonno 3:0ac64c4ca40f 19 fPatternIndex = 0;
isonno 3:0ac64c4ca40f 20 fLightSensor = 1;
isonno 3:0ac64c4ca40f 21
isonno 3:0ac64c4ca40f 22 LoadState();
isonno 3:0ac64c4ca40f 23 }
isonno 3:0ac64c4ca40f 24
isonno 3:0ac64c4ca40f 25 void SystemState::DumpState()
isonno 3:0ac64c4ca40f 26 {
isonno 3:0ac64c4ca40f 27 printf( "ModeSelector: %d\r\n", (int) fModeSelector );
isonno 3:0ac64c4ca40f 28 printf( "BrightLevel: %d\r\n", (int) fBrightLevel );
isonno 3:0ac64c4ca40f 29 printf( "SatColorIndex: %d\r\n",(int) fSatColorIndex);
isonno 3:0ac64c4ca40f 30 printf( "PatternIndex: %d\r\n", (int) fPatternIndex );
isonno 3:0ac64c4ca40f 31 printf( "LightSensor: %d\r\n", (int) fLightSensor );
isonno 3:0ac64c4ca40f 32 }
isonno 3:0ac64c4ca40f 33
isonno 3:0ac64c4ca40f 34 void SystemState::Modified()
isonno 3:0ac64c4ca40f 35 {
isonno 3:0ac64c4ca40f 36 fStateTimer.detach(); // Reset any previous timer
isonno 3:0ac64c4ca40f 37 fStateTimer.attach( this, &SystemState::SaveState, 30.0 );
isonno 3:0ac64c4ca40f 38 }
isonno 3:0ac64c4ca40f 39
isonno 3:0ac64c4ca40f 40 void SystemState::SaveState()
isonno 3:0ac64c4ca40f 41 {
isonno 3:0ac64c4ca40f 42 HoldInterrupts noint();
isonno 3:0ac64c4ca40f 43 SDFileSystem sdcard( p5, p6, p7, p8, "sd" );
isonno 3:0ac64c4ca40f 44
isonno 3:0ac64c4ca40f 45 printf("Saving state...");
isonno 3:0ac64c4ca40f 46 FILE * f = fopen( kStateFilename, "w" );
isonno 3:0ac64c4ca40f 47 if (f)
isonno 3:0ac64c4ca40f 48 {
isonno 3:0ac64c4ca40f 49 fprintf( f, "ModeSelector: %d\n", (int) fModeSelector );
isonno 3:0ac64c4ca40f 50 fprintf( f, "BrightLevel: %d\n", (int) fBrightLevel );
isonno 3:0ac64c4ca40f 51 fprintf( f, "SatColorIndex: %d\n",(int) fSatColorIndex);
isonno 3:0ac64c4ca40f 52 fprintf( f, "PatternIndex: %d\n", (int) fPatternIndex );
isonno 3:0ac64c4ca40f 53 fprintf( f, "LightSensor: %d\n", (int) fLightSensor );
isonno 3:0ac64c4ca40f 54 fclose(f);
isonno 3:0ac64c4ca40f 55 printf("saved\r\n");
isonno 3:0ac64c4ca40f 56 }
isonno 3:0ac64c4ca40f 57 else
isonno 3:0ac64c4ca40f 58 printf("failed.\r\n");
isonno 3:0ac64c4ca40f 59 }
isonno 3:0ac64c4ca40f 60
isonno 3:0ac64c4ca40f 61 static int getInt( const char * buffer )
isonno 3:0ac64c4ca40f 62 {
isonno 3:0ac64c4ca40f 63 string s(buffer);
isonno 3:0ac64c4ca40f 64 size_t colon = s.find(':');
isonno 3:0ac64c4ca40f 65 if ((colon > 0) && (colon < s.size()))
isonno 3:0ac64c4ca40f 66 {
isonno 3:0ac64c4ca40f 67 int value;
isonno 3:0ac64c4ca40f 68 s.erase( 0, colon + 1 );
isonno 3:0ac64c4ca40f 69 sscanf( s.c_str(), "%d", &value );
isonno 3:0ac64c4ca40f 70 return value;
isonno 3:0ac64c4ca40f 71 }
isonno 3:0ac64c4ca40f 72 else
isonno 3:0ac64c4ca40f 73 printf("ERROR: problem reading integer in state file\r\n" );
isonno 3:0ac64c4ca40f 74 return 0;
isonno 3:0ac64c4ca40f 75 }
isonno 3:0ac64c4ca40f 76
isonno 3:0ac64c4ca40f 77 /*
isonno 3:0ac64c4ca40f 78 static string getStr( const char * buffer )
isonno 3:0ac64c4ca40f 79 {
isonno 3:0ac64c4ca40f 80 char junk[20];
isonno 3:0ac64c4ca40f 81 char value[20];
isonno 3:0ac64c4ca40f 82 int numRead = sscanf( buffer, "%s : %s", junk, value );
isonno 3:0ac64c4ca40f 83 if (numRead != 2)
isonno 3:0ac64c4ca40f 84 printf("ERROR: problem reading string in state file\n" );
isonno 3:0ac64c4ca40f 85 return string(value);
isonno 3:0ac64c4ca40f 86 }
isonno 3:0ac64c4ca40f 87 */
isonno 3:0ac64c4ca40f 88
isonno 3:0ac64c4ca40f 89 static string GetKeyword( const char * buffer )
isonno 3:0ac64c4ca40f 90 {
isonno 3:0ac64c4ca40f 91 string s(buffer);
isonno 3:0ac64c4ca40f 92 size_t colon = s.find(':');
isonno 3:0ac64c4ca40f 93 if ((colon > 0) && (colon < s.size()))
isonno 3:0ac64c4ca40f 94 s.resize( colon ); // Truncate
isonno 3:0ac64c4ca40f 95 else
isonno 3:0ac64c4ca40f 96 printf("ERROR: problem reading keyword in state file\r\n" );
isonno 3:0ac64c4ca40f 97 return s;
isonno 3:0ac64c4ca40f 98 }
isonno 3:0ac64c4ca40f 99
isonno 3:0ac64c4ca40f 100 #define READ_STATE_VALUE( key, reader ) \
isonno 3:0ac64c4ca40f 101 if (GetKeyword( buffer ) == string(#key)) f ## key = reader(buffer);
isonno 3:0ac64c4ca40f 102
isonno 3:0ac64c4ca40f 103 bool SystemState::LoadState()
isonno 3:0ac64c4ca40f 104 {
isonno 3:0ac64c4ca40f 105 HoldInterrupts noint();
isonno 3:0ac64c4ca40f 106 SDFileSystem sdcard( p5, p6, p7, p8, "sd" );
isonno 3:0ac64c4ca40f 107
isonno 3:0ac64c4ca40f 108 char buffer[200];
isonno 3:0ac64c4ca40f 109 printf("Loading state...");
isonno 3:0ac64c4ca40f 110
isonno 3:0ac64c4ca40f 111 FILE * f = fopen( kStateFilename, "r" );
isonno 3:0ac64c4ca40f 112 if (f)
isonno 3:0ac64c4ca40f 113 {
isonno 3:0ac64c4ca40f 114 while (fgets( buffer, sizeof(buffer), f))
isonno 3:0ac64c4ca40f 115 {
isonno 3:0ac64c4ca40f 116 READ_STATE_VALUE( ModeSelector, (ESelector)getInt );
isonno 3:0ac64c4ca40f 117 READ_STATE_VALUE( BrightLevel, getInt );
isonno 3:0ac64c4ca40f 118 READ_STATE_VALUE( SatColorIndex,getInt );
isonno 3:0ac64c4ca40f 119 READ_STATE_VALUE( PatternIndex, getInt );
isonno 3:0ac64c4ca40f 120 READ_STATE_VALUE( LightSensor, getInt );
isonno 3:0ac64c4ca40f 121 }
isonno 3:0ac64c4ca40f 122 fclose(f);
isonno 3:0ac64c4ca40f 123 printf("loaded.\r\n");
isonno 3:0ac64c4ca40f 124 return true;
isonno 3:0ac64c4ca40f 125 }
isonno 3:0ac64c4ca40f 126 printf("failed.\r\n");
isonno 3:0ac64c4ca40f 127 return false;
isonno 3:0ac64c4ca40f 128 }