Guest User

ESPLEDv01

a guest
Oct 25th, 2020
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.18 KB | None | 0 0
  1. #include <NeoPixelBus.h>
  2. #include <ESP8266WiFi.h>
  3. #include <unordered_map>
  4. #include <vector>
  5.  
  6. //#define serial_dbg
  7.  
  8. const uint16_t PixelCount = 858; // Exactly 858 LEDs (0 to 857)
  9. const uint8_t PixelPin = 2;
  10.  
  11. const char* ssid = "#######"; // privacy :P
  12. const char* password = "#######"; // privacy :P
  13.  
  14. const byte MSG_BEGIN = 0xF0;
  15.  
  16. const byte C_ACTIVE = 0xC0;
  17. const byte C_OFF = 0xC1;
  18. const byte C_ON = 0xC2;
  19. const byte C_RGBW = 0xC3;
  20. const byte C_STATUS = 0xC4;
  21. const byte C_RGBW_RANGE = 0xC5;
  22.  
  23. const byte CA_OK = 0xA0;
  24. const byte CA_STATUS = 0xA1;
  25. const byte CA_ERROR = 0xAF;
  26.  
  27. const unsigned long switchThreshold = 1000;
  28.  
  29. WiFiServer server(22255);
  30. NeoPixelBus<NeoGrbwFeature, NeoEsp8266Uart1800KbpsMethod> strip(PixelCount, PixelPin);
  31.  
  32. WiFiClient stdClient;
  33.  
  34. //RgbwColor currentCol;
  35. bool whiteLight;
  36. bool isOn;
  37. int lastSwitch;
  38. unsigned long switchTime;
  39. //short first, last;
  40. unsigned long nextCheck;
  41.  
  42. struct RgbwHash {
  43.     std::size_t operator()(RgbwColor const& col) const noexcept {
  44.         return std::hash<int>{}((col.R << 12) | (col.G << 8) | (col.B << 4) | col.W);
  45.     }
  46. };
  47.  
  48. const int lineIndex[] = { 0,    63,   // llWR ( 64 LEDs)
  49.                           64,  163,   // llWB (100 LEDs)
  50.                           164, 227,   // llWL ( 64 LEDs)
  51.                           228, 327,   // llWF (100 LEDs)
  52.  
  53.                           328, 393,   // llDL ( 66 LEDs)
  54.                           394, 493,   // llDF (100 LEDs)
  55.                           494, 558,   // llDR ( 65 LEDs)
  56.                           559, 656,   // llDB ( 98 LEDs)
  57.  
  58.                           657, 668,   // llTW ( 12 LEDs)
  59.                           669, 711,   // llTC ( 43 LEDs)
  60.                           712, 741,   // llTP ( 30 LEDs)
  61.                           742, 754,   // llTS ( 13 LEDs)
  62.                           755, 797,   // llTB ( 43 LEDs)
  63.  
  64.                           798, 857 }; // llB  ( 60 LEDs)
  65.  
  66. RgbwColor lineColors[14];
  67.  
  68. void updateLight() {
  69.     if (!isOn) {
  70.         strip.ClearTo(RgbwColor(0, 0, 0, 0));
  71.     }
  72.     else {
  73.         if (whiteLight) {
  74.             strip.ClearTo(RgbwColor(0, 0, 0, 128), 0, 656); // Clear all ceiling strips to white
  75.             strip.ClearTo(RgbwColor(0, 0, 0, 0), 657, 857); // And the rest to black
  76.         }
  77.         else {
  78.             for (int i = 0; i < 14; i++) {
  79.                 strip.ClearTo(lineColors[i], lineIndex[2 * i], lineIndex[(2 * i) + 1]);
  80.             }
  81.         }
  82.     }
  83.     strip.Show();
  84. #ifdef serial_dbg
  85.     Serial.println("Light updated, new Values are:");
  86.     for (int i = 0; i < 14; i++) {
  87.         Serial.println(String(i) + ": " + String(lineColors[i].R) + " , " + String(lineColors[i].G) + " , " + String(lineColors[i].B) + " , " + String(lineColors[i].W));
  88.     }
  89. #endif
  90. }
  91.  
  92. void sendStatus(WiFiClient& client) {
  93.     std::vector<byte> barr;
  94.     barr.push_back(MSG_BEGIN);
  95.     barr.push_back(CA_STATUS);
  96.     barr.push_back((byte)isOn);
  97.  
  98.     std::unordered_map<RgbwColor, uint16_t, RgbwHash> map;
  99.     std::unordered_map<RgbwColor, uint16_t, RgbwHash>::iterator it;
  100.  
  101.     for (int i = 0; i < 14; i++) {
  102.         it = map.find(lineColors[i]);
  103.         if (it != map.end()) {
  104.             it->second = it->second | (1 << i);
  105.         }
  106.         else {
  107.             map.emplace(lineColors[i], 1 << i);
  108.         }
  109.     }
  110.  
  111.     for (auto item : map) {
  112.         barr.push_back(highByte(item.second));
  113.         barr.push_back(lowByte(item.second));
  114.         barr.push_back(item.first.R);
  115.         barr.push_back(item.first.G);
  116.         barr.push_back(item.first.B);
  117.         barr.push_back(item.first.W);
  118.     }
  119.     client.write(barr.data(), barr.size());
  120. #ifdef serial_dbg
  121.     Serial.println("Sending CA_STATUS");
  122. #endif
  123. }
  124.  
  125. void runCommand(WiFiClient& client) {
  126.     /*byte mess[128] = { 0 };
  127.     client.peekBytes(mess, 128);
  128.     Serial.print("Client Message is: ");
  129.     for (byte b : mess) {
  130.         Serial.print(String(b) + " ");
  131.     }*/
  132.  
  133.     if (client.read() != MSG_BEGIN) exit;
  134.     byte cmd = client.read();
  135.     //Serial.println("Client Command is " + String(cmd));
  136.  
  137.     switch (cmd) {
  138.     case C_ACTIVE: {
  139.         if (!stdClient.connected()) {
  140.             stdClient = client;
  141. #ifdef serial_dbg
  142.             Serial.println("Sending CA_OK");
  143. #endif
  144.         }
  145.         break;
  146.     }
  147.     case C_OFF: {
  148.         if (isOn) {
  149.             isOn = false;
  150.             updateLight();
  151.             sendStatus(client);
  152.         }
  153.         break;
  154.     }
  155.     case C_ON: {
  156.         if (!isOn) {
  157.             isOn = true;
  158.             updateLight();
  159.             sendStatus(client);
  160.         }
  161.         break;
  162.     }
  163.     case C_RGBW: {
  164. #ifdef serial_dbg
  165.         Serial.println(String(client.available()));
  166. #endif
  167.         do {
  168.             uint16_t mask = (client.read() << 8) | client.read();
  169. #ifdef serial_dbg
  170.             Serial.println(String(mask, BIN));
  171. #endif
  172.             RgbwColor currCol = RgbwColor(client.read(), client.read(), client.read(), client.read());
  173. #ifdef serial_dbg
  174.             Serial.println("CurrCol: " + String(currCol.R) + " , " + String(currCol.G) + " , " + String(currCol.B) + " , " + String(currCol.W));
  175. #endif
  176.             for (int i = 0; i < 14; i++) {
  177.                 if (mask & (1 << i)) {
  178.                     lineColors[i] = currCol;
  179. #ifdef serial_dbg
  180.                     Serial.println("Line " + String(i) + " | " + String(currCol.R) + " , " + String(currCol.G) + " , " + String(currCol.B) + " , " + String(currCol.W));
  181. #endif
  182.                 }
  183.             }
  184.         } while (client.available() >= 6);
  185.        
  186.         isOn = true;
  187.  
  188.         updateLight();
  189.  
  190.         sendStatus(client);
  191.         break;
  192.     }
  193.     case C_STATUS: {
  194.         sendStatus(client);
  195.         break;
  196.     }
  197.     default:
  198.         break;
  199.     }
  200. }
  201.  
  202. void setup() {
  203.     // put your setup code here, to run once:
  204.    
  205. #ifdef serial_dbg
  206.     Serial.begin(115200);
  207.     while (!Serial);
  208.     Serial.println();
  209.     Serial.println();
  210.  
  211.     Serial.print("Connecting to WiFi ");
  212.     Serial.println(ssid);
  213. #endif
  214.  
  215.     WiFi.mode(WIFI_STA);
  216.     WiFi.begin(ssid, password);
  217.  
  218.     while (WiFi.status() != WL_CONNECTED) {
  219.         delay(500);
  220. #ifdef serial_dbg
  221.         Serial.print(".");
  222. #endif
  223.     }
  224. #ifdef serial_dbg
  225.     Serial.println("");
  226.     Serial.println("WiFi connected");
  227. #endif
  228.  
  229.     server.begin();
  230. #ifdef serial_dbg
  231.     Serial.println("Server started at port 22255");
  232.     Serial.println(WiFi.localIP());
  233. #endif
  234.  
  235.     strip.Begin();
  236.     strip.Show();
  237. #ifdef serial_dbg
  238.     Serial.println("LEDs initialized");
  239. #endif
  240.  
  241.     //currentCol = RgbwColor(0, 0, 0, 127);
  242.     for (int i = 0; i < 14; i++) {
  243.         lineColors[i] = RgbwColor(0, 0, 0, 64);
  244.     }
  245.     isOn = true;
  246.     whiteLight = true;
  247.     switchTime = 0;
  248.     pinMode(3, INPUT);
  249.     lastSwitch = digitalRead(3);
  250. #ifdef serial_dbg
  251.     Serial.println("Status of Switch: " + String(lastSwitch));
  252. #endif
  253.     updateLight();
  254. #ifdef serial_dbg
  255.     Serial.println("Running...");
  256. #endif
  257. }
  258.  
  259. void loop() {
  260.  
  261.     // Light Switch
  262.     unsigned long currentTime = millis();
  263.     if (currentTime > nextCheck) {
  264.         if (digitalRead(3) != lastSwitch) {
  265.             lastSwitch = digitalRead(3);
  266. #ifdef serial_dbg
  267.             Serial.println("Status changed to: " + String(lastSwitch));
  268. #endif
  269.             isOn = !isOn;
  270.  
  271.             if (isOn) {
  272.                 if (!whiteLight && switchTime != 0 && currentTime - switchTime <= switchThreshold) {
  273.                     whiteLight = true;
  274.                 }
  275.                 else {
  276.                     whiteLight = false;
  277.                 }
  278.  
  279.                 switchTime = 0;
  280.             }
  281.             else {
  282.                 switchTime = currentTime;
  283.             }
  284.  
  285.             updateLight();
  286.         }
  287.         nextCheck = currentTime + 50;
  288.     }
  289.  
  290.     // Message handling
  291.     if (stdClient.available() > 0) {
  292. #ifdef serial_dbg
  293.         Serial.println("sdtClient message");
  294. #endif
  295.         runCommand(stdClient);
  296.         stdClient.flush();
  297.     }
  298.     else {
  299.         WiFiClient client = server.available();
  300.         if (!client) return;
  301.         int del = 0;
  302. #ifdef serial_dbg
  303.         Serial.println("New Client");
  304. #endif
  305.         while (!client.available()) {
  306.             if (del == 500) {
  307. #ifdef serial_dbg
  308.                 Serial.println("Timeout, client disconnected");
  309. #endif
  310.                 return;
  311.             }
  312.             ++del;
  313.             delay(5);
  314.         }
  315.         runCommand(client);
  316.         client.flush();
  317. #ifdef serial_dbg
  318.         Serial.println("Client Disconnected");
  319. #endif
  320.     }  
  321. }
  322.  
Advertisement
Add Comment
Please, Sign In to add comment