Bluetooth Low Energy for Smart Plug
Dependencies: BLE_API mbed nRF51822
Fork of SmartPlugBLE by
Services/SmartPlugService.h@4:721ae30c92f8, 2015-07-14 (annotated)
- Committer:
- Slepnir
- Date:
- Tue Jul 14 10:10:19 2015 +0000
- Revision:
- 4:721ae30c92f8
- Parent:
- 3:aaa92c61931a
For debugging
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:6db5c9a2894c | 39 | void updateVoltage(uint32_t value); |
Slepnir | 2:6db5c9a2894c | 40 | void updateCurrent(uint32_t value); |
Slepnir | 2:6db5c9a2894c | 41 | void updatePower(uint32_t value); |
Slepnir | 2:6db5c9a2894c | 42 | void updatePowerFactor(uint32_t value); |
Slepnir | 0:25ad6eba7916 | 43 | void onDataWritten(const GattWriteCallbackParams *params); |
Slepnir | 2:6db5c9a2894c | 44 | void updateObserver(void* data); |
Slepnir | 1:e036e77762fa | 45 | void updateRelay(Relay* relay); |
Slepnir | 2:6db5c9a2894c | 46 | void updateEnergy(uint32_t value); |
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 | 2:6db5c9a2894c | 52 | static const uint8_t MAX_SIZE_BYTES = 2; |
Slepnir | 2:6db5c9a2894c | 53 | static const uint8_t STATE_BIT_INDEX = 7; |
Slepnir | 2:6db5c9a2894c | 54 | static const uint8_t STATE_BYTE_INDEX = 0; |
Slepnir | 2:6db5c9a2894c | 55 | static const uint8_t HOUR_TIMER_BYTE_INDEX = 0; |
Slepnir | 2:6db5c9a2894c | 56 | static const uint8_t MINUTE_TIMER_BYTE_INDEX = 1; |
Slepnir | 1:e036e77762fa | 57 | |
Slepnir | 1:e036e77762fa | 58 | RelayValueBytes() |
Slepnir | 1:e036e77762fa | 59 | { |
Slepnir | 2:6db5c9a2894c | 60 | memset(data,0,MAX_SIZE_BYTES); |
Slepnir | 1:e036e77762fa | 61 | } |
Slepnir | 1:e036e77762fa | 62 | |
Slepnir | 1:e036e77762fa | 63 | void updateData(Relay* relay) |
Slepnir | 1:e036e77762fa | 64 | { |
Slepnir | 3:aaa92c61931a | 65 | printf("updateData:: min = %d,hour = %d,state = %d\r\n",relay->getMinCounter(),relay->getHrCounter(),relay->getState()); |
Slepnir | 2:6db5c9a2894c | 66 | data[STATE_BYTE_INDEX] |= ((relay->getState())<<STATE_BIT_INDEX); |
Slepnir | 2:6db5c9a2894c | 67 | data[HOUR_TIMER_BYTE_INDEX] |= relay->getHrCounter(); |
Slepnir | 2:6db5c9a2894c | 68 | data[MINUTE_TIMER_BYTE_INDEX] = relay->getMinCounter(); |
Slepnir | 2:6db5c9a2894c | 69 | } |
Slepnir | 2:6db5c9a2894c | 70 | |
Slepnir | 2:6db5c9a2894c | 71 | void swap(uint8_t* ptr1,uint8_t* ptr2) |
Slepnir | 2:6db5c9a2894c | 72 | { |
Slepnir | 2:6db5c9a2894c | 73 | uint8_t temp; |
Slepnir | 2:6db5c9a2894c | 74 | temp = *ptr1; |
Slepnir | 2:6db5c9a2894c | 75 | *ptr1 = *ptr2; |
Slepnir | 2:6db5c9a2894c | 76 | *ptr2 = temp; |
Slepnir | 2:6db5c9a2894c | 77 | } |
Slepnir | 2:6db5c9a2894c | 78 | |
Slepnir | 2:6db5c9a2894c | 79 | void reverse(uint8_t* ptr) |
Slepnir | 2:6db5c9a2894c | 80 | { |
Slepnir | 3:aaa92c61931a | 81 | swap(&ptr[0],&ptr[1]); |
Slepnir | 1:e036e77762fa | 82 | } |
Slepnir | 1:e036e77762fa | 83 | |
Slepnir | 1:e036e77762fa | 84 | uint8_t* getDataPointer() |
Slepnir | 1:e036e77762fa | 85 | { |
Slepnir | 3:aaa92c61931a | 86 | //reverse(data); |
Slepnir | 1:e036e77762fa | 87 | return data; |
Slepnir | 1:e036e77762fa | 88 | } |
Slepnir | 1:e036e77762fa | 89 | |
Slepnir | 1:e036e77762fa | 90 | uint8_t getLenBytes() |
Slepnir | 1:e036e77762fa | 91 | { |
Slepnir | 2:6db5c9a2894c | 92 | if(data[HOUR_TIMER_BYTE_INDEX] == 0 && data[MINUTE_TIMER_BYTE_INDEX] == 0) |
Slepnir | 1:e036e77762fa | 93 | return 1; |
Slepnir | 1:e036e77762fa | 94 | else |
Slepnir | 1:e036e77762fa | 95 | return MAX_SIZE_BYTES; |
Slepnir | 1:e036e77762fa | 96 | } |
Slepnir | 1:e036e77762fa | 97 | private: |
Slepnir | 1:e036e77762fa | 98 | uint8_t data[MAX_SIZE_BYTES]; |
Slepnir | 1:e036e77762fa | 99 | }; |
Slepnir | 1:e036e77762fa | 100 | |
Slepnir | 0:25ad6eba7916 | 101 | BLE ble; |
Slepnir | 2:6db5c9a2894c | 102 | DigitalOut led; |
Slepnir | 0:25ad6eba7916 | 103 | |
Slepnir | 2:6db5c9a2894c | 104 | uint8_t voltage[4]; |
Slepnir | 2:6db5c9a2894c | 105 | uint8_t current[4]; |
Slepnir | 2:6db5c9a2894c | 106 | uint8_t power[4]; |
Slepnir | 2:6db5c9a2894c | 107 | uint8_t powerFactor[4]; |
Slepnir | 2:6db5c9a2894c | 108 | uint8_t energy[4]; |
Slepnir | 2:6db5c9a2894c | 109 | |
Slepnir | 1:e036e77762fa | 110 | RelayValueBytes relayValue; |
Slepnir | 0:25ad6eba7916 | 111 | //uint8_t updateValue; |
Slepnir | 0:25ad6eba7916 | 112 | SmartPlugBLE& system; |
Slepnir | 0:25ad6eba7916 | 113 | |
Slepnir | 2:6db5c9a2894c | 114 | ReadOnlyGattCharacteristic<uint8_t[4]> voltageChar; |
Slepnir | 2:6db5c9a2894c | 115 | ReadOnlyGattCharacteristic<uint8_t[4]> currentChar; |
Slepnir | 2:6db5c9a2894c | 116 | ReadOnlyGattCharacteristic<uint8_t[4]> powerChar; |
Slepnir | 2:6db5c9a2894c | 117 | ReadOnlyGattCharacteristic<uint8_t[4]> powerFactorChar; |
Slepnir | 2:6db5c9a2894c | 118 | ReadOnlyGattCharacteristic<uint8_t[4]> energyChar; |
Slepnir | 1:e036e77762fa | 119 | GattCharacteristic relayChar; |
Slepnir | 0:25ad6eba7916 | 120 | WriteOnlyGattCharacteristic<uint8_t> updateChar; |
Slepnir | 0:25ad6eba7916 | 121 | }; |
Slepnir | 0:25ad6eba7916 | 122 | |
Slepnir | 0:25ad6eba7916 | 123 | #endif |