Skip to content

Commit 7e6cb04

Browse files
scan I2C devices if not yet know TOUCH_MODULE_ADDR
1 parent 01e1459 commit 7e6cb04

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

examples/TouchCalibration/TouchCalibration.ino

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
* Resistive touchscreen libraries
1111
* XPT2046: https://github.com/PaulStoffregen/XPT2046_Touchscreen.git
12-
*
12+
*
1313
* Capacitive touchscreen libraries
1414
* TouchLib: https://github.com/mmMicky/TouchLib.git
1515
******************************************************************************/
@@ -95,6 +95,23 @@ void setup(void)
9595
h = gfx->height();
9696
touch_init(w, h, gfx->getRotation());
9797

98+
// not yet know TOUCH_MODULE_ADDR, scan I2C devices
99+
#if defined(TOUCH_SDA) && !defined(TOUCH_MODULE_ADDR)
100+
for (uint8_t addr = 0x01; addr < 0x7f; addr++)
101+
{
102+
Wire.beginTransmission(addr);
103+
uint8_t error = Wire.endTransmission();
104+
if (error == 0)
105+
{
106+
Serial.printf("I2C device found at 0x%02X\n", addr);
107+
}
108+
else if (error != 2)
109+
{
110+
Serial.printf("Error %d at 0x%02X\n", error, addr);
111+
}
112+
}
113+
#endif
114+
98115
// Top left
99116
point_x[0] = w / 8;
100117
point_y[0] = h / 8;

examples/TouchCalibration/touch.h

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,17 @@ int16_t touch_max_x = 0, touch_max_y = 0;
4848
int16_t touch_raw_x = 0, touch_raw_y = 0;
4949
int16_t touch_last_x = 0, touch_last_y = 0;
5050

51+
#if defined(TOUCH_SDA)
52+
#include <Wire.h>
53+
#endif
54+
5155
#if defined(TOUCH_XPT2046)
5256
#include <XPT2046_Touchscreen.h>
5357
#include <SPI.h>
5458
XPT2046_Touchscreen ts(TOUCH_XPT2046_CS, TOUCH_XPT2046_INT);
5559

5660
#elif defined(TOUCH_MODULE_ADDR) // TouchLib
57-
#include <Wire.h>
61+
5862
#include <TouchLib.h>
5963
TouchLib touch(Wire, TOUCH_SDA, TOUCH_SCL, TOUCH_MODULE_ADDR);
6064

@@ -99,21 +103,26 @@ void touch_init(int16_t w, int16_t h, uint8_t r)
99103
}
100104
}
101105

102-
#if defined(TOUCH_XPT2046)
103-
SPI.begin(TOUCH_XPT2046_SCK, TOUCH_XPT2046_MISO, TOUCH_XPT2046_MOSI, TOUCH_XPT2046_CS);
104-
ts.begin();
105-
ts.setRotation(TOUCH_XPT2046_ROTATION);
106-
107-
#elif defined(TOUCH_MODULE_ADDR) // TouchLib
108106
// Reset touchscreen
109-
#if (TOUCH_RES > 0)
107+
#if defined(TOUCH_RES) && (TOUCH_RES > 0)
110108
pinMode(TOUCH_RES, OUTPUT);
111109
digitalWrite(TOUCH_RES, 0);
112110
delay(200);
113111
digitalWrite(TOUCH_RES, 1);
114112
delay(200);
115113
#endif
114+
115+
#if defined(TOUCH_SDA)
116116
Wire.begin(TOUCH_SDA, TOUCH_SCL);
117+
#endif
118+
119+
#if defined(TOUCH_XPT2046)
120+
SPI.begin(TOUCH_XPT2046_SCK, TOUCH_XPT2046_MISO, TOUCH_XPT2046_MOSI, TOUCH_XPT2046_CS);
121+
ts.begin();
122+
ts.setRotation(TOUCH_XPT2046_ROTATION);
123+
124+
#elif defined(TOUCH_MODULE_ADDR) // TouchLib
125+
117126
touch.init();
118127

119128
#endif // TouchLib

0 commit comments

Comments
 (0)