Heltec SSD1306 Display Support (#1283)

My environment

[env:heltec-display]   ; Heltec ESP32 Board with SSD1306 display
platform = ${com.esp32_platform}
board = heltec_wifi_lora_32_V2
build_type = debug
monitor_filters = esp32_exception_decoder
board_build.partitions = min_spiffs.csv
extra_scripts = pre:scripts/omg_firmware_version.py
lib_deps =
  ${com-esp.lib_deps}
  ${libraries.wifimanager32}
  ${libraries.heltec}
;  ${libraries.m5stickcp}

build_flags =
  ${com-esp.build_flags}
  ${test_env.build_flags}
; *** OpenMQTTGateway Config ***
  '-UZmqttDiscovery'          ; disables MQTT Discovery
  '-DvalueAsATopic=true'
  '-DLOG_LEVEL=LOG_LEVEL_TRACE'
; *** OpenMQTTGateway Modules ***
  '-DZboardHELTEC="HELTEC"'
; *** Heltec Board Options ***
  '-DDEBUG_OLEDDISPLAY(...)=Serial.printf( __VA_ARGS__ )'
  '-DLOG_TO_LCD=true'         ; Enable log to LCD
  '-DLED_ERROR=2'
  '-DONBOARD_LED=25'          ; Onboard LED is GPIO 25 on the Heltec Board

monitor_port = /dev/cu.SLAB_USBtoUART
monitor_speed = 921600
upload_port = /dev/cu.SLAB_USBtoUART
upload_speed = 921600

1 - Set default LCD log level to WARNING
2 - Implemented a macro based displayPrint function that will be usable across different displays

- Added logo display when idle
- Fixed log-lcd mqtt function
- Enable OLED Display on Heltec Boards
This commit is contained in:
Northern Man authored and GitHub committed 2022-10-13 21:17:25 -05:00
1 parent b0fc25bfe0
commit c45181ef7e
7 files changed
+10701 -19

No files matched your search

+5
View File
@@ -496,4 +496,9 @@ int lowpowermode = DEFAULT_LOW_POWER_MODE;
# define LOG_LEVEL LOG_LEVEL_NOTICE
#endif
/*-----------PLACEHOLDERS FOR OLED/LCD DISPLAY--------------*/
// The real definitions are in config_M5.h / config_HELTEC.h
#define displayPrint(...) // only print if not in low power mode
#define lpDisplayPrint(...) // print in low power mode
#endif
+258
View File
@@ -0,0 +1,258 @@
/*
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
Send and receiving command by MQTT
HELTEC ESP32 LORA - SSD1306 / Onboard 0.96-inch 128*64 dot matrix OLED display
Copyright: (c)Florian ROBERT
Contributors:
- 1technophile
- NorthernMan54
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "User_config.h"
#if defined(ZboardHELTEC)
# include "ArduinoLog.h"
# include "config_HELTEC.h"
// # include "heltec.h"
void logToLCD(bool display) {
display ? Log.begin(LOG_LEVEL_LCD, &Oled) : Log.begin(LOG_LEVEL, &Serial); // Log on LCD following LOG_LEVEL_LCD
}
void setupHELTEC() {
Log.trace(F("Setup HELTEC Display" CR));
Oled.begin();
Log.notice(F("Setup HELTEC Display end" CR));
# if LOG_TO_LCD
Log.begin(LOG_LEVEL_LCD, &Oled); // Log on LCD following LOG_LEVEL_LCD
# endif
}
void loopHELTEC() {
static int previousLogLevel;
int currentLogLevel = Log.getLastMsgLevel();
if (previousLogLevel != currentLogLevel && lowpowermode != 2) {
switch (currentLogLevel) {
case 1:
case 2:
// wakeScreen(NORMAL_LCD_BRIGHTNESS);
// M5.Lcd.fillScreen(TFT_RED); // FATAL, ERROR
// M5.Lcd.setTextColor(TFT_BLACK, TFT_RED);
break;
case 3:
// wakeScreen(NORMAL_LCD_BRIGHTNESS);
// M5.Lcd.fillScreen(TFT_ORANGE); // WARNING
// M5.Lcd.setTextColor(TFT_BLACK, TFT_ORANGE);
break;
default:
// wakeScreen(SLEEP_LCD_BRIGHTNESS);
// M5.Lcd.fillScreen(TFT_WHITE);
Oled.fillScreen(WHITE);
Oled.drawLogo((int)OLED_WIDTH * 0.24, (int)(OLED_WIDTH / 2) - OLED_WIDTH * 0.2, (int)(OLED_HEIGHT / 2) + OLED_HEIGHT * 0.2, true, true, true, true, true, true); // Name
break;
}
}
previousLogLevel = currentLogLevel;
}
void MQTTtoHELTEC(char* topicOri, JsonObject& HELTECdata) { // json object decoding
if (cmpToMainTopic(topicOri, subjectMQTTtoHELTECset)) {
Log.trace(F("MQTTtoHELTEC json set" CR));
// Log display set between HELTEC lcd (true) and serial monitor (false)
if (HELTECdata.containsKey("log-lcd")) {
bool displayOnLCD = HELTECdata["log-lcd"];
Log.notice(F("Set lcd log: %T" CR), displayOnLCD);
logToLCD(displayOnLCD);
}
}
}
// Simple print methonds
void heltecPrint(char* line1, char* line2, char* line3) {
Oled.println(line1);
Oled.println(line2);
Oled.println(line3);
delay(2000);
}
void heltecPrint(char* line1, char* line2) {
Oled.println(line1);
Oled.println(line2);
delay(2000);
}
void heltecPrint(char* line1) {
Oled.println(line1);
delay(2000);
}
// This pattern was borrowed from HardwareSerial and modified to support the ssd1306 display
OledSerial Oled(0); // Not sure about this, came from Hardwareserial
OledSerial::OledSerial(int x) {
pinMode(RST_OLED, OUTPUT);
digitalWrite(RST_OLED, LOW);
delay(50);
digitalWrite(RST_OLED, HIGH);
# if defined(WIFI_Kit_32) || defined(WIFI_LoRa_32) || defined(WIFI_LoRa_32_V2)
display = new SSD1306Wire(0x3c, SDA_OLED, SCL_OLED, GEOMETRY_128_64);
# elif defined(Wireless_Stick)
display = new SSD1306Wire(0x3c, SDA_OLED, SCL_OLED, GEOMETRY_64_32);
# endif
} // Not sure about this, came from Hardwareserial
void OledSerial::begin() {
// Heltec.begin(); // User OMG serial support
display->init();
display->flipScreenVertically();
display->setFont(ArialMT_Plain_10);
display->setColor(WHITE);
display->fillRect(0, 0, OLED_WIDTH, OLED_HEIGHT);
display->display();
displayIntro(OLED_WIDTH * 0.24, (OLED_WIDTH / 2) - OLED_WIDTH * 0.2, (OLED_HEIGHT / 2) + OLED_HEIGHT * 0.2);
display->setLogBuffer(OLED_TEXT_ROWS, OLED_TEXT_BUFFER);
delay(1000);
}
// Dummy virtual's from Serial
int OledSerial::available(void) {
}
int OledSerial::peek(void) {
}
int OledSerial::read(void) {
}
void OledSerial::flush(void) {
}
void OledSerial::fillScreen(OLEDDISPLAY_COLOR color) {
display->clear();
display->setColor(color);
display->fillRect(0, 0, OLED_WIDTH, OLED_HEIGHT);
}
size_t OledSerial::write(uint8_t c) {
display->clear();
display->setColor(WHITE);
display->setFont(ArialMT_Plain_10);
display->write((char)c);
display->drawLogBuffer(0, 0);
display->display();
return 1;
}
size_t OledSerial::write(const uint8_t* buffer, size_t size) {
display->clear();
display->setColor(WHITE);
display->setFont(ArialMT_Plain_10);
while (size) {
display->write((char)*buffer++);
size--;
}
display->drawLogBuffer(0, 0);
display->display();
return size;
}
/*
Display OpenMQTTGateway logo - borrowed from ZboardM5.ino and tweaked for ssd1306 display ( removed color and tweaked size/location )
*/
void OledSerial::displayIntro(int scale, int displayWidth, int displayHeight) {
drawLogo(scale, displayWidth, displayHeight, false, true, false, false, false, false); // Circle 2
drawLogo(scale, displayWidth, displayHeight, false, false, true, false, false, false); // Circle 3
drawLogo(scale, displayWidth, displayHeight, false, true, true, true, false, false); // Line 1
drawLogo(scale, displayWidth, displayHeight, false, true, true, false, true, false); // Line 2
drawLogo(scale, displayWidth, displayHeight, true, true, true, true, true, false); // Circle 1
drawLogo(scale, displayWidth, displayHeight, true, true, true, true, true, true); // Name
}
void OledSerial::drawLogo(int logoSize, int circle1X, int circle1Y, bool circle1, bool circle2, bool circle3, bool line1, bool line2, bool name) {
int circle1T = logoSize / 15;
int circle2T = logoSize / 25;
int circle3T = logoSize / 30;
int circle3Y = circle1Y - (logoSize * 1.2);
int circle3X = circle1X - (logoSize * 0.13);
int circle2X = circle1X - (logoSize * 1.05);
int circle2Y = circle1Y - (logoSize * 0.8);
if (line1) {
display->setColor(BLACK);
display->drawLine(circle1X - 2, circle1Y, circle2X - 2, circle2Y);
display->drawLine(circle1X - 1, circle1Y, circle2X - 1, circle2Y);
display->drawLine(circle1X, circle1Y, circle2X, circle2Y);
display->drawLine(circle1X + 1, circle1Y, circle2X + 1, circle2Y);
display->drawLine(circle1X + 2, circle1Y, circle2X + 2, circle2Y);
display->setColor(WHITE);
display->fillCircle(circle3X, circle3Y, logoSize / 4 - circle3T * 2); // , WHITE);
}
if (line2) {
display->setColor(BLACK);
display->drawLine(circle1X - 2, circle1Y, circle3X - 2, circle3Y);
display->drawLine(circle1X - 1, circle1Y, circle3X - 1, circle3Y);
display->drawLine(circle1X, circle1Y, circle3X, circle3Y);
display->drawLine(circle1X + 1, circle1Y, circle3X + 1, circle3Y);
display->setColor(WHITE);
display->fillCircle(circle2X, circle2Y, logoSize / 3 - circle2T * 2); // , WHITE);
}
if (circle1) {
display->setColor(WHITE);
display->fillCircle(circle1X, circle1Y, logoSize / 2); // , WHITE);
display->setColor(BLACK);
display->fillCircle(circle1X, circle1Y, logoSize / 2 - circle1T); // , TFT_GREEN);
display->setColor(WHITE);
display->fillCircle(circle1X, circle1Y, logoSize / 2 - circle1T * 2); // , WHITE);
}
if (circle2) {
display->setColor(WHITE);
display->fillCircle(circle2X, circle2Y, logoSize / 3); // , WHITE);
display->setColor(BLACK);
display->fillCircle(circle2X, circle2Y, logoSize / 3 - circle2T); // , TFT_ORANGE);
display->setColor(WHITE);
display->fillCircle(circle2X, circle2Y, logoSize / 3 - circle2T * 2); // , WHITE);
}
if (circle3) {
display->setColor(WHITE);
display->fillCircle(circle3X, circle3Y, logoSize / 4); // , WHITE);
display->setColor(BLACK);
display->fillCircle(circle3X, circle3Y, logoSize / 4 - circle3T); // , TFT_PINK);
display->setColor(WHITE);
display->fillCircle(circle3X, circle3Y, logoSize / 4 - circle3T * 2); // , WHITE);
}
if (name) {
display->setColor(BLACK);
display->drawString(circle1X + (circle1X * 0.27), circle1Y, "penMQTTGateway");
}
display->display();
delay(50);
}
#endif
+116
View File
@@ -0,0 +1,116 @@
/*
OpenMQTTGateway Addon - ESP8266 or Arduino program for home automation
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
Send and receiving command by MQTT
HELTEC ESP32 LORA - SSD1306 / Onboard 0.96-inch 128*64 dot matrix OLED display
Copyright: (c)Florian ROBERT
Contributors:
- 1technophile
- NorthernMan54
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef config_HELTEC_h
#define config_HELTEC_h
#include <Arduino.h>
#include <Wire.h>
#include "SSD1306Wire.h"
#ifdef ZboardHELTEC
# define OLED_WIDTH 128
# define OLED_HEIGHT 64
# define OLED_TEXT_BUFFER 1000
# define OLED_TEXT_ROWS 5
#endif
extern void setupHELTEC();
extern void loopHELTEC();
size_t OledPrint(String msg);
/*-------------------DEFINE LOG LEVEL----------------------*/
#ifndef LOG_LEVEL_LCD
# define LOG_LEVEL_LCD LOG_LEVEL_WARNING // Default to only display Notice level messages
#endif
#ifndef LOG_TO_LCD
# define LOG_TO_LCD false // Default to not display log messages on display
#endif
/*-------------------DEFINE MQTT TOPIC FOR CONFIG----------------------*/
#define subjectMQTTtoHELTECset "/commands/MQTTtoHELTEC/config"
// Simple construct for displaying message in lcd and oled displays
#define displayPrint(...) \
if (lowpowermode < 2) heltecPrint(__VA_ARGS__) // only print if not in low power mode
#define lpDisplayPrint(...) heltecPrint(__VA_ARGS__) // print in low power mode
void heltecPrint(char*, char*, char*);
void heltecPrint(char*, char*);
void heltecPrint(char*);
// This pattern was borrowed from HardwareSerial and modified to support the ssd1306 display
class OledSerial : public Stream {
private:
void displayIntro(int scale, int displayWidth, int displayHeight);
public:
OledSerial(int);
void begin();
void drawLogo(int logoSize, int circle1X, int circle1Y, bool circle1, bool circle2, bool circle3, bool line1, bool line2, bool name);
SSD1306Wire* display;
int available(void); // Dummy functions
int peek(void); // Dummy functions
int read(void); // Dummy functions
void flush(void); // Dummy functions
void fillScreen(OLEDDISPLAY_COLOR); // fillScreen display and set color
size_t write(uint8_t);
size_t write(const uint8_t* buffer, size_t size);
inline size_t write(const char* buffer, size_t size) {
return write((uint8_t*)buffer, size);
}
inline size_t write(const char* s) {
return write((uint8_t*)s, strlen(s));
}
inline size_t write(unsigned long n) {
return write((uint8_t)n);
}
inline size_t write(long n) {
return write((uint8_t)n);
}
inline size_t write(unsigned int n) {
return write((uint8_t)n);
}
inline size_t write(int n) {
return write((uint8_t)n);
}
protected:
};
extern OledSerial Oled;
#endif
+10
View File
@@ -65,4 +65,14 @@ extern void loopM5();
/*-------------------DEFINE MQTT TOPIC FOR CONFIG----------------------*/
#define subjectMQTTtoM5set "/commands/MQTTtoM5/config"
// Simple print macro
// Simple construct for displaying message in lcd and oled displays
#define displayPrint(...) \
if (lowpowermode < 2) M5Print(__VA_ARGS__) // only print if not in low power mode
#define lpDisplayPrint(...) M5Print(__VA_ARGS__) // print in low power mode
void M5Print(char*, char* = "", char* = "");
#endif
+21 -17
View File
@@ -153,6 +153,9 @@ struct GfSun2000Data {};
#if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
# include "config_M5.h"
#endif
#if defined(ZboardHELTEC)
# include "config_HELTEC.h"
#endif
#if defined(ZgatewayRS232)
# include "config_RS232.h"
#endif
@@ -524,10 +527,8 @@ void connectMQTT() {
#else
if (client.connect(gateway_name, mqtt_user, mqtt_pass, topic, will_QoS, will_Retain, will_Message)) {
#endif
#if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
if (lowpowermode < 2)
M5Print("MQTT connected", "", "");
#endif
displayPrint("MQTT connected");
Log.notice(F("Connected to broker" CR));
failure_number_mqtt = 0;
// Once connected, publish an announcement...
@@ -620,6 +621,10 @@ void setup() {
preferences.end();
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
setupM5();
# endif
# if defined(ZboardHELTEC)
setupHELTEC();
modules.add(ZboardHELTEC);
# endif
Log.notice(F("OpenMQTTGateway Version: " OMG_VERSION CR));
# endif
@@ -873,9 +878,7 @@ void setOTA() {
# if defined(ZgatewayBT) && defined(ESP32)
stopProcessing();
# endif
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
M5Print("OTA in progress", "", "");
# endif
lpDisplayPrint("OTA in progress");
});
ArduinoOTA.onEnd([]() {
Log.trace(F("\nOTA done" CR));
@@ -884,9 +887,7 @@ void setOTA() {
# if defined(ZgatewayBT) && defined(ESP32)
startProcessing();
# endif
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
M5Print("OTA done", "", "");
# endif
lpDisplayPrint("OTA done");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Log.trace(F("Progress: %u%%\r" CR), (progress / (total / 100)));
@@ -1200,9 +1201,7 @@ void setup_wifimanager(bool reset_settings) {
{
# ifdef ESP32
if (lowpowermode < 2) {
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
M5Print("Connect your phone to WIFI AP:", WifiManager_ssid, WifiManager_password);
# endif
displayPrint("Connect your phone to WIFI AP:", WifiManager_ssid, WifiManager_password);
} else { // in case of low power mode we put the ESP to sleep again if we didn't get connected (typical in case the wifi is down)
# ifdef ZgatewayBT
lowPowerESP32();
@@ -1227,10 +1226,7 @@ void setup_wifimanager(bool reset_settings) {
digitalWrite(LED_INFO, !LED_INFO_ON);
}
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
if (lowpowermode < 2)
M5Print("Wifi connected", "", "");
# endif
displayPrint("Wifi connected");
if (shouldSaveConfig) {
//read updated parameters
@@ -1525,6 +1521,11 @@ void loop() {
#if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
loopM5();
#endif
// Function that doesn't need an active connection
#if defined(ZboardHELTEC)
loopHELTEC();
#endif
}
/**
@@ -1742,6 +1743,9 @@ void receivingMQTT(char* topicOri, char* datacallback) {
# if defined(ZboardM5STICKC) || defined(ZboardM5STICKCP) || defined(ZboardM5STACK) || defined(ZboardM5TOUGH)
MQTTtoM5(topicOri, jsondata);
# endif
# if defined(ZboardHELTEC)
MQTTtoHELTEC(topicOri, jsondata);
# endif
# ifdef ZactuatorONOFF
MQTTtoONOFF(topicOri, jsondata);
# endif
+10278
View File
File diff suppressed because it is too large. Load diff
+13 -2
View File
@@ -137,6 +137,7 @@ rtl_433_ESP = https://github.com/NorthernMan54/rtl_433_ESP.git#v0.0.4
emodbus = miq19/eModbus@1.0.0
gfSunInverter = https://github.com/BlackSmith/GFSunInverter.git#v1.0.1
decoder = https://github.com/theengs/decoder.git#v0.9.0
ssd1306 = thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays@^4.3.0
[env]
framework = arduino
@@ -859,15 +860,20 @@ build_flags =
# Reading battery level every minutes should be more than enought
'-DTimeBetweenReadingADC=60000'
[env:heltec-wifi-lora-32-868]
[env:heltec-wifi-lora-32-868] ; Heltec ESP32 Board with SSD1306 display
platform = ${com.esp32_platform}
board = heltec_wifi_lora_32
lib_deps =
${com-esp.lib_deps}
${libraries.wifimanager32}
${libraries.lora}
${libraries.ssd1306}
build_flags =
${com-esp.build_flags}
; *** OpenMQTTGateway Modules ***
'-DZboardHELTEC="HELTEC"'
'-DLOG_TO_LCD=true' ; Enable log to LCD
; '-DLOG_LEVEL_LCD=LOG_LEVEL_TRACE' // default is notice log level
'-DZgatewayLORA="LORA"'
'-DLORA_BAND=868E6'
'-DGateway_Name="OpenMQTTGateway_ESP32_LORA"'
@@ -875,15 +881,20 @@ build_flags =
'-DTimeLedON=0.1'
'-DLED_SEND_RECEIVE_ON=1'
[env:heltec-wifi-lora-32-915]
[env:heltec-wifi-lora-32-915] ; Heltec ESP32 Board with SSD1306 display
platform = ${com.esp32_platform}
board = heltec_wifi_lora_32
lib_deps =
${com-esp.lib_deps}
${libraries.wifimanager32}
${libraries.lora}
${libraries.ssd1306}
build_flags =
${com-esp.build_flags}
; *** OpenMQTTGateway Modules ***
'-DZboardHELTEC="HELTEC"'
'-DLOG_TO_LCD=true' ; Enable log to LCD
; '-DLOG_LEVEL_LCD=LOG_LEVEL_TRACE' // default is notice log level
'-DZgatewayLORA="LORA"'
'-DLORA_BAND=915E6'
'-DGateway_Name="OpenMQTTGateway_ESP32_LORA"'