Skip to content

Commit

Permalink
Add configurable SPI device for Touchscreens
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-bennett committed Apr 25, 2024
1 parent d35e60f commit 7afd000
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
21 changes: 19 additions & 2 deletions src/lgfx/v1/Touch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Original Source:

#include <stdint.h>
#include <stddef.h>
#include "platforms/common.hpp"

namespace lgfx
{
Expand Down Expand Up @@ -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;
Expand All @@ -85,6 +99,9 @@ namespace lgfx
protected:
config_t _cfg;
bool _inited = false;
#if defined (ARDUINO_DEFAULT)
HardwareSPI *touch_SPI = nullptr;
#endif
};

//----------------------------------------------------------------------------
Expand Down
11 changes: 7 additions & 4 deletions src/lgfx/v1/platforms/arduino_default/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,21 @@ namespace lgfx

//----------------------------------------------------------------------------

/// unimplemented.
namespace spi
{
HardwareSPI *SPI_dev = &SPI;
cpp::result<void, error_t> init(int spi_host, int spi_sclk, int spi_miso, int spi_mosi) { cpp::result<void, error_t> 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);
} }

//----------------------------------------------------------------------------

Expand Down
4 changes: 4 additions & 0 deletions src/lgfx/v1/platforms/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Original Source:

#elif defined (ARDUINO)

#define ARDUINO_DEFAULT
#include "arduino_default/common.hpp"

#elif __has_include(<SDL2/SDL.h>) || __has_include(<SDL.h>)
Expand Down Expand Up @@ -160,6 +161,9 @@ namespace lgfx
namespace spi
{
cpp::result<void, error_t> 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);
Expand Down
12 changes: 8 additions & 4 deletions src/lgfx/v1/touch/Touch_XPT2046.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit 7afd000

Please sign in to comment.