LeeT WiFiLamp code and test

Dependencies:   ESP8266_WebServer mbed

Fork of WiFiLamp by Sebastian Schocke

Revision:
4:4a502f72cbe3
Parent:
1:f07afcffeb5a
Child:
7:f15c81074400
diff -r 34cd0bb34971 -r 4a502f72cbe3 main.cpp
--- a/main.cpp	Mon Dec 01 08:46:33 2014 +0000
+++ b/main.cpp	Mon Dec 01 13:19:53 2014 +0000
@@ -67,25 +67,27 @@
     return 0;
 }
 
+void setColor( uint8_t r, uint8_t g, uint8_t b )
+{
+    for ( int c = 0; c < LED_COUNT; c++ )
+    {
+        colors[c].red = r;
+        colors[c].green = g;
+        colors[c].blue = b;
+    }
+    
+    ledStrip.write(colors, LED_COUNT);
+}
+
 int main() {
     pc.printf("WiFi Lamp Test...\r\n");    
-    colors[0].red = 0;
-    colors[0].green = 0;
-    colors[0].blue = 0;
-    colors[1].red = 0;
-    colors[1].green = 0;
-    colors[1].blue = 0;
-    colors[2].red = 0;
-    colors[2].green = 0;
-    colors[2].blue = 0;
-    colors[3].red = 0;
-    colors[3].green = 0;
-    colors[3].blue = 0;
+
+    setColor( 250, 0, 0);
+    
     wifiCHPD = 0;
     wifiReset = 0;
     wifiSerial.baud(9600);
     wifiSerial.attach(&rxint);
-    ledStrip.write(colors, LED_COUNT);
     wait_ms(1000);
     
     pc.printf("Powering WiFi...\r\n");    
@@ -124,6 +126,10 @@
     
     pc.printf("Done\r\n");    
     
+    setColor( 0, 250, 0);
+    wait_ms(500);
+    setColor( 0, 0, 0);
+    
     while(true) {
         if( (string_waiting("+IPD") == 1) && (string_waiting("\r\nOK\r\n") == 1) ) {
             pc.printf("\r\nGot Data\r\n");
@@ -158,17 +164,29 @@
                 if( uri == "/" ) {
                     httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp is alive(<a href='/red'>Red</a>)</h1></body></html>";
                     httpReplyPacket = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";
-                    colors[0].red = 0;
-                    colors[0].green = 0;
-                    colors[0].blue = 0;
-                    ledStrip.write(colors, LED_COUNT);
+                    setColor(0,0,0);
+                
                 } else if( uri == "/red" ) {
                     httpReply = "<html><head><title>WiFi Lamp</title></head><body><h1>The WiFi Lamp(Red) is alive(<a href='/'>Off</a>)</h1></body></html>";
                     httpReplyPacket = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";
-                    colors[0].red = 64;
-                    colors[0].green = 0;
-                    colors[0].blue = 0;
-                    ledStrip.write(colors, LED_COUNT);
+                    setColor(100, 0, 0);
+                    
+                } else if ( uri.substr(0, 9) == "/setcolor" ) {
+                    
+                    int r=0, g=0, b=0;
+                                       
+                    r = atoi( uri.substr( 12,3 ).c_str() );
+                    g = atoi( uri.substr( 18,3 ).c_str() );
+                    b = atoi( uri.substr( 24,3 ).c_str() );
+                    
+                    pc.printf( "Set color to (%i, %i, %i)\r\n", r,g,b);
+                    
+                    setColor( r,g,b );
+                    
+                    
+                    httpReply = "<html><head><title>WiFi Lamp</title></head><body>ok</body></html>";
+                    httpReplyPacket = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";                
+                    
                 } else {
                     httpReply = "404 Not Found";
                     httpReplyPacket = "HTTP/1.1 404 Not Found\r\nContent-Type: text/html\r\nContent-Length: %d\r\n\r\n%s";