simple project to control mirrorless camera

Dependencies:   mbed BLE_API nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ServicePower.h Source File

ServicePower.h

00001 #ifndef __BLE_SERVICE_POWER_H__
00002 #define __BLE_SERVICE_POWER_H__
00003 
00004 #include "BLE.h"
00005 #include "PowerSwitch.h"
00006 #include "Light.h"
00007 #include "FlashStore.h"
00008 
00009 extern const uint8_t Power_service_UUID[UUID::LENGTH_OF_LONG_UUID];
00010 extern const uint8_t Power_value_char_UUID[UUID::LENGTH_OF_LONG_UUID];
00011 extern const uint8_t Power_state_char_UUID[UUID::LENGTH_OF_LONG_UUID];
00012 
00013 class ServicePower {
00014    
00015 public:
00016     ServicePower(BLE &_ble, Serial &_Serial, PowerSwitch &_Power, Light &_dayLight, FlashStore &_Store) :
00017         ble(_ble),
00018         Tserial(_Serial),
00019         Power(_Power),
00020         dayLight(_dayLight),
00021         Store(_Store),        
00022         valueCharacteristic(Power_value_char_UUID, &powerValue, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00023         stateCharacteristic(Power_state_char_UUID, &powerState)
00024     {
00025         setupService();
00026     }
00027     
00028     
00029     virtual void onDataWritten(const GattWriteCallbackParams *params) {
00030         if (params->handle == stateCharacteristic.getValueAttribute().getHandle() && (params->len == sizeof(powerState))) {           
00031             powerState = *(params->data);    
00032             
00033             powerState ? Power.doSwitch(true,2.0) : Power.doSwitch(false, 2.0);
00034             Tserial.printf("Power: %s\n", powerState ? "on" : "off");    
00035                             
00036         }        
00037     }
00038 
00039 protected:
00040     void setupService(void) {
00041         GattCharacteristic *characteristics[] = {&stateCharacteristic, &valueCharacteristic};
00042         GattService powerService(Power_service_UUID, characteristics, sizeof(characteristics) / sizeof(GattCharacteristic *));
00043 
00044         powerState = false;
00045         powerValue = 0.0;
00046 
00047         ble.addService(powerService);
00048         ble.onDataWritten(this, &ServicePower::onDataWritten);
00049         
00050         updatePowerValue();
00051         measurePeriod.attach(this, &ServicePower::updatePowerValue, 1.0);
00052 
00053               
00054     }
00055     
00056      void updatePowerValue(void) {
00057         
00058         powerValue = dayLight.getLightValue();        
00059         ble.updateCharacteristicValue(valueCharacteristic.getValueAttribute().getHandle(), (uint8_t *)&powerValue, sizeof(powerValue));                
00060         Tserial.printf("updatePowerValue: %f \n", powerValue);
00061     
00062     }
00063     
00064 
00065 protected:
00066     BLE    &ble;
00067     PowerSwitch &Power;
00068     Light &dayLight;
00069     FlashStore &Store;
00070     
00071     Ticker measurePeriod;
00072     
00073     bool  powerState;
00074     float  powerValue;    
00075     
00076     ReadWriteGattCharacteristic<bool> stateCharacteristic;
00077     ReadOnlyGattCharacteristic<float> valueCharacteristic;
00078     
00079     Serial &Tserial; // tx, rx
00080 
00081 };
00082 
00083 #endif /* #ifndef __BLE_SERVICE_POWER_H__*/