Dependencies: AccelSensor BLE_API mbed nRF51822
RELAYService.h@0:027b7829b46a, 2018-01-19 (annotated)
- Committer:
- fjaviercifuentes
- Date:
- Fri Jan 19 02:38:38 2018 +0000
- Revision:
- 0:027b7829b46a
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fjaviercifuentes | 0:027b7829b46a | 1 | #ifndef __BLE_RELAY_SERVICE_H__ |
fjaviercifuentes | 0:027b7829b46a | 2 | #define __BLE_RELAY_SERVICE_H__ |
fjaviercifuentes | 0:027b7829b46a | 3 | |
fjaviercifuentes | 0:027b7829b46a | 4 | #include "mbed.h" |
fjaviercifuentes | 0:027b7829b46a | 5 | #include "ble/BLE.h" |
fjaviercifuentes | 0:027b7829b46a | 6 | #include "ble/Gap.h" |
fjaviercifuentes | 0:027b7829b46a | 7 | #include "ImobStateService.h" |
fjaviercifuentes | 0:027b7829b46a | 8 | |
fjaviercifuentes | 0:027b7829b46a | 9 | #define RELAY_TIME 1000000 // us |
fjaviercifuentes | 0:027b7829b46a | 10 | #define CTR12V_TIME 100000 // us |
fjaviercifuentes | 0:027b7829b46a | 11 | |
fjaviercifuentes | 0:027b7829b46a | 12 | class RELAYService { |
fjaviercifuentes | 0:027b7829b46a | 13 | public: |
fjaviercifuentes | 0:027b7829b46a | 14 | const static uint16_t RELAY_SERVICE_UUID = 0xC000; |
fjaviercifuentes | 0:027b7829b46a | 15 | const static uint16_t RELAY_STATE_CHARACTERISTIC_UUID = 0xC001; |
fjaviercifuentes | 0:027b7829b46a | 16 | |
fjaviercifuentes | 0:027b7829b46a | 17 | RELAYService(BLEDevice &_ble, ImobStateService * imobStateServicePtr) : |
fjaviercifuentes | 0:027b7829b46a | 18 | ble(_ble), |
fjaviercifuentes | 0:027b7829b46a | 19 | relayState(0), |
fjaviercifuentes | 0:027b7829b46a | 20 | actuatedRelay(P0_10,0), |
fjaviercifuentes | 0:027b7829b46a | 21 | Ctr12v(P0_3,0), |
fjaviercifuentes | 0:027b7829b46a | 22 | ISS(imobStateServicePtr), |
fjaviercifuentes | 0:027b7829b46a | 23 | RelayCharacteristic(RELAY_STATE_CHARACTERISTIC_UUID, &relayState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) |
fjaviercifuentes | 0:027b7829b46a | 24 | { |
fjaviercifuentes | 0:027b7829b46a | 25 | GattCharacteristic *charTable[] = {&RelayCharacteristic}; |
fjaviercifuentes | 0:027b7829b46a | 26 | GattService relayService(RELAY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
fjaviercifuentes | 0:027b7829b46a | 27 | |
fjaviercifuentes | 0:027b7829b46a | 28 | ble.addService(relayService); |
fjaviercifuentes | 0:027b7829b46a | 29 | |
fjaviercifuentes | 0:027b7829b46a | 30 | ble.gap().onDisconnection(this, &RELAYService::onDisconnectionFilter); |
fjaviercifuentes | 0:027b7829b46a | 31 | ble.gattServer().onDataWritten(this, &RELAYService::onDataWritten); |
fjaviercifuentes | 0:027b7829b46a | 32 | } |
fjaviercifuentes | 0:027b7829b46a | 33 | |
fjaviercifuentes | 0:027b7829b46a | 34 | GattAttribute::Handle_t getValueHandle() const |
fjaviercifuentes | 0:027b7829b46a | 35 | { |
fjaviercifuentes | 0:027b7829b46a | 36 | return RelayCharacteristic.getValueHandle(); |
fjaviercifuentes | 0:027b7829b46a | 37 | } |
fjaviercifuentes | 0:027b7829b46a | 38 | |
fjaviercifuentes | 0:027b7829b46a | 39 | void updateRelayState(uint8_t newRelayState) { |
fjaviercifuentes | 0:027b7829b46a | 40 | relayState = newRelayState; |
fjaviercifuentes | 0:027b7829b46a | 41 | actuatedRelay = newRelayState; |
fjaviercifuentes | 0:027b7829b46a | 42 | ble.gattServer().write(RelayCharacteristic.getValueHandle(), &relayState, 1); |
fjaviercifuentes | 0:027b7829b46a | 43 | } |
fjaviercifuentes | 0:027b7829b46a | 44 | |
fjaviercifuentes | 0:027b7829b46a | 45 | void activate() |
fjaviercifuentes | 0:027b7829b46a | 46 | { |
fjaviercifuentes | 0:027b7829b46a | 47 | Ctr12v = 1; |
fjaviercifuentes | 0:027b7829b46a | 48 | wait_us(CTR12V_TIME); |
fjaviercifuentes | 0:027b7829b46a | 49 | updateRelayState(1); |
fjaviercifuentes | 0:027b7829b46a | 50 | wait_us(RELAY_TIME);//depending of the time switching desired |
fjaviercifuentes | 0:027b7829b46a | 51 | updateRelayState(0); |
fjaviercifuentes | 0:027b7829b46a | 52 | wait_us(CTR12V_TIME); |
fjaviercifuentes | 0:027b7829b46a | 53 | Ctr12v = 0; |
fjaviercifuentes | 0:027b7829b46a | 54 | } |
fjaviercifuentes | 0:027b7829b46a | 55 | |
fjaviercifuentes | 0:027b7829b46a | 56 | protected: |
fjaviercifuentes | 0:027b7829b46a | 57 | |
fjaviercifuentes | 0:027b7829b46a | 58 | void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params) |
fjaviercifuentes | 0:027b7829b46a | 59 | { |
fjaviercifuentes | 0:027b7829b46a | 60 | if(authenticated && activated) |
fjaviercifuentes | 0:027b7829b46a | 61 | { |
fjaviercifuentes | 0:027b7829b46a | 62 | activate(); |
fjaviercifuentes | 0:027b7829b46a | 63 | //ISS->resetAuthenticationValues(); |
fjaviercifuentes | 0:027b7829b46a | 64 | } |
fjaviercifuentes | 0:027b7829b46a | 65 | } |
fjaviercifuentes | 0:027b7829b46a | 66 | |
fjaviercifuentes | 0:027b7829b46a | 67 | virtual void onDataWritten(const GattWriteCallbackParams *params) |
fjaviercifuentes | 0:027b7829b46a | 68 | { |
fjaviercifuentes | 0:027b7829b46a | 69 | if ((params->handle == RelayCharacteristic.getValueHandle()) && (params->len == 1) && authenticated) |
fjaviercifuentes | 0:027b7829b46a | 70 | { |
fjaviercifuentes | 0:027b7829b46a | 71 | activate(); |
fjaviercifuentes | 0:027b7829b46a | 72 | ISS->resetAuthenticationValues(); |
fjaviercifuentes | 0:027b7829b46a | 73 | |
fjaviercifuentes | 0:027b7829b46a | 74 | if(!activated) |
fjaviercifuentes | 0:027b7829b46a | 75 | ISS->updateActivationValue(1); |
fjaviercifuentes | 0:027b7829b46a | 76 | } |
fjaviercifuentes | 0:027b7829b46a | 77 | else |
fjaviercifuentes | 0:027b7829b46a | 78 | updateRelayState(0); |
fjaviercifuentes | 0:027b7829b46a | 79 | } |
fjaviercifuentes | 0:027b7829b46a | 80 | |
fjaviercifuentes | 0:027b7829b46a | 81 | private: |
fjaviercifuentes | 0:027b7829b46a | 82 | |
fjaviercifuentes | 0:027b7829b46a | 83 | BLEDevice &ble; |
fjaviercifuentes | 0:027b7829b46a | 84 | uint8_t relayState; |
fjaviercifuentes | 0:027b7829b46a | 85 | DigitalOut actuatedRelay; |
fjaviercifuentes | 0:027b7829b46a | 86 | DigitalOut Ctr12v; |
fjaviercifuentes | 0:027b7829b46a | 87 | |
fjaviercifuentes | 0:027b7829b46a | 88 | ImobStateService * ISS; |
fjaviercifuentes | 0:027b7829b46a | 89 | |
fjaviercifuentes | 0:027b7829b46a | 90 | ReadWriteGattCharacteristic<uint8_t> RelayCharacteristic; |
fjaviercifuentes | 0:027b7829b46a | 91 | }; |
fjaviercifuentes | 0:027b7829b46a | 92 | |
fjaviercifuentes | 0:027b7829b46a | 93 | #endif /* #ifndef __BLE_RELAY_SERVICE_H__ */ |