From 3e9241f53a383677f917039fa97397ce9b14b6a3 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 22 Nov 2023 10:24:17 +0100 Subject: [PATCH] Allow to couple/uncouple CE pin (#23) Allow the specific display instance to be controlled by another instance, which has been initialized with the same pins except the CE one. Enables having multiple displays on the same data, clock and reset pins. --- src/Nokia_LCD.cpp | 17 +++++++++++++++-- src/Nokia_LCD.h | 11 +++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/Nokia_LCD.cpp b/src/Nokia_LCD.cpp index a07db85..29404bb 100644 --- a/src/Nokia_LCD.cpp +++ b/src/Nokia_LCD.cpp @@ -112,6 +112,15 @@ void Nokia_LCD::begin() { sendCommand(0x0C); // Set display control, normal mode. } +void Nokia_LCD::couple() { + digitalWrite(kCe_pin, LOW); + mCoupled = true; +} +void Nokia_LCD::uncouple() { + digitalWrite(kCe_pin, HIGH); + mCoupled = false; +} + void Nokia_LCD::setContrast(uint8_t contrast) { sendCommand(0x21); // Tell LCD that extended commands follow sendCommand(0x80 | contrast); // Set LCD Vop (Contrast) @@ -291,7 +300,9 @@ bool Nokia_LCD::send(const unsigned char lcd_byte, const bool is_data, digitalWrite(kDc_pin, is_data); // Send the byte - digitalWrite(kCe_pin, LOW); + if (!mCoupled) { + digitalWrite(kCe_pin, LOW); + } if (kUsingHardwareSPI) { constexpr uint32_t kSPiClockSpeed{F_CPU / 4U}; SPI.beginTransaction(SPISettings{kSPiClockSpeed, MSBFIRST, SPI_MODE0}); @@ -301,7 +312,9 @@ bool Nokia_LCD::send(const unsigned char lcd_byte, const bool is_data, shiftOut(kDin_pin, kClk_pin, MSBFIRST, lcd_byte); } - digitalWrite(kCe_pin, HIGH); + if (!mCoupled) { + digitalWrite(kCe_pin, HIGH); + } // If we just sent the command, there was no out-of-bounds error // and we don't have to calculate the new cursor position diff --git a/src/Nokia_LCD.h b/src/Nokia_LCD.h index ac42cde..dc2dab2 100644 --- a/src/Nokia_LCD.h +++ b/src/Nokia_LCD.h @@ -231,6 +231,16 @@ class Nokia_LCD { */ void setDefaultFont(); + /** + * @brief Allow the specific display instance to be controlled by another + * instance, which has been initialized with the same pins except the CE one. + * Enables having multiple displays on the same data, clock and reset pins. + * @example Allow `lcd1` and `lcd2 to both display the same as `lcd3` + * @example `lcd1.couple(); lcd2.couple(); lcd3.print("Hi there");` + */ + void couple(); + void uncouple(); + private: /** * Sends the specified byte to the LCD via software SPI as data or a @@ -275,6 +285,7 @@ class Nokia_LCD { */ bool printCharacter(char character); + bool mCoupled = false; const uint8_t kClk_pin, kDin_pin, kDc_pin, kCe_pin, kRst_pin, kBl_pin; bool mInverted = false; const bool kUsingBacklight;