TS library is for the 4-wire resistive Touch Screen (without an interface) which is usually included on TFT LCD like are MCUFriend displays.

Committer:
JohnnyK
Date:
Sun Jan 17 15:58:15 2021 +0000
Revision:
1:ca8fdb4d4afc
Parent:
0:f2fda7f69e5a
Corrections - include guards was added

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JohnnyK 0:f2fda7f69e5a 1 #include "TS.h"
JohnnyK 0:f2fda7f69e5a 2
JohnnyK 0:f2fda7f69e5a 3 bool TS_point::operator==(TS_point p) {
JohnnyK 0:f2fda7f69e5a 4 return ((p.x == x) && (p.y == y));
JohnnyK 0:f2fda7f69e5a 5 }
JohnnyK 0:f2fda7f69e5a 6
JohnnyK 0:f2fda7f69e5a 7 bool TS_point::operator!=(TS_point p) {
JohnnyK 0:f2fda7f69e5a 8 return ((p.x != x) || (p.y != y));
JohnnyK 0:f2fda7f69e5a 9 }
JohnnyK 0:f2fda7f69e5a 10
JohnnyK 0:f2fda7f69e5a 11 TouchScreen::TouchScreen(int pinYP, int pinYM, int pinXP, int pinXM, TS_config configuration):
JohnnyK 0:f2fda7f69e5a 12 pYP(pinYP), pYM(pinYM), pXP(pinXP), pXM(pinXM), conf(configuration){
JohnnyK 0:f2fda7f69e5a 13 pxX= 1.0/(conf.TS_adc_Xmax - conf.TS_adc_Xmin);
JohnnyK 0:f2fda7f69e5a 14 pxY = 1.0/(conf.TS_adc_Ymax - conf.TS_adc_Ymin);
JohnnyK 0:f2fda7f69e5a 15 }
JohnnyK 0:f2fda7f69e5a 16
JohnnyK 0:f2fda7f69e5a 17 void TouchScreen::setOrientation(TS_Orientation orientation){
JohnnyK 0:f2fda7f69e5a 18 ts_orient = orientation;
JohnnyK 0:f2fda7f69e5a 19 }
JohnnyK 0:f2fda7f69e5a 20
JohnnyK 0:f2fda7f69e5a 21
JohnnyK 0:f2fda7f69e5a 22 bool TouchScreen::getPoint(TS_point *point){
JohnnyK 0:f2fda7f69e5a 23 bool valid = true;
JohnnyK 0:f2fda7f69e5a 24
JohnnyK 0:f2fda7f69e5a 25 int xV = getCoords(setX);
JohnnyK 0:f2fda7f69e5a 26 int yV = getCoords(setY);
JohnnyK 0:f2fda7f69e5a 27
JohnnyK 0:f2fda7f69e5a 28 point->xV = xV;
JohnnyK 0:f2fda7f69e5a 29 point->yV = yV;
JohnnyK 0:f2fda7f69e5a 30
JohnnyK 0:f2fda7f69e5a 31 if(xV < conf.TS_adc_Xmin || xV > conf.TS_adc_Xmax) valid = false;
JohnnyK 0:f2fda7f69e5a 32 int x = ((1 - nrm_adcX(xV)) * conf.TFTWIDTH);
JohnnyK 0:f2fda7f69e5a 33
JohnnyK 0:f2fda7f69e5a 34 if(yV < conf.TS_adc_Ymin || yV > conf.TS_adc_Ymax) valid = false;
JohnnyK 0:f2fda7f69e5a 35 int y = ((1 - nrm_adcY(yV)) * conf.TFTHEIGHT);
JohnnyK 0:f2fda7f69e5a 36
JohnnyK 0:f2fda7f69e5a 37 switch(ts_orient) {
JohnnyK 0:f2fda7f69e5a 38 case TS_PORTRAITE:
JohnnyK 0:f2fda7f69e5a 39 point->x = x;
JohnnyK 0:f2fda7f69e5a 40 point->y = y;
JohnnyK 0:f2fda7f69e5a 41 break;
JohnnyK 0:f2fda7f69e5a 42 case TS_LANDSCAPE:
JohnnyK 0:f2fda7f69e5a 43 point->x = y;
JohnnyK 0:f2fda7f69e5a 44 point->y = conf.TFTWIDTH - x;
JohnnyK 0:f2fda7f69e5a 45 break;
JohnnyK 0:f2fda7f69e5a 46 case TS_PORTRAITE_REV:
JohnnyK 0:f2fda7f69e5a 47 point->x = conf.TFTWIDTH - x;
JohnnyK 0:f2fda7f69e5a 48 point->y = conf.TFTHEIGHT - y;
JohnnyK 0:f2fda7f69e5a 49 break;
JohnnyK 0:f2fda7f69e5a 50 case TS_LANDSCAPE_REV:
JohnnyK 0:f2fda7f69e5a 51 point->x = conf.TFTHEIGHT - y;
JohnnyK 0:f2fda7f69e5a 52 point->y = x;
JohnnyK 0:f2fda7f69e5a 53 break;
JohnnyK 0:f2fda7f69e5a 54 default:
JohnnyK 0:f2fda7f69e5a 55 point->x = x;
JohnnyK 0:f2fda7f69e5a 56 point->y = y;
JohnnyK 0:f2fda7f69e5a 57 }
JohnnyK 0:f2fda7f69e5a 58 return valid;
JohnnyK 0:f2fda7f69e5a 59 }
JohnnyK 0:f2fda7f69e5a 60
JohnnyK 0:f2fda7f69e5a 61 int TouchScreen::getCoords(Axis axis){
JohnnyK 0:f2fda7f69e5a 62 int value = 0;
JohnnyK 0:f2fda7f69e5a 63 switch(axis){
JohnnyK 0:f2fda7f69e5a 64 case setX:
JohnnyK 0:f2fda7f69e5a 65 setDigitalPin(pXM,pOutput,0); // XM = Output Low
JohnnyK 0:f2fda7f69e5a 66 setDigitalPin(pXP,pOutput,1); // XP = Output high
JohnnyK 0:f2fda7f69e5a 67 setDigitalPin(pYM,pInput,0); // YM = Input
JohnnyK 0:f2fda7f69e5a 68 value = getADCvalue(pYP); // YP = ADC
JohnnyK 0:f2fda7f69e5a 69 setDigitalPin(pXP,pOutput,0); // XP = Output Low
JohnnyK 0:f2fda7f69e5a 70 setDigitalPin(pYM,pOutput,0); // YM = Output Low
JohnnyK 0:f2fda7f69e5a 71 setDigitalPin(pYP,pOutput,0); // YP = Output Low
JohnnyK 0:f2fda7f69e5a 72 break;
JohnnyK 0:f2fda7f69e5a 73 case setY:
JohnnyK 0:f2fda7f69e5a 74 setDigitalPin(pYM,pOutput,0); // YM = Output Low
JohnnyK 0:f2fda7f69e5a 75 setDigitalPin(pYP,pOutput,1); // YP = Output high
JohnnyK 0:f2fda7f69e5a 76 setDigitalPin(pXP,pInput,0); // XP = Input
JohnnyK 0:f2fda7f69e5a 77 value = getADCvalue(pXM); // XM = ADC
JohnnyK 0:f2fda7f69e5a 78 setDigitalPin(pYP,pOutput,0); // YP = Output Low
JohnnyK 0:f2fda7f69e5a 79 setDigitalPin(pXP,pOutput,0); // XP = Output Low
JohnnyK 0:f2fda7f69e5a 80 setDigitalPin(pXM,pOutput,0); // XM = Output Low
JohnnyK 0:f2fda7f69e5a 81 break;
JohnnyK 0:f2fda7f69e5a 82 }
JohnnyK 0:f2fda7f69e5a 83 return value;
JohnnyK 0:f2fda7f69e5a 84 }
JohnnyK 0:f2fda7f69e5a 85
JohnnyK 0:f2fda7f69e5a 86 float TouchScreen::nrm_adcX(unsigned int x){
JohnnyK 0:f2fda7f69e5a 87 return (x - conf.TS_adc_Xmin) * pxX;
JohnnyK 0:f2fda7f69e5a 88 }
JohnnyK 0:f2fda7f69e5a 89
JohnnyK 0:f2fda7f69e5a 90 float TouchScreen::nrm_adcY(unsigned int y){
JohnnyK 0:f2fda7f69e5a 91 return (y - conf.TS_adc_Ymin) * pxY;
JohnnyK 0:f2fda7f69e5a 92 }
JohnnyK 0:f2fda7f69e5a 93