Bluetooth Low Energy for Smart Plug

Dependencies:   BLE_API mbed nRF51822

Committer:
Slepnir
Date:
Tue Jul 07 04:58:28 2015 +0000
Revision:
1:e036e77762fa
Parent:
0:25ad6eba7916
Child:
2:6db5c9a2894c
V2:; - Change to unsigned long; - Add relay

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Slepnir 0:25ad6eba7916 1 #ifndef SP_SERVICE_H
Slepnir 0:25ad6eba7916 2 #define SP_SERVICE_H
Slepnir 0:25ad6eba7916 3
Slepnir 0:25ad6eba7916 4 #include "ble/BLE.h"
Slepnir 0:25ad6eba7916 5 #include "MVC.h"
Slepnir 0:25ad6eba7916 6 #include "mbed.h"
Slepnir 0:25ad6eba7916 7 #include "SmartPlugBLE.h"
Slepnir 0:25ad6eba7916 8
Slepnir 0:25ad6eba7916 9
Slepnir 0:25ad6eba7916 10 #define FULL_UUID(uuid_16) { \
Slepnir 0:25ad6eba7916 11 0x9B,0x3F,uuid_16>>16,uuid_16&0xFF,0x65,0x80,0x47,0x51, \
Slepnir 0:25ad6eba7916 12 0xB0,0x2F,0xA7,0xB5,0x19,0x4C,0x7F,0x23 \
Slepnir 0:25ad6eba7916 13 }
Slepnir 0:25ad6eba7916 14
Slepnir 0:25ad6eba7916 15 static const uint16_t SPS_UUID_SERVICE = 0x1522;
Slepnir 0:25ad6eba7916 16 static const uint16_t SPS_UUID_VOLTAGE_CHAR = 0x1523;
Slepnir 0:25ad6eba7916 17 static const uint16_t SPS_UUID_CURERNT_CHAR = 0x1524;
Slepnir 0:25ad6eba7916 18 static const uint16_t SPS_UUID_POWER_CHAR = 0x1525;
Slepnir 0:25ad6eba7916 19 static const uint16_t SPS_UUID_POWER_FACTOR_CHAR = 0x1526;
Slepnir 0:25ad6eba7916 20 static const uint16_t SPS_UUID_ENERGY_CHAR = 0x1527;
Slepnir 0:25ad6eba7916 21 static const uint16_t SPS_UUID_RELAY_CHAR = 0x1528;
Slepnir 0:25ad6eba7916 22 static const uint16_t SPS_UUID_UPDATE_CHAR = 0x1529;
Slepnir 0:25ad6eba7916 23
Slepnir 0:25ad6eba7916 24 static const uint8_t SPS_UUID_SERVICE_FULL[] = FULL_UUID(SPS_UUID_SERVICE);
Slepnir 0:25ad6eba7916 25 static const uint8_t SPS_UUID_VOLTAGE_CHAR_FULL[] = FULL_UUID(SPS_UUID_VOLTAGE_CHAR);
Slepnir 0:25ad6eba7916 26 static const uint8_t SPS_UUID_CURERNT_CHAR_FULL[] = FULL_UUID(SPS_UUID_CURERNT_CHAR);
Slepnir 0:25ad6eba7916 27 static const uint8_t SPS_UUID_POWER_CHAR_FULL[] = FULL_UUID(SPS_UUID_POWER_CHAR);
Slepnir 0:25ad6eba7916 28 static const uint8_t SPS_UUID_POWER_FACTOR_CHAR_FULL[] = FULL_UUID(SPS_UUID_POWER_FACTOR_CHAR);
Slepnir 0:25ad6eba7916 29 static const uint8_t SPS_UUID_ENERGY_CHAR_FULL[] = FULL_UUID(SPS_UUID_ENERGY_CHAR);
Slepnir 0:25ad6eba7916 30 static const uint8_t SPS_UUID_RELAY_CHAR_FULL[] = FULL_UUID(SPS_UUID_RELAY_CHAR);
Slepnir 0:25ad6eba7916 31 static const uint8_t SPS_UUID_UPDATE_CHAR_FULL[] = FULL_UUID(SPS_UUID_UPDATE_CHAR);
Slepnir 0:25ad6eba7916 32
Slepnir 0:25ad6eba7916 33
Slepnir 0:25ad6eba7916 34 class SmartPlugService: public Observer
Slepnir 0:25ad6eba7916 35 {
Slepnir 0:25ad6eba7916 36 public:
Slepnir 0:25ad6eba7916 37 SmartPlugService(BLE &_ble, SmartPlugBLE &sys);
Slepnir 0:25ad6eba7916 38
Slepnir 0:25ad6eba7916 39 void updateVoltage(unsigned long value);
Slepnir 0:25ad6eba7916 40 void updateCurrent(unsigned long value);
Slepnir 0:25ad6eba7916 41 void updatePower(unsigned long value);
Slepnir 0:25ad6eba7916 42 void updatePowerFactor(unsigned long value);
Slepnir 0:25ad6eba7916 43 void onDataWritten(const GattWriteCallbackParams *params);
Slepnir 0:25ad6eba7916 44 void update(void* data);
Slepnir 0:25ad6eba7916 45 void updateData(SmartPlug* data);
Slepnir 1:e036e77762fa 46 void updateRelay(Relay* relay);
Slepnir 0:25ad6eba7916 47 void setupService(void);
Slepnir 0:25ad6eba7916 48 private:
Slepnir 1:e036e77762fa 49 class RelayValueBytes
Slepnir 1:e036e77762fa 50 {
Slepnir 1:e036e77762fa 51 public:
Slepnir 1:e036e77762fa 52 static const uint8_t MAX_SIZE_BYTES = 3;
Slepnir 1:e036e77762fa 53 static const uint8_t STATE_INDEX = 0;
Slepnir 1:e036e77762fa 54 static const uint8_t HOUR_TIMER_INDEX = 1;
Slepnir 1:e036e77762fa 55 static const uint8_t MINUTE_TIMER_INDEX = 2;
Slepnir 1:e036e77762fa 56
Slepnir 1:e036e77762fa 57 RelayValueBytes()
Slepnir 1:e036e77762fa 58 {
Slepnir 1:e036e77762fa 59
Slepnir 1:e036e77762fa 60 }
Slepnir 1:e036e77762fa 61
Slepnir 1:e036e77762fa 62 void updateData(Relay* relay)
Slepnir 1:e036e77762fa 63 {
Slepnir 1:e036e77762fa 64 data[STATE_INDEX] = relay->getState();
Slepnir 1:e036e77762fa 65 data[HOUR_TIMER_INDEX] = relay->getHrCounter();
Slepnir 1:e036e77762fa 66 data[MINUTE_TIMER_INDEX] = relay->getMinCounter();
Slepnir 1:e036e77762fa 67 }
Slepnir 1:e036e77762fa 68
Slepnir 1:e036e77762fa 69 uint8_t* getDataPointer()
Slepnir 1:e036e77762fa 70 {
Slepnir 1:e036e77762fa 71 return data;
Slepnir 1:e036e77762fa 72 }
Slepnir 1:e036e77762fa 73
Slepnir 1:e036e77762fa 74 uint8_t getLenBytes()
Slepnir 1:e036e77762fa 75 {
Slepnir 1:e036e77762fa 76 if(data[HOUR_TIMER_INDEX] == 0 && data[MINUTE_TIMER_INDEX] == 0)
Slepnir 1:e036e77762fa 77 return 1;
Slepnir 1:e036e77762fa 78 else
Slepnir 1:e036e77762fa 79 return MAX_SIZE_BYTES;
Slepnir 1:e036e77762fa 80 }
Slepnir 1:e036e77762fa 81 private:
Slepnir 1:e036e77762fa 82 uint8_t data[MAX_SIZE_BYTES];
Slepnir 1:e036e77762fa 83 };
Slepnir 1:e036e77762fa 84
Slepnir 0:25ad6eba7916 85 BLE ble;
Slepnir 0:25ad6eba7916 86
Slepnir 1:e036e77762fa 87 unsigned long voltage;
Slepnir 1:e036e77762fa 88 unsigned long current;
Slepnir 1:e036e77762fa 89 unsigned long power;
Slepnir 1:e036e77762fa 90 unsigned long powerFactor;
Slepnir 1:e036e77762fa 91 unsigned long energy;
Slepnir 1:e036e77762fa 92 RelayValueBytes relayValue;
Slepnir 0:25ad6eba7916 93 //uint8_t updateValue;
Slepnir 0:25ad6eba7916 94 SmartPlugBLE& system;
Slepnir 0:25ad6eba7916 95
Slepnir 1:e036e77762fa 96 ReadOnlyGattCharacteristic<unsigned long> voltageChar;
Slepnir 1:e036e77762fa 97 ReadOnlyGattCharacteristic<unsigned long> currentChar;
Slepnir 1:e036e77762fa 98 ReadOnlyGattCharacteristic<unsigned long> powerChar;
Slepnir 1:e036e77762fa 99 ReadOnlyGattCharacteristic<unsigned long> powerFactorChar;
Slepnir 1:e036e77762fa 100 ReadOnlyGattCharacteristic<unsigned long> energyChar;
Slepnir 1:e036e77762fa 101 GattCharacteristic relayChar;
Slepnir 0:25ad6eba7916 102 WriteOnlyGattCharacteristic<uint8_t> updateChar;
Slepnir 0:25ad6eba7916 103 };
Slepnir 0:25ad6eba7916 104
Slepnir 0:25ad6eba7916 105 #endif