simple project to control mirrorless camera
Dependencies: mbed BLE_API nRF51822
Diff: bleservices/ServicePower.h
- Revision:
- 0:81f1818af032
- Child:
- 1:0932324a41f7
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/bleservices/ServicePower.h Wed Oct 21 11:32:45 2015 +0000 @@ -0,0 +1,79 @@ +#ifndef __BLE_SERVICE_POWER_H__ +#define __BLE_SERVICE_POWER_H__ + +#include "BLE.h" +#include "PowerSwitch.h" +#include "Light.h" +#include "FlashStore.h" + +class ServicePower { + +public: + ServicePower(BLE &_ble, Serial &_Serial, PowerSwitch &_Power, Light &_dayLight, FlashStore &_Store) : + ble(_ble), + Power(_Power), + Tserial(_Serial), + dayLight(_dayLight), + Store(_Store), + valueCharacteristic(GattCharacteristic::UUID_USER_CONTROL_HUMIDITY, &powerValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), + stateCharacteristic(GattCharacteristic::UUID_USER_CONTROL_POINT, &powerState) + { + setupService(); + } + + + virtual void onDataWritten(const GattWriteCallbackParams *params) { + if (params->handle == stateCharacteristic.getValueAttribute().getHandle() && (params->len == sizeof(powerState))) { + powerState = *(params->data); + + powerState ? Power.doSwitch(true,2.0) : Power.doSwitch(false, 2.0); + Tserial.printf("Power: %s\n", powerState ? "on" : "off"); + + } + } + +protected: + void setupService(void) { + GattCharacteristic *characteristics[] = {&stateCharacteristic, &valueCharacteristic}; + GattService powerService(GattService::UUID_SCAN_PARAMETERS_SERVICE, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *)); + + powerState = false; + powerValue = 0.0; + + ble.addService(powerService); + ble.onDataWritten(this, &ServicePower::onDataWritten); + + updatePowerValue(); + measurePeriod.attach(this, &ServicePower::updatePowerValue, 1.0); + + + } + + void updatePowerValue(void) { + + powerValue = dayLight.getLightValue(); + ble.updateCharacteristicValue(valueCharacteristic.getValueAttribute().getHandle(), (uint8_t *)&powerValue, sizeof(powerValue)); + Tserial.printf("updatePowerValue: %f \n", powerValue); + + } + + +protected: + BLE &ble; + PowerSwitch &Power; + Light &dayLight; + FlashStore &Store; + + Ticker measurePeriod; + + bool powerState; + float powerValue; + + ReadWriteGattCharacteristic<bool> stateCharacteristic; + ReadOnlyGattCharacteristic<float> valueCharacteristic; + + Serial &Tserial; // tx, rx + +}; + +#endif /* #ifndef __BLE_SERVICE_POWER_H__*/ \ No newline at end of file