Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for capacitive display calibration #339

Merged
merged 2 commits into from
Mar 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 28 additions & 31 deletions examples/arduino/diag_ard_touch_calib/diag_ard_touch_calib.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,9 @@
// ------------------------------------------------------------
// CALIB: Check configuration settings
// ------------------------------------------------------------
#if defined(DRV_TOUCH_ADA_STMPE610)
#elif defined(DRV_TOUCH_ADA_SIMPLE)
#elif defined(DRV_TOUCH_ADA_RA8875)
#elif defined(DRV_TOUCH_ADA_RA8875_SUMO)
#elif defined(DRV_TOUCH_XPT2046_STM)
#elif defined(DRV_TOUCH_XPT2046_PS)
#elif defined(DRV_TOUCH_TFT_ESPI)
#else
#error "Calibration only supported for resistive touch displays"
#endif // DRV_TOUCH_*
#if !defined(DRV_TOUCH_CALIB)
#warning "Calibration not expected for this display"
#endif // DRV_TOUCH_CALIB
#define DO_CALIB
// ------------------------------------------------------------

Expand Down Expand Up @@ -196,23 +189,27 @@ uint16_t m_nPointBufCnt = 0;
// displays, a typical range might be 200...3700, implying
// a full-scale range of 3500. One would expect to obtain
// calibration ranges in excess of 10% of the full-scale range.
#define DET_RAW_RANGE_MIN 350
// For resistive displays, a default of 350 is reasonable
// For capacitive displays, a default of 50 is reasonable
#define DET_RAW_RANGE_MIN 50

// Define the maximum variance between readings that are
// expected to be on the same side of the display (eg. X:TL/BL)
// Exceeding this value will cause the sketch to report
// that the calibration has failed, likely due to a pin
// wiring issue.
// For resistive displays, a default of 350 is reasonable
// For capacitive displays, a default of 50 is reasonable
#define DET_RAW_VAR_MAX 350

int16_t m_nPointFinalX = -1;
int16_t m_nPointFinalY = -1;

// Corner readings before adjusting for offsets
uint16_t m_nTouchCalXMinRaw;
uint16_t m_nTouchCalXMaxRaw;
uint16_t m_nTouchCalYMinRaw;
uint16_t m_nTouchCalYMaxRaw;
int16_t m_nTouchCalXMinRaw;
int16_t m_nTouchCalXMaxRaw;
int16_t m_nTouchCalYMinRaw;
int16_t m_nTouchCalYMaxRaw;
// Preserve reading ranges
int16_t m_nTouchCalXRngRaw;
int16_t m_nTouchCalYRngRaw;
Expand All @@ -236,10 +233,10 @@ int16_t m_nTouchYMax;
int16_t m_nTouchZMin;
int16_t m_nTouchZMax;

uint16_t m_nTouchCalXMin;
uint16_t m_nTouchCalXMax;
uint16_t m_nTouchCalYMin;
uint16_t m_nTouchCalYMax;
int16_t m_nTouchCalXMin;
int16_t m_nTouchCalXMax;
int16_t m_nTouchCalYMin;
int16_t m_nTouchCalYMax;

bool m_bRemapYX = false;

Expand Down Expand Up @@ -766,10 +763,10 @@ void ReportCalibResult()
return;
}

GSLC_DEBUG_PRINT(" #define ADATOUCH_X_MIN %u\n", m_nTouchCalXMin);
GSLC_DEBUG_PRINT(" #define ADATOUCH_X_MAX %u\n", m_nTouchCalXMax);
GSLC_DEBUG_PRINT(" #define ADATOUCH_Y_MIN %u\n", m_nTouchCalYMin);
GSLC_DEBUG_PRINT(" #define ADATOUCH_Y_MAX %u\n", m_nTouchCalYMax);
GSLC_DEBUG_PRINT(" #define ADATOUCH_X_MIN %d\n", m_nTouchCalXMin);
GSLC_DEBUG_PRINT(" #define ADATOUCH_X_MAX %d\n", m_nTouchCalXMax);
GSLC_DEBUG_PRINT(" #define ADATOUCH_Y_MIN %d\n", m_nTouchCalYMin);
GSLC_DEBUG_PRINT(" #define ADATOUCH_Y_MAX %d\n", m_nTouchCalYMax);

GSLC_DEBUG_PRINT(" #define ADATOUCH_REMAP_YX %u\n", m_bRemapYX);

Expand Down Expand Up @@ -805,13 +802,13 @@ void ReportCalibResult()

void DrawCalibResult()
{
snprintf(m_acTxt, MAX_STR, "ADATOUCH_X_MIN: %u", m_nTouchCalXMin);
snprintf(m_acTxt, MAX_STR, "ADATOUCH_X_MIN: %d", m_nTouchCalXMin);
gslc_DrvDrawTxt(&m_gui, m_rReport.x, m_rReport.y + 00, m_pFont, m_acTxt, GSLC_TXT_DEFAULT, GSLC_COL_YELLOW, GSLC_COL_BLACK);
snprintf(m_acTxt, MAX_STR, "ADATOUCH_Y_MIN: %u", m_nTouchCalYMin);
snprintf(m_acTxt, MAX_STR, "ADATOUCH_Y_MIN: %d", m_nTouchCalYMin);
gslc_DrvDrawTxt(&m_gui, m_rReport.x, m_rReport.y + 20, m_pFont, m_acTxt, GSLC_TXT_DEFAULT, GSLC_COL_YELLOW, GSLC_COL_BLACK);
snprintf(m_acTxt, MAX_STR, "ADATOUCH_X_MAX: %u", m_nTouchCalXMax);
snprintf(m_acTxt, MAX_STR, "ADATOUCH_X_MAX: %d", m_nTouchCalXMax);
gslc_DrvDrawTxt(&m_gui, m_rReport.x, m_rReport.y + 40, m_pFont, m_acTxt, GSLC_TXT_DEFAULT, GSLC_COL_YELLOW, GSLC_COL_BLACK);
snprintf(m_acTxt, MAX_STR, "ADATOUCH_Y_MAX: %u", m_nTouchCalYMax);
snprintf(m_acTxt, MAX_STR, "ADATOUCH_Y_MAX: %d", m_nTouchCalYMax);
gslc_DrvDrawTxt(&m_gui, m_rReport.x, m_rReport.y + 60, m_pFont, m_acTxt, GSLC_TXT_DEFAULT, GSLC_COL_YELLOW, GSLC_COL_BLACK);

if (m_bTouchCalFail) {
Expand Down Expand Up @@ -1252,7 +1249,7 @@ void setup()
GSLC_DEBUG_PRINT("\n=== Touch Testing ===\n\n", "");
#endif

#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
// Reset to calibration defaults from configuration
m_nTouchCalXMin = ADATOUCH_X_MIN;
m_nTouchCalXMax = ADATOUCH_X_MAX;
Expand All @@ -1263,7 +1260,7 @@ void setup()
#else
m_bRemapYX = false;
#endif
GSLC_DEBUG_PRINT("CALIB: Config defaults: XMin=%u XMax=%u YMin=%u YMax=%u RemapYX=%u\n",
GSLC_DEBUG_PRINT("CALIB: Config defaults: XMin=%d XMax=%d YMin=%d YMax=%d RemapYX=%u\n",
ADATOUCH_X_MIN, ADATOUCH_X_MAX, ADATOUCH_Y_MIN, ADATOUCH_Y_MAX,m_bRemapYX);

#if defined(DRV_DISP_ADAGFX_MCUFRIEND)
Expand All @@ -1272,7 +1269,7 @@ void setup()
GSLC_DEBUG_PRINT("%s ", m_acTxt);
#endif

#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB
GSLC_DEBUG_PRINT("\n", "");
}

Expand Down Expand Up @@ -1318,4 +1315,4 @@ void loop()
gslc_Quit(&m_gui);
while (1) {}
}
}
}
8 changes: 4 additions & 4 deletions examples/arduino/diag_ard_touch_test/diag_ard_touch_test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void setup()
GSLC_DEBUG_PRINT("\n=== Touch Testing ===\n\n", "");
#endif

#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
// Reset to calibration defaults from configuration
m_nTouchCalXMin = ADATOUCH_X_MIN;
m_nTouchCalXMax = ADATOUCH_X_MAX;
Expand All @@ -407,7 +407,7 @@ void setup()
#else
m_bRemapYX = false;
#endif
GSLC_DEBUG_PRINT("CALIB: Config defaults: XMin=%u XMax=%u YMin=%u YMax=%u RemapYX=%u\n",
GSLC_DEBUG_PRINT("CALIB: Config defaults: XMin=%d XMax=%d YMin=%d YMax=%d RemapYX=%u\n",
ADATOUCH_X_MIN, ADATOUCH_X_MAX, ADATOUCH_Y_MIN, ADATOUCH_Y_MAX,m_bRemapYX);

#if defined(DRV_DISP_ADAGFX_MCUFRIEND)
Expand All @@ -416,7 +416,7 @@ void setup()
GSLC_DEBUG_PRINT("%s ", m_acTxt);
#endif

#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB
GSLC_DEBUG_PRINT("\n", "");
}

Expand Down Expand Up @@ -462,4 +462,4 @@ void loop()
gslc_Quit(&m_gui);
while (1) {}
}
}
}
8 changes: 4 additions & 4 deletions src/GUIslice.h
Original file line number Diff line number Diff line change
Expand Up @@ -729,10 +729,10 @@ typedef struct {
uint8_t nFlipX; ///< Adafruit GFX Touch Flip x axis
uint8_t nFlipY; ///< Adafruit GFX Touch Flip x axis
// Calibration for resistive touch displays
uint16_t nTouchCalXMin; ///< Calibration X minimum reading
uint16_t nTouchCalXMax; ///< Calibration X maximum reading
uint16_t nTouchCalYMin; ///< Calibration Y minimum reading
uint16_t nTouchCalYMax; ///< Calibration Y maximum reading
int16_t nTouchCalXMin; ///< Calibration X minimum reading
int16_t nTouchCalXMax; ///< Calibration X maximum reading
int16_t nTouchCalYMin; ///< Calibration Y minimum reading
int16_t nTouchCalYMax; ///< Calibration Y maximum reading
#endif

gslc_tsFont* asFont; ///< Collection of loaded fonts
Expand Down
12 changes: 6 additions & 6 deletions src/GUIslice_drv_adagfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2277,12 +2277,12 @@ bool gslc_TDrvInitTouch(gslc_tsGui* pGui,const char* acDev) {
(void)acDev; // Unused

// Capture default calibration settings for resistive displays
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
pGui->nTouchCalXMin = ADATOUCH_X_MIN;
pGui->nTouchCalXMax = ADATOUCH_X_MAX;
pGui->nTouchCalYMin = ADATOUCH_Y_MIN;
pGui->nTouchCalYMax = ADATOUCH_Y_MAX;
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB

// Support touch controllers with swapped X & Y
#if defined(ADATOUCH_REMAP_YX)
Expand Down Expand Up @@ -2925,7 +2925,7 @@ bool gslc_TDrvGetTouch(gslc_tsGui* pGui,int16_t* pnX,int16_t* pnY,uint16_t* pnPr
nInputY = nRawY;

// For resistive displays, perform constraint and scaling
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
if (pGui->bTouchRemapEn) {
// Perform scaling from input to output
// - Calibration done in native orientation (GSLC_ROTATE=0)
Expand Down Expand Up @@ -2953,14 +2953,14 @@ bool gslc_TDrvGetTouch(gslc_tsGui* pGui,int16_t* pnX,int16_t* pnY,uint16_t* pnPr
// No scaling from input to output
nOutputX = nInputX;
nOutputY = nInputY;
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB

#ifdef DBG_TOUCH
GSLC_DEBUG_PRINT("DBG: PreRotate: x=%u y=%u\n", nOutputX, nOutputY);
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
GSLC_DEBUG_PRINT("DBG: RotateCfg: remap=%u nSwapXY=%u nFlipX=%u nFlipY=%u\n",
pGui->bTouchRemapEn,pGui->nSwapXY,pGui->nFlipX,pGui->nFlipY);
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB
#endif // DBG_TOUCH

// Perform remapping due to current orientation
Expand Down
24 changes: 24 additions & 0 deletions src/GUIslice_drv_adagfx.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,30 @@ extern "C" {
#elif defined(DRV_TOUCH_NONE)
#endif // DRV_TOUCH_*

// Determine if calibration required
// - Enable for resistive displays
// - User config can also enable for capacitive displays by adding:
// #define DRV_TOUCH_CALIB
#if defined(DRV_TOUCH_TYPE_RES)
#define DRV_TOUCH_CALIB
#endif

#if defined(DRV_TOUCH_CALIB)
// Ensure calibration settings are present in the config file
#if !defined(ADATOUCH_X_MIN)
// We didn't locate the calibration settings, so provide some
// temporary defaults. This typically only occurs if user has
// decided to force calibration on a capacitive display by
// adding DRV_TOUCH_CALIB, but hasn't yet added the calibration
// settings (ADATOUCH_X/Y_MIN/MAX) from the calibration sketch yet.
#warning Calibration settings (ADATOUCH_X/Y_MIN/MAX) need to be added to config. Using defaults.
#define ADATOUCH_X_MIN 0
#define ADATOUCH_X_MAX 4000
#define ADATOUCH_Y_MIN 0
#define ADATOUCH_Y_MAX 4000
#define ADATOUCH_REMAP_YX 0
#endif // ADATOUCH_X_MIN
#endif // DRV_TOUCH_CALIB

// =======================================================================
// API support definitions
Expand Down
20 changes: 10 additions & 10 deletions src/GUIslice_drv_tft_espi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,12 @@ bool gslc_DrvInitTouch(gslc_tsGui* pGui, const char* acDev) {
#endif

// Load calibration settings for resistive displays from config
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
pGui->nTouchCalXMin = ADATOUCH_X_MIN;
pGui->nTouchCalXMax = ADATOUCH_X_MAX;
pGui->nTouchCalYMin = ADATOUCH_Y_MIN;
pGui->nTouchCalYMax = ADATOUCH_Y_MAX;
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB

// Support touch controllers with swapped X & Y
#if defined(ADATOUCH_REMAP_YX)
Expand Down Expand Up @@ -1339,7 +1339,7 @@ bool gslc_DrvGetTouch(gslc_tsGui* pGui, int16_t* pnX, int16_t* pnY, uint16_t* pn
nInputY = nRawY;

// For resistive displays, perform constraint and scaling
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
if (pGui->bTouchRemapEn) {
// Perform scaling from input to output
// - Calibration done in native orientation (GSLC_ROTATE=0)
Expand Down Expand Up @@ -1378,7 +1378,7 @@ bool gslc_DrvGetTouch(gslc_tsGui* pGui, int16_t* pnX, int16_t* pnY, uint16_t* pn
// No scaling from input to output
nOutputX = nInputX;
nOutputY = nInputY;
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB

#ifdef DBG_TOUCH
GSLC_DEBUG_PRINT("DBG: PreRotate: x=%u y=%u\n", nOutputX, nOutputY);
Expand Down Expand Up @@ -1571,12 +1571,12 @@ bool gslc_DrvGetTouch(gslc_tsGui* pGui, int16_t* pnX, int16_t* pnY, uint16_t* pn
bool gslc_TDrvInitTouch(gslc_tsGui* pGui,const char* acDev) {

// Capture default calibration settings for resistive displays
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
pGui->nTouchCalXMin = ADATOUCH_X_MIN;
pGui->nTouchCalXMax = ADATOUCH_X_MAX;
pGui->nTouchCalYMin = ADATOUCH_Y_MIN;
pGui->nTouchCalYMax = ADATOUCH_Y_MAX;
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB

// Support touch controllers with swapped X & Y
#if defined(ADATOUCH_REMAP_YX)
Expand Down Expand Up @@ -2017,7 +2017,7 @@ bool gslc_TDrvGetTouch(gslc_tsGui* pGui,int16_t* pnX,int16_t* pnY,uint16_t* pnPr
nInputY = nRawY;

// For resistive displays, perform constraint and scaling
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
if (pGui->bTouchRemapEn) {
// Perform scaling from input to output
// - Calibration done in native orientation (GSLC_ROTATE=0)
Expand Down Expand Up @@ -2045,14 +2045,14 @@ bool gslc_TDrvGetTouch(gslc_tsGui* pGui,int16_t* pnX,int16_t* pnY,uint16_t* pnPr
// No scaling from input to output
nOutputX = nInputX;
nOutputY = nInputY;
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB

#ifdef DBG_TOUCH
GSLC_DEBUG_PRINT("DBG: PreRotate: x=%u y=%u\n", nOutputX, nOutputY);
#if defined(DRV_TOUCH_TYPE_RES)
#if defined(DRV_TOUCH_CALIB)
GSLC_DEBUG_PRINT("DBG: RotateCfg: remap=%u nSwapXY=%u nFlipX=%u nFlipY=%u\n",
pGui->bTouchRemapEn,pGui->nSwapXY,pGui->nFlipX,pGui->nFlipY);
#endif // DRV_TOUCH_TYPE_RES
#endif // DRV_TOUCH_CALIB
#endif // DBG_TOUCH

// Perform remapping due to current orientation
Expand Down
25 changes: 25 additions & 0 deletions src/GUIslice_drv_tft_espi.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,31 @@ extern "C" {
#elif defined(DRV_TOUCH_NONE)
#endif // DRV_TOUCH_*

// Determine if calibration required
// - Enable for resistive displays
// - User config can also enable for capacitive displays by adding:
// #define DRV_TOUCH_CALIB
#if defined(DRV_TOUCH_TYPE_RES)
#define DRV_TOUCH_CALIB
#endif

#if defined(DRV_TOUCH_CALIB)
// Ensure calibration settings are present in the config file
#if !defined(ADATOUCH_X_MIN)
// We didn't locate the calibration settings, so provide some
// temporary defaults. This typically only occurs if user has
// decided to force calibration on a capacitive display by
// adding DRV_TOUCH_CALIB, but hasn't yet added the calibration
// settings (ADATOUCH_X/Y_MIN/MAX) from the calibration sketch yet.
#warning Calibration settings (ADATOUCH_X/Y_MIN/MAX) need to be added to config. Using defaults.
#define ADATOUCH_X_MIN 0
#define ADATOUCH_X_MAX 4000
#define ADATOUCH_Y_MIN 0
#define ADATOUCH_Y_MAX 4000
#define ADATOUCH_REMAP_YX 0
#endif // ADATOUCH_X_MIN
#endif // DRV_TOUCH_CALIB

// Additional defines
// - Provide default if not in config file
#if !defined(GSLC_SPIFFS_EN)
Expand Down
Loading