Skip to content

Commit

Permalink
Multi TFT_eSPI now working
Browse files Browse the repository at this point in the history
  • Loading branch information
peteGSX committed Jan 2, 2025
1 parent 6279cf8 commit dd47f94
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 28 deletions.
4 changes: 1 addition & 3 deletions Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,7 @@ void Controller::update() {
LOG(LogLevel::DEBUG, "_pauseDisplayUpdatesUntil time exceeded, resume display updates");
_pauseDisplayUpdatesUntil = 0;
_pauseDisplayUpdates = false;
for (auto *display = _displayManager->getFirstDisplay(); display; display = display->getNext()) {
display->clearScreen();
}
_displayManager->clearAllDisplays();
}

// Process user input provided there is a valid InputManager and InputInterface
Expand Down
33 changes: 24 additions & 9 deletions DisplayManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,24 @@ void DisplayManager::startDisplays() {
if (_firstDisplay == nullptr) {
return;
}
_setupAllSPICSPins();
for (auto *display = _firstDisplay; display; display = display->getNext()) {
// If display's CS pin is defined, set the pin mode and select the display
_setSPIDisplayCSPin(display);
_selectSPIDisplay(display);
display->begin();
}
}

void DisplayManager::clearAllDisplays() {
LOG(LogLevel::DEBUG, "DisplayManager::clearAllDisplays()");
if (_firstDisplay == nullptr) {
return;
}
for (auto *display = _firstDisplay; display; display = display->getNext()) {
// Select this SPI display if necessary
_selectSPIDisplay(display);
display->clearScreen();
}
}

void DisplayManager::displayStartupInfo(const char *version) {
LOG(LogLevel::DEBUG, "DisplayManager::displayStartupInfo(%s)", version);
// Do nothing if we don't have a DisplayManager or any displays
Expand Down Expand Up @@ -131,13 +141,18 @@ DisplayManager::~DisplayManager() {
_logger = nullptr;
}

void DisplayManager::_setSPIDisplayCSPin(DisplayInterface *display) {
// If not set, don't do anything
if (display->getCSPin() == -1) {
return;
void DisplayManager::_setupAllSPICSPins() {
for (DisplayInterface *display = _firstDisplay; display; display = display->getNext()) {
int csPin = display->getCSPin();
// If not set, don't do anything and move to the next display
if (csPin == -1) {
continue;
}
// Set the pin to output mode
pinMode(csPin, OUTPUT);
// Then enable the display ready for _tft->init()
digitalWrite(csPin, LOW);
}
// Set the pin to output mode
pinMode(display->getCSPin(), OUTPUT);
}

void DisplayManager::_selectSPIDisplay(DisplayInterface *display) {
Expand Down
8 changes: 5 additions & 3 deletions DisplayManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class DisplayManager {
/// @brief Call the begin() method for all associated Display instances
void startDisplays();

/// @brief Clear all displays
void clearAllDisplays();

/// @brief Calls the displayStartupInfo of all Display instances to display the version
/// @param version EX-Display version
void displayStartupInfo(const char *version);
Expand Down Expand Up @@ -67,9 +70,8 @@ class DisplayManager {
Logger *_logger;
uint8_t _nextDisplayId;

/// @brief Set the chip select pin state for any display that has it set
/// @param display Pointer to the DisplayInterface instance
void _setSPIDisplayCSPin(DisplayInterface *display);
/// @brief Set the chip select pin state for all displays that have it set and select them
void _setupAllSPICSPins();

/// @brief Manually selects SPI displays that require manual selecting via the CS pin - sets pin low
/// @param display Pointer to the DisplayInterface instance
Expand Down
19 changes: 13 additions & 6 deletions TFT_eSPIDisplay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@
// Do not load when testing, TFT_eSPI library is incompatible and will cause failures.
#ifndef PIO_UNIT_TESTING

TFT_eSPI *TFT_eSPIDisplay::_tft = nullptr;
bool TFT_eSPIDisplay::_tftInitialised = false;

TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour) {
_rotation = rotation;
_textSize = textSize;
_textColour = textColour;
_backgroundColour = backgroundColour;
_tft = new TFT_eSPI();
if (_tft == nullptr) {
_tft = new TFT_eSPI();
}
_gfxFont = TEXT_FONT;
_tftInitialised = false;
}

TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t textColour, uint16_t backgroundColour,
Expand All @@ -37,15 +41,18 @@ TFT_eSPIDisplay::TFT_eSPIDisplay(uint8_t rotation, uint8_t textSize, uint16_t te
_textColour = textColour;
_backgroundColour = backgroundColour;
_csPin = csPin;
_tft = new TFT_eSPI();
if (_tft == nullptr) {
_tft = new TFT_eSPI();
}
_gfxFont = TEXT_FONT;
_tftInitialised = false;
}

void TFT_eSPIDisplay::begin() {
LOG(LogLevel::DEBUG, "TFT_eSPIDisplay::begin[%d]()", _displayId);
_tft->init();
_tftInitialised = true;
if (!_tftInitialised) {
_tft->init();
_tftInitialised = true;
}
_tft->setTextSize(_textSize);
_tft->setRotation(_rotation);
_tft->setTextColor(_textColour);
Expand Down
4 changes: 2 additions & 2 deletions TFT_eSPIDisplay.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ class TFT_eSPIDisplay : public DisplayInterface {
~TFT_eSPIDisplay() override;

private:
TFT_eSPI *_tft;
static TFT_eSPI *_tft;
const GFXfont *_gfxFont;
bool _tftInitialised;
static bool _tftInitialised;

/// @brief Get the X/Y coordinates to draw the specified row, starting at the specified column
/// @param row Row number
Expand Down
4 changes: 3 additions & 1 deletion Version.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#define VERSION_H

// Numeric version here: major.minor.patch
#define VERSION "0.0.18"
#define VERSION "0.0.19"

// 0.0.19 includes:
// - Resolved multiple TFT_eSPI screens not operating independently by using static TFT_eSPI instance
// 0.0.18 includes:
// - Added ability to force touchscreen calibration if required
// 0.0.17 includes:
Expand Down
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ build_flags =
-DTFT_MISO=23
-DTFT_MOSI=19
-DTFT_SCLK=18
; -DTFT_CS=-1
-DTFT_CS=-1
-DTFT_DC=2
-DTFT_RST=4
-DTOUCH_CS=32
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ TEST_F(CompilerCreationTests, CreateDevices) {
// display1
EXPECT_CALL(MockArduino::getInstance(), mockPinMode(22, OUTPUT)).Times(1);
EXPECT_CALL(MockArduino::getInstance(), mockDigitalWrite(22, LOW)).Times(1);
EXPECT_CALL(MockArduino::getInstance(), mockDigitalWrite(22, HIGH)).Times(1);
// display2
EXPECT_CALL(MockArduino::getInstance(), mockPinMode(23, OUTPUT)).Times(1);
EXPECT_CALL(MockArduino::getInstance(), mockDigitalWrite(23, LOW)).Times(1);
EXPECT_CALL(MockArduino::getInstance(), mockDigitalWrite(23, HIGH)).Times(1);

displayManager->startDisplays();

// Now create the input
inputManager->createInput();

Expand Down Expand Up @@ -110,6 +108,7 @@ TEST_F(CompilerCreationTests, CreateDevices) {
testing::Mock::VerifyAndClearExpectations(display2);
testing::Mock::VerifyAndClearExpectations(callback);
testing::Mock::VerifyAndClearExpectations(input);
testing::Mock::VerifyAndClearExpectations(&MockArduino::getInstance());

// Clean up
delete displayManager;
Expand Down

0 comments on commit dd47f94

Please sign in to comment.