Bluetooth Low Energy for Smart Plug
Dependencies: BLE_API mbed nRF51822
SmartPlugBLE.h@0:25ad6eba7916, 2015-07-07 (annotated)
- Committer:
- Slepnir
- Date:
- Tue Jul 07 04:02:14 2015 +0000
- Revision:
- 0:25ad6eba7916
- Child:
- 2:6db5c9a2894c
V1:; - Successfully advertise and connect ; - Update fail; - Can read only 4 bit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Slepnir | 0:25ad6eba7916 | 1 | #ifndef SMART_PLUG_BLE_H |
Slepnir | 0:25ad6eba7916 | 2 | #define SMART_PLUG_BLE_H |
Slepnir | 0:25ad6eba7916 | 3 | |
Slepnir | 0:25ad6eba7916 | 4 | #include "SmartPlug.h" |
Slepnir | 0:25ad6eba7916 | 5 | #include "MVC.h" |
Slepnir | 0:25ad6eba7916 | 6 | #include "mbed.h" |
Slepnir | 0:25ad6eba7916 | 7 | #include "SPCommunication.h" |
Slepnir | 0:25ad6eba7916 | 8 | #include <vector> |
Slepnir | 0:25ad6eba7916 | 9 | |
Slepnir | 0:25ad6eba7916 | 10 | |
Slepnir | 0:25ad6eba7916 | 11 | class SmartPlugBLE: public Observable |
Slepnir | 0:25ad6eba7916 | 12 | { |
Slepnir | 0:25ad6eba7916 | 13 | public: |
Slepnir | 0:25ad6eba7916 | 14 | SmartPlugBLE() |
Slepnir | 0:25ad6eba7916 | 15 | { |
Slepnir | 0:25ad6eba7916 | 16 | ticker.attach(this,&SmartPlugBLE::periodicCallback, 1); // blink LED every second |
Slepnir | 0:25ad6eba7916 | 17 | } |
Slepnir | 0:25ad6eba7916 | 18 | |
Slepnir | 0:25ad6eba7916 | 19 | void onRelayWrite(uint8_t *data) |
Slepnir | 0:25ad6eba7916 | 20 | { |
Slepnir | 0:25ad6eba7916 | 21 | updateRelay(data); |
Slepnir | 0:25ad6eba7916 | 22 | notifyObservers(); |
Slepnir | 0:25ad6eba7916 | 23 | } |
Slepnir | 0:25ad6eba7916 | 24 | |
Slepnir | 0:25ad6eba7916 | 25 | void updateRelay(const uint8_t *data) |
Slepnir | 0:25ad6eba7916 | 26 | { |
Slepnir | 0:25ad6eba7916 | 27 | smartPlug.setRelay(data); |
Slepnir | 0:25ad6eba7916 | 28 | } |
Slepnir | 0:25ad6eba7916 | 29 | |
Slepnir | 0:25ad6eba7916 | 30 | void notifyObservers() |
Slepnir | 0:25ad6eba7916 | 31 | { |
Slepnir | 0:25ad6eba7916 | 32 | for(int i=0;i<observers.size();i++) |
Slepnir | 0:25ad6eba7916 | 33 | { |
Slepnir | 0:25ad6eba7916 | 34 | observers[i].update((void*)&smartPlug); |
Slepnir | 0:25ad6eba7916 | 35 | } |
Slepnir | 0:25ad6eba7916 | 36 | } |
Slepnir | 0:25ad6eba7916 | 37 | |
Slepnir | 0:25ad6eba7916 | 38 | void addObserver(Observer o) |
Slepnir | 0:25ad6eba7916 | 39 | { |
Slepnir | 0:25ad6eba7916 | 40 | observers.push_back(o); |
Slepnir | 0:25ad6eba7916 | 41 | } |
Slepnir | 0:25ad6eba7916 | 42 | |
Slepnir | 0:25ad6eba7916 | 43 | void periodicCallback(void) |
Slepnir | 0:25ad6eba7916 | 44 | { |
Slepnir | 0:25ad6eba7916 | 45 | //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
Slepnir | 0:25ad6eba7916 | 46 | } |
Slepnir | 0:25ad6eba7916 | 47 | |
Slepnir | 0:25ad6eba7916 | 48 | void updateData(void) |
Slepnir | 0:25ad6eba7916 | 49 | { |
Slepnir | 0:25ad6eba7916 | 50 | smartPlug.setVoltage(smartPlug.getVoltage()+1);//(spComm.getVoltage()); |
Slepnir | 0:25ad6eba7916 | 51 | smartPlug.setCurrent(smartPlug.getCurrent()+1);//(spComm.getCurrent()); |
Slepnir | 0:25ad6eba7916 | 52 | smartPlug.setPower(smartPlug.getPower()+1);//(spComm.getPower()); |
Slepnir | 0:25ad6eba7916 | 53 | smartPlug.setPowerFactor(smartPlug.getPowerFactor()+1);//(spComm.getPowerFactor()); |
Slepnir | 0:25ad6eba7916 | 54 | notifyObservers(); |
Slepnir | 0:25ad6eba7916 | 55 | } |
Slepnir | 0:25ad6eba7916 | 56 | |
Slepnir | 0:25ad6eba7916 | 57 | private: |
Slepnir | 0:25ad6eba7916 | 58 | SmartPlug smartPlug; |
Slepnir | 0:25ad6eba7916 | 59 | Ticker ticker; |
Slepnir | 0:25ad6eba7916 | 60 | //DigitalOut led1; |
Slepnir | 0:25ad6eba7916 | 61 | SPCommunication spComm; |
Slepnir | 0:25ad6eba7916 | 62 | //BLECommunication bleComm; |
Slepnir | 0:25ad6eba7916 | 63 | vector<Observer> observers; |
Slepnir | 0:25ad6eba7916 | 64 | }; |
Slepnir | 0:25ad6eba7916 | 65 | |
Slepnir | 0:25ad6eba7916 | 66 | #endif |