Skip to content

Commit

Permalink
0.67
Browse files Browse the repository at this point in the history
- Adds wakeup command to the notification API. closes #145
- Increase dev json buffer. closes #147
- fixes a bug with mulitple pages command. closes #146
- clear json buffer after modifing the apps vector.
- adds hostname
- adds new HA sensor to get the mqtt prefix
- fixes a bug where alarms doesnt work
- Switch http api returns an error, if the app doesnt exist. closes #140
  • Loading branch information
Blueforcer committed Jun 1, 2023
1 parent 79e917a commit 2af991e
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 33 deletions.
1 change: 1 addition & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ The JSON object has the following properties,
| `draw` | array of objects | Array of drawing instructions. Each object represents a drawing command. | See the drawing instructions below |
| `lifetime` | integer | Removes the custom app when there is no update after the given time in seconds | 0 |
| `stack` | boolean | Defines if the **notification** will be stacked. false will immediately replace the current notification | true |
| `wakeup` | boolean | If the Matrix is off, the notification will wake it up for the time of the notification. | false |


Color values can have a hex string or an array of R,G,B values:
Expand Down
10 changes: 10 additions & 0 deletions src/Apps.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ struct Notification
uint16_t pColor;
uint16_t background = 0;
uint16_t pbColor;
bool wakeup;
};
std::vector<Notification> notifications;
bool notifyFlag = false;
Expand Down Expand Up @@ -675,6 +676,11 @@ void NotifyApp(FastLED_NeoMatrix *matrix, MatrixDisplayUiState *state, GifPlayer
// Set current app name
CURRENT_APP = "Notification";

if (notifications[0].wakeup && MATRIX_OFF)
{
DisplayManager.setBrightness(BRIGHTNESS);
}

// Check if notification duration has expired or if repeat count is 0 and hold is not enabled
if ((((millis() - notifications[0].startime >= notifications[0].duration) && notifications[0].repeat == -1) || notifications[0].repeat == 0) && !notifications[0].hold)
{
Expand All @@ -685,6 +691,10 @@ void NotifyApp(FastLED_NeoMatrix *matrix, MatrixDisplayUiState *state, GifPlayer
notifications[1].startime = millis();
}
notifications.erase(notifications.begin());
if (notifications[0].wakeup && MATRIX_OFF)
{
DisplayManager.setBrightness(0);
}
return;
}

Expand Down
6 changes: 6 additions & 0 deletions src/Dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ const char HAappID[] PROGMEM = {"%s_app"};
const char HAappIcon[] PROGMEM = {"mdi:apps"};
const char HAappName[] PROGMEM = {"Current app"};


const char HAIDID[] PROGMEM = {"%s_id"};
const char HAIDIcon[] PROGMEM = {"mdi:id-card"};
const char HAIDName[] PROGMEM = {"Device topic"};


const char HAtempID[] PROGMEM = {"%s_temp"};
const char HAtempIcon[] PROGMEM = {"mdi:thermometer"};
const char HAtempName[] PROGMEM = {"Temperature"};
Expand Down
4 changes: 4 additions & 0 deletions src/Dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ extern const char HAappID[];
extern const char HAappIcon[];
extern const char HAappName[];

extern const char HAIDID[];
extern const char HAIDIcon[];
extern const char HAIDName[];

extern const char HAtempID[];
extern const char HAtempIcon[];
extern const char HAtempName[];
Expand Down
58 changes: 41 additions & 17 deletions src/DisplayManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,13 @@ DisplayManager_ &DisplayManager = DisplayManager.getInstance();

void DisplayManager_::setBrightness(int bri)
{
if (MATRIX_OFF && !ALARM_ACTIVE)
bool wakeup;
if (!notifications.empty())
{
wakeup = notifications[0].wakeup;
}

if (MATRIX_OFF && !ALARM_ACTIVE && !wakeup)
{
matrix->setBrightness(0);
}
Expand Down Expand Up @@ -298,10 +304,8 @@ bool parseFragmentsText(const String &jsonText, std::vector<uint16_t> &colors, s
{
colors.clear();
fragments.clear();

StaticJsonDocument<2048> doc;
DeserializationError error = deserializeJson(doc, jsonText);

if (error)
{
return false;
Expand All @@ -311,7 +315,7 @@ bool parseFragmentsText(const String &jsonText, std::vector<uint16_t> &colors, s

for (JsonObject fragmentObj : fragmentArray)
{
String textFragment = fragmentObj["t"].as<String>();
String textFragment = utf8ascii(fragmentObj["t"].as<String>());
uint16_t color;
if (fragmentObj.containsKey("c"))
{
Expand All @@ -331,7 +335,6 @@ bool parseFragmentsText(const String &jsonText, std::vector<uint16_t> &colors, s

bool DisplayManager_::parseCustomPage(const String &name, const char *json)
{

if ((strcmp(json, "") == 0) || (strcmp(json, "{}") == 0))
{
removeCustomAppFromApps(name, true);
Expand All @@ -347,6 +350,7 @@ bool DisplayManager_::parseCustomPage(const String &name, const char *json)

if (doc.is<JsonObject>())
{
DEBUG_PRINTLN("Single Page");
return generateCustomPage(name, json);
}
else if (doc.is<JsonArray>())
Expand All @@ -355,25 +359,26 @@ bool DisplayManager_::parseCustomPage(const String &name, const char *json)
int cpIndex = 0;
for (JsonVariant customPage : customPagesArray)
{
Serial.printf("Multiple Page: %i", cpIndex);
JsonObject customPageObject = customPage.as<JsonObject>();
String customPageJson;
serializeJson(customPageObject, customPageJson);
if (generateCustomPage(name + String(cpIndex), customPageJson.c_str()))
return false;
generateCustomPage(name + String(cpIndex), customPageJson.c_str());
++cpIndex;
}
}

doc.clear();
return true;
}

bool DisplayManager_::generateCustomPage(const String &name, const char *json)
{

DynamicJsonDocument doc(4096);
DeserializationError error = deserializeJson(doc, json);
if (error)
{
DEBUG_PRINTLN("PARSING FAILED");
DEBUG_PRINTLN(error.c_str());
doc.clear();
return false;
}
Expand Down Expand Up @@ -587,6 +592,7 @@ bool DisplayManager_::generateCustomPage(const String &name, const char *json)
pushCustomApp(name, pos - 1);
customApps[name] = customApp;
doc.clear();
DEBUG_PRINTLN("PARSING FINISHED");
return true;
}

Expand Down Expand Up @@ -643,6 +649,7 @@ bool DisplayManager_::generateNotification(const char *json)
newNotification.repeat = doc.containsKey("repeat") ? doc["repeat"].as<uint16_t>() : -1;
newNotification.rainbow = doc.containsKey("rainbow") ? doc["rainbow"].as<bool>() : false;
newNotification.hold = doc.containsKey("hold") ? doc["hold"].as<bool>() : false;
newNotification.wakeup = doc.containsKey("wakeup") ? doc["wakeup"].as<bool>() : false;
newNotification.pushIcon = doc.containsKey("pushIcon") ? doc["pushIcon"] : 0;
newNotification.textCase = doc.containsKey("textCase") ? doc["textCase"] : 0;
newNotification.textOffset = doc.containsKey("textOffset") ? doc["textOffset"] : 0;
Expand All @@ -651,10 +658,6 @@ bool DisplayManager_::generateNotification(const char *json)
newNotification.iconWasPushed = false;
newNotification.iconPosition = 0;
newNotification.scrollDelay = 0;
if (doc.containsKey("sound"))
{
PeripheryManager.playFromFile(doc["sound"].as<String>());
}

bool autoscale = true;
if (doc.containsKey("autoscale"))
Expand Down Expand Up @@ -794,6 +797,15 @@ bool DisplayManager_::generateNotification(const char *json)
notifications[0] = newNotification;
}
}

if (doc.containsKey("sound"))
{
if (!MATRIX_OFF || (MATRIX_OFF && newNotification.wakeup))
{
PeripheryManager.playFromFile(doc["sound"].as<String>());
}
}

doc.clear();
return true;
}
Expand Down Expand Up @@ -1134,17 +1146,24 @@ void DisplayManager_::gererateTimer(String Payload)
TimerTicker.once_ms(interval, timerCallback);
}

void DisplayManager_::switchToApp(const char *json)
bool DisplayManager_::switchToApp(const char *json)
{
DynamicJsonDocument doc(512);
DeserializationError error = deserializeJson(doc, json);
if (error)
return;
return false;
String name = doc["name"].as<String>();
bool fast = doc["fast"] | false;
int index = findAppIndexByName(name);
if (index > -1)
{
ui->transitionToApp(index);
return true;
}
else
{
return false;
}
}

void DisplayManager_::drawProgressBar(int16_t x, int16_t y, int progress, uint16_t pColor, uint16_t pbColor)
Expand Down Expand Up @@ -1250,10 +1269,11 @@ void DisplayManager_::updateAppVector(const char *json)
{
DEBUG_PRINTLN(F("New apps vector received"));
DEBUG_PRINTLN(json);
StaticJsonDocument<2048> doc;
DynamicJsonDocument doc(2048);
DeserializationError error = deserializeJson(doc, json);
if (error)
{
doc.clear();
DEBUG_PRINTLN(F("Failed to parse json"));
return;
}
Expand Down Expand Up @@ -1319,11 +1339,12 @@ void DisplayManager_::updateAppVector(const char *json)
saveSettings();
sendAppLoop();
setAutoTransition(AUTO_TRANSITION);
doc.clear();
}

String DisplayManager_::getStats()
{
StaticJsonDocument<256> doc;
StaticJsonDocument<512> doc;
char buffer[5];
#ifdef ULANZI
doc[BatKey] = BATTERY_PERCENT;
Expand All @@ -1346,6 +1367,9 @@ String DisplayManager_::getStats()
doc[UpdateKey] = UPDATE_AVAILABLE;
doc[MessagesKey] = RECEIVED_MESSAGES;
doc[VersionKey] = VERSION;
doc[F("indicator1")] = ui->indicator1State;
doc[F("indicator2")] = ui->indicator2State;
doc[F("indicator3")] = ui->indicator3State;
String jsonString;
return serializeJson(doc, jsonString), jsonString;
}
Expand Down
2 changes: 1 addition & 1 deletion src/DisplayManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class DisplayManager_
bool generateCustomPage(const String &name, const char *json);
void printText(int16_t x, int16_t y, const char *text, bool centered, byte textCase);
bool setAutoTransition(bool active);
void switchToApp(const char *json);
bool switchToApp(const char *json);
void setNewSettings(const char *json);
void drawJPG(uint16_t x, uint16_t y, fs::File jpgFile);
void drawProgressBar(int16_t x, int16_t y, int progress, uint16_t pColor, uint16_t pbColor);
Expand Down
4 changes: 2 additions & 2 deletions src/Globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ void loadDevSettings()
if (LittleFS.exists("/dev.json"))
{
File file = LittleFS.open("/dev.json", "r");
DynamicJsonDocument doc(128);
DynamicJsonDocument doc(256);
DeserializationError error = deserializeJson(doc, file);
if (error)
{
Expand Down Expand Up @@ -216,7 +216,7 @@ IPAddress gateway;
IPAddress subnet;
IPAddress primaryDNS;
IPAddress secondaryDNS;
const char *VERSION = "0.66";
const char *VERSION = "0.67";

String MQTT_HOST = "";
uint16_t MQTT_PORT = 1883;
Expand Down
11 changes: 9 additions & 2 deletions src/MQTTManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ HASwitch *transition = nullptr;
#ifdef ULANZI
HASensor *battery = nullptr;
#endif
HASensor *temperature, *humidity, *illuminance, *uptime, *strength, *version, *ram, *curApp = nullptr;
HASensor *temperature, *humidity, *illuminance, *uptime, *strength, *version, *ram, *curApp, *myOwnID = nullptr;
HABinarySensor *btnleft, *btnmid, *btnright = nullptr;

char matID[40], ind1ID[40], ind2ID[40], ind3ID[40], briID[40], btnAID[40], btnBID[40], btnCID[40], appID[40], tempID[40], humID[40], luxID[40], verID[40], ramID[40], upID[40], sigID[40], btnLID[40], btnMID[40], btnRID[40], transID[40], doUpdateID[40], batID[40];
char matID[40], ind1ID[40], ind2ID[40], ind3ID[40], briID[40], btnAID[40], btnBID[40], btnCID[40], appID[40], tempID[40], humID[40], luxID[40], verID[40], ramID[40], upID[40], sigID[40], btnLID[40], btnMID[40], btnRID[40], transID[40], doUpdateID[40], batID[40], myID[40];

// The getter for the instantiated singleton instance
MQTTManager_ &MQTTManager_::getInstance()
Expand Down Expand Up @@ -328,6 +328,8 @@ void onMqttConnected()
mqtt.subscribe(fullTopic.c_str());
delay(30);
}
if (HA_DISCOVERY)
myOwnID->setValue(MQTT_PREFIX.c_str());
}

void connect()
Expand Down Expand Up @@ -441,6 +443,11 @@ void MQTTManager_::setup()
curApp->setIcon(HAappIcon);
curApp->setName(HAappName);

sprintf(myID, HAIDID, macStr);
myOwnID = new HASensor(myID);
myOwnID->setIcon(HAIDIcon);
myOwnID->setName(HAIDName);

sprintf(btnBID, HAbtnbID, macStr);
nextApp = new HAButton(btnBID);
nextApp->setIcon(HAbtnbIcon);
Expand Down
11 changes: 4 additions & 7 deletions src/MatrixDisplayUi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ void MatrixDisplayUi::previousApp()
}
}

void MatrixDisplayUi::switchToApp(uint8_t app)
bool MatrixDisplayUi::switchToApp(uint8_t app)
{
if (app >= this->AppCount)
return;
return false;
this->state.ticksSinceLastStateSwitch = 0;
if (app == this->state.currentApp)
return;
return false;
this->state.appState = FIXED;
this->state.currentApp = app;
return true;
}

void MatrixDisplayUi::transitionToApp(uint8_t app)
Expand Down Expand Up @@ -381,8 +382,6 @@ void MatrixDisplayUi::setIndicator1Blink(int blink)
this->indicator1Blink = blink;
}



void MatrixDisplayUi::setIndicator2Color(uint16_t color)
{
this->indicator2Color = color;
Expand All @@ -398,8 +397,6 @@ void MatrixDisplayUi::setIndicator2Blink(int blink)
this->indicator2Blink = blink;
}



void MatrixDisplayUi::setIndicator3Color(uint16_t color)
{
this->indicator3Color = color;
Expand Down
2 changes: 1 addition & 1 deletion src/MatrixDisplayUi.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class MatrixDisplayUi
/**
* Switch without transition to app `app`.
*/
void switchToApp(uint8_t app);
bool switchToApp(uint8_t app);

/**
* Transition to app `app`, when the `app` number is bigger than the current
Expand Down
2 changes: 1 addition & 1 deletion src/PeripheryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ void PeripheryManager_::tick()
CURRENT_HUM = bme280.readHumidity();
#endif
}
// checkAlarms();
checkAlarms();
MQTTManager.sendStats();
}

Expand Down
Loading

0 comments on commit 2af991e

Please sign in to comment.