Completing Lifx driver that uses Particle cloud; Particle phone app provides a nice...
authorrtrimana <rtrimana@uci.edu>
Wed, 9 May 2018 16:51:49 +0000 (09:51 -0700)
committerrtrimana <rtrimana@uci.edu>
Wed, 9 May 2018 16:51:49 +0000 (09:51 -0700)
version2/src/RPi/IoTAddress.h
version2/src/RPi/IoTDeviceAddress.h
version2/src/RPi/IoTUDP.h
version2/src/RPi/LifxLightBulb.cpp
version2/src/RPi/LifxLightBulb.h
version2/src/RPi/LightsController.ino
version2/src/RPi/Makefile

index a057d3d38b1b0ea1f89b2ced759bd71f8a69c6df..360caed507cfb3d5646fd3af9c4e1753c7f137db 100644 (file)
@@ -2,6 +2,8 @@
 #define _IOTADDRESS_HPP__
 #include <iostream>
 
+#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<std::string> hashVal;
-                       return hashVal(inetAddress);
-               }*/
-
-
        // IoTAddress class properties
        protected:
-               uint8_t* inetAddress;
+               IPAddress inetAddress;
 };
 #endif 
index c9c2828f48144169fea0ed0b63e354cf4d79028f..d6f1f2777ffdb432c862f782849d4c98389fdbc2 100644 (file)
@@ -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;
index e9f50ca44c4cd4698a54f82e77c773667333d958..f53855330eaf5a3f755689b71eea74f3ae1dfc55 100644 (file)
@@ -3,7 +3,6 @@
 #include <iostream>
 
 #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 
index aff004ff43f92908002a77409b82c1aa0bb88042..247fb22a678754ad88bc8c2c738b82c7afb00eda 100644 (file)
@@ -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<void*>* setDevAddress = new IoTSet<void*>(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<void*>* _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<void*>::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;
        }
 }
index 216a47a726603f9911fecfc8bf2eaaf8fae8c07c..f2852b4df3aab68b303305f6e60a9fb206edbf63 100644 (file)
@@ -36,10 +36,6 @@ using namespace std;
 // Driver LifxLightBulb
 // Implemented based on LightBulb virtual class (interface)
 
-//std::atomic
-//std::atomic<bool> didAlreadyInit(false);
-//std::atomic<bool> 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<bool> didAlreadyInit;
-               bool didAlreadyInit;
-               //atomic<bool> 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<IoTDeviceAddress*>* _devAddress, string macAddress);
-               LifxLightBulb(IoTSet<void*>* _devAddress, string macAddress);
+               LifxLightBulb(IPAddress ipAddress, char* macAddress, int srcPort);
                ~LifxLightBulb();
                // Initialize the lightbulb
                void init();
index 9ffe162726f08d5ebc98745acd2e4835c014a79b..b47d15d2cf69daa30085192edbd214fda8333cd6 100644 (file)
@@ -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);
-       }
 }
 
 
index 276d090b0986d414eb75c717a17b42c0b86f4228..948d8e1a64c44da78201352df4b3f223a3b1c953 100644 (file)
@@ -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: