LineLedControl

Dependencies:   LPD8806 mbed

Fork of LPD8806_Test by Jelmer Tiete

Revision:
3:b0a1b4b24d3c
Parent:
2:c9d9fa02a4e4
--- a/main.cpp	Fri Dec 16 10:43:27 2011 +0000
+++ b/main.cpp	Mon Aug 19 12:14:37 2013 +0000
@@ -1,19 +1,121 @@
-// Mbed program to test LPD8806-based RGB LED Strips
-// (c) 2011 Jelmer Tiete
-// This library is ported from the Arduino implementation of Adafruit Industries
-// found at: http://github.com/adafruit/LPD8806
-// and their strips: http://www.adafruit.com/products/306
-// Released under the MIT License: http://mbed.org/license/mit
-//
 // standard connected to 1st hardware SPI
 // LPD8806  <> MBED
-// DATA     -> P5
-// CLOCK    -> p7
+// DATA     -> P5( mosi ) or P11( mosi )
+// CLOCK    -> p7( sclk ) or p13( sclk )
 /*****************************************************************************/
-
+#include "mbed.h"
+#include "mbedOSC.h"
 #include "LPD8806.h"
 
-LPD8806 strip = LPD8806(5);
+extern "C" void mbed_reset();
+
+//OSC setup----------------------------------------------------------------------
+// mbed IP address (server):
+//#ifdef DHCP
+//EthernetNetIf eth;
+//#else
+EthernetNetIf eth(
+    IpAddr(192,168,12,210),//my mbed IP Address
+    IpAddr(255,255,255,0),//Network Mask
+    IpAddr(),//Gateway
+    IpAddr()//DNS
+);
+//#endif
+
+//receive
+uint8_t receiveIp[] = { 192, 168, 12, 210 };//IP Address
+int receivePort     = 5678;//port
+
+//destination.
+uint8_t destIp[] = { 192, 168, 12, 51};//IP Address
+int destPort     = 10000;//port
+
+//send value propaty.
+char *topAddress    = "/hangers";
+char *subAddress[3] = {"/line1","/line2","/line3"};
+
+//receive value propaty
+char *requestStr       = "hangers";
+char *requestTopStr[3] = {"mbed1","mbed2","mbed3"};
+char *requestSubStr[3] = {"test1","test2","test3"};
+int requestNumber[10]  = {101,102,103,104,105,106,107,108,109,110};
+const int resetNumber  = 777;
+
+//instance
+OSCMessage recMes;
+OSCMessage sendMes;
+OSCClass osc;
+
+//digital pin
+DigitalOut csPin1(p8);
+DigitalOut csPin2(p9);
+DigitalOut csPin3(p10);
+DigitalOut csPin4(p21);
+DigitalOut csPin5(p22);
+DigitalOut csPin6(p23);
+DigitalOut csPin7(p24);
+DigitalOut csPin8(p25);
+DigitalOut csPin9(p26);
+DigitalOut led(LED1);
+
+//mode
+bool sendMode = false;
+bool receiveMode = true;
+//----------------------------------------------------------------------
+
+LPD8806 strip = LPD8806(10);
+
+//LOW
+void low()
+{
+    for (int i=0; i < strip.numPixels(); i++) {
+        strip.setPixelColor(i, 0);  // turn all pixels off
+    }
+    strip.show();
+    
+    printf("low complete.");
+}
+
+//HIGH
+void high(uint32_t c)
+{
+    for (int i=0; i < strip.numPixels(); i++) {
+        strip.setPixelColor(i, c);  // turn all pixels off
+    }
+    strip.show();
+    
+    printf("high complete.");
+}
+
+//FADE
+void fade(uint16_t colorNum,uint8_t delay)
+{
+    //fade in
+    for(int i = 0; i < colorNum; i++)
+    {
+        for(int j = 0; j < strip.numPixels(); j++)
+        {
+            strip.setPixelColor(j,strip.Color(i,i,i));
+        }
+        strip.show();
+        wait_ms(delay);
+    }
+    
+    //fade out
+    for(int i = colorNum; i > 0; i--)
+    {
+        for(int j = 0; j < strip.numPixels(); j++)
+        {
+            strip.setPixelColor(j,strip.Color(i,i,i));
+        }
+        strip.show();
+        wait_ms(delay);
+    }
+    //low
+    low();
+    
+    printf("fade complete.");
+}
 
 // Chase a dot down the strip
 // good for testing purposes
@@ -142,36 +244,254 @@
     }
 }
 
-int main() {
+void processOSC(UDPSocketEvent e);
+
+//osc callback function
+void processOSC(UDPSocketEvent e)
+{
+    osc.onUDPSocketEvent(e);
+    
+    printf("new message!!");
+
+    if (osc.newMessage)
+    {
+//        osc.newMessage = false; // note: if using: message = osc.getMessage(), then we don't need to do this explicitly.
+        
+        if(strcmp(recMes.getAddress(0),requestStr)==0)
+        {
+            int val = recMes.getArgInt(0);
+            
+            switch(val)
+            {
+                case 101:
+                    //action
+//                    led = 1;
+//                    wait(0.2);
+//                    led = 0;//led LOW
+//                    wait(0.2);
+                    
+                    printf("\n");
+                    printf("receive ... 101\n");
+                    printf("\n");
+                    
+//                    fade(127,2);
+                    high(strip.Color(127,127,127));
+                    wait(1.0);
+                    low();
+                    wait(1.0);
+
+                    printf("101 ... complete.\n");
+                    break;
+                case 102:
+                
+                    printf("\n");
+                    printf("receive ... 102\n");
+                    printf("\n");
+                
+                    //action
+                    led = 1;
+                    wait(0.2);
+                    led = 0;//led LOW
+                    wait(0.2);
+                    led = 1;
+                    wait(0.2);
+                    led = 0;//led LOW
+                    wait(0.2);
+                    
+                    printf("102 ... complete.\n");
+                    break;
+                case 103:
+                
+                    printf("\n");
+                    printf("receive ... 103\n");
+                    printf("\n");
+                
+                    //action
+                    led = 1;
+                    wait(1.0);
+                    led = 0;//led LOW
+                    wait(1.0);
+                    led = 1;
+                    wait(1.0);
+                    led = 0;//led LOW
+                    wait(1.0);
+                    led = 1;
+                    wait(1.0);
+                    led = 0;//led LOW
+                    wait(1.0);
+                    printf("103 ... complete.\n");
+                    break;
+                case 104:
+                    //action
+                    break;
+                case 105:
+                    //action
+                    break;
+                case 106:
+                    //action
+                    break;
+                case 107:
+                    //action
+                    break;
+                case 108:
+                    //action
+                    break;
+                case 109:
+                    //action
+                    break;
+                case resetNumber://reset
+                    mbed_reset();
+                    break;
+                default:
+                    break;
+            }
+        }
+    }
+}
+
+void pinStatusChange(int st1=0,int st2=0,int st3=0,int st4=0,int st5=0,int st6=0,int st7=0,int st8=0)
+{
+    csPin1 = st1;
+    csPin2 = st2;
+    csPin3 = st3;
+    csPin4 = st4;
+    csPin5 = st5;
+    csPin6 = st6;
+    csPin7 = st7;
+    csPin8 = st8;
+}
+
+int main(void)
+{
+    //chip select pin setup
+    csPin1 = 1;
+    csPin2 = 1;
+    csPin3 = 1;
+    csPin4 = 1;
+    csPin5 = 1;
+    csPin6 = 1;
+    csPin7 = 1;
+    csPin8 = 1;
+    csPin9 = 1;
 
     // Start up the LED strip
     strip.begin();
 
     // Update the strip, to start they are all 'off'
     strip.show();
-    while (1) {
+    
+    // Set the Ethernet port:
+    EthernetErr ethErr = eth.setup();
+    if(ethErr)
+    {
+        //error
+        return -1;
+    }
 
-        colorChase(strip.Color(127,127,127), 100);
+
+    //(1) Sending message >>>
+    // Set IP and Port:
+    if(sendMode)
+    {
+        sendMes.setIp( destIp );
+        sendMes.setPort( destPort );
+    }
 
-        // Send a simple pixel chase in...
-        colorChase(strip.Color(127,0,0), 100);      // full brightness red
-        colorChase(strip.Color(127,127,0), 100);    // orange
-        colorChase(strip.Color(0,127,0), 100);        // green
-        colorChase(strip.Color(0,127,127), 100);    // teal
-        colorChase(strip.Color(0,0,127), 100);        // blue
-        colorChase(strip.Color(127,0,127), 100);    // violet
+    //(2) Receiving <<<
+    if(receiveMode)
+    {
+        recMes.setIp(receiveIp);
+        osc.setReceiveMessage(&recMes); // this sets the receiver container for the OSC packets (we can avoid doing this if we use osc.getMessage() to get messages)
+        osc.begin(receivePort, &processOSC); // binds the upd (osc) messages to an arbitrary listening port ("server" port), and callback function
+    }
+    
+    //loop
+    while (receiveMode||sendMode)
+    {
+        printf("loop...");
+    
+        Net::poll();
+//            
+//        if(sendMode)
+//        {
+//            sendMes.setTopAddress(topAddress);//top address
+//            sendMes.setSubAddress(subAddress[0]);//sub address
+//            sendMes.setArgs("i", 1);//type , value
+//            osc.sendOsc(&sendMes);//send!   
+//        }
+
+//        high(strip.Color(127,127,127));
+//        wait(1.0);
+//        low();
+//        wait(1.0);
 
-        // fill the entire strip with...
-        colorWipe(strip.Color(127,0,0), 100);        // red
-        colorWipe(strip.Color(0, 127,0), 100);        // green
-        colorWipe(strip.Color(0,0,127), 100);        // blue
+//        fade(127,5);
+//        wait(1.0);
+//        scanner(127,127,127,0);//white,slow
+        
+//        colorChase(strip.Color(127,127,127), 2);//white
+//        wait(1.0);
+//
+//        pinStatusChange(1,1,1,1,1,1,1,1);//pin status change
+//        colorChase(strip.Color(127,127,127), 1);//white
+//        wait(0.2);
+//
+//        pinStatusChange(0,0,0,0,0,0,0,0);//pin status change
+//        colorChase(strip.Color(127,127,127), 1);//white
+//        wait(0.2);
+//
+//        pinStatusChange(1,1,0);//pin status change
+//        colorChase(strip.Color(127,127,127), 1);//white
+//        wait(0.2);
+//        
+//        pinStatusChange(0,1,1);//pin status change
+//        colorChase(strip.Color(127,127,127), 1);//white
+//        wait(0.2);
+//        
+//        pinStatusChange(0,0,1);//pin status change
+//        colorChase(strip.Color(127,127,127), 1);//white
+//        wait(0.2);
+//
+//        pinStatusChange(0,0,0);//pin status change
+//        colorChase(strip.Color(127,127,127), 1);//white
+//        wait(0.2);
+        
+    }
 
-        rainbow(10);
-        rainbowCycle(5);  // make it go through the cycle fairly fast
-
-        // Back-and-forth lights
-        scanner(127,0,0, 50);        // red, slow
-        scanner(0,0,127, 5);        // blue, fast
-    }
-}
-
+    //loop
+//    while(1)
+//    {        
+        /*
+            sample
+        */
+//        allBlink(strip.Color(127,127,127));
+//        colorChase(strip.Color(127,127,127), 2);//white
+//        colorWipe(strip.Color(127,127,127), 2);//white
+//        colorChase(strip.Color(127,0,0), 10);      // full brightness red
+//        colorChase(strip.Color(127,127,0), 10);    // orange
+//        colorChase(strip.Color(0,127,0), 10);        // green
+//        colorChase(strip.Color(0,127,127), 10);    // teal
+//        colorChase(strip.Color(0,0,127), 10);        // blue
+//        colorChase(strip.Color(127,0,127), 10);    // violet
+//        // fill the entire strip with...
+//        colorWipe(strip.Color(127,0,0), 1000);        // red
+//        colorWipe(strip.Color(0, 127,0), 10);        // green
+//        colorWipe(strip.Color(0,0,127), 10);        // blue
+//        rainbow(10);
+//        rainbowCycle(5);  // make it go through the cycle fairly fast
+//        // Back-and-forth lights
+//        scanner(127,0,0, 50);        // red, slow
+//        scanner(0,0,127, 5);        // blue, fast
+    
+        //Set data
+//        if(sendMode)
+//        {
+//            Net::poll();
+//            
+//            sendMes.setTopAddress(topAddress);//top address
+//            sendMes.setSubAddress(subAddress[0]);//sub address
+//            sendMes.setArgs("i", 1);//type , value
+//            osc.sendOsc(&sendMes);//send!
+//        }
+//    }
+}
\ No newline at end of file