From f6aef1b8572f8ad7955179c6f6ebd645eee58867 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Wed, 9 May 2018 09:51:49 -0700 Subject: [PATCH] Completing Lifx driver that uses Particle cloud; Particle phone app provides a nice interface to control the bulbs from a smartphone (and the website interface is avaiable as well). --- version2/src/RPi/IoTAddress.h | 23 ++---- version2/src/RPi/IoTDeviceAddress.h | 2 +- version2/src/RPi/IoTUDP.h | 17 ++--- version2/src/RPi/LifxLightBulb.cpp | 100 +++----------------------- version2/src/RPi/LifxLightBulb.h | 16 +---- version2/src/RPi/LightsController.ino | 40 +++++++---- version2/src/RPi/Makefile | 4 +- 7 files changed, 53 insertions(+), 149 deletions(-) diff --git a/version2/src/RPi/IoTAddress.h b/version2/src/RPi/IoTAddress.h index a057d3d..360caed 100644 --- a/version2/src/RPi/IoTAddress.h +++ b/version2/src/RPi/IoTAddress.h @@ -2,6 +2,8 @@ #define _IOTADDRESS_HPP__ #include +#include "application.h" + using namespace std; @@ -17,7 +19,7 @@ class IoTAddress public: // Constructor - IoTAddress(uint8_t* _sAddress) { + IoTAddress(IPAddress _sAddress) { inetAddress = _sAddress; } @@ -32,28 +34,13 @@ class IoTAddress } - uint8_t* getAddress() { + IPAddress getAddress() { return inetAddress; } - - /*string getURL(string strURLComplete) { - - return "http://" + inetAddress + strURLComplete; - } - - - // Custom hasher for IoTAddress / IoTDeviceAddress iterator - size_t hash(IoTAddress const& devAddress) const { - - std::hash hashVal; - return hashVal(inetAddress); - }*/ - - // IoTAddress class properties protected: - uint8_t* inetAddress; + IPAddress inetAddress; }; #endif diff --git a/version2/src/RPi/IoTDeviceAddress.h b/version2/src/RPi/IoTDeviceAddress.h index c9c2828..d6f1f27 100644 --- a/version2/src/RPi/IoTDeviceAddress.h +++ b/version2/src/RPi/IoTDeviceAddress.h @@ -20,7 +20,7 @@ class IoTDeviceAddress final : public IoTAddress public: // Constructor - IoTDeviceAddress(uint8_t* _sAddress, int _iSrcPort, int _iDstPort, bool _isSrcPortWildCard, bool _isDstPortWildCard) : IoTAddress(_sAddress) { + IoTDeviceAddress(IPAddress _sAddress, int _iSrcPort, int _iDstPort, bool _isSrcPortWildCard, bool _isDstPortWildCard) : IoTAddress(_sAddress) { iSrcPort = _iSrcPort; iDstPort = _iDstPort; diff --git a/version2/src/RPi/IoTUDP.h b/version2/src/RPi/IoTUDP.h index e9f50ca..f538553 100644 --- a/version2/src/RPi/IoTUDP.h +++ b/version2/src/RPi/IoTUDP.h @@ -3,7 +3,6 @@ #include #include "IoTDeviceAddress.h" -#include "application.h" using namespace std; @@ -20,7 +19,7 @@ class IoTUDP final private: //UDPSocket *socket; UDP socket; - uint8_t* strHostAddress; + IPAddress strHostAddress; int iSrcPort; int iDstPort; bool didClose; @@ -36,7 +35,6 @@ class IoTUDP final iDstPort = iotDevAdd->getDestinationPortNumber(); timeOut = 0; - //socket = new UDPSocket(iSrcPort); socket.begin(iSrcPort); didClose = false; } @@ -46,7 +44,7 @@ class IoTUDP final } - uint8_t* getHostAddress() { + IPAddress getHostAddress() { return strHostAddress; } @@ -69,20 +67,17 @@ class IoTUDP final // Send data packet void sendData(const char* buffer, int bufferLen) { - //void sendData(const void* buffer, int bufferLen) { + unsigned short destinationPort = (unsigned short) iDstPort; - IPAddress ipFromBytes = strHostAddress; - socket.sendPacket(buffer, bufferLen, ipFromBytes, destinationPort); - //socket->sendTo(buffer, bufferLen, strHostAddress, destinationPort); + IPAddress ipFromBytes(strHostAddress); + socket.sendPacket(buffer, bufferLen, strHostAddress, destinationPort); } // Receive data packet int receiveData(char* buffer, int iMaxDataLength) { - //int receiveData(void* buffer, int iMaxDataLength) { - //unsigned short destinationPort = (unsigned short) iDstPort; + return socket.receivePacket(buffer, iMaxDataLength); - //return socket->recvFrom(buffer, iMaxDataLength, strHostAddress, destinationPort); } }; #endif diff --git a/version2/src/RPi/LifxLightBulb.cpp b/version2/src/RPi/LifxLightBulb.cpp index aff004f..247fb22 100644 --- a/version2/src/RPi/LifxLightBulb.cpp +++ b/version2/src/RPi/LifxLightBulb.cpp @@ -7,79 +7,22 @@ #include "LifxLightBulb.h" #include "IoTSet.h" #include "IoTDeviceAddress.h" +#include "application.h" using namespace std; -/* TODO: Uncomment this to do normal C++ (g++) -int main(int argc, char *argv[]) -{ - string macAddress1 = "D073D5128E300000"; - //string macAddress1 = "D073D50241DA0000"; - string devIPAddress1 = "192.168.1.126"; - //string devIPAddress1 = "192.168.1.232"; - LifxLightBulb *llb1 = new LifxLightBulb(devIPAddress1, macAddress1, 12345); - - llb1->init(); - for(int i=0; i < 1; i++) { - llb1->turnOff(); - this_thread::sleep_for (chrono::milliseconds(1000)); - cout << "Turn off" << endl; - llb1->turnOn(); - this_thread::sleep_for (chrono::milliseconds(1000)); - cout << "Turn on" << endl; - } - - - //delete setDevAddress1; - delete llb1; - - return 0; -}*/ - -// Constructor +// Default constructor LifxLightBulb::LifxLightBulb() { - // LB1 macAddress: d0:73:d5:12:8e:30 - // LB2 macAddress: d0:73:d5:02:41:da - string macAddress = "D073D5128E300000"; - //string macAddress = "D073D50241DA0000"; - /*bulbMacAddress[0] = 0xD0; - bulbMacAddress[1] = 0x73; - bulbMacAddress[2] = 0xD5; - bulbMacAddress[3] = 0x02; - bulbMacAddress[4] = 0x41; - bulbMacAddress[5] = 0xDA; - bulbMacAddress[6] = 0x00; - bulbMacAddress[7] = 0x00;*/ - - char tmpMacAddress[16]; - strcpy(tmpMacAddress, macAddress.c_str()); - for(int i=0; i<16; i=i+2) { - // Take 2 digits and then convert - char tmpMacByte[2]; - tmpMacByte[0] = tmpMacAddress[i]; - tmpMacByte[1] = tmpMacAddress[i+1]; - bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); - } + } // Constructor -LifxLightBulb::LifxLightBulb(uint8_t* ipAddress, string macAddress, int srcPort) { - - // Initialize macAddress - char tmpMacAddress[16]; - strcpy(tmpMacAddress, macAddress.c_str()); - //test[0] = (char) strtol(strTest.c_str(), NULL, 16); - for(int i=0; i<16; i=i+2) { - // Take 2 digits and then convert - char tmpMacByte[2]; - tmpMacByte[0] = tmpMacAddress[i]; - tmpMacByte[1] = tmpMacAddress[i+1]; - bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); - } - //cout << "MAC address is set. Value: "; +LifxLightBulb::LifxLightBulb(IPAddress ipAddress, char* macAddress, int srcPort) { + memcpy(bulbMacAddress, macAddress, 8); + // Initialize device address // Port 56700 is the default port for Lifx IoTDeviceAddress* devAddress = new IoTDeviceAddress(ipAddress, srcPort, 56700, false, false); @@ -87,30 +30,6 @@ LifxLightBulb::LifxLightBulb(uint8_t* ipAddress, string macAddress, int srcPort) myset1->insert(devAddress); IoTSet* setDevAddress = new IoTSet(myset1); lb_addresses = setDevAddress; - //cout << "Device address is set! " << endl; - -} - - -// Driver constructor always gets a pointer to device address, trailed by class arguments of generic type -LifxLightBulb::LifxLightBulb(IoTSet* _devAddress, string macAddress) { - - // Initialize macAddress - char tmpMacAddress[16]; - strcpy(tmpMacAddress, macAddress.c_str()); - //test[0] = (char) strtol(strTest.c_str(), NULL, 16); - for(int i=0; i<16; i=i+2) { - // Take 2 digits and then convert - char tmpMacByte[2]; - tmpMacByte[0] = tmpMacAddress[i]; - tmpMacByte[1] = tmpMacAddress[i+1]; - bulbMacAddress[i/2] = (char) strtol(tmpMacByte, NULL, 16); - } - //cout << "MAC address is set. Value: "; - - // Initialize device address - lb_addresses = _devAddress; - //cout << "Device address is set! " << endl; } @@ -139,7 +58,6 @@ LifxLightBulb::~LifxLightBulb() { // Initialize the lightbulb void LifxLightBulb::init() { - //if (didAlreadyInit.exchange(true)) if (didAlreadyInit) return; // Set to true if not yet @@ -147,7 +65,6 @@ void LifxLightBulb::init() { unordered_set::const_iterator itr = lb_addresses->begin(); IoTDeviceAddress* deviceAddress = (IoTDeviceAddress*) *itr; - //cout << "Address: " << deviceAddress->getAddress() << endl; // Create IoTUDP socket communicationSocket = new IoTUDP(deviceAddress); @@ -162,8 +79,6 @@ void LifxLightBulb::init() { // the correct references to stuff thread th1 (&LifxLightBulb::workerFunction, this, this); th1.detach(); - - //cout << "Initialized LifxLightBulb!" << endl; } @@ -455,7 +370,8 @@ void LifxLightBulb::workerFunction(LifxLightBulb* llb) { } // Wait a bit as to not tie up system resources - this_thread::sleep_for (chrono::milliseconds(100)); + //this_thread::sleep_for (chrono::milliseconds(100)); + delay(100); //cout << endl << "Sleep and wake up!" << endl; } } diff --git a/version2/src/RPi/LifxLightBulb.h b/version2/src/RPi/LifxLightBulb.h index 216a47a..f2852b4 100644 --- a/version2/src/RPi/LifxLightBulb.h +++ b/version2/src/RPi/LifxLightBulb.h @@ -36,10 +36,6 @@ using namespace std; // Driver LifxLightBulb // Implemented based on LightBulb virtual class (interface) -//std::atomic -//std::atomic didAlreadyInit(false); -//std::atomic didGetBulbVersion(false); - class LifxLightBulb : public LightBulb { private: @@ -50,7 +46,6 @@ class LifxLightBulb : public LightBulb IoTUDP *communicationSocket; char bulbMacAddress[8]; //TODO: - //static Semaphore socketMutex = new Semaphore(1); bool sendSocketFlag = false; // Current Bulb Values @@ -60,11 +55,8 @@ class LifxLightBulb : public LightBulb int currentTemperature = 9000; bool bulbIsOn = false; - //std::atomic - //atomic didAlreadyInit; - bool didAlreadyInit; - //atomic didGetBulbVersion; - bool didGetBulbVersion; + bool didAlreadyInit = false; + bool didGetBulbVersion = false; // Mutex locks mutex socketMutex; @@ -95,9 +87,7 @@ class LifxLightBulb : public LightBulb // Constructor LifxLightBulb(); - LifxLightBulb(uint8_t* ipAddress, string macAddress, int srcPort); - //LifxLightBulb(IoTSet* _devAddress, string macAddress); - LifxLightBulb(IoTSet* _devAddress, string macAddress); + LifxLightBulb(IPAddress ipAddress, char* macAddress, int srcPort); ~LifxLightBulb(); // Initialize the lightbulb void init(); diff --git a/version2/src/RPi/LightsController.ino b/version2/src/RPi/LightsController.ino index 9ffe162..b47d15d 100644 --- a/version2/src/RPi/LightsController.ino +++ b/version2/src/RPi/LightsController.ino @@ -8,34 +8,48 @@ LifxLightBulb *llb1; LifxLightBulb *llb2; +int led1 = D7; + void setup() { // Bulb 1 - string macAddress1 = "D073D5128E300000"; - uint8_t devIPAddress1[] = { 192, 168, 1, 126 }; + char macAddress1[8]; + macAddress1[0] = 0xD0; + macAddress1[1] = 0x73; + macAddress1[2] = 0xD5; + macAddress1[3] = 0x12; + macAddress1[4] = 0x8E; + macAddress1[5] = 0x30; + macAddress1[6] = 0x00; + macAddress1[7] = 0x00; + IPAddress devIPAddress1(192, 168, 1, 126); + llb1 = new LifxLightBulb(devIPAddress1, macAddress1, 12345); llb1->init(); // Bulb 2 - string macAddress2 = "D073D50241DA0000"; - uint8_t devIPAddress2[] = { 192, 168, 1, 232 }; + char macAddress2[8]; + macAddress2[0] = 0xD0; + macAddress2[1] = 0x73; + macAddress2[2] = 0xD5; + macAddress2[3] = 0x02; + macAddress2[4] = 0x41; + macAddress2[5] = 0xDA; + macAddress2[6] = 0x00; + macAddress2[7] = 0x00; + IPAddress devIPAddress2(192, 168, 1, 232); + llb2 = new LifxLightBulb(devIPAddress2, macAddress2, 12346); llb2->init(); - + + pinMode(led1, OUTPUT); Particle.function("lifx1",lifx1Toggle); Particle.function("lifx2",lifx2Toggle); } -void loop() -{ +void loop() { // Nothing to do here - for(int i=0; i < 3; i++) { - llb1->turnOff(); - delay(1000); - llb1->turnOn(); - delay(1000); - } } diff --git a/version2/src/RPi/Makefile b/version2/src/RPi/Makefile index 276d090..948d8e1 100644 --- a/version2/src/RPi/Makefile +++ b/version2/src/RPi/Makefile @@ -16,9 +16,11 @@ compile-c: compile: rm -f *.bin particle compile raspberry-pi + #particle compile photon + flash: - particle flash Pi-3 raspberry-pi*.bin + particle flash Pi-1 raspberry-pi*.bin PHONY += clean clean: -- 2.34.1