Bluetooth Low Energy for Smart Plug
Dependencies: BLE_API mbed nRF51822
Diff: Services/SmartPlugService.h
- Revision:
- 2:6db5c9a2894c
- Parent:
- 1:e036e77762fa
- Child:
- 3:aaa92c61931a
diff -r e036e77762fa -r 6db5c9a2894c Services/SmartPlugService.h --- a/Services/SmartPlugService.h Tue Jul 07 04:58:28 2015 +0000 +++ b/Services/SmartPlugService.h Wed Jul 08 07:25:11 2015 +0000 @@ -36,44 +36,60 @@ public: SmartPlugService(BLE &_ble, SmartPlugBLE &sys); - void updateVoltage(unsigned long value); - void updateCurrent(unsigned long value); - void updatePower(unsigned long value); - void updatePowerFactor(unsigned long value); + void updateVoltage(uint32_t value); + void updateCurrent(uint32_t value); + void updatePower(uint32_t value); + void updatePowerFactor(uint32_t value); void onDataWritten(const GattWriteCallbackParams *params); - void update(void* data); - void updateData(SmartPlug* data); + void updateObserver(void* data); void updateRelay(Relay* relay); + void updateEnergy(uint32_t value); void setupService(void); private: class RelayValueBytes { public: - static const uint8_t MAX_SIZE_BYTES = 3; - static const uint8_t STATE_INDEX = 0; - static const uint8_t HOUR_TIMER_INDEX = 1; - static const uint8_t MINUTE_TIMER_INDEX = 2; + static const uint8_t MAX_SIZE_BYTES = 2; + static const uint8_t STATE_BIT_INDEX = 7; + static const uint8_t STATE_BYTE_INDEX = 0; + static const uint8_t HOUR_TIMER_BYTE_INDEX = 0; + static const uint8_t MINUTE_TIMER_BYTE_INDEX = 1; RelayValueBytes() { - + memset(data,0,MAX_SIZE_BYTES); } void updateData(Relay* relay) { - data[STATE_INDEX] = relay->getState(); - data[HOUR_TIMER_INDEX] = relay->getHrCounter(); - data[MINUTE_TIMER_INDEX] = relay->getMinCounter(); + data[STATE_BYTE_INDEX] |= ((relay->getState())<<STATE_BIT_INDEX); + data[HOUR_TIMER_BYTE_INDEX] |= relay->getHrCounter(); + data[MINUTE_TIMER_BYTE_INDEX] = relay->getMinCounter(); + } + + void swap(uint8_t* ptr1,uint8_t* ptr2) + { + uint8_t temp; + temp = *ptr1; + *ptr1 = *ptr2; + *ptr2 = temp; + } + + void reverse(uint8_t* ptr) + { + swap(&ptr[0],&ptr[3]); + swap(&ptr[1],&ptr[2]); } uint8_t* getDataPointer() { + reverse(data); return data; } uint8_t getLenBytes() { - if(data[HOUR_TIMER_INDEX] == 0 && data[MINUTE_TIMER_INDEX] == 0) + if(data[HOUR_TIMER_BYTE_INDEX] == 0 && data[MINUTE_TIMER_BYTE_INDEX] == 0) return 1; else return MAX_SIZE_BYTES; @@ -83,21 +99,23 @@ }; BLE ble; + DigitalOut led; - unsigned long voltage; - unsigned long current; - unsigned long power; - unsigned long powerFactor; - unsigned long energy; + uint8_t voltage[4]; + uint8_t current[4]; + uint8_t power[4]; + uint8_t powerFactor[4]; + uint8_t energy[4]; + RelayValueBytes relayValue; //uint8_t updateValue; SmartPlugBLE& system; - ReadOnlyGattCharacteristic<unsigned long> voltageChar; - ReadOnlyGattCharacteristic<unsigned long> currentChar; - ReadOnlyGattCharacteristic<unsigned long> powerChar; - ReadOnlyGattCharacteristic<unsigned long> powerFactorChar; - ReadOnlyGattCharacteristic<unsigned long> energyChar; + ReadOnlyGattCharacteristic<uint8_t[4]> voltageChar; + ReadOnlyGattCharacteristic<uint8_t[4]> currentChar; + ReadOnlyGattCharacteristic<uint8_t[4]> powerChar; + ReadOnlyGattCharacteristic<uint8_t[4]> powerFactorChar; + ReadOnlyGattCharacteristic<uint8_t[4]> energyChar; GattCharacteristic relayChar; WriteOnlyGattCharacteristic<uint8_t> updateChar; };