From bbecf8f7c76af66028771a30e5fa333a46180033 Mon Sep 17 00:00:00 2001 From: Jeroi Date: Tue, 24 Mar 2015 20:22:42 +0200 Subject: [PATCH 1/6] First creat fixes and addons: Added: - isTouching() function atlast to the lib - Internal cleanPins() function which is drived now every function that alters pins and there for now TouchScreen is pinsafe library and supports LCD in same pins out of box. Speedup: - Created private port, mask variables and moved their intializing to object creating functions. Now they use prosessor speed only when object is initialized, not every time when point is readed. - Every write command are now direct port operations instead of digitalWrites. Fixes: - Library generally affected LCD pins everywhere. Since this added cleanPins funtion which is drived where needed in the code. - function pressure() was most badly affecting LCD pins, the cleanup code made it pinsafe and thus for were able to write simple isTouching() function without any cleanups. - isTouching() function firstly made LCD crazy because of pressure(). Todo: - One user has commented that port manipulation does not work in DUE. This needs attention. - More features? Maybe. --- TouchScreen.cpp | 150 +++++++++++++++++++++++++++++++++++++----------- TouchScreen.h | 12 +++- 2 files changed, 125 insertions(+), 37 deletions(-) diff --git a/TouchScreen.cpp b/TouchScreen.cpp index d4aca3e..7f273a6 100644 --- a/TouchScreen.cpp +++ b/TouchScreen.cpp @@ -53,18 +53,6 @@ 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); @@ -130,8 +118,8 @@ TSPoint TouchScreen::getPoint(void) { //digitalWrite(_yp, LOW); 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 @@ -151,7 +139,10 @@ TSPoint TouchScreen::getPoint(void) { if (! valid) { z = 0; } - + + // Clean pins for LCD fix + cleanPins(); + return TSPoint(x, y, z); } @@ -161,6 +152,18 @@ TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym) { _ym = ym; _xp = xp; _rxplate = 0; + + //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); + pressureThreshhold = 10; } @@ -172,58 +175,98 @@ TouchScreen::TouchScreen(uint8_t xp, uint8_t yp, uint8_t xm, uint8_t ym, _ym = ym; _xp = xp; _rxplate = rxplate; + + //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); + pressureThreshhold = 10; } -int TouchScreen::readTouchX(void) { +uint16_t TouchScreen::readTouchX(void) { + //Added int to save result + uint16_t result; + pinMode(_yp, INPUT); pinMode(_ym, INPUT); - digitalWrite(_yp, LOW); - digitalWrite(_ym, LOW); + *portOutputRegister(yp_port) &= ~yp_pin; + //digitalWrite(_yp, LOW); + *portOutputRegister(ym_port) &= ~ym_pin; + //digitalWrite(_ym, LOW); pinMode(_xp, OUTPUT); - digitalWrite(_xp, HIGH); + *portOutputRegister(xp_port) |= xp_pin; + //digitalWrite(_xp, HIGH); + pinMode(_xm, OUTPUT); - digitalWrite(_xm, LOW); + *portOutputRegister(xm_port) &= ~xm_pin; + //digitalWrite(_xm, LOW); + + result = 1023-analogRead(_yp); + + //Clean pins for LCD fix + cleanPins(); - return (1023-analogRead(_yp)); + return result; } -int TouchScreen::readTouchY(void) { +uint16_t TouchScreen::readTouchY(void) { + //Added int to save result + uint16_t result; + pinMode(_xp, INPUT); pinMode(_xm, INPUT); - digitalWrite(_xp, LOW); - digitalWrite(_xm, LOW); + *portOutputRegister(xp_port) &= ~xp_pin; + //digitalWrite(_xp, LOW); + *portOutputRegister(xm_port) &= ~xm_pin; + //digitalWrite(_xm, LOW); pinMode(_yp, OUTPUT); - digitalWrite(_yp, HIGH); + *portOutputRegister(yp_port) |= yp_pin; + //digitalWrite(_yp, HIGH); pinMode(_ym, OUTPUT); - digitalWrite(_ym, LOW); + *portOutputRegister(ym_port) &= ~ym_pin; + //digitalWrite(_ym, LOW); - return (1023-analogRead(_xm)); + result = 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); - + *portOutputRegister(xp_port) &= ~xp_pin; + //digitalWrite(_xp, LOW); // Set Y- to VCC pinMode(_ym, OUTPUT); - digitalWrite(_ym, HIGH); + *portOutputRegister(ym_port) |= ym_pin; + //digitalWrite(_ym, HIGH); // Hi-Z X- and Y+ - digitalWrite(_xm, LOW); + *portOutputRegister(xm_port) &= ~xm_pin; + //digitalWrite(_xm, LOW); pinMode(_xm, INPUT); - digitalWrite(_yp, LOW); + + *portOutputRegister(yp_port) &= ~yp_pin; + //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; @@ -233,9 +276,46 @@ 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(); + + Serial.print("Kosketus paine on: "); + Serial.print(touch); + //No need to clear pins, because pressure does it already + + //Minimum and maximum that we define as good touch + if (touch > 100 && 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) { + pinMode(_xm, OUTPUT); + *portOutputRegister(xm_port) &= ~xm_pin; + //digitalWrite(_xm, LOW); + pinMode(_yp, OUTPUT); + *portOutputRegister(yp_port) |= yp_pin; + //digitalWrite(_yp, HIGH); + pinMode(_ym, OUTPUT); + *portOutputRegister(ym_port) &= ~ym_pin; + //digitalWrite(_ym, LOW); + pinMode(_xp, OUTPUT); + *portOutputRegister(xp_port) |= xp_pin; + //digitalWrite(_xp, HIGH); +} \ No newline at end of file diff --git a/TouchScreen.h b/TouchScreen.h index 39b7a2e..c7e8046 100644 --- a/TouchScreen.h +++ b/TouchScreen.h @@ -25,14 +25,22 @@ class TouchScreen { bool isTouching(void); uint16_t pressure(void); - int readTouchY(); - int readTouchX(); + uint16_t readTouchY(); + uint16_t readTouchX(); TSPoint getPoint(); int16_t pressureThreshhold; private: + //Input pins uint8_t _yp, _ym, _xm, _xp; + //Input pins port registers + uint8_t xp_port, yp_port, xm_port, ym_port; + //Input pins converted to mask + uint8_t xp_pin, yp_pin, xm_pin ,ym_pin; + uint16_t _rxplate; + //Internal cleanup function + void cleanPins(void); }; #endif From ba5efb5745eb21a522de98c25b660bb34b04bc8b Mon Sep 17 00:00:00 2001 From: Jeroi Date: Tue, 24 Mar 2015 21:07:08 +0200 Subject: [PATCH 2/6] Removed serial prints from isTouching. --- TouchScreen.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/TouchScreen.cpp b/TouchScreen.cpp index 7f273a6..37f22fa 100644 --- a/TouchScreen.cpp +++ b/TouchScreen.cpp @@ -293,8 +293,6 @@ bool TouchScreen::isTouching(void) { //read current pressure level uint16_t touch = pressure(); - Serial.print("Kosketus paine on: "); - Serial.print(touch); //No need to clear pins, because pressure does it already //Minimum and maximum that we define as good touch From 84d1ccbb30efa889f5932d28f61ff26ac02615fc Mon Sep 17 00:00:00 2001 From: Jeroi Date: Wed, 25 Mar 2015 10:31:23 +0200 Subject: [PATCH 3/6] ADDED - Due support and AMR prosessor support, basically via #ifdefs __ARM__ then use digitalWrite's, because at this point, could not find sufficient info how to handle directly ports in Arduino Core with due processor which is 32bit. Either webiste's had not much info regarding this. --- TouchScreen.cpp | 254 +++++++++++++++++++++++++++++------------------- TouchScreen.h | 15 +-- 2 files changed, 163 insertions(+), 106 deletions(-) diff --git a/TouchScreen.cpp b/TouchScreen.cpp index 37f22fa..b015d34 100644 --- a/TouchScreen.cpp +++ b/TouchScreen.cpp @@ -57,18 +57,23 @@ TSPoint TouchScreen::getPoint(void) { 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 100 && touch < 980) { + if (touch > 20 && touch < 980) { return true; } else return false; @@ -304,16 +351,23 @@ bool TouchScreen::isTouching(void) { //Pin clearing function. This clears used pins for better LCD support on same pins. void TouchScreen::cleanPins(void) { - pinMode(_xm, OUTPUT); - *portOutputRegister(xm_port) &= ~xm_pin; - //digitalWrite(_xm, LOW); - pinMode(_yp, OUTPUT); - *portOutputRegister(yp_port) |= yp_pin; - //digitalWrite(_yp, HIGH); - pinMode(_ym, OUTPUT); - *portOutputRegister(ym_port) &= ~ym_pin; - //digitalWrite(_ym, LOW); - pinMode(_xp, OUTPUT); - *portOutputRegister(xp_port) |= xp_pin; - //digitalWrite(_xp, HIGH); + #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 } \ No newline at end of file diff --git a/TouchScreen.h b/TouchScreen.h index c7e8046..ba4356e 100644 --- a/TouchScreen.h +++ b/TouchScreen.h @@ -33,12 +33,15 @@ class TouchScreen { private: //Input pins uint8_t _yp, _ym, _xm, _xp; - //Input pins port registers - uint8_t xp_port, yp_port, xm_port, ym_port; - //Input pins converted to mask - uint8_t xp_pin, yp_pin, xm_pin ,ym_pin; - - uint16_t _rxplate; + #if defined(__arm__) + //No port manipulation in DUE and ARM boards + #elde + //Input pins port registers + uint8_t xp_port, yp_port, xm_port, ym_port; + //Input pins converted to mask + uint8_t xp_pin, yp_pin, xm_pin ,ym_pin; + #endif + uint16_t _rxplate; //Internal cleanup function void cleanPins(void); }; From c3d3083a7099bee30a1f170126d810fdf16a394a Mon Sep 17 00:00:00 2001 From: Jeroi Date: Wed, 25 Mar 2015 18:40:38 +0200 Subject: [PATCH 4/6] Update README.txt --- README.txt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.txt b/README.txt index cf94dd1..d1ed3a2 100644 --- a/README.txt +++ b/README.txt @@ -1,4 +1,9 @@ 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 it LCD. No need to pull 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 \ No newline at end of file +I also made in UNO ja MEGA the libray fully porst writes so touch screen would be faster. + +I removed porthandling intializings from getPoint function to seepd up the point fetching. + +I added cleanPins function which is driven on pin affecting functions. From cb532b285c0bde55fed221cdc806e33ab586d93a Mon Sep 17 00:00:00 2001 From: Jeroi Date: Wed, 25 Mar 2015 18:43:35 +0200 Subject: [PATCH 5/6] Update README.txt --- README.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/README.txt b/README.txt index d1ed3a2..8ccaf92 100644 --- a/README.txt +++ b/README.txt @@ -1,9 +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 it LCD. No need to pull pins anymore in sketches. +I have comoleted the the library in the sense of now the library is pinsafe with LCD. No need to pull pins anymore in sketches. -I also made in UNO ja MEGA the libray fully porst writes so touch screen would be faster. +I also made in UNO ja MEGA the libray fully port writes so touch screen would be faster. -I removed porthandling intializings from getPoint function to seepd up the point fetching. +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. From c9508a9be8c49880bdc967bec64680d19b84f27b Mon Sep 17 00:00:00 2001 From: Jeroi Date: Wed, 25 Mar 2015 18:45:31 +0200 Subject: [PATCH 6/6] Update README.txt --- README.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.txt b/README.txt index 8ccaf92..13282f4 100644 --- a/README.txt +++ b/README.txt @@ -1,8 +1,8 @@ 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 pins anymore in sketches. +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. -I also made in UNO ja MEGA the libray fully port writes so touch screen would be faster. +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.