From c999052eaad8b52dd50cf40bfad0b6ba093a3bd7 Mon Sep 17 00:00:00 2001 From: Maciej Witkowiak Date: Thu, 24 Oct 2024 23:59:29 +0200 Subject: [PATCH] don't use u8g2 if the display is not connected (i2c address 0x3c) --- SEEED/C64_keyboard/C64_keyboard.ino | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/SEEED/C64_keyboard/C64_keyboard.ino b/SEEED/C64_keyboard/C64_keyboard.ino index 493241f..3942431 100644 --- a/SEEED/C64_keyboard/C64_keyboard.ino +++ b/SEEED/C64_keyboard/C64_keyboard.ino @@ -58,6 +58,8 @@ constexpr const unsigned long inactivePeriodMillis = 5000; // keep the message o // 128x32 // https://github.com/olikraus/u8g2 +bool displayPresent = false; + #include U8G2_SSD1306_128X32_UNIVISION_1_HW_I2C u8g2(U8G2_R0); @@ -135,6 +137,7 @@ void keyReleased() { ////////////////////////////////////////////////////////////// void displayKeyMap() { + if (!displayPresent) { return; } u8g2.firstPage(); do { u8g2.setFlipMode(true); @@ -151,6 +154,7 @@ void displayKeyMap() { void displayKey() { + if (!displayPresent) { return; } u8g2.firstPage(); do { u8g2.setFlipMode(true); @@ -167,6 +171,7 @@ void displayKey() { void displayState(const char* s) { + if (!displayPresent) { return; } u8g2.firstPage(); do { u8g2.setFlipMode(true); @@ -181,8 +186,16 @@ void displayState(const char* s) { void setup() { ckey = new C128keyboard(); - u8g2.begin(); Wire.begin(); + + // detect if display is connected + Wire.beginTransmission(0x3C); + displayPresent = (0 == Wire.endTransmission()); + + if (displayPresent) { + u8g2.begin(); + } + usb.Init(); displayState("Hello world!"); @@ -194,7 +207,7 @@ uint32_t lastUSBstate = 0; void loop() { currentMillis = millis(); - if (currentMillis - lastPressedMillis > inactivePeriodMillis) { + if (displayPresent && (currentMillis - lastPressedMillis > inactivePeriodMillis)) { // clear screen if there was no activity for inactivePeriodMillis u8g2.firstPage(); do { } while ( u8g2.nextPage() );