projectwerk

Dependencies:   NeoPixelString SimplyLog

Fork of NeoPixelI2cSlave by Nico De Witte

Files at this revision

API Documentation at this revision

Comitter:
BramHeldenbergh
Date:
Sat Jan 09 13:10:19 2016 +0000
Parent:
2:e0269262d1f2
Commit message:
programma projectwerk

Changed in this revision

neopixel_i2c_daemon.cpp Show annotated file Show diff for this revision Revisions of this file
neopixel_i2c_daemon.h Show annotated file Show diff for this revision Revisions of this file
strobe.cpp Show annotated file Show diff for this revision Revisions of this file
strobe.h Show annotated file Show diff for this revision Revisions of this file
--- a/neopixel_i2c_daemon.cpp	Sun Oct 25 11:36:21 2015 +0000
+++ b/neopixel_i2c_daemon.cpp	Sat Jan 09 13:10:19 2016 +0000
@@ -1,38 +1,31 @@
 #include "neopixel_i2c_daemon.h"
-
+#include "strobe.h"
 #define DEBUG_MODE 1
 #include "log.h"
-
 NeoPixelI2cDaemon::NeoPixelI2cDaemon(I2cDevice * i2c, PinName alive_pin) : alive(alive_pin) {
     this->i2c = i2c;
     cAlive = 0;
 }
-
 void NeoPixelI2cDaemon::attachPixelString(NeoPixelString * pixelstring) {
     pixelstrings.push_back(pixelstring);   
 }
-
 void NeoPixelI2cDaemon::allOff(void) {
     for (unsigned int i = 0; i < pixelstrings.size(); i++) {
         pixelstrings[i]->update(Colors::Black);
     }   
 }
-
 void NeoPixelI2cDaemon::allSingleColor(neopixel::Pixel singlecolor) {
     for (unsigned int i = 0; i < pixelstrings.size(); i++) {
         pixelstrings[i]->update(singlecolor);
     }   
 }
-
 void NeoPixelI2cDaemon::diagnoseAll(void) {
     for (unsigned int i = 0; i < pixelstrings.size(); i++) {
         pixelstrings[i]->diagnose();
     }   
 }
-
 void NeoPixelI2cDaemon::listen(bool blocking=false) {
     char buffer[10];
-
     while (blocking) {
         // Check for i2c message
         int i = i2c->receive();
@@ -40,22 +33,18 @@
             // ReadAddressed - the master has requested a read from this slave
             // WriteAddressed - the master is writing to this slave
             // WriteGeneral - the master is writing to all slave
-
         switch (i) {
-
             case I2cDevice::ReadAddressed:
                 char sendbuffer[1];
                 sendbuffer[0] = pixelstrings.size();
                 int result = i2c->write(sendbuffer, sizeof(sendbuffer));
                 i2c->stop();
-
                 if (!result) {     // 0 on success, non-0 otherwise
                     SimplyLog::Log::i("Send info to master successfully\r\n");
                 } else {
                     SimplyLog::Log::w("Send info to master failed\r\n");
                 }
                 break;
-
             case I2cDevice::WriteAddressed:
             {
                 // First we read the command byte
@@ -68,15 +57,15 @@
                         i2c->stop();    // Necessary !! Otherwise write will fail on master end
                         SimplyLog::Log::i("Killing all neopixel leds\r\n");
                         allOff();
+                        
                         break;
-
                     case DIAGNOSTIC:
                         i2c->stop();    // Necessary !! Otherwise write will fail on master end
                         SimplyLog::Log::i("Executing diagnostic of neopixel strings\r\n");
                         diagnoseAll();
                         SimplyLog::Log::i("Diagnostic finished\r\n");
+                        
                         break;
-
                     case SINGLE_COLOR:
                         // Expect 3 more bytes [red green blue]
                         if(!i2c->read(buffer, 3)) {      //0 on success, non-0 otherwise
@@ -85,11 +74,20 @@
                             color.green = buffer[1];
                             color.blue = buffer[2];
                             allSingleColor(color);
-
                             SimplyLog::Log::i("Setting single color [r,g,b] = [%d,%d,%d]\r\n", color.red, color.green, color.blue);
                         } else {
                             SimplyLog::Log::w("Single color set received with missing params\r\n");
                         }
+                        
+                        break;
+                        
+                    
+                    case STROBO:
+                        i2c->stop();
+                        SimplyLog::Log::w("We gaan een ticker aanroepen\r\n");
+                        hanzi= new Strobe(this);
+                        hanzi->strstart();    
+                        SimplyLog::Log::w("Single color set received with missing params\r\n");
                         break;
                     
                     default:
@@ -98,7 +96,6 @@
                 }
             }
         }
-
         for(int i = 0; i < 10; i++) {
             buffer[i] = 0;    // Clear buffer
         }
@@ -108,5 +105,7 @@
         if (!cAlive) {
             alive = !alive;
         }
+        
+        hanzi->execute();
     }
 }
\ No newline at end of file
--- a/neopixel_i2c_daemon.h	Sun Oct 25 11:36:21 2015 +0000
+++ b/neopixel_i2c_daemon.h	Sat Jan 09 13:10:19 2016 +0000
@@ -1,13 +1,12 @@
 #pragma once
-
 #include "i2c_device.h"
 #include "neopixel_string.h"
 #include <vector>
-
-enum I2cCommand { OFF = 0x01, DIAGNOSTIC = 0x02, SINGLE_COLOR = 0x03 };
-
+#include "strobe.h"
+enum I2cCommand { OFF = 0x01, DIAGNOSTIC = 0x02, SINGLE_COLOR = 0x03, STROBO = 0x04 };
+class Strobe;
+class Licht;
 class NeoPixelI2cDaemon {
-
     private:
         I2cDevice * i2c;
         std::vector<NeoPixelString *> pixelstrings;
@@ -18,6 +17,7 @@
         NeoPixelI2cDaemon(I2cDevice * i2c, PinName alive_pin);
         
     public:
+        Strobe * hanzi;  
         void attachPixelString(NeoPixelString * pixelstring);
         void listen(bool blocking);
         void allOff(void);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/strobe.cpp	Sat Jan 09 13:10:19 2016 +0000
@@ -0,0 +1,53 @@
+#include "neopixel_i2c_daemon.h"
+#include "strobe.h"
+#define DEBUG_MODE 1
+#include "log.h"
+#include "neopixel.h"
+
+DigitalOut myled(LED2);
+
+Strobe::Strobe(NeoPixelI2cDaemon * deamon){
+    freddy=deamon;
+    SimplyLog::Log::i("6\r\n");
+    state = SOFF;
+    doExecute = false;    
+}
+void Strobe::execute(){
+    
+    if(doExecute){
+        if(state==SON){
+           SimplyLog::Log::i("4\r\n");
+            neopixel::Pixel test;
+            test.red = 255;
+            test.green = 255;
+            test.blue = 255;
+            myled = !myled;
+            freddy->allSingleColor(test);
+           state=SOFF; 
+            }
+        else if(state==SOFF){
+            SimplyLog::Log::i("9\r\n");
+            freddy->allOff();
+            SimplyLog::Log::i("10\r\n");
+            state=SON;
+            SimplyLog::Log::i("7\r\n");
+            }
+        doExecute=false;
+        }
+    }
+void Strobe::isrTicker(){
+    SimplyLog::Log::i("3\r\n");
+    doExecute=true;
+    }
+void Strobe::strstart(){
+    SimplyLog::Log::i("1\r\n");
+    ticker.attach(this, &Strobe::isrTicker, 0.1);
+    doExecute=false;
+    state = SOFF;
+    }
+void Strobe::strstop(){
+    SimplyLog::Log::i("2\r\n");
+    ticker.detach();
+    doExecute=false;
+    state = SOFF;
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/strobe.h	Sat Jan 09 13:10:19 2016 +0000
@@ -0,0 +1,24 @@
+#pragma once
+#include "mbed.h"
+#include "neopixel_i2c_daemon.h"
+#include "neopixel_string.h"
+#include <vector>
+#include "neopixel.h"
+
+enum StrobeState { SON, SOFF } ;
+class NeoPixelI2cDaemon;
+class Strobe{
+    private:
+    Ticker ticker;
+    bool doExecute;
+    StrobeState state;
+    NeoPixelI2cDaemon * freddy;
+    
+    public:
+    
+    Strobe(NeoPixelI2cDaemon * deamon);
+    void execute();
+    void isrTicker();
+    void strstart();
+    void strstop(); 
+    };
\ No newline at end of file