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

Faster, PinSAFE and ARM like DUE support #9

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
9 changes: 8 additions & 1 deletion README.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
This is the 4-wire resistive touch screen firmware for Arduino. Works with all Arduinos and the Mega

I have comoleted the the library in the sense of now the library is pinsafe with LCD. No need to pull LCD pins anymore in sketches.

To install, click DOWNLOAD SOURCE in the top right corner, and rename the uncompressed folder "TouchScreen". See our tutorial at http://www.ladyada.net/library/arduino/libraries.html on Arduino Library installation
I also made in UNO and MEGA the library fully port writes so touch screen would be faster.

I removed porthandling initializings from getPoint function to seepd up the point fetching.

I added cleanPins function which is driven on pin affecting functions.

Added bool isTouching function for looping pressure where needed.
274 changes: 203 additions & 71 deletions TouchScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,27 @@ TSPoint TouchScreen::getPoint(void) {
int samples[NUMSAMPLES];
uint8_t i, valid;


uint8_t xp_port = digitalPinToPort(_xp);
uint8_t yp_port = digitalPinToPort(_yp);
uint8_t xm_port = digitalPinToPort(_xm);
uint8_t ym_port = digitalPinToPort(_ym);

uint8_t xp_pin = digitalPinToBitMask(_xp);
uint8_t yp_pin = digitalPinToBitMask(_yp);
uint8_t xm_pin = digitalPinToBitMask(_xm);
uint8_t ym_pin = digitalPinToBitMask(_ym);


valid = 1;

pinMode(_yp, INPUT);
pinMode(_ym, INPUT);

#if defined(__arm__)
digitalWrite(_yp, LOW);
digitalWrite(_ym, LOW);
#else
*portOutputRegister(yp_port) &= ~yp_pin;
*portOutputRegister(ym_port) &= ~ym_pin;
//digitalWrite(_yp, LOW);
//digitalWrite(_ym, LOW);
#endif

pinMode(_xp, OUTPUT);
pinMode(_xm, OUTPUT);
//digitalWrite(_xp, HIGH);
//digitalWrite(_xm, LOW);
*portOutputRegister(xp_port) |= xp_pin;
*portOutputRegister(xm_port) &= ~xm_pin;
pinMode(_xp, OUTPUT);
pinMode(_xm, OUTPUT);
#if defined(__arm__)
digitalWrite(_xp, HIGH);
digitalWrite(_xm, LOW);
#else
*portOutputRegister(xp_port) |= xp_pin;
*portOutputRegister(xm_port) &= ~xm_pin;
#endif

for (i=0; i<NUMSAMPLES; i++) {
samples[i] = analogRead(_yp);
Expand All @@ -95,12 +88,18 @@ TSPoint TouchScreen::getPoint(void) {

pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
*portOutputRegister(xp_port) &= ~xp_pin;
//digitalWrite(_xp, LOW);
#if defined(__arm__)
digitalWrite(_xp, LOW);
#else
*portOutputRegister(xp_port) &= ~xp_pin;
#endif

pinMode(_yp, OUTPUT);
*portOutputRegister(yp_port) |= yp_pin;
//digitalWrite(_yp, HIGH);
#if defined(__arm__)
digitalWrite(_yp, HIGH);
#else
*portOutputRegister(yp_port) |= yp_pin;
#endif
pinMode(_ym, OUTPUT);

for (i=0; i<NUMSAMPLES; i++) {
Expand All @@ -116,22 +115,25 @@ TSPoint TouchScreen::getPoint(void) {

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

// Set X+ to ground

pinMode(_xp, OUTPUT);
*portOutputRegister(xp_port) &= ~xp_pin;
//digitalWrite(_xp, LOW);

// Set Y- to VCC
*portOutputRegister(ym_port) |= ym_pin;
//digitalWrite(_ym, HIGH);

// Hi-Z X- and Y+
*portOutputRegister(yp_port) &= ~yp_pin;
//digitalWrite(_yp, LOW);
#if defined(__arm__)
digitalWrite(_xp, LOW);
digitalWrite(_ym, HIGH);
digitalWrite(_yp, LOW);
#else
// Set X+ to ground
*portOutputRegister(xp_port) &= ~xp_pin;
// Set Y- to VCC
*portOutputRegister(ym_port) |= ym_pin;
// Hi-Z X- and Y+
*portOutputRegister(yp_port) &= ~yp_pin;
#endif

pinMode(_yp, INPUT);

int z1 = analogRead(_xm);
int z2 = analogRead(_yp);
uint16_t z1 = analogRead(_xm);
uint16_t z2 = analogRead(_yp);

if (_rxplate != 0) {
// now read the x
Expand All @@ -151,7 +153,10 @@ TSPoint TouchScreen::getPoint(void) {
if (! valid) {
z = 0;
}


// Clean pins for LCD fix
cleanPins();

return TSPoint(x, y, z);
}

Expand All @@ -161,6 +166,21 @@ TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym) {
_ym = ym;
_xp = xp;
_rxplate = 0;
#if defined(__arm__)
//No direct port manipulation on DUE and ARM
#else
//Added these to here for speed up the point detection
xp_port = digitalPinToPort(_xp);
yp_port = digitalPinToPort(_yp);
xm_port = digitalPinToPort(_xm);
ym_port = digitalPinToPort(_ym);

xp_pin = digitalPinToBitMask(_xp);
yp_pin = digitalPinToBitMask(_yp);
xm_pin = digitalPinToBitMask(_xm);
ym_pin = digitalPinToBitMask(_ym);
#endif

pressureThreshhold = 10;
}

Expand All @@ -172,58 +192,128 @@ TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym,
_ym = ym;
_xp = xp;
_rxplate = rxplate;

#if defined(__arm__)
//No direct port manipulation on DUE and ARM
#else

//Added these to here for speed up the point detection
xp_port = digitalPinToPort(_xp);
yp_port = digitalPinToPort(_yp);
xm_port = digitalPinToPort(_xm);
ym_port = digitalPinToPort(_ym);

xp_pin = digitalPinToBitMask(_xp);
yp_pin = digitalPinToBitMask(_yp);
xm_pin = digitalPinToBitMask(_xm);
ym_pin = digitalPinToBitMask(_ym);
#endif
pressureThreshhold = 10;
}

int TouchScreen::readTouchX(void) {
pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
digitalWrite(_yp, LOW);
digitalWrite(_ym, LOW);
uint16_t TouchScreen::readTouchX(void) {
//Added int to save result
uint16_t result;
#if defined(__arm__)
pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
//digitalWrite(_yp, LOW);
//digitalWrite(_ym, LOW);
pinMode(_xp, OUTPUT);
digitalWrite(_xp, HIGH);
pinMode(_xm, OUTPUT);
digitalWrite(_xm, LOW);
digitalWrite(_xm, LOW);
#else

pinMode(_yp, INPUT);
pinMode(_ym, INPUT);
*portOutputRegister(yp_port) &= ~yp_pin;
*portOutputRegister(ym_port) &= ~ym_pin;
pinMode(_xp, OUTPUT);
*portOutputRegister(xp_port) |= xp_pin;
pinMode(_xm, OUTPUT);
*portOutputRegister(xm_port) &= ~xm_pin;
#endif

pinMode(_xp, OUTPUT);
digitalWrite(_xp, HIGH);
pinMode(_xm, OUTPUT);
digitalWrite(_xm, LOW);
result = 1023-analogRead(_yp);

return (1023-analogRead(_yp));
//Clean pins for LCD fix
cleanPins();

return result;
}


int TouchScreen::readTouchY(void) {
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
digitalWrite(_xp, LOW);
digitalWrite(_xm, LOW);
uint16_t TouchScreen::readTouchY(void) {
//Added int to save result
uint16_t result;

pinMode(_yp, OUTPUT);
digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW);
#if defined(__arm__)
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
digitalWrite(_xp, LOW);
digitalWrite(_xm, LOW);
pinMode(_yp, OUTPUT);
digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW);
#else
pinMode(_xp, INPUT);
pinMode(_xm, INPUT);
*portOutputRegister(xp_port) &= ~xp_pin;
*portOutputRegister(xm_port) &= ~xm_pin;
pinMode(_yp, OUTPUT);
*portOutputRegister(yp_port) |= yp_pin;
pinMode(_ym, OUTPUT);
*portOutputRegister(ym_port) &= ~ym_pin;
#endif

result = 1023-analogRead(_xm);

return (1023-analogRead(_xm));
//Clean pins for LCD fix
cleanPins();

return result;
}


uint16_t TouchScreen::pressure(void) {
// Set X+ to ground
pinMode(_xp, OUTPUT);
digitalWrite(_xp, LOW);

// Set Y- to VCC
pinMode(_ym, OUTPUT);
digitalWrite(_ym, HIGH);
// Force LCD display pins for touchscreen operation
#if defined(__arm__)
// Set X+ to ground
pinMode(_xp, OUTPUT);
digitalWrite(_xp, LOW);
// Set Y- to VCC
pinMode(_ym, OUTPUT);
digitalWrite(_ym, HIGH);

// Hi-Z X- and Y+
digitalWrite(_xm, LOW);
digitalWrite(_yp, LOW);
#else
// Set X+ to ground
pinMode(_xp, OUTPUT);
*portOutputRegister(xp_port) &= ~xp_pin;

// Set Y- to VCC
pinMode(_ym, OUTPUT);
*portOutputRegister(ym_port) |= ym_pin;

// Hi-Z X- and Y+
*portOutputRegister(xm_port) &= ~xm_pin;
*portOutputRegister(yp_port) &= ~yp_pin;

#endif

// Hi-Z X- and Y+
digitalWrite(_xm, LOW);
//Read settings
pinMode(_xm, INPUT);
digitalWrite(_yp, LOW);
pinMode(_yp, INPUT);

int z1 = analogRead(_xm);
int z2 = analogRead(_yp);

int16_t z1 = analogRead(_xm);
int16_t z2 = analogRead(_yp);
if (_rxplate != 0) {
// now read the x
float rtouch;
Expand All @@ -233,9 +323,51 @@ uint16_t TouchScreen::pressure(void) {
rtouch *= readTouchX();
rtouch *= _rxplate;
rtouch /= 1024;

//Mo need to clear pins here
//because readRouchX() does it already


return rtouch;
} else {
// Clean pins for LCD fix
cleanPins();
return (1023-(z2-z1));
}
}

bool TouchScreen::isTouching(void) {
//read current pressure level
uint16_t touch = pressure();

//No need to clear pins, because pressure does it already

//Minimum and maximum that we define as good touch
if (touch > 20 && touch < 980) {
return true;
}
else return false;
}

//Pin clearing function. This clears used pins for better LCD support on same pins.
void TouchScreen::cleanPins(void) {
#if defined(__arm__)
pinMode(_xm, OUTPUT);
digitalWrite(_xm, LOW);
pinMode(_yp, OUTPUT);
digitalWrite(_yp, HIGH);
pinMode(_ym, OUTPUT);
digitalWrite(_ym, LOW);
pinMode(_xp, OUTPUT);
digitalWrite(_xp, HIGH);
#else
pinMode(_xm, OUTPUT);
*portOutputRegister(xm_port) &= ~xm_pin;
pinMode(_yp, OUTPUT);
*portOutputRegister(yp_port) |= yp_pin;
pinMode(_ym, OUTPUT);
*portOutputRegister(ym_port) &= ~ym_pin
pinMode(_xp, OUTPUT);
*portOutputRegister(xp_port) |= xp_pin;
#endif
}
Loading