Skip to content

Commit

Permalink
don't use u8g2 if the display is not connected (i2c address 0x3c)
Browse files Browse the repository at this point in the history
  • Loading branch information
ytmytm committed Oct 24, 2024
1 parent 78693d7 commit c999052
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions SEEED/C64_keyboard/C64_keyboard.ino
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ constexpr const unsigned long inactivePeriodMillis = 5000; // keep the message o
// 128x32
// https://github.com/olikraus/u8g2

bool displayPresent = false;

#include <U8g2lib.h>

U8G2_SSD1306_128X32_UNIVISION_1_HW_I2C u8g2(U8G2_R0);
Expand Down Expand Up @@ -135,6 +137,7 @@ void keyReleased() {
//////////////////////////////////////////////////////////////

void displayKeyMap() {
if (!displayPresent) { return; }
u8g2.firstPage();
do {
u8g2.setFlipMode(true);
Expand All @@ -151,6 +154,7 @@ void displayKeyMap() {


void displayKey() {
if (!displayPresent) { return; }
u8g2.firstPage();
do {
u8g2.setFlipMode(true);
Expand All @@ -167,6 +171,7 @@ void displayKey() {


void displayState(const char* s) {
if (!displayPresent) { return; }
u8g2.firstPage();
do {
u8g2.setFlipMode(true);
Expand All @@ -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!");
Expand All @@ -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() );
Expand Down

0 comments on commit c999052

Please sign in to comment.