Skip to content
Open
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
19 changes: 10 additions & 9 deletions TouchScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ TSPoint TouchScreen::getPoint(void) {
}
#endif

x = (1023 - samples[NUMSAMPLES / 2]);
x = (_adcMax - samples[NUMSAMPLES / 2]);

pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
Expand Down Expand Up @@ -152,7 +152,7 @@ TSPoint TouchScreen::getPoint(void) {
}
#endif

y = (1023 - samples[NUMSAMPLES / 2]);
y = (_adcMax - samples[NUMSAMPLES / 2]);

// Set X+ to ground
// Set Y- to VCC
Expand All @@ -179,11 +179,11 @@ TSPoint TouchScreen::getPoint(void) {
rtouch -= 1;
rtouch *= x;
rtouch *= _rxplate;
rtouch /= 1024;
rtouch /= _adcMax + 1;

z = rtouch;
} else {
z = (1023 - (z2 - z1));
z = (_adcMax - (z2 - z1));
}

if (!valid) {
Expand All @@ -194,12 +194,13 @@ TSPoint TouchScreen::getPoint(void) {
}

TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym,
uint16_t rxplate = 0) {
uint16_t rxplate, uint8_t adcBits) {
_yp = yp;
_xm = xm;
_ym = ym;
_xp = xp;
_rxplate = rxplate;
_adcMax = (1 << adcBits) - 1;

#if defined(USE_FAST_PINIO)
xp_port = portOutputRegister(digitalPinToPort(_xp));
Expand Down Expand Up @@ -231,7 +232,7 @@ int TouchScreen::readTouchX(void) {
pinMode(_xm, OUTPUT);
digitalWrite(_xm, LOW);

return (1023 - analogRead(_yp));
return (_adcMax - analogRead(_yp));
}
/**
* @brief Read the touch event's Y value
Expand All @@ -249,7 +250,7 @@ int TouchScreen::readTouchY(void) {
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW);

return (1023 - analogRead(_xm));
return (_adcMax - analogRead(_xm));
}
/**
* @brief Read the touch event's Z/pressure value
Expand Down Expand Up @@ -282,10 +283,10 @@ uint16_t TouchScreen::pressure(void) {
rtouch -= 1;
rtouch *= readTouchX();
rtouch *= _rxplate;
rtouch /= 1024;
rtouch /= _adcMax + 1;

return rtouch;
} else {
return (1023 - (z2 - z1));
return (_adcMax - (z2 - z1));
}
}
7 changes: 5 additions & 2 deletions TouchScreen.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ class TouchScreen {
* @param ym Y- pin. Can be a digital pin
* @param rx The resistance in ohms between X+ and X- to calibrate pressure
* sensing
* @param adcBits for µC with higher resolution ADCs provide the number of
* bits
*/
TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, uint16_t rx);
TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, uint16_t rx = 0,
uint8_t adcBits = 10);

/**
* @brief **NOT IMPLEMENTED** Test if the screen has been touched
Expand All @@ -70,7 +73,7 @@ class TouchScreen {
private:
uint8_t _yp, _ym, _xm, _xp;
uint16_t _rxplate;

uint16_t _adcMax; // maximum reading of ADC (= 2^adcBits - 1)
#if defined(USE_FAST_PINIO)
volatile RwReg *xp_port, *yp_port, *xm_port, *ym_port;
RwReg xp_pin, xm_pin, yp_pin, ym_pin;
Expand Down