Library to control a Graphics TFT connected to 4-wire SPI - revised for the Raio RA8875 Display Controller.

Dependents:   FRDM_RA8875_mPaint RA8875_Demo RA8875_KeyPadDemo SignalGenerator ... more

Fork of SPI_TFT by Peter Drescher

See Components - RA8875 Based Display

Enhanced touch-screen support - where it previous supported both the Resistive Touch and Capacitive Touch based on the FT5206 Touch Controller, now it also has support for the GSL1680 Touch Controller.

Offline Help Manual (Windows chm)

/media/uploads/WiredHome/ra8875.zip.bin (download, rename to .zip and unzip)

Revision:
165:695c24cc5197
Parent:
164:76edd7d9cb68
Child:
166:53fd4a876dac
--- a/RA8875.h	Thu Feb 14 12:34:52 2019 +0000
+++ b/RA8875.h	Sun Feb 24 00:40:00 2019 +0000
@@ -71,6 +71,8 @@
 #include <mbed.h>
 
 #include "RA8875_Regs.h"
+#include "RA8875_Touch_FT5206.h"
+#include "RA8875_Touch_GSL1680.h"
 #include "GraphicsDisplay.h"
 
 #define RA8875_DEFAULT_SPI_FREQ 5000000
@@ -118,98 +120,6 @@
 #define max(a,b) ((a>b)?a:b)
 
 
-/// FT5206 definitions follow
-#define FT5206_I2C_FREQUENCY                400000
-
-#define FT5206_I2C_ADDRESS                  0x38
-#define FT5206_NUMBER_OF_REGISTERS          31   // there are more registers, but this
-                                                 // is enough to get all 5 touch coordinates.
-
-#define FT5206_NUMBER_OF_TOTAL_REGISTERS    0xFE
-
-#define FT5206_DEVICE_MODE                  0x00 // Normal, test, etc.
-#define FT5206_GEST_ID                      0x01 // Gesture detected
-#define FT5206_TD_STATUS                    0x02 // How many points detected (3:0). 1-5 is valid.
-
-#define FT5206_TOUCH1_XH                    0x03 // Event Flag, Touch X Position
-#define FT5206_TOUCH1_XL                    0x04
-#define FT5206_TOUCH1_YH                    0x05 // Touch ID, Touch Y Position
-#define FT5206_TOUCH1_YL                    0x06
-
-#define FT5206_TOUCH2_XH                    0x09 // Event Flag, Touch X Position
-#define FT5206_TOUCH2_XL                    0x0a
-#define FT5206_TOUCH2_YH                    0x0b // Touch ID, Touch Y Position
-#define FT5206_TOUCH2_YL                    0x0c
-
-#define FT5206_TOUCH3_XH                    0x0f // Event Flag, Touch X Position
-#define FT5206_TOUCH3_XL                    0x10
-#define FT5206_TOUCH3_YH                    0x11 // Touch ID, Touch Y Position
-#define FT5206_TOUCH3_YL                    0x12
-
-#define FT5206_TOUCH4_XH                    0x15 // Event Flag, Touch X Position
-#define FT5206_TOUCH4_XL                    0x16
-#define FT5206_TOUCH4_YH                    0x17 // Touch ID, Touch Y Position
-#define FT5206_TOUCH4_YL                    0x18
-
-#define FT5206_TOUCH5_XH                    0x1b // Event Flag, Touch X Position
-#define FT5206_TOUCH5_XL                    0x1c
-#define FT5206_TOUCH5_YH                    0x1d // Touch ID, Touch Y Position
-#define FT5206_TOUCH5_YL                    0x1e
-
-// For typical usage, the registers listed below are not used.
-#define FT5206_ID_G_THGROUP                 0x80 // Valid touching detect threshold
-#define FT5206_ID_G_THPEAK                  0x81 // Valid touching peak detect threshold
-#define FT5206_ID_G_THCAL                   0x82 // The threshold when calculating the focus of touching
-#define FT5206_ID_G_THWATER                 0x83 // The threshold when there is surface water
-#define FT5206_ID_G_THTEMP                  0x84 // The threshold of temperature compensation
-#define FT5206_ID_G_CTRL                    0x86 // Power control mode
-#define FT5206_ID_G_TIME_ENTER_MONITOR      0x87 // The timer of entering monitor status
-#define FT5206_ID_G_PERIODACTIVE            0x88 // Period Active
-#define FT5206_ID_G_PERIODMONITOR           0x89 // The timer of entering idle while in monitor status
-#define FT5206_ID_G_AUTO_CLB_MODE           0xA0 // Auto calibration mode
-
-#define FT5206_TOUCH_LIB_VERSION_H          0xA1 // Firmware Library Version H byte
-#define FT5206_TOUCH_LIB_VERSION_L          0xA2 // Firmware Library Version L byte
-#define FT5206_ID_G_CIPHER                  0xA3 // Chip vendor ID
-#define FT5206_G_MODE                       0xA4 // The interrupt status to host
-#define FT5206_ID_G_PMODE                   0xA5 // Power Consume Mode
-#define FT5206_FIRMID                       0xA6 // Firmware ID
-#define FT5206_ID_G_STATE                   0xA7 // Running State
-#define FT5206_ID_G_FT5201ID                0xA8 // CTPM Vendor ID
-#define FT5206_ID_G_ERR                     0xA9 // Error Code
-#define FT5206_ID_G_CLB                     0xAA // Configure TP module during calibration in Test Mode
-#define FT5206_ID_G_B_AREA_TH               0xAE // The threshold of big area
-#define FT5206_LOG_MSG_CNT                  0xFE // The log MSG count
-#define FT5206_LOG_CUR_CHA                  0xFF // Current character of log message, will point to the next
-                                                 // character when one character is read.
-#define FT5206_GEST_ID_MOVE_UP              0x10
-#define FT5206_GEST_ID_MOVE_LEFT            0x14
-#define FT5206_GEST_ID_MOVE_DOWN            0x18
-#define FT5206_GEST_ID_MOVE_RIGHT           0x1c
-#define FT5206_GEST_ID_ZOOM_IN              0x48
-#define FT5206_GEST_ID_ZOOM_OUT             0x49
-#define FT5206_GEST_ID_NO_GESTURE           0x00
-
-#define FT5206_EVENT_FLAG_PUT_DOWN          0x00
-#define FT5206_EVENT_FLAG_PUT_UP            0x01
-#define FT5206_EVENT_FLAG_CONTACT           0x02
-#define FT5206_EVENT_FLAG_RESERVED          0x03
-
-#define FT5206_ID_G_POLLING_MODE            0x00
-#define FT5206_ID_G_TRIGGER_MODE            0x01
-
-#define FT5206_ID_G_PMODE_ACTIVE            0x00
-#define FT5206_ID_G_PMODE_MONITOR           0x01
-#define FT5206_ID_G_PMODE_HIBERNATE         0x03
-
-#define FT5206_ID_G_STATE_CONFIGURE         0x00
-#define FT5206_ID_G_STATE_WORK              0x01
-#define FT5206_ID_G_STATE_CALIBRATION       0x02
-#define FT5206_ID_G_STATE_FACTORY           0x03
-#define FT5206_ID_G_STATE_AUTO_CALIBRATION  0x04
-/// end of FT5206 definitions
-
-
 //namespace SW_graphics
 //{
 
@@ -460,11 +370,8 @@
         const char * name = "lcd");
     
     
-    /// Constructor for a display based on the RAiO RA8875 
-    /// display controller (use for TouchScreen: Capacitive only)
-    ///
-    /// This constructor differs from the alternate by including support
-    /// for the Capactive Touch screen.
+    /// Constructor for a display based on the RAiO RA8875 display controller 
+    /// (using the FT5206 Capacitive TouchScreen Controller)
     ///
     /// @code
     /// #include "RA8875.h"
@@ -497,7 +404,44 @@
     ///
     RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset, 
         PinName sda, PinName scl, PinName irq, const char * name = "lcd");
+ 
     
+    /// Constructor for a display based on the RAiO RA8875 display controller 
+    /// (using the GSL1680 Capacitive TouchScreen Controller)
+    ///
+    /// @code
+    /// #include "RA8875.h"
+    /// RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13,p14, "tft");
+    /// 
+    /// int main()
+    /// {
+    ///     lcd.init();
+    ///     lcd.printf("printing 3 x 2 = %d", 3*2);
+    ///     lcd.circle(400,25, 25, BrightRed);
+    ///     TouchCode_t tp = lcd.TouchPanelReadable();
+    ///     if (tp == touch)
+    ///         ...
+    /// }
+    /// @endcode
+    ///
+    /// @param[in] mosi is the SPI master out slave in pin on the mbed.
+    /// @param[in] miso is the SPI master in slave out pin on the mbed.
+    /// @param[in] sclk is the SPI shift clock pin on the mbed.
+    /// @param[in] csel is the DigitalOut pin on the mbed to use as the
+    ///         active low chip select for the display controller.
+    /// @param[in] reset is the DigitalOut pin on the mbed to use as the 
+    ///         active low reset input on the display controller - 
+    ///         but this is not currently used.
+    /// @param[in] sda is the I2C Serial Data pin you are wiring to the GSL1680.
+    /// @param[in] scl is the I2C Serial Clock pin you are wiring to the GSL1680.
+    /// @param[in] wake is the wake control pin you are wiring to the GSL1680.
+    /// @param[in] irq is the Interrupt Request pin you are wiring to the GSL1680.
+    /// @param[in] name is a text name for this object, which will permit
+    ///         capturing stdout to puts() and printf() directly to it.
+    ///
+    RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset, 
+        PinName sda, PinName scl, PinName wake, PinName irq, const char * name = "lcd");
+ 
     
     // Destructor doesn't have much to do as this would typically be created
     // at startup, and not at runtime.
@@ -762,7 +706,8 @@
     ///    } while (t.read_ms() < 30000);
     /// @endcode
     ///
-    /// @param[out] TouchPoint is a pointer to a point_t, which is set as the touch point, if a touch is registered.
+    /// @param[out] TouchPoint is a pointer to a point_t, which is set as the touch point, 
+    ///             if a touch is registered.
     /// @returns a value indicating the state of the touch,
     ///         - no_cal:   no calibration matrix is available, touch coordinates are not returned.
     ///         - no_touch: no touch is detected, touch coordinates are not returned.
@@ -2822,7 +2767,9 @@
     typedef enum {
         TP_NONE,            ///< no touch panel in use
         TP_RES,             ///< resistive touch panel using RA8875
-        TP_CAP,             ///< capacitive touch panel using FT5206
+        TP_FT5206,          ///< Capacitive touch panel using FT5206
+        TP_CAP=TP_FT5206,   ///< DEPRECATED: used TP_CAP5206 for that chip-set
+        TP_GSL1680,         ///< Capacitive touch panel using GSL1680 chip
     } WhichTP_T;
     
     /// boolean flag set true when using Capacitive touch panel, and false
@@ -2835,19 +2782,26 @@
     ////////////////// Start of Capacitive Touch Panel parameters
     
     int RoundUp(int value, int roundTo);
-    uint8_t getTouchPositions(void);
+    uint8_t FT5206_TouchPositions(void);
+    uint8_t FT5206_ReadRegU8(uint8_t reg);
+    uint8_t GSL1680_TouchPositions(void);
+    uint8_t GSL1680_ReadRegU8(uint8_t reg, uint8_t * buf, int count);
+    RetCode_t FT5206_Init();
+    RetCode_t GSL1680_Init();
+
     void TouchPanelISR(void);
     uint16_t numberOfTouchPoints;
-    uint8_t gesture;            ///< Holds the reported gesture information.
+    uint8_t gesture;            ///< Holds the reported gesture information (which doesn't work well for the FT5206)
     
     /// Touch Information data structure
     typedef struct {
         uint8_t touchID;        ///< Contains the touch ID, which is the "order" of touch, from 0 to n-1
         TouchCode_t touchCode;  ///< Contains the touch code; no_touch, touch, held, release
         point_t coordinates;    ///< Contains the X,Y coordinate of the touch
+        
     } touchInfo_T;
 
-    touchInfo_T touchInfo[5];   /// Contains the actual touch information in an array from 0 to n-1
+    touchInfo_T touchInfo[10];   /// Contains the actual touch information in an array from 0 to n-1
 
     #if MBED_VERSION >= MBED_ENCODE_VERSION(5,8,0)
     Thread eventThread;
@@ -2855,13 +2809,10 @@
     #endif
     InterruptIn * m_irq;
     I2C * m_i2c;
-    int m_addr;
-    uint8_t data[2];
+    int m_addr;                 /// I2C Address of the Cap Touch Controller
+    //uint8_t data[2];            /// Transfer data for the I2C interface
 
     bool panelTouched;
-    void writeRegister8(uint8_t reg, uint8_t val);
-    uint8_t readRegister8(uint8_t reg);
-
     
     ////////////////// Start of Resistive Touch Panel parameters
     
@@ -2891,10 +2842,14 @@
     // and partial new.
     
     /// Touch Panel ticker
+    /// This it bound to a timer to call the _TouchTicker() function periodically.
+    ///
     Ticker touchTicker;
     
     /// Touch Panel timer
-    Timer touchTimer;
+    /// Reset on a touch, to expire on a non-touch
+    ///
+    Timer timeSinceTouch;
     
     /// keeps track of which sample we're collecting to filter out the noise.
     int touchSample;
@@ -3039,8 +2994,9 @@
     bool spiWriteSpeed;             ///< indicates if the current mode is write or read
     unsigned long spiwritefreq;     ///< saved write freq
     unsigned long spireadfreq;      ///< saved read freq
-    DigitalOut cs;                  ///< chip select pin, assumed active low
-    DigitalOut res;                 ///< reset pin, assumed active low
+    DigitalOut cs;                  ///< RA8875 chip select pin, assumed active low
+    DigitalOut res;                 ///< RA8875 reset pin, assumed active low
+    DigitalOut * m_wake;            ///< GSL1680 wake pin
     
     // display metrics to avoid lengthy spi read queries
     uint8_t screenbpp;              ///< configured bits per pixel