Skip to content

Commit

Permalink
Allow to couple/uncouple CE pin (#23)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jjtmp committed Nov 22, 2023
1 parent c012d0f commit 3e9241f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/Nokia_LCD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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});
Expand All @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/Nokia_LCD.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 3e9241f

Please sign in to comment.