Pavit Noinongyao / Mbed 2 deprecated SmartPlugBLE

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SmartPlugBLE.h Source File

SmartPlugBLE.h

00001 #ifndef SMART_PLUG_BLE_H
00002 #define SMART_PLUG_BLE_H
00003 
00004 #include "SmartPlug.h"
00005 #include "MVC.h"
00006 #include "mbed.h"
00007 #include "SPCommunication.h"
00008 #include <vector>
00009 
00010 
00011 class SmartPlugBLE: public Observable
00012 {
00013 public:
00014     SmartPlugBLE():counter(60),led(LED3,0)
00015      {
00016         ticker.attach(this,&SmartPlugBLE::periodicCallback, 1); // blink LED every second
00017     }
00018 
00019     void onRelayWrite(const uint8_t *data)
00020      {
00021         counter = 60;
00022         updateRelay(data);
00023         notifyObservers();
00024     }
00025 
00026     void updateRelay(const uint8_t *data) 
00027     {
00028         uint8_t state = data[0]>>7;
00029         uint8_t hour = data[0]&0x7F;
00030         uint8_t min = data[1];
00031         //instant behavior
00032         if(state)
00033         {
00034             printf("Open!\r\n");
00035             led = 0;
00036         }
00037         else if(!state && hour == 0 && min == 0)
00038         {
00039             printf("Close!\r\n");
00040             led = 1;
00041         }
00042         //
00043         smartPlug.setRelay(state,hour,min);
00044     }
00045 
00046     void notifyObservers()
00047      {
00048         for(int i=0; i<observers.size(); i++) 
00049         {
00050             observers[i]->updateObserver((void*)&smartPlug);
00051         }
00052     }
00053 
00054     void addObserver(Observer* o) 
00055     {
00056         observers.push_back(o);
00057     }
00058 
00059     void periodicCallback(void)
00060      {
00061         if(smartPlug.isCounting)
00062          {
00063             counter--;
00064              if(counter > 60) 
00065             {
00066                 smartPlug.getRelay()->setMinCounter(smartPlug.getRelay()->getMinCounter()-1);
00067                 if(smartPlug.getRelay()->getMinCounter() == 0) 
00068                 {
00069                     smartPlug.getRelay()->setHrCounter(smartPlug.getRelay()->getHrCounter()-1);
00070                     smartPlug.getRelay()->setMinCounter(0);
00071                 }
00072                 if(smartPlug.getRelay()->getHrCounter() >128) 
00073                 {
00074                     smartPlug.getRelay()->setHrCounter(0);
00075                     //spComm.setRelay(smartPlug.getRelay()->getState());
00076                     if(smartPlug.getRelay()->getState())
00077                         printf("Open!\r\n");
00078                     else
00079                     {
00080                         printf("Close!\r\n");
00081                         led = 1;
00082                         smartPlug.isCounting = false;
00083                     }
00084                 }
00085                 counter = 60;
00086             }
00087         }
00088         else
00089             counter = 60;
00090     }
00091 
00092     void updateData(void) {
00093         smartPlug.setVoltage/*(smartPlug.getVoltage()+1);*/(spComm.getVoltage());
00094         smartPlug.setCurrent/*(smartPlug.getCurrent()+1);*/(spComm.getCurrent());
00095         smartPlug.setPower/*(smartPlug.getPower()+1);*/(spComm.getPower());
00096         smartPlug.setPowerFactor/*(smartPlug.getPowerFactor()+1);*/(spComm.getPowerFactor());
00097         smartPlug.setEnergy/*(smartPlug.getEnergy()+1);*/(spComm.getEnergy());
00098         //smartPlug.getRelay()->setState(spComm.getRelay());
00099         notifyObservers();
00100     }
00101     
00102     SmartPlug getSmartPlug()
00103     {
00104         return smartPlug;
00105     }
00106     
00107     uint8_t getCounter()
00108     {
00109         return counter;
00110     }
00111     
00112     void setCounter(uint8_t val)
00113     {
00114         counter = val;
00115     }
00116 private:
00117     SmartPlug smartPlug;
00118     Ticker ticker;
00119     uint8_t counter;
00120     DigitalOut led;
00121     SPCommunication spComm;
00122     //BLECommunication bleComm;
00123     vector<Observer*> observers;
00124 };
00125 
00126 #endif