/* * Display.cpp * * Wrapper class for Adafruit OLED display. * * Created on: 28.01.2022 * Author: FSmilari */ #include "Display.h" /***************** ** Constructors. ****************/ Display::Display() { ssd1306 = Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT); u8g2_gfx.begin(ssd1306); } /****************** ** Public methods *****************/ void Display::init() { if (!ssd1306.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS, true, true)) { Serial.println(F("SSD1306 allocation failed")); for (;;) ; // Don't proceed, loop forever } } void Display::display() { ssd1306.display(); } void Display::clearDisplay() { ssd1306.clearDisplay(); } void Display::showBrand() { ssd1306.clearDisplay(); ssd1306.drawBitmap(0, 0, epd_bitmap_SFTools_Logo, 128, 34, SSD1306_WHITE); char *s = &String("Frästisch N172")[0]; int16_t w = 0, h = 0; u8g2_gfx.setFont(u8g2_font_helvB12_tf); w = u8g2_gfx.getUTF8Width(s); h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent(); u8g2_gfx.setForegroundColor(SSD1306_WHITE); u8g2_gfx.setCursor((SCREEN_WIDTH - w) / 2, 63 - (30 - h) / 2); u8g2_gfx.println(F(s)); display(); } void Display::showInitialization() { ssd1306.clearDisplay(); char *s = &String("Initialisieren")[0]; int16_t w = 0; u8g2_gfx.setFont(u8g2_font_helvB12_tf); w = u8g2_gfx.getUTF8Width(s); Serial.println("w: " + String(w)); u8g2_gfx.setForegroundColor(SSD1306_WHITE); u8g2_gfx.setCursor((SCREEN_WIDTH - w) / 2, 30); u8g2_gfx.println(F(s)); char *g; w = u8g2_gfx.getUTF8Width(". . . . ."); Serial.println("w: " + String(w)); u8g2_gfx.setCursor((SCREEN_WIDTH - w) / 2, 50); String gauge = ""; for (int i = 0; i < 6; i++) { g = &gauge[0]; u8g2_gfx.print(g); display(); delay(500); if (i < 4) { gauge = ". "; g = &gauge[0]; } else { gauge = "."; g = &gauge[0]; } } } void Display::showFrame(Status status) { ssd1306.clearDisplay(); ssd1306.drawRoundRect(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 3, SSD1306_WHITE); ssd1306.drawLine(37, 0, 37, SCREEN_HEIGHT - 1, SSD1306_WHITE); ssd1306.drawLine(0, 15, 37, 15, SSD1306_WHITE); switch (status) { case TOOL_CHANGE: drawStatusText(STATUS_TXT_TOOLCHG); break; case CONFIGURATION: ssd1306.drawLine(37, SCREEN_HEIGHT / 2 - 1, SCREEN_WIDTH - 1, SCREEN_HEIGHT / 2 - 1, SSD1306_WHITE); drawStatusText (STATUS_TXT_CFG); drawConfigText("Höhe WLS"); drawConfigOption("03.00 mm"); break; case IDLE: ssd1306.drawLine(37, 43, SCREEN_WIDTH - 1, 43, SSD1306_WHITE); drawStatusText(STATUS_TXT_IDLE); break; default: break; } display(); } /******************************** ** Private methods *******************************/ void Display::calculateWH(String units, uint16_t &w, uint16_t &h) { int x = 0; int y = 0; int16_t x1, y1; uint16_t w1, h1; ssd1306.getTextBounds(units, x, y, &x1, &y1, &w1, &h1); w = w1; h = h1; } void Display::drawStatusText(String txt) { char *s = &txt[0]; int16_t w = 0; u8g2_gfx.setFont(u8g2_font_helvR08_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall w = u8g2_gfx.getUTF8Width(s); u8g2_gfx.setCursor((37 - w) / 2, 12); u8g2_gfx.println(F(s)); } void Display::drawConfigText(String txt) { char *s = &txt[0]; int16_t w = 0, h = 0; u8g2_gfx.setFont(u8g2_font_helvB12_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall w = u8g2_gfx.getUTF8Width(s); h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent(); u8g2_gfx.setCursor((SCREEN_WIDTH + 37 - w) / 2, SCREEN_HEIGHT / 4 + h / 2 - 1); // start writing at this position u8g2_gfx.println(F(s)); } void Display::drawConfigOption(String txt) { char *s = &txt[0]; int16_t w = 0, h = 0; u8g2_gfx.setFont(u8g2_font_helvB12_tf); w = u8g2_gfx.getUTF8Width(s); h = u8g2_gfx.getFontAscent() - u8g2_gfx.getFontDescent(); u8g2_gfx.setCursor((SCREEN_WIDTH + 37 - w) / 2, SCREEN_HEIGHT / 4 * 3 + h / 2 - 1); u8g2_gfx.println(F(s)); }