Skip to content

Commit

Permalink
[SSD1306] Partial MAC address in BLE title (#1498)
Browse files Browse the repository at this point in the history
* partial MAC in title

• Last three octets of MAC address in title to differentiate devices with the same model name.
• fixed memory leak
• process optimisation

* lcd to oled

lcd to oled renaming
  • Loading branch information
DigiH authored Mar 3, 2023
1 parent 578f522 commit 1b5215d
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 60 deletions.
18 changes: 9 additions & 9 deletions docs/use/displays.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Several options are available for the display of information on the SSD1306 disp

The current SSD1306 display states are being published to the `SSD1306toMQTT` topic, e.g.

`{"onstate":true,"brightness":50,"display-metric":true,"display-flip":true,"idlelogo":true,"log-lcd":false,"json-lcd":true}`
`{"onstate":true,"brightness":50,"display-metric":true,"display-flip":true,"idlelogo":true,"log-oled":false,"json-oled":true}`

### Display ON/OFF
To turn the SSD1306 display on or off.
Expand Down Expand Up @@ -59,27 +59,27 @@ or at runtime with

### Setting the log output

The display of serial log messages to the display can be enabled via compiler directive `-DLOG_TO_LCD=true` or via MQTT commands.
The display of serial log messages to the display can be enabled via compiler directive `-DLOG_TO_OLED=true` or via MQTT commands.

For example if you want to set the serial log to LCD
For example if you want to set the serial log to OLED

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306/config -m '{"log-lcd":true}'`
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306/config -m '{"log-oled":true}'`

you can also revert it back to the serial monitor:
you can also revert it back with

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306/config -m '{"log-lcd":false}'`
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306/config -m '{"log-oled":false}'`

The log level of the messages displayed is Errors and Warnings, and this can only be changed via the compiler directive `-DLOG_LEVEL_LCD=LOG_LEVEL_NOTICE`.
The log level of the messages displayed is Errors and Warnings, and this can only be changed via the compiler directive `-DLOG_LEVEL_OLED=LOG_LEVEL_NOTICE`.

### Displaying Module json messages (default)

The display of messages from various modules is also supported. Currently supported modules include `ZgatewayRTL_433`, `ZgatewayBT` and `ZsensorBME280`.

This can be enabled with the compiler directive `-DJSON_TO_LCD=true`.
This can be enabled with the compiler directive `-DJSON_TO_OLED=true`.

You can also change it by MQTT. For example if you want to display module json messages:

`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306/config -m '{"json-lcd":true}'`
`mosquitto_pub -t home/OpenMQTTGateway/commands/MQTTtoSSD1306/config -m '{"json-oled":true}'`

And to disable the display of module json messages:

Expand Down
24 changes: 12 additions & 12 deletions environments.ini
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,9 @@ build_flags =
'-DZradioSX127x="SX127x"'
; *** ssd1306 Display Options ***
'-DZdisplaySSD1306="HELTEC_SSD1306"'
; '-DLOG_TO_LCD=false' ; Enable log to LCD
; '-DJSON_TO_LCD=true'
; '-DLOG_LEVEL_LCD=LOG_LEVEL_NOTICE'
; '-DLOG_TO_OLED=false' ; Enable log to OLED
; '-DJSON_TO_OLED=true'
; '-DLOG_LEVEL_OLED=LOG_LEVEL_NOTICE'
; '-DDISPLAY_IDLE_LOGO=false'
; '-DDISPLAY_BRIGHTNESS=80'
; '-DDISPLAY_METRIC=false'
Expand Down Expand Up @@ -793,9 +793,9 @@ build_flags =
'-DZradioSX127x="SX127x"'
; *** ssd1306 Display Options ***
'-DZdisplaySSD1306="LilyGo_SSD1306"'
; '-DLOG_TO_LCD=true' ; Enable log to LCD
; '-DJSON_TO_LCD=true'
; '-DLOG_LEVEL_LCD=LOG_LEVEL_NOTICE'
; '-DLOG_TO_OLED=true' ; Enable log to OLED
; '-DJSON_TO_OLED=true'
; '-DLOG_LEVEL_OLED=LOG_LEVEL_NOTICE'
; '-DDISPLAY_IDLE_LOGO=false'
; '-DDISPLAY_BRIGHTNESS=80'
; '-DDISPLAY_METRIC=false'
Expand Down Expand Up @@ -960,9 +960,9 @@ build_flags =
'-DLED_SEND_RECEIVE_ON=1'
; *** ssd1306 Display Options ***
'-DZdisplaySSD1306="HELTEC_SSD1306"'
; '-DLOG_TO_LCD=true' ; Enable log to LCD
; '-DJSON_TO_LCD=true'
; '-DLOG_LEVEL_LCD=LOG_LEVEL_NOTICE'
; '-DLOG_TO_OLED=true' ; Enable log to OLED
; '-DJSON_TO_OLED=true'
; '-DLOG_LEVEL_OLED=LOG_LEVEL_NOTICE'
; '-DDISPLAY_IDLE_LOGO=false'
; '-DDISPLAY_BRIGHTNESS=80'
; '-DDISPLAY_METRIC=false'
Expand All @@ -988,9 +988,9 @@ build_flags =
'-DLED_SEND_RECEIVE_ON=1'
; *** ssd1306 Display Options ***
'-DZdisplaySSD1306="HELTEC_SSD1306"'
; '-DLOG_TO_LCD=true' ; Enable log to LCD
; '-DJSON_TO_LCD=true'
; '-DLOG_LEVEL_LCD=LOG_LEVEL_NOTICE'
; '-DLOG_TO_OLED=true' ; Enable log to OLED
; '-DJSON_TO_OLED=true'
; '-DLOG_LEVEL_OLED=LOG_LEVEL_NOTICE'
; '-DDISPLAY_IDLE_LOGO=false'
; '-DDISPLAY_BRIGHTNESS=80'
; '-DDISPLAY_METRIC=false'
Expand Down
78 changes: 48 additions & 30 deletions main/ZdisplaySSD1306.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@

SemaphoreHandle_t semaphoreOLEDOperation;
QueueHandle_t displayQueue;
boolean logToLCDDisplay = LOG_TO_LCD;
boolean jsonDisplay = JSON_TO_LCD;
boolean logToOLEDDisplay = LOG_TO_OLED;
boolean jsonDisplay = JSON_TO_OLED;
boolean displayMetric = DISPLAY_METRIC;
boolean displayFlip = DISPLAY_FLIP;
boolean displayState = DISPLAY_STATE;
Expand All @@ -52,9 +52,9 @@ uint8_t displayBrightness = round(DISPLAY_BRIGHTNESS * 2.55);
/*
Toogle log display
*/
void logToLCD(bool display) {
logToLCDDisplay = display;
display ? Log.begin(LOG_LEVEL_LCD, &Oled) : Log.begin(LOG_LEVEL, &Serial); // Log on LCD following LOG_LEVEL_LCD
void logToOLED(bool display) {
logToOLEDDisplay = display;
display ? Log.begin(LOG_LEVEL_OLED, &Oled) : Log.begin(LOG_LEVEL, &Serial); // Log on OLED following LOG_LEVEL_OLED
}

/*
Expand All @@ -63,8 +63,8 @@ module setup, for use in Arduino setup
void setupSSD1306() {
Log.trace(F("Setup SSD1306 Display" CR));
Log.trace(F("ZdisplaySSD1306 command topic: %s" CR), subjectMQTTtoSSD1306set);
Log.trace(F("ZdisplaySSD1306 log-lcd: %T" CR), logToLCDDisplay);
Log.trace(F("ZdisplaySSD1306 json-lcd: %T" CR), jsonDisplay);
Log.trace(F("ZdisplaySSD1306 log-oled: %T" CR), logToOLEDDisplay);
Log.trace(F("ZdisplaySSD1306 json-oled: %T" CR), jsonDisplay);
Log.trace(F("ZdisplaySSD1306 DISPLAY_PAGE_INTERVAL: %d" CR), DISPLAY_PAGE_INTERVAL);
Log.trace(F("ZdisplaySSD1306 DISPLAY_IDLE_LOGO: %T" CR), DISPLAY_IDLE_LOGO);
Log.trace(F("ZdisplaySSD1306 DISPLAY_METRIC: %T" CR), displayMetric);
Expand All @@ -73,8 +73,8 @@ void setupSSD1306() {
Oled.begin();
Log.notice(F("Setup SSD1306 Display end" CR));

# if LOG_TO_LCD
Log.begin(LOG_LEVEL_LCD, &Oled); // Log on LCD following LOG_LEVEL_LCD
# if LOG_TO_OLED
Log.begin(LOG_LEVEL_OLED, &Oled); // Log on OLED following LOG_LEVEL_OLED
jsonDisplay = false;
# else
jsonDisplay = true;
Expand All @@ -93,7 +93,7 @@ void loopSSD1306() {
long enough since the last message and display not being used and a queue message waiting
*/
if (jsonDisplay) {
if (jsonDisplay && displayState) {
if (uptime() >= nextDisplayPage && uxSemaphoreGetCount(semaphoreOLEDOperation) && uxQueueMessagesWaiting(displayQueue)) {
displayQueueMessage* message = nullptr;
xQueueReceive(displayQueue, &message, portMAX_DELAY);
Expand All @@ -108,7 +108,8 @@ void loopSSD1306() {
/*
Display logo if it has been more than DISPLAY_PAGE_INTERVAL
*/
if (uptime() > nextDisplayPage + 1 && !logoDisplayed && idlelogo) {
if (uptime() > nextDisplayPage + 1 && !logoDisplayed && idlelogo && displayState) {
Oled.display->normalDisplay();
Oled.fillScreen(BLACK);
Oled.drawLogo(rand() % 13 - 5, rand() % 32 - 13);
logoDisplayed = true;
Expand All @@ -117,43 +118,48 @@ void loopSSD1306() {

/*
Handler for mqtt commands sent to the module
- log-lcd: boolean
- log-oled: boolean
Enable / Disable display of log messages on display
*/
void MQTTtoSSD1306(char* topicOri, JsonObject& SSD1306data) { // json object decoding
bool success = false;
if (cmpToMainTopic(topicOri, subjectMQTTtoSSD1306set)) {
Log.trace(F("MQTTtoSSD1306 json set" CR));
// Log display set between SSD1306 lcd (true) and serial monitor (false)
// Log display set between SSD1306 OLED (true) and serial monitor (false)
if (SSD1306data.containsKey("onstate")) {
if (displayState != SSD1306data["onstate"]) {
displayState = SSD1306data["onstate"];
Oled.begin();
if (!displayState) {
Oled.display->displayOff();
} else {
Oled.display->displayOn();
Oled.begin();
}
}
displayState = SSD1306data["onstate"];
Log.notice(F("Set display state: %T" CR), logToLCDDisplay);
Log.notice(F("Set display state: %T" CR), logToOLEDDisplay);
success = true;
} else if (SSD1306data.containsKey("brightness")) {
displayBrightness = SSD1306data["brightness"];
displayBrightness = round(displayBrightness * 2.55);
Oled.display->setBrightness(displayBrightness);
Log.notice(F("Set brightness: %d" CR), displayBrightness);
success = true;
} else if (SSD1306data.containsKey("log-lcd")) {
logToLCDDisplay = SSD1306data["log-lcd"];
Log.notice(F("Set lcd log: %T" CR), logToLCDDisplay);
logToLCD(logToLCDDisplay);
if (logToLCDDisplay) {
} else if (SSD1306data.containsKey("log-oled")) {
logToOLEDDisplay = SSD1306data["log-oled"];
Log.notice(F("Set OLED log: %T" CR), logToOLEDDisplay);
logToOLED(logToOLEDDisplay);
if (logToOLEDDisplay) {
jsonDisplay = false;
}
success = true;
} else if (SSD1306data.containsKey("json-lcd")) {
jsonDisplay = SSD1306data["json-lcd"];
} else if (SSD1306data.containsKey("json-oled")) {
jsonDisplay = SSD1306data["json-oled"];
if (jsonDisplay) {
logToLCDDisplay = false;
logToLCD(logToLCDDisplay);
logToOLEDDisplay = false;
logToOLED(logToOLEDDisplay);
}
Log.notice(F("Set json-lcd: %T" CR), jsonDisplay);
Log.notice(F("Set json-oled: %T" CR), jsonDisplay);
success = true;
} else if (SSD1306data.containsKey("display-metric")) {
displayMetric = SSD1306data["display-metric"];
Expand Down Expand Up @@ -192,15 +198,14 @@ constexpr unsigned int hash(const char* s, int off = 0) { // workaround for swit
Parse json message from module into a format for displaying on screen, and queue for display
*/
void ssd1306PubPrint(const char* topicori, JsonObject& data) {
if (jsonDisplay) {
if (jsonDisplay && displayState) {
displayQueueMessage* message = (displayQueueMessage*)malloc(sizeof(displayQueueMessage));
if (message != NULL) {
char* topic = strdup(topicori);
strlcpy(message->title, strtok(topic, "/"), OLED_TEXT_WIDTH);
free(topic);

Oled.display->normalDisplay();
// Oled.display->normalDisplay();

switch (hash(message->title)) {
case hash("SYStoMQTT"): {
Expand Down Expand Up @@ -303,6 +308,7 @@ void ssd1306PubPrint(const char* topicori, JsonObject& data) {

if (xQueueSend(displayQueue, (void*)&message, 0) != pdTRUE) {
Log.error(F("[ SSD1306 ] displayQueue full, discarding signal %s" CR), message->title);
free(message);
} else {
// Log.notice(F("Queued %s" CR), message->title);
}
Expand Down Expand Up @@ -667,6 +673,13 @@ void ssd1306PubPrint(const char* topicori, JsonObject& data) {
line4 = properties[4] + properties[5];

if (!(line2 == "" && line3 == "" && line4 == "")) {
// Titel
char* topic = strdup(topicori);
String heading = strtok(topic, "/");
String line0 = heading + " " + data["id"].as<String>().substring(9, 17);
line0.toCharArray(message->title, OLED_TEXT_WIDTH);
free(topic);

// Line 1
strlcpy(message->line1, data["model"], OLED_TEXT_WIDTH);

Expand All @@ -680,9 +693,13 @@ void ssd1306PubPrint(const char* topicori, JsonObject& data) {
} else {
// Log.notice(F("Queued %s" CR), message->title);
}
} else {
free(message);
}

break;
} else {
free(message);
}
}
# endif
Expand Down Expand Up @@ -751,7 +768,7 @@ OledSerial::OledSerial(int x) {
}

/*
Initialize ssd1306 oled display for use, and display animated OMG logo
Initialize ssd1306 oled display for use, and display OMG logo
*/
void OledSerial::begin() {
// SSD1306.begin(); // User OMG serial support
Expand Down Expand Up @@ -845,6 +862,7 @@ Display full page message on the display.
boolean OledSerial::displayPage(displayQueueMessage* message) {
if (xPortGetCoreID() == CONFIG_ARDUINO_RUNNING_CORE) {
if (xSemaphoreTake(semaphoreOLEDOperation, pdMS_TO_TICKS(30000)) == pdTRUE) {
display->normalDisplay();
display->clear();
display->setColor(WHITE);
display->setFont(ArialMT_Plain_10);
Expand Down Expand Up @@ -909,8 +927,8 @@ void stateSSD1306Display() {
DISPLAYdata["display-metric"] = (bool)displayMetric;
DISPLAYdata["display-flip"] = (bool)displayFlip;
DISPLAYdata["idlelogo"] = (bool)idlelogo;
DISPLAYdata["log-lcd"] = (bool)logToLCDDisplay;
DISPLAYdata["json-lcd"] = (bool)jsonDisplay;
DISPLAYdata["log-oled"] = (bool)logToOLEDDisplay;
DISPLAYdata["json-oled"] = (bool)jsonDisplay;
pub(subjectSSD1306toMQTT, DISPLAYdata);
}

Expand Down
5 changes: 2 additions & 3 deletions main/ZgatewayBT.ino
Original file line number Diff line number Diff line change
Expand Up @@ -991,9 +991,8 @@ void launchBTDiscovery(bool overrideDiscovery) {
void PublishDeviceData(JsonObject& BLEdata, bool processBLEData) {
if (abs((int)BLEdata["rssi"] | 0) < abs(BTConfig.minRssi)) { // process only the devices close enough
if (processBLEData) process_bledata(BLEdata);
String topic = subjectBTtoMQTT;
if (BLEdata.containsKey("model") || BLEdata.containsKey("distance")) { // Only display sensor data
pubOled((char*)topic.c_str(), BLEdata);
if (BLEdata.containsKey("type") && (BLEdata.containsKey("model") || BLEdata.containsKey("distance"))) { // Only display sensor data with type
pubOled(subjectBTtoMQTT, BLEdata);
}
if (!BTConfig.pubAdvData) {
BLEdata.remove("servicedatauuid");
Expand Down
12 changes: 6 additions & 6 deletions main/config_SSD1306.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,16 @@

/*-------------------DEFINE LOG LEVEL----------------------*/

#ifndef LOG_LEVEL_LCD
# define LOG_LEVEL_LCD LOG_LEVEL_WARNING // Default to only display Warning level messages
#ifndef LOG_LEVEL_OLED
# define LOG_LEVEL_OLED LOG_LEVEL_WARNING // Default to only display Warning level messages
#endif

#ifndef LOG_TO_LCD
# define LOG_TO_LCD false // Default to not display log messages on display
#ifndef LOG_TO_OLED
# define LOG_TO_OLED false // Default to not display log messages on display
#endif

#ifndef JSON_TO_LCD
# define JSON_TO_LCD true // Default to displaying JSON messages on the display
#ifndef JSON_TO_OLED
# define JSON_TO_OLED true // Default to displaying JSON messages on the display
#endif

#ifndef DISPLAY_PAGE_INTERVAL
Expand Down

0 comments on commit 1b5215d

Please sign in to comment.