.

Committer:
smejky
Date:
Wed Dec 08 16:08:26 2021 +0000
Revision:
1:88d79fcedec7
Parent:
0:a275ef704eb8
wokring;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rpocc 0:a275ef704eb8 1 /** \file UTouch.h
rpocc 0:a275ef704eb8 2 * \brief mbed library for XPT2046 SPI Touchscreen Controller (TFT_320QVT module).
rpocc 0:a275ef704eb8 3 * \copyright GNU Public License, v2. or later
rpocc 0:a275ef704eb8 4 *
rpocc 0:a275ef704eb8 5 * A known display with this type of controller chip is the ITDB02-3.2S
rpocc 0:a275ef704eb8 6 * from http://imall.iteadstudio.com
rpocc 0:a275ef704eb8 7 *
rpocc 0:a275ef704eb8 8 * This library is based on the Arduino/chipKIT UTFT library by Henning
rpocc 0:a275ef704eb8 9 * Karlsen, http://www.rinkydinkelectronics.com/library.php?id=55
rpocc 0:a275ef704eb8 10 *
rpocc 0:a275ef704eb8 11 * Pressure sensivity added by Dmitry Shtatnov
rpocc 0:a275ef704eb8 12 *
rpocc 0:a275ef704eb8 13 * Copyright (C)2010-2015 Henning Karlsen. All right reserved.
rpocc 0:a275ef704eb8 14 *
rpocc 0:a275ef704eb8 15 * Copyright (C)2016 Dmitry Shtatnov. All right reserved.
rpocc 0:a275ef704eb8 16 * http://modularsynth.ru/
rpocc 0:a275ef704eb8 17 *
rpocc 0:a275ef704eb8 18 * This library is free software; you can redistribute it and/or
rpocc 0:a275ef704eb8 19 * modify it under the terms of the GNU Lesser General Public
rpocc 0:a275ef704eb8 20 * License as published by the Free Software Foundation; either
rpocc 0:a275ef704eb8 21 * version 2.1 of the License, or (at your option) any later version.
rpocc 0:a275ef704eb8 22 *
rpocc 0:a275ef704eb8 23 * This library is distributed in the hope that it will be useful,
rpocc 0:a275ef704eb8 24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpocc 0:a275ef704eb8 25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
rpocc 0:a275ef704eb8 26 * Lesser General Public License for more details.
rpocc 0:a275ef704eb8 27 *
rpocc 0:a275ef704eb8 28 * You should have received a copy of the GNU Lesser General Public
rpocc 0:a275ef704eb8 29 * License along with this library; if not, write to:
rpocc 0:a275ef704eb8 30 *
rpocc 0:a275ef704eb8 31 * Free Software Foundation, Inc.
rpocc 0:a275ef704eb8 32 * 51 Franklin St, 5th Floor, Boston, MA 02110-1301, USA
rpocc 0:a275ef704eb8 33 *
rpocc 0:a275ef704eb8 34 *********************************************************************/
rpocc 0:a275ef704eb8 35
rpocc 0:a275ef704eb8 36 #include "UTouch.h"
rpocc 0:a275ef704eb8 37 #include "UTouchCD.h"
rpocc 0:a275ef704eb8 38
rpocc 0:a275ef704eb8 39 void UTouch::touch_WriteData(byte data)
rpocc 0:a275ef704eb8 40 {
rpocc 0:a275ef704eb8 41 byte temp;
rpocc 0:a275ef704eb8 42
rpocc 0:a275ef704eb8 43 temp=data;
rpocc 0:a275ef704eb8 44 P_CLK = LOW;
rpocc 0:a275ef704eb8 45
rpocc 0:a275ef704eb8 46 for(byte count=0; count<8; count++)
rpocc 0:a275ef704eb8 47 {
rpocc 0:a275ef704eb8 48 if(temp & 0x80)
rpocc 0:a275ef704eb8 49 P_DIN = HIGH;
rpocc 0:a275ef704eb8 50 else
rpocc 0:a275ef704eb8 51 P_DIN = LOW;
rpocc 0:a275ef704eb8 52 temp = temp << 1;
rpocc 0:a275ef704eb8 53 P_CLK = LOW;
rpocc 0:a275ef704eb8 54 P_CLK = HIGH;
rpocc 0:a275ef704eb8 55 }
rpocc 0:a275ef704eb8 56 }
rpocc 0:a275ef704eb8 57
rpocc 0:a275ef704eb8 58 word UTouch::touch_ReadData()
rpocc 0:a275ef704eb8 59 {
rpocc 0:a275ef704eb8 60 word data = 0;
rpocc 0:a275ef704eb8 61
rpocc 0:a275ef704eb8 62 for(byte count=0; count<12; count++)
rpocc 0:a275ef704eb8 63 {
rpocc 0:a275ef704eb8 64 data <<= 1;
rpocc 0:a275ef704eb8 65 P_CLK = HIGH;
rpocc 0:a275ef704eb8 66 P_CLK = LOW;
rpocc 0:a275ef704eb8 67 if (P_DOUT)
rpocc 0:a275ef704eb8 68 data++;
rpocc 0:a275ef704eb8 69 }
rpocc 0:a275ef704eb8 70 return(data);
rpocc 0:a275ef704eb8 71 }
rpocc 0:a275ef704eb8 72
rpocc 0:a275ef704eb8 73 UTouch::UTouch(PinName tclk, PinName tcs, PinName din, PinName dout, PinName irq)
rpocc 0:a275ef704eb8 74 :P_CLK(tclk), P_CS(tcs), P_DIN(din), P_DOUT(dout), P_IRQ(irq)
rpocc 0:a275ef704eb8 75 {
rpocc 0:a275ef704eb8 76 }
rpocc 0:a275ef704eb8 77
rpocc 0:a275ef704eb8 78 void UTouch::InitTouch(byte orientation)
rpocc 0:a275ef704eb8 79 {
rpocc 0:a275ef704eb8 80 orient = orientation;
rpocc 0:a275ef704eb8 81 _default_orientation = CAL_S>>31;
rpocc 0:a275ef704eb8 82 touch_x_left = (CAL_X>>14) & 0x3FFF;
rpocc 0:a275ef704eb8 83 touch_x_right = CAL_X & 0x3FFF;
rpocc 0:a275ef704eb8 84 touch_y_top = (CAL_Y>>14) & 0x3FFF;
rpocc 0:a275ef704eb8 85 touch_y_bottom = CAL_Y & 0x3FFF;
rpocc 0:a275ef704eb8 86 disp_x_size = (CAL_S>>12) & 0x0FFF;
rpocc 0:a275ef704eb8 87 disp_y_size = CAL_S & 0x0FFF;
rpocc 0:a275ef704eb8 88 prec = 10;
rpocc 0:a275ef704eb8 89
rpocc 0:a275ef704eb8 90 P_IRQ.output();
rpocc 0:a275ef704eb8 91
rpocc 0:a275ef704eb8 92 P_CS = HIGH;
rpocc 0:a275ef704eb8 93 P_CLK = HIGH;
rpocc 0:a275ef704eb8 94 P_DIN = HIGH;
rpocc 0:a275ef704eb8 95 P_IRQ = HIGH;
rpocc 0:a275ef704eb8 96 touch_tresholdLow = -1024;
rpocc 0:a275ef704eb8 97 touch_tresholdHigh = 2048;
rpocc 0:a275ef704eb8 98
rpocc 0:a275ef704eb8 99 }
rpocc 0:a275ef704eb8 100
rpocc 0:a275ef704eb8 101 int16_t UTouch::touch_getrawdata(uint8_t code) {
rpocc 0:a275ef704eb8 102 int16_t tmp;
rpocc 0:a275ef704eb8 103 P_CS = LOW;
rpocc 0:a275ef704eb8 104 P_IRQ.input();
rpocc 0:a275ef704eb8 105 if(!P_IRQ) {
rpocc 0:a275ef704eb8 106 touch_WriteData(code);
rpocc 0:a275ef704eb8 107 P_CLK = HIGH; P_CLK = LOW;
rpocc 0:a275ef704eb8 108 tmp = touch_ReadData();
rpocc 0:a275ef704eb8 109 }
rpocc 0:a275ef704eb8 110 else {
rpocc 0:a275ef704eb8 111 tmp = 0;
rpocc 0:a275ef704eb8 112 }
rpocc 0:a275ef704eb8 113 P_IRQ.output();
rpocc 0:a275ef704eb8 114 P_CS = LOW;
rpocc 0:a275ef704eb8 115 return tmp;
rpocc 0:a275ef704eb8 116 }
rpocc 0:a275ef704eb8 117
rpocc 0:a275ef704eb8 118 int16_t UTouch::GetPressure()
rpocc 0:a275ef704eb8 119 {
rpocc 0:a275ef704eb8 120 int16_t _ry, _rz1, _rz2;
rpocc 0:a275ef704eb8 121 _ry = GetYraw();
rpocc 0:a275ef704eb8 122 _rz1 = GetZ1raw();
rpocc 0:a275ef704eb8 123 _rz2 = GetZ2raw();
rpocc 0:a275ef704eb8 124 return _ry*_rz1/_rz2-_ry-5000;
rpocc 0:a275ef704eb8 125 }
rpocc 0:a275ef704eb8 126
rpocc 0:a275ef704eb8 127 void UTouch::SetTreshold(int16_t trshLow, int16_t trshHigh)
rpocc 0:a275ef704eb8 128 {
rpocc 0:a275ef704eb8 129 touch_tresholdLow = trshLow;
rpocc 0:a275ef704eb8 130 touch_tresholdHigh = trshLow;
rpocc 0:a275ef704eb8 131 }
rpocc 0:a275ef704eb8 132
rpocc 0:a275ef704eb8 133 int16_t UTouch::GetXraw()
rpocc 0:a275ef704eb8 134 {
rpocc 0:a275ef704eb8 135 return touch_getrawdata(0x90);
rpocc 0:a275ef704eb8 136 }
rpocc 0:a275ef704eb8 137
rpocc 0:a275ef704eb8 138 int16_t UTouch::GetYraw()
rpocc 0:a275ef704eb8 139 {
rpocc 0:a275ef704eb8 140 return touch_getrawdata(0xD0);
rpocc 0:a275ef704eb8 141 }
rpocc 0:a275ef704eb8 142
rpocc 0:a275ef704eb8 143 int16_t UTouch::GetZ1raw()
rpocc 0:a275ef704eb8 144 {
rpocc 0:a275ef704eb8 145 return touch_getrawdata(0xC0);
rpocc 0:a275ef704eb8 146 }
rpocc 0:a275ef704eb8 147
rpocc 0:a275ef704eb8 148 int16_t UTouch::GetZ2raw()
rpocc 0:a275ef704eb8 149 {
rpocc 0:a275ef704eb8 150 return touch_getrawdata(0xB0);
rpocc 0:a275ef704eb8 151 }
rpocc 0:a275ef704eb8 152
rpocc 0:a275ef704eb8 153 bool UTouch::Read()
rpocc 0:a275ef704eb8 154 {
rpocc 0:a275ef704eb8 155 unsigned long tx=0, temp_x=0;
rpocc 0:a275ef704eb8 156 unsigned long ty=0, temp_y=0;
rpocc 0:a275ef704eb8 157 unsigned long minx=99999, maxx=0;
rpocc 0:a275ef704eb8 158 unsigned long miny=99999, maxy=0;
rpocc 0:a275ef704eb8 159 int datacount=0;
rpocc 0:a275ef704eb8 160 uint16_t _press;
rpocc 0:a275ef704eb8 161
rpocc 0:a275ef704eb8 162 _press = GetPressure();
rpocc 0:a275ef704eb8 163 if((_press>touch_tresholdLow)&&(_press<touch_tresholdHigh))
rpocc 0:a275ef704eb8 164 {
rpocc 0:a275ef704eb8 165
rpocc 0:a275ef704eb8 166 P_CS = LOW;
rpocc 0:a275ef704eb8 167
rpocc 0:a275ef704eb8 168 P_IRQ.input();
rpocc 0:a275ef704eb8 169 for (int i=0; i<prec; i++)
rpocc 0:a275ef704eb8 170 {
rpocc 0:a275ef704eb8 171 if (!P_IRQ)
rpocc 0:a275ef704eb8 172 {
rpocc 0:a275ef704eb8 173 touch_WriteData(0x90);
rpocc 0:a275ef704eb8 174 P_CLK = HIGH; P_CLK = LOW;
rpocc 0:a275ef704eb8 175 temp_x=touch_ReadData();
rpocc 0:a275ef704eb8 176
rpocc 0:a275ef704eb8 177 if (!P_IRQ)
rpocc 0:a275ef704eb8 178 {
rpocc 0:a275ef704eb8 179 touch_WriteData(0xD0);
rpocc 0:a275ef704eb8 180 P_CLK = HIGH; P_CLK = LOW;
rpocc 0:a275ef704eb8 181 temp_y=touch_ReadData();
rpocc 0:a275ef704eb8 182
rpocc 0:a275ef704eb8 183 if ((temp_x>0) and (temp_x<4096) and (temp_y>0) and (temp_y<4096))
rpocc 0:a275ef704eb8 184 {
rpocc 0:a275ef704eb8 185 tx+=temp_x;
rpocc 0:a275ef704eb8 186 ty+=temp_y;
rpocc 0:a275ef704eb8 187 if (prec>5)
rpocc 0:a275ef704eb8 188 {
rpocc 0:a275ef704eb8 189 if (temp_x<minx)
rpocc 0:a275ef704eb8 190 minx=temp_x;
rpocc 0:a275ef704eb8 191 if (temp_x>maxx)
rpocc 0:a275ef704eb8 192 maxx=temp_x;
rpocc 0:a275ef704eb8 193 if (temp_y<miny)
rpocc 0:a275ef704eb8 194 miny=temp_y;
rpocc 0:a275ef704eb8 195 if (temp_y>maxy)
rpocc 0:a275ef704eb8 196 maxy=temp_y;
rpocc 0:a275ef704eb8 197 }
rpocc 0:a275ef704eb8 198 datacount++;
rpocc 0:a275ef704eb8 199 }
rpocc 0:a275ef704eb8 200 }
rpocc 0:a275ef704eb8 201 }
rpocc 0:a275ef704eb8 202 }
rpocc 0:a275ef704eb8 203 }
rpocc 0:a275ef704eb8 204 P_IRQ.output();
rpocc 0:a275ef704eb8 205
rpocc 0:a275ef704eb8 206 if (prec>5)
rpocc 0:a275ef704eb8 207 {
rpocc 0:a275ef704eb8 208 tx = tx-(minx+maxx);
rpocc 0:a275ef704eb8 209 ty = ty-(miny+maxy);
rpocc 0:a275ef704eb8 210 datacount -= 2;
rpocc 0:a275ef704eb8 211 }
rpocc 0:a275ef704eb8 212
rpocc 0:a275ef704eb8 213 P_CS = HIGH;
rpocc 0:a275ef704eb8 214 if ((datacount==(prec-2)) or (datacount==PREC_LOW))
rpocc 0:a275ef704eb8 215 {
rpocc 0:a275ef704eb8 216 if (orient == _default_orientation)
rpocc 0:a275ef704eb8 217 {
rpocc 0:a275ef704eb8 218 TP_X=ty/datacount;
rpocc 0:a275ef704eb8 219 TP_Y=tx/datacount;
rpocc 0:a275ef704eb8 220 return true;
rpocc 0:a275ef704eb8 221 }
rpocc 0:a275ef704eb8 222 else
rpocc 0:a275ef704eb8 223 {
rpocc 0:a275ef704eb8 224 TP_X=tx/datacount;
rpocc 0:a275ef704eb8 225 TP_Y=ty/datacount;
rpocc 0:a275ef704eb8 226 return true;
rpocc 0:a275ef704eb8 227 }
rpocc 0:a275ef704eb8 228 }
rpocc 0:a275ef704eb8 229 else
rpocc 0:a275ef704eb8 230 {
rpocc 0:a275ef704eb8 231 TP_X=-1;
rpocc 0:a275ef704eb8 232 TP_Y=-1;
rpocc 0:a275ef704eb8 233 return false;
rpocc 0:a275ef704eb8 234 }
rpocc 0:a275ef704eb8 235 }
rpocc 0:a275ef704eb8 236
rpocc 0:a275ef704eb8 237 bool UTouch::DataAvailable()
rpocc 0:a275ef704eb8 238 {
rpocc 0:a275ef704eb8 239 bool avail;
rpocc 0:a275ef704eb8 240 P_IRQ.input();
rpocc 0:a275ef704eb8 241 avail = !(P_IRQ);
rpocc 0:a275ef704eb8 242 P_IRQ.output();
rpocc 0:a275ef704eb8 243 return avail;
rpocc 0:a275ef704eb8 244 }
rpocc 0:a275ef704eb8 245
rpocc 0:a275ef704eb8 246 int16_t UTouch::GetX()
rpocc 0:a275ef704eb8 247 {
rpocc 0:a275ef704eb8 248 long c;
rpocc 0:a275ef704eb8 249
rpocc 0:a275ef704eb8 250 if ((TP_X==-1) or (TP_Y==-1))
rpocc 0:a275ef704eb8 251 return -1;
rpocc 0:a275ef704eb8 252 if (orient == _default_orientation)
rpocc 0:a275ef704eb8 253 {
rpocc 0:a275ef704eb8 254 c = long(long(TP_X - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
rpocc 0:a275ef704eb8 255 if (c<0)
rpocc 0:a275ef704eb8 256 c = 0;
rpocc 0:a275ef704eb8 257 if (c>disp_x_size)
rpocc 0:a275ef704eb8 258 c = disp_x_size;
rpocc 0:a275ef704eb8 259 }
rpocc 0:a275ef704eb8 260 else
rpocc 0:a275ef704eb8 261 {
rpocc 0:a275ef704eb8 262 if (_default_orientation == PORTRAIT)
rpocc 0:a275ef704eb8 263 c = long(long(TP_X - touch_y_top) * (-disp_y_size)) / long(touch_y_bottom - touch_y_top) + long(disp_y_size);
rpocc 0:a275ef704eb8 264 else
rpocc 0:a275ef704eb8 265 c = long(long(TP_X - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
rpocc 0:a275ef704eb8 266 if (c<0)
rpocc 0:a275ef704eb8 267 c = 0;
rpocc 0:a275ef704eb8 268 if (c>disp_y_size)
rpocc 0:a275ef704eb8 269 c = disp_y_size;
rpocc 0:a275ef704eb8 270 }
rpocc 0:a275ef704eb8 271 return c;
rpocc 0:a275ef704eb8 272 }
rpocc 0:a275ef704eb8 273
rpocc 0:a275ef704eb8 274 int16_t UTouch::GetY()
rpocc 0:a275ef704eb8 275 {
rpocc 0:a275ef704eb8 276 int c;
rpocc 0:a275ef704eb8 277
rpocc 0:a275ef704eb8 278 if ((TP_X==-1) or (TP_Y==-1))
rpocc 0:a275ef704eb8 279 return -1;
rpocc 0:a275ef704eb8 280 if (orient == _default_orientation)
rpocc 0:a275ef704eb8 281 {
rpocc 0:a275ef704eb8 282 c = long(long(TP_Y - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
rpocc 0:a275ef704eb8 283 if (c<0)
rpocc 0:a275ef704eb8 284 c = 0;
rpocc 0:a275ef704eb8 285 if (c>disp_y_size)
rpocc 0:a275ef704eb8 286 c = disp_y_size;
rpocc 0:a275ef704eb8 287 }
rpocc 0:a275ef704eb8 288 else
rpocc 0:a275ef704eb8 289 {
rpocc 0:a275ef704eb8 290 if (_default_orientation == PORTRAIT)
rpocc 0:a275ef704eb8 291 c = long(long(TP_Y - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
rpocc 0:a275ef704eb8 292 else
rpocc 0:a275ef704eb8 293 c = long(long(TP_Y - touch_x_left) * (-disp_x_size)) / long(touch_x_right - touch_x_left) + long(disp_x_size);
rpocc 0:a275ef704eb8 294 if (c<0)
rpocc 0:a275ef704eb8 295 c = 0;
rpocc 0:a275ef704eb8 296 if (c>disp_x_size)
rpocc 0:a275ef704eb8 297 c = disp_x_size;
rpocc 0:a275ef704eb8 298 }
rpocc 0:a275ef704eb8 299 return c;
rpocc 0:a275ef704eb8 300 }
rpocc 0:a275ef704eb8 301
rpocc 0:a275ef704eb8 302 void UTouch::SetPrecision(byte precision)
rpocc 0:a275ef704eb8 303 {
rpocc 0:a275ef704eb8 304 switch (precision)
rpocc 0:a275ef704eb8 305 {
rpocc 0:a275ef704eb8 306 case PREC_LOW:
rpocc 0:a275ef704eb8 307 prec=1; // DO NOT CHANGE!
rpocc 0:a275ef704eb8 308 break;
rpocc 0:a275ef704eb8 309 case PREC_MEDIUM:
rpocc 0:a275ef704eb8 310 prec=12; // Iterations + 2
rpocc 0:a275ef704eb8 311 break;
rpocc 0:a275ef704eb8 312 case PREC_HI:
rpocc 0:a275ef704eb8 313 prec=27; // Iterations + 2
rpocc 0:a275ef704eb8 314 break;
rpocc 0:a275ef704eb8 315 case PREC_EXTREME:
rpocc 0:a275ef704eb8 316 prec=102; // Iterations + 2
rpocc 0:a275ef704eb8 317 break;
rpocc 0:a275ef704eb8 318 default:
rpocc 0:a275ef704eb8 319 prec=12; // Iterations + 2
rpocc 0:a275ef704eb8 320 break;
rpocc 0:a275ef704eb8 321 }
rpocc 0:a275ef704eb8 322 }
rpocc 0:a275ef704eb8 323
rpocc 0:a275ef704eb8 324 void UTouch::CalibrateRead()
rpocc 0:a275ef704eb8 325 {
rpocc 0:a275ef704eb8 326 unsigned long tx=0;
rpocc 0:a275ef704eb8 327 unsigned long ty=0;
rpocc 0:a275ef704eb8 328
rpocc 0:a275ef704eb8 329 P_CS = LOW;
rpocc 0:a275ef704eb8 330
rpocc 0:a275ef704eb8 331 touch_WriteData(0x90);
rpocc 0:a275ef704eb8 332 P_CLK = HIGH; P_CLK = LOW;
rpocc 0:a275ef704eb8 333 tx=touch_ReadData();
rpocc 0:a275ef704eb8 334
rpocc 0:a275ef704eb8 335 touch_WriteData(0xD0);
rpocc 0:a275ef704eb8 336 P_CLK = HIGH; P_CLK = LOW;
rpocc 0:a275ef704eb8 337 ty=touch_ReadData();
rpocc 0:a275ef704eb8 338
rpocc 0:a275ef704eb8 339 P_CS = HIGH;
rpocc 0:a275ef704eb8 340
rpocc 0:a275ef704eb8 341 TP_X=ty;
rpocc 0:a275ef704eb8 342 TP_Y=tx;
rpocc 0:a275ef704eb8 343 }
rpocc 0:a275ef704eb8 344