diff --git a/src/lgfx/v1/Touch.hpp b/src/lgfx/v1/Touch.hpp index 6725f07c..edcce459 100644 --- a/src/lgfx/v1/Touch.hpp +++ b/src/lgfx/v1/Touch.hpp @@ -19,6 +19,7 @@ Original Source: #include #include +#include "platforms/common.hpp" namespace lgfx { @@ -67,14 +68,27 @@ namespace lgfx }; }; int16_t pin_cs = -1; +#if defined (ARDUINO_DEFAULT) + HardwareSPI *touch_SPI = nullptr; +#endif }; virtual ~ITouch(void) = default; config_t config(void) const { return _cfg; } - void config(const config_t& config) { _cfg = config; } - + void config(const config_t& config) + { + _cfg = config; +#if defined (ARDUINO_DEFAULT) + if (_cfg.touch_SPI != nullptr) + lgfx::v1::spi::set_SPI(_cfg.touch_SPI); +#endif + } +#if defined (ARDUINO_DEFAULT) + inline bool isSPI(void) const { return _cfg.pin_cs >= 0 || _cfg.touch_SPI != nullptr; } +#else inline bool isSPI(void) const { return _cfg.pin_cs >= 0; } +#endif virtual bool init(void) = 0; virtual void wakeup(void) = 0; @@ -85,6 +99,9 @@ namespace lgfx protected: config_t _cfg; bool _inited = false; +#if defined (ARDUINO_DEFAULT) + HardwareSPI *touch_SPI = nullptr; +#endif }; //---------------------------------------------------------------------------- diff --git a/src/lgfx/v1/platforms/arduino_default/common.cpp b/src/lgfx/v1/platforms/arduino_default/common.cpp index 5237a127..f6a2dd07 100644 --- a/src/lgfx/v1/platforms/arduino_default/common.cpp +++ b/src/lgfx/v1/platforms/arduino_default/common.cpp @@ -64,18 +64,21 @@ namespace lgfx //---------------------------------------------------------------------------- - /// unimplemented. namespace spi { + HardwareSPI *SPI_dev = &SPI; cpp::result init(int spi_host, int spi_sclk, int spi_miso, int spi_mosi) { cpp::result res = {}; return res; } + void set_SPI(HardwareSPI * new_SPI) { SPI_dev = new_SPI; } void release(int spi_host) {} void beginTransaction(int spi_host, uint32_t freq, int spi_mode) { SPISettings setting(freq, MSBFIRST, SPI_MODE0); - SPI.beginTransaction(setting); + SPI_dev->beginTransaction(setting); } - void endTransaction(int spi_host) {SPI.endTransaction();} + void endTransaction(int spi_host) { SPI_dev->endTransaction();} void writeBytes(int spi_host, const uint8_t* data, size_t length) {} - void readBytes(int spi_host, uint8_t* data, size_t length) {SPI.transfer(data, length);} } + void readBytes(int spi_host, uint8_t* data, size_t length) { + SPI_dev->transfer(data, length); + } } //---------------------------------------------------------------------------- diff --git a/src/lgfx/v1/platforms/common.hpp b/src/lgfx/v1/platforms/common.hpp index 4789dd69..d152f316 100644 --- a/src/lgfx/v1/platforms/common.hpp +++ b/src/lgfx/v1/platforms/common.hpp @@ -47,6 +47,7 @@ Original Source: #elif defined (ARDUINO) +#define ARDUINO_DEFAULT #include "arduino_default/common.hpp" #elif __has_include() || __has_include() @@ -160,6 +161,9 @@ namespace lgfx namespace spi { cpp::result init(int spi_host, int spi_sclk, int spi_miso, int spi_mosi); +#if defined (ARDUINO_DEFAULT) + void set_SPI(HardwareSPI * new_SPI); +#endif void release(int spi_host); void beginTransaction(int spi_host, uint32_t freq, int spi_mode = 0); void endTransaction(int spi_host); diff --git a/src/lgfx/v1/touch/Touch_XPT2046.cpp b/src/lgfx/v1/touch/Touch_XPT2046.cpp index d9545256..633c4e92 100644 --- a/src/lgfx/v1/touch/Touch_XPT2046.cpp +++ b/src/lgfx/v1/touch/Touch_XPT2046.cpp @@ -31,8 +31,10 @@ namespace lgfx _inited = false; if (!isSPI()) return false; - lgfx::gpio_hi(_cfg.pin_cs); - lgfx::pinMode(_cfg.pin_cs, lgfx::pin_mode_t::output); + if (_cfg.pin_cs > -1) { + lgfx::gpio_hi(_cfg.pin_cs); + lgfx::pinMode(_cfg.pin_cs, lgfx::pin_mode_t::output); + } if (_cfg.spi_host < 0) { pinMode(_cfg.pin_sclk, lgfx::pin_mode_t::output); @@ -110,9 +112,11 @@ namespace lgfx else { spi::beginTransaction(_cfg.spi_host, _cfg.freq, 0); - lgfx::gpio_lo(_cfg.pin_cs); + if (_cfg.pin_cs > -1) + lgfx::gpio_lo(_cfg.pin_cs); spi::readBytes(_cfg.spi_host, data, 57); - lgfx::gpio_hi(_cfg.pin_cs); + if (_cfg.pin_cs > -1) + lgfx::gpio_hi(_cfg.pin_cs); spi::endTransaction(_cfg.spi_host); }