Skip to content

Commit

Permalink
1 Hour fading
Browse files Browse the repository at this point in the history
  • Loading branch information
nickcrisci committed Oct 18, 2024
1 parent f30065f commit 85523a2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
6 changes: 4 additions & 2 deletions include/NanoleafApiWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,15 @@ class NanoleafApiWrapper

bool setPower(const bool &state);

bool setStaticColors(const JsonObject &doc);

bool registerEvents(const std::vector<int> &eventIds);

void processEvents();

typedef std::function<void()> LayoutChangeCallback;
typedef std::function<void()> ColorCallback;
void setLayoutChangeCallback(LayoutChangeCallback callback);
void setColorCallback(ColorCallback callback);
bool setStaticColors(const JsonObject &doc);

private:
bool sendRequest(
Expand All @@ -56,6 +57,7 @@ class NanoleafApiWrapper
WiFiClient *eventClient;
bool registeredForEvents = false;
LayoutChangeCallback layoutChangeCallback;
ColorCallback colorCallback;
};

#endif
34 changes: 25 additions & 9 deletions src/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ char nanoleafBaseUrl[55] = ""; // NanoLeaf Baseurl (http://<ip>:<port>)
char nanoleafAuthToken[33] = ""; // Nanoleaf Auth Token

// Setup Vars
char friendId[4] = "";
char friendId[36] = "";
char name[36] = "";
char groupId[36] = "";

Expand All @@ -28,6 +28,8 @@ unsigned long lastPublishTime = 0;
bool shouldSaveConfig = false;
bool layoutChanged = false;
bool initialSetupDone = false;
unsigned long lastColorTime = 0;
bool currentlyShowingCustomColor = false;

#if defined(ESP32)
#define LED_BUILTIN 2
Expand Down Expand Up @@ -67,12 +69,12 @@ void connectToWifi(bool useSavedCredentials)

void setupWiFiManager()
{
wifiManager.setDebugOutput(false); // Disable the debug output to keep Serial clean
wifiManager.setDebugOutput(true); // Disable the debug output to keep Serial clean
wifiManager.setSaveConfigCallback(saveConfigCallback);

WiFiManagerParameter customGroupId("groupId", "Group ID", groupId, 36);
WiFiManagerParameter customName("name", "Name", name, 36);
WiFiManagerParameter customFriendId("friendId", "FriendID", friendId, 4);
WiFiManagerParameter customFriendId("friendId", "FriendID", friendId, 36);

wifiManager.addParameter(&customGroupId);
wifiManager.addParameter(&customName);
Expand Down Expand Up @@ -428,8 +430,8 @@ void initialSetup()
}
}

Serial.println("MQTT Verbindung wird aufgebaut...");
setupMQTTClient();
/*Serial.println("MQTT Verbindung wird aufgebaut...");
setupMQTTClient();*/

Serial.println("Generating Auth token");
while (!nanoleaf.isConnected())
Expand All @@ -440,20 +442,26 @@ void initialSetup()
}

initialSetupDone = true;
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LED_BUILTIN, HIGH);
saveConfigToFile();

Serial.println("Ersteinrichtung abgeschlossen. Der ESP wird neu gestartet...");
ESP.restart();
}

void colorCallback()
{
lastColorTime = millis();
currentlyShowingCustomColor = true;
}

void setup()
{
Serial.begin(115200);

// Initialize onboard led and turn off
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LED_BUILTIN, HIGH);

loadConfigFromFile();

Expand All @@ -467,6 +475,7 @@ void setup()
ensureNanoleafURL();
setupMQTTClient();
attemptNanoleafConnection();
nanoleaf.setColorCallback(colorCallback);
publishStatus();
publishInitialHeartbeat();
}
Expand All @@ -475,16 +484,23 @@ void loop()
{
mqttClient.loop();
nanoleaf.processEvents();
unsigned long now = millis();

if (now - lastColorTime >= 20000 && currentlyShowingCustomColor)
{
nanoleaf.setPower(false);
currentlyShowingCustomColor = false;
}

if (layoutChanged)
{
publishStatus();
layoutChanged = false;
}

if (millis() - lastPublishTime >= PUBLISH_INTERVAL)
if (now - lastPublishTime >= PUBLISH_INTERVAL)
{
publishHeartbeat();
lastPublishTime = millis();
lastPublishTime = now;
}
}
14 changes: 12 additions & 2 deletions src/NanoleafApiWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ void NanoleafApiWrapper::setLayoutChangeCallback(LayoutChangeCallback callback)
this->layoutChangeCallback = callback;
}

void NanoleafApiWrapper::setColorCallback(ColorCallback callback)
{
this->colorCallback = callback;
}

bool NanoleafApiWrapper::identify()
{
return sendRequest("PUT", "/identify", nullptr, nullptr, true);
Expand Down Expand Up @@ -234,10 +239,14 @@ bool NanoleafApiWrapper::setStaticColors(const JsonObject &doc)

auto rgb = kv.value().as<JsonArray>();

animData += tileId + " 2 " + String(rgb[0].as<int>()) + " " + String(rgb[1].as<int>()) + " " +
String(rgb[2].as<int>()) + " 0 " + String(10 * 360) + " 0 0 0 0 360 ";
// animData += tileId + " 2 " + String(rgb[0].as<int>()) + " " + String(rgb[1].as<int>()) + " " +
// String(rgb[2].as<int>()) + " 0 " + String(10 * 360) + " 0 0 0 0 360 ";
animData += tileId + " 2 " + "0 0 0 0 30 " + String(rgb[0].as<int>()) + " " + String(rgb[1].as<int>()) + " " +
String(rgb[2].as<int>()) + " 0 50 ";
}

Serial.println(animData);

for (const auto &triangleId : triangleIds)
{
animData += triangleId + " 2 " + String(fromFriendColor[0].as<int>()) + " " + String(fromFriendColor[1].as<int>()) + " " +
Expand All @@ -255,5 +264,6 @@ bool NanoleafApiWrapper::setStaticColors(const JsonObject &doc)
jsonPayload["write"]["palette"].add(JsonObject());
jsonPayload["write"]["palette"][0]["hue"] = 0;

this->colorCallback();
return sendRequest("PUT", "/effects", &jsonPayload, nullptr, true);
}

0 comments on commit 85523a2

Please sign in to comment.