Bluetooth Low Energy for Smart Plug

Dependencies:   BLE_API mbed nRF51822

Revision:
2:6db5c9a2894c
Parent:
1:e036e77762fa
Child:
3:aaa92c61931a
--- a/Services/SmartPlugService.cpp	Tue Jul 07 04:58:28 2015 +0000
+++ b/Services/SmartPlugService.cpp	Wed Jul 08 07:25:11 2015 +0000
@@ -4,8 +4,17 @@
 #include "SmartPlugBLE.h"
 
 
+
+void convert(uint8_t* res,uint32_t &num)
+{
+    res[3] = (num&0xFF000000)>>24;
+    res[2] = (num&0x00FF0000)>>16;
+    res[1] = (num&0x0000FF00)>>8;
+    res[0] = num&0x000000FF;
+}
+
 SmartPlugService::SmartPlugService(BLE &_ble, SmartPlugBLE &sys):
-        ble(_ble), system(sys),
+        ble(_ble), led(LED3,1),system(sys),
         voltageChar(SPS_UUID_VOLTAGE_CHAR,&voltage,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
         currentChar(SPS_UUID_CURERNT_CHAR,&current,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
         powerChar(SPS_UUID_POWER_CHAR,&power,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
@@ -17,7 +26,7 @@
                 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ),
         updateChar(SPS_UUID_UPDATE_CHAR,NULL)
 {
-    (&system)->addObserver(*this);
+    system.addObserver(this);
 }
 
 void SmartPlugService::onDataWritten(const GattWriteCallbackParams *params) 
@@ -26,53 +35,70 @@
     {
         system.updateData();
     }
-}
-void SmartPlugService::update(void* data)
-{
-    updateData((SmartPlug*)data);
+    else if(params->handle == relayChar.getValueHandle())
+    {//like stack
+            printf("SmartPlugService::onDataWritten params->len = %d\r\n",params->len);
+            for(int i=0;i<params->len;i++)
+            {
+                printf("data = 0x%08x\r\n",params->data[i]);
+            }
+            system.onRelayWrite(params->data);
+    }
 }
 
-void SmartPlugService::updateData(SmartPlug* data)
+void SmartPlugService::updateObserver(void* data)
 {
-    updateVoltage(data->getVoltage());
-    updateCurrent(data->getCurrent());
-    updatePower(data->getPower());
-    updatePowerFactor(data->getPowerFactor());
+    SmartPlug* sp = (SmartPlug*)data;
+    updateVoltage(sp->getVoltage());
+    updateCurrent(sp->getCurrent());
+    updatePower(sp->getPower());
+    updatePowerFactor(sp->getPowerFactor());
+    updateEnergy(sp->getEnergy());
+    updateRelay(&sp->getRelay());
 }
 
-void SmartPlugService::updateVoltage(unsigned long v)
+void SmartPlugService::updateVoltage(uint32_t v)
 {
     if (ble.getGapState().connected) 
     {
-        voltage = v;
-        ble.updateCharacteristicValue(voltageChar.getValueHandle(),&voltage,4);
+        convert(voltage,v);
+        ble.updateCharacteristicValue(voltageChar.getValueHandle(),voltage,sizeof(voltage));
     }
 }
 
-void SmartPlugService::updateCurrent(unsigned long c)
+void SmartPlugService::updateCurrent(uint32_t c)
 {
     if (ble.getGapState().connected) 
     {
-        current = c;
-        ble.updateCharacteristicValue(currentChar.getValueHandle(),&current,4);
+        convert(current,c);
+        ble.updateCharacteristicValue(currentChar.getValueHandle(),(current),sizeof(current));
     }
 }
 
-void SmartPlugService::updatePower(unsigned long p)
+void SmartPlugService::updatePower(uint32_t p)
 {
     if (ble.getGapState().connected) 
     {
-        power = p;
-        ble.updateCharacteristicValue(powerChar.getValueHandle(),&power,4);        
+        convert(power,p);
+        ble.updateCharacteristicValue(powerChar.getValueHandle(),(power),sizeof(power));        
     }
 }
 
-void SmartPlugService::updatePowerFactor(unsigned long pf)
+void SmartPlugService::updatePowerFactor(uint32_t pf)
 {
     if (ble.getGapState().connected) 
     {    
-        powerFactor = pf;
-        ble.updateCharacteristicValue(powerFactorChar.getValueHandle(),&powerFactor,4);
+        convert(powerFactor,pf);
+        ble.updateCharacteristicValue(powerFactorChar.getValueHandle(),(powerFactor),sizeof(powerFactor));
+    }
+}
+
+void SmartPlugService::updateEnergy(uint32_t e)
+{
+    if(ble.getGapState().connected)
+    {
+        convert(energy,e);
+        ble.updateCharacteristicValue(energyChar.getValueHandle(),energy,sizeof(energy));
     }
 }
 
@@ -80,7 +106,18 @@
 {
     if (ble.getGapState().connected) 
     {   
+        if(relay->getState())
+        {
+            led = 0;
+            printf("Open\r\n");
+        }
+        else
+        {
+            led = 1;
+            printf("Close\r\n");
+        }
         relayValue.updateData(relay);
+        //convert(relayValue.getDataPointer(),*relayValue.getDataPointer());
         ble.updateCharacteristicValue(relayChar.getValueHandle(),relayValue.getDataPointer(),
         relayValue.getLenBytes());
     }