Skip to content

Commit

Permalink
Switch to U8g2 display library
Browse files Browse the repository at this point in the history
OpenSans fonts were reconverted for U8g2.
  • Loading branch information
j000bs committed Feb 4, 2024
1 parent a74041e commit 3c1875f
Show file tree
Hide file tree
Showing 15 changed files with 953 additions and 2,007 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ lib_deps =
; https://arduinojson.org/v6/api/
bblanchon/ArduinoJson @ ^6.21.2
rlogiacco/CircularBuffer @ ^1.3.3
thingpulse/ESP8266 and ESP32 OLED driver for SSD1306 displays @ ^4.4.0
olikraus/U8g2 @ ^2.35.9
adafruit/Adafruit BMP280 Library@^2.6.8
pololu/VL53L0X@^1.3.1
; https://github.com/fhessel/esp32_https_server
Expand Down
37 changes: 14 additions & 23 deletions src/displays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#include "displays.h"

#include "fonts/fonts.h"
#include "fonts/logos.h"

void DisplayDevice::showNumConfirmed() {
String val = String(confirmedMeasurements);
Expand Down Expand Up @@ -137,7 +137,7 @@ void DisplayDevice::showValues(
showTemperatureValue(TemperaturValue);
}

m_display->display();
m_display->updateDisplay();

}

Expand All @@ -161,45 +161,44 @@ void DisplayDevice::showBatterieValue(int16_t input_val){
xlocation += 1;
}

if(input_val >= 0){
String val = String(input_val);
if(input_val >= 0){
String val = String(input_val);
//showLogo(true);
this->showTextOnGrid(xlocation, 0, val + "%", TINY_FONT, 6, 0);

if(input_val > 90){
cleanBattery(x_offset_batterie_logo, y_offset_batterie_logo);
m_display->drawXbm(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo1);
m_display->drawXBM(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo1);
}else if (input_val > 70)
{
cleanBattery(x_offset_batterie_logo, y_offset_batterie_logo);
m_display->drawXbm(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo2);
m_display->drawXBM(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo2);
}else if (input_val> 50)
{
cleanBattery(x_offset_batterie_logo, y_offset_batterie_logo);
m_display->drawXbm(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo3);
m_display->drawXBM(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo3);
}else if (input_val > 30)
{
cleanBattery(x_offset_batterie_logo, y_offset_batterie_logo);
m_display->drawXbm(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo4);
m_display->drawXBM(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo4);
}else if (input_val >10)
{
cleanBattery(x_offset_batterie_logo, y_offset_batterie_logo);
m_display->drawXbm(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo5);
m_display->drawXBM(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo5);
}else
{
cleanBattery(x_offset_batterie_logo, y_offset_batterie_logo);
m_display->drawXbm(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo6);
m_display->drawXBM(x_offset_batterie_logo, y_offset_batterie_logo, 8, 9, BatterieLogo6);
}

}
//m_display->display();
}

void DisplayDevice::showTemperatureValue(int16_t input_val){
uint8_t x_offset_temp_logo = 30;
uint8_t y_offset_temp_logo = 2;
cleanTemperatur(x_offset_temp_logo,y_offset_temp_logo);
m_display->drawXbm(x_offset_temp_logo, y_offset_temp_logo, 8, 9, TempLogo);
m_display->drawXBM(x_offset_temp_logo, y_offset_temp_logo, 8, 9, TempLogo);
String val = String(input_val);
this->showTextOnGrid(1, 0, val + "°C", TINY_FONT);
}
Expand Down Expand Up @@ -231,7 +230,7 @@ uint8_t DisplayDevice::scrollUp() {
for (uint8_t i = 0; i < 5; i++) {
prepareTextOnGrid(2, i, obsDisplay->get_gridTextofCell(2, i + 1));
}
m_display->display();
m_display->updateDisplay();
return mCurrentLine--;
}

Expand All @@ -242,22 +241,14 @@ uint8_t DisplayDevice::startLine() {
void DisplayDevice::highlight(uint32_t highlightTimeMillis) {
mHighlightTill = millis() + highlightTimeMillis;
if (!mHighlighted) {
if (mInverted) {
m_display->normalDisplay();
} else {
m_display->invertDisplay();
}
setInversion(!mInverted);
mHighlighted = true;
}
}

void DisplayDevice::handleHighlight() {
if (mHighlighted && mHighlightTill < millis()) {
if (mInverted) {
m_display->invertDisplay();
} else {
m_display->normalDisplay();
}
setInversion(mInverted);
mHighlighted = false;
}
}
130 changes: 69 additions & 61 deletions src/displays.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,27 @@
#define OBS_DISPLAYS_H

#include <Arduino.h>
#include <SSD1306.h>
#include <U8g2lib.h>

#include "config.h"
#include "globals.h"
#include "gps.h"
#include "logo.h"
#include "fonts/fonts.h"
#include "sensor.h"


extern const uint8_t Open_Sans_Regular_Plain_8[];
//// this font is part of OLEDDisplay::OLEDDisplay :/
//extern const uint8_t ArialMT_Plain_10[]; // :(
extern const uint8_t Open_Sans_Regular_Plain_10[];
extern const uint8_t Open_Sans_Regular_Plain_20[];
extern const uint8_t Open_Sans_Regular_Plain_34[];
extern const uint8_t Open_Sans_Regular_Plain_50[];
#define BLACK 0
#define WHITE 1

extern const uint8_t BatterieLogo1[];
extern const uint8_t TempLogo[];

#define TINY_FONT Open_Sans_Regular_Plain_8
#define SMALL_FONT Open_Sans_Regular_Plain_10
#define MEDIUM_FONT Open_Sans_Regular_Plain_20
#define LARGE_FONT Open_Sans_Regular_Plain_34
#define HUGE_FONT Open_Sans_Regular_Plain_50
#define TINY_FONT OpenSans_Regular_6
#define SMALL_FONT OpenSans_Regular_7
#define MEDIUM_FONT OpenSans_Regular_15
#define LARGE_FONT OpenSans_Regular_26
#define HUGE_FONT OpenSans_Regular_37

// Forward declare classes to build (because there is a cyclic dependency between sensor.h and displays.h)
class HCSR04SensorInfo;
Expand All @@ -64,21 +61,24 @@ class DisplayDevice {
private:
void handleHighlight();
void displaySimple(uint16_t value);
SSD1306* m_display;
U8G2* m_display = new U8G2_SSD1306_128X64_NONAME_F_HW_I2C(U8G2_R0, U8X8_PIN_NONE); // original OBS display
String gridText[ 4 ][ 6 ];
uint8_t mLastProgress = 255;
uint8_t mCurrentLine = 0;
bool mInverted = false;
bool mFlipped = true;
uint32_t mHighlightTill = 0;
bool mHighlighted = false;

public:
DisplayDevice() {
m_display = new SSD1306(0x3c, 21, 22); // ADDRESS, SDA, SCL
m_display->init();
m_display->setBrightness(255);
m_display->setTextAlignment(TEXT_ALIGN_LEFT);
m_display->display();
m_display->begin();
m_display->setFlipMode(mFlipped);
m_display->setContrast(74);
m_display->setFontPosTop();
m_display->setFontMode(1);
m_display->setDrawColor(WHITE);
m_display->updateDisplay();
}

~DisplayDevice() {
Expand All @@ -96,20 +96,19 @@ class DisplayDevice {
//##############################################################

void invert() {
m_display->invertDisplay();
m_display->display();
mInverted = true;
setInversion(mInverted);
}

void normalDisplay() {
m_display->normalDisplay();
m_display->display();
mInverted = false;
setInversion(mInverted);
}

void flipScreen() {
m_display->flipScreenVertically();
m_display->display();
mFlipped = !mFlipped;
m_display->setFlipMode(mFlipped);
m_display->updateDisplay();
}

void clear() {
Expand All @@ -122,8 +121,8 @@ class DisplayDevice {
//##############################################################

void showLogo(bool val) {
m_display->drawXbm(0, 0, OBSLogo_width, OBSLogo_height, OBSLogo);
m_display->display();
m_display->drawXBM(0, 0, OBSLogo_width, OBSLogo_height, OBSLogo);
m_display->updateDisplay();
}

//##############################################################
Expand All @@ -132,18 +131,18 @@ class DisplayDevice {

void showGrid(bool val) {
// Horizontal lines
m_display->drawHorizontalLine(0, 2, 128);
m_display->drawHorizontalLine(0, 12, 128);
m_display->drawHorizontalLine(0, 22, 128);
m_display->drawHorizontalLine(0, 32, 128);
m_display->drawHorizontalLine(0, 42, 128);
m_display->drawHorizontalLine(0, 52, 128);
m_display->drawHorizontalLine(0, 62, 128);
m_display->drawHLine(0, 2, 128);
m_display->drawHLine(0, 12, 128);
m_display->drawHLine(0, 22, 128);
m_display->drawHLine(0, 32, 128);
m_display->drawHLine(0, 42, 128);
m_display->drawHLine(0, 52, 128);
m_display->drawHLine(0, 62, 128);

// Vertical lines
m_display->drawVerticalLine(32, 0, 64);
m_display->drawVerticalLine(64, 0, 64);
m_display->drawVerticalLine(96, 0, 64);
m_display->drawVLine(32, 0, 64);
m_display->drawVLine(64, 0, 64);
m_display->drawVLine(96, 0, 64);
}

//##############################################################
Expand All @@ -166,7 +165,7 @@ class DisplayDevice {

void showTextOnGrid(int16_t x, int16_t y, String text, const uint8_t* font = SMALL_FONT, int8_t offset_x_ = 0, int8_t offset_y_ = 0) {
if (prepareTextOnGrid(x, y, text, font,offset_x_,offset_y_)) {
m_display->display();
m_display->updateDisplay();
}
}

Expand All @@ -186,7 +185,7 @@ class DisplayDevice {
// 2 => 8 - (2*2) = 4
// 3 => 8 - (3*2) = 2
int x_offset = 8 - (x * 2);
m_display->drawString(x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y]);
m_display->drawStr(x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y].c_str());
changed = true;
}
return changed;
Expand All @@ -199,7 +198,7 @@ class DisplayDevice {
gridText[x][y] = "";
}
}
m_display->display();
m_display->updateDisplay();
}

// Override the existing WHITE text with BLACK
Expand All @@ -208,23 +207,23 @@ class DisplayDevice {
}

void cleanGridCell(int16_t x, int16_t y,int8_t offset_x_, int8_t offset_y_) {
m_display->setColor(BLACK);
m_display->setDrawColor(BLACK);
int x_offset = 8 - (x * 2);
m_display->drawString(x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y]);
m_display->drawStr(x * 32 + x_offset + offset_x_, y * 10 + 1 + offset_y_, gridText[x][y].c_str());
gridText[x][y] = "";
m_display->setColor(WHITE);
m_display->setDrawColor(WHITE);
}

void cleanBattery(int16_t x, int16_t y){
m_display->setColor(BLACK);
m_display->drawXbm(x, y, 8, 9, BatterieLogo1);
m_display->setColor(WHITE);
m_display->setDrawColor(BLACK);
m_display->drawXBM(x, y, 8, 9, BatterieLogo1);
m_display->setDrawColor(WHITE);
}

void cleanTemperatur(int16_t x, int16_t y){
m_display->setColor(BLACK);
m_display->drawXbm(x, y, 8, 9, TempLogo);
m_display->setColor(WHITE);
m_display->setDrawColor(BLACK);
m_display->drawXBM(x, y, 8, 9, TempLogo);
m_display->setDrawColor(WHITE);
}

void drawProgressBar(uint8_t y, uint32_t current, uint32_t target) {
Expand All @@ -242,34 +241,34 @@ class DisplayDevice {
uint16_t rowOffset = y * 10 + 3;

if (mLastProgress != progress) {
m_display->drawRect(12, rowOffset, 104, 8);
m_display->drawFrame(12, rowOffset, 104, 8);
if (progress != 0) {
m_display->fillRect(14, rowOffset + 2, progress > 100 ? 100 : progress, 4);
m_display->drawBox(14, rowOffset + 2, progress > 100 ? 100 : progress, 4);
}
m_display->display();
m_display->updateDisplay();
mLastProgress = progress;
}
}

void drawWaitBar(uint8_t y, uint16_t tick) {
uint16_t rowOffset = y * 10 + 3;
m_display->drawRect(12, rowOffset, 104, 8);
m_display->setColor(BLACK);
m_display->fillRect(14, rowOffset + 2, 100, 4);
m_display->setColor(WHITE);
m_display->drawFrame(12, rowOffset, 104, 8);
m_display->setDrawColor(BLACK);
m_display->drawBox(14, rowOffset + 2, 100, 4);
m_display->setDrawColor(WHITE);

int16_t pos = 45 + (45.0 * sin(tick / 10.0));
m_display->fillRect(14 + pos, rowOffset + 2, 10, 4);
m_display->display();
m_display->drawBox(14 + pos, rowOffset + 2, 10, 4);
m_display->updateDisplay();
}

void clearProgressBar(uint8_t y) {
if (UINT8_MAX != mLastProgress) {
clearTextLine(y);
uint16_t rowOffset = y * 10 + 3;
m_display->setColor(BLACK);
m_display->fillRect(12, rowOffset, 104, 8);
m_display->setColor(WHITE);
m_display->setDrawColor(BLACK);
m_display->drawBox(12, rowOffset, 104, 8);
m_display->setDrawColor(WHITE);
m_display->display();
mLastProgress = UINT8_MAX;
}
Expand Down Expand Up @@ -310,6 +309,15 @@ class DisplayDevice {
uint16_t minDistanceToConfirm, int16_t batteryPercentage, int16_t TemperaturValue,
int lastMeasurements, boolean insidePrivacyArea, double speed, uint8_t satellites);


//##############################################################
// Internal methods
//##############################################################

private:
void setInversion(bool enabled) {
m_display->sendF("c", enabled ? 0xa7 : 0xa6); // set inversion of the SSD1306 OLED
}
};

#endif
Loading

0 comments on commit 3c1875f

Please sign in to comment.