|
| 1 | +#include "CheerLights.h" |
| 2 | + |
| 3 | +#define MIN_UPDATE_INTERVAL 5000 |
| 4 | + |
| 5 | +CheerLights::CheerLights() { |
| 6 | + _colorName = "black"; |
| 7 | + _colorHex = 0x000000; |
| 8 | +} |
| 9 | + |
| 10 | +void CheerLights::begin(const char* ssid, const char* password) { |
| 11 | + Serial.begin(115200); |
| 12 | + |
| 13 | + // Connect to WiFi |
| 14 | + Serial.print("Connecting to WiFi"); |
| 15 | + |
| 16 | + #if defined(ESP8266) || defined(ESP32) |
| 17 | + WiFi.begin(ssid, password); |
| 18 | + while (WiFi.status() != WL_CONNECTED) { |
| 19 | + delay(500); |
| 20 | + Serial.print("."); |
| 21 | + } |
| 22 | + #elif defined(ARDUINO_SAMD_MKR1000) || defined(ARDUINO_SAMD_MKRWIFI1010) || defined(ARDUINO_AVR_UNO_WIFI_REV2) || defined(ARDUINO_ARCH_SAMD) |
| 23 | + while (WiFi.begin(ssid, password) != WL_CONNECTED) { |
| 24 | + delay(5000); |
| 25 | + Serial.print("."); |
| 26 | + } |
| 27 | + #else |
| 28 | + // Default WiFi connection method |
| 29 | + WiFi.begin(ssid, password); |
| 30 | + while (WiFi.status() != WL_CONNECTED) { |
| 31 | + delay(500); |
| 32 | + Serial.print("."); |
| 33 | + } |
| 34 | + #endif |
| 35 | + |
| 36 | + Serial.println("\nConnected to WiFi"); |
| 37 | +} |
| 38 | + |
| 39 | +void CheerLights::_fetchColor() { |
| 40 | + static unsigned long lastUpdate = 0; |
| 41 | + unsigned long currentTime = millis(); |
| 42 | + |
| 43 | + if (currentTime - lastUpdate < MIN_UPDATE_INTERVAL) { |
| 44 | + return; |
| 45 | + } |
| 46 | + lastUpdate = currentTime; |
| 47 | + |
| 48 | + if (WiFi.status() != WL_CONNECTED) { |
| 49 | + Serial.println(F("WiFi not connected")); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + HTTPClient http; |
| 54 | + http.begin(F("http://api.thingspeak.com/channels/1417/field/1/last.txt")); |
| 55 | + int httpCode = http.GET(); |
| 56 | + |
| 57 | + if (httpCode != HTTP_CODE_OK) { |
| 58 | + Serial.println(F("Failed to fetch color")); |
| 59 | + http.end(); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + _colorName = http.getString(); |
| 64 | + http.end(); |
| 65 | + |
| 66 | + _colorName.trim(); |
| 67 | + _colorName.toLowerCase(); |
| 68 | + |
| 69 | + // Use progmem for color mapping |
| 70 | + static const struct { |
| 71 | + const char* name; |
| 72 | + uint32_t color; |
| 73 | + } colorMap[] PROGMEM = { |
| 74 | + {"red", 0xFF0000}, |
| 75 | + {"green", 0x00FF00}, |
| 76 | + {"blue", 0x0000FF}, |
| 77 | + {"cyan", 0x00FFFF}, |
| 78 | + {"white", 0xFFFFFF}, |
| 79 | + {"warmwhite", 0xFDF5E6}, |
| 80 | + {"oldlace", 0xFDF5E6}, |
| 81 | + {"magenta", 0xFF00FF}, |
| 82 | + {"yellow", 0xFFFF00}, |
| 83 | + {"orange", 0xFFA500}, |
| 84 | + {"purple", 0x800080}, |
| 85 | + {"pink", 0xFFC0CB}, |
| 86 | + {"black", 0x000000} |
| 87 | + }; |
| 88 | + |
| 89 | + _colorHex = 0x000000; // Default to black |
| 90 | + for (const auto& color : colorMap) { |
| 91 | + if (_colorName == FPSTR(color.name)) { |
| 92 | + _colorHex = pgm_read_dword(&color.color); |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +String CheerLights::getColorName() { |
| 99 | + _fetchColor(); |
| 100 | + return _colorName; |
| 101 | +} |
| 102 | + |
| 103 | +uint32_t CheerLights::getColorHex() { |
| 104 | + _fetchColor(); |
| 105 | + return _colorHex; |
| 106 | +} |
| 107 | + |
| 108 | +uint8_t CheerLights::getRed() { |
| 109 | + return (_colorHex >> 16) & 0xFF; |
| 110 | +} |
| 111 | + |
| 112 | +uint8_t CheerLights::getGreen() { |
| 113 | + return (_colorHex >> 8) & 0xFF; |
| 114 | +} |
| 115 | + |
| 116 | +uint8_t CheerLights::getBlue() { |
| 117 | + return _colorHex & 0xFF; |
| 118 | +} |
0 commit comments