Advertisement
MrRockchip

URTouch.cpp

Jan 23rd, 2020
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.85 KB | None | 0 0
  1. /*
  2. URTouch.cpp - Arduino/chipKit library support for Color TFT LCD Touch screens
  3. Copyright (C)2018 Rinky-Dink Electronics, Henning Karlsen. All right reserved
  4.  
  5. Basic functionality of this library are based on the demo-code provided by
  6. ITead studio.
  7.  
  8. You can find the latest version of the library at
  9. http://www.RinkyDinkElectronics.com/
  10.  
  11. This library is free software; you can redistribute it and/or
  12. modify it under the terms of the CC BY-NC-SA 3.0 license.
  13. Please see the included documents for further information.
  14.  
  15. Commercial use of this library requires you to buy a license that
  16. will allow commercial use. This includes using the library,
  17. modified or not, as a tool to sell products.
  18.  
  19. Checksum of my build: Z1Pi02KmEF26dG6ktUhd3BS4NwwyL7kMPCWL3OMwjQ
  20.  
  21. The license applies to all part of the library including the
  22. examples and tools supplied with the library.
  23. */
  24.  
  25. #include "URTouch.h"
  26. #include "URTouchCD.h"
  27.  
  28. #if defined(__AVR__)
  29. #include "hardware/avr/HW_AVR.inc"
  30. #elif defined(__PIC32MX__)
  31. #include "hardware/pic32/HW_PIC32.inc"
  32. #elif defined(__arm__)
  33. #include "hardware/arm/HW_ARM.inc"
  34. #endif
  35.  
  36. URTouch::URTouch(byte tclk, byte tcs, byte din, byte dout, byte irq)
  37. {
  38. T_CLK = tclk;
  39. T_CS = tcs;
  40. T_DIN = din;
  41. T_DOUT = dout;
  42. T_IRQ = irq;
  43. }
  44.  
  45. void URTouch::InitTouch(byte orientation)
  46. {
  47. orient = orientation;
  48. _default_orientation = CAL_S>>31;
  49. touch_x_left = (CAL_X>>14) & 0x3FFF;
  50. touch_x_right = CAL_X & 0x3FFF;
  51. touch_y_top = (CAL_Y>>14) & 0x3FFF;
  52. touch_y_bottom = CAL_Y & 0x3FFF;
  53. disp_x_size = (CAL_S>>12) & 0x0FFF;
  54. disp_y_size = CAL_S & 0x0FFF;
  55. prec = 10;
  56.  
  57. P_CLK = portOutputRegister(digitalPinToPort(T_CLK));
  58. B_CLK = digitalPinToBitMask(T_CLK);
  59. P_CS = portOutputRegister(digitalPinToPort(T_CS));
  60. B_CS = digitalPinToBitMask(T_CS);
  61. P_DIN = portOutputRegister(digitalPinToPort(T_DIN));
  62. B_DIN = digitalPinToBitMask(T_DIN);
  63. P_DOUT = portInputRegister(digitalPinToPort(T_DOUT));
  64. B_DOUT = digitalPinToBitMask(T_DOUT);
  65. P_IRQ = portInputRegister(digitalPinToPort(T_IRQ));
  66. B_IRQ = digitalPinToBitMask(T_IRQ);
  67.  
  68. pinMode(T_CLK, OUTPUT);
  69. pinMode(T_CS, OUTPUT);
  70. pinMode(T_DIN, OUTPUT);
  71. pinMode(T_DOUT, INPUT);
  72. pinMode(T_IRQ, OUTPUT);
  73.  
  74. sbi(P_CS, B_CS);
  75. sbi(P_CLK, B_CLK);
  76. sbi(P_DIN, B_DIN);
  77. sbi(P_IRQ, B_IRQ);
  78. }
  79.  
  80. void URTouch::read()
  81. {
  82. unsigned long tx=0, temp_x=0;
  83. unsigned long ty=0, temp_y=0;
  84. unsigned long minx=99999, maxx=0;
  85. unsigned long miny=99999, maxy=0;
  86. int datacount=0;
  87.  
  88. cbi(P_CS, B_CS);
  89.  
  90. pinMode(T_IRQ, INPUT);
  91. for (int i=0; i<prec; i++)
  92. {
  93. if (!rbi(P_IRQ, B_IRQ))
  94. {
  95. touch_WriteData(0x90);
  96. pulse_high(P_CLK, B_CLK);
  97. temp_x=touch_ReadData();
  98.  
  99. if (!rbi(P_IRQ, B_IRQ))
  100. {
  101. touch_WriteData(0xD0);
  102. pulse_high(P_CLK, B_CLK);
  103. temp_y=touch_ReadData();
  104.  
  105. if ((temp_x>0) and (temp_x<4096) and (temp_y>0) and (temp_y<4096))
  106. {
  107. tx+=temp_x;
  108. ty+=temp_y;
  109. if (prec>5)
  110. {
  111. if (temp_x<minx)
  112. minx=temp_x;
  113. if (temp_x>maxx)
  114. maxx=temp_x;
  115. if (temp_y<miny)
  116. miny=temp_y;
  117. if (temp_y>maxy)
  118. maxy=temp_y;
  119. }
  120. datacount++;
  121. }
  122. }
  123. }
  124. }
  125. pinMode(T_IRQ, OUTPUT);
  126.  
  127. if (prec>5)
  128. {
  129. tx = tx-(minx+maxx);
  130. ty = ty-(miny+maxy);
  131. datacount -= 2;
  132. }
  133.  
  134. sbi(P_CS, B_CS);
  135. if ((datacount==(prec-2)) or (datacount==PREC_LOW))
  136. {
  137. if (orient == _default_orientation)
  138. {
  139. TP_X=ty/datacount;
  140. TP_Y=tx/datacount;
  141. }
  142. else
  143. {
  144. TP_X=tx/datacount;
  145. TP_Y=ty/datacount;
  146. }
  147. }
  148. else
  149. {
  150. TP_X=-1;
  151. TP_Y=-1;
  152. }
  153. }
  154.  
  155. bool URTouch::dataAvailable()
  156. {
  157. bool avail;
  158. pinMode(T_IRQ, INPUT);
  159. avail = !(rbi(P_IRQ, B_IRQ));
  160. pinMode(T_IRQ, OUTPUT);
  161. return avail;
  162. }
  163.  
  164. int16_t URTouch::getX()
  165. {
  166. long c;
  167.  
  168. if ((TP_X==-1) or (TP_Y==-1))
  169. return -1;
  170. if (orient == _default_orientation)
  171. {
  172. c = long(long(TP_X - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
  173. if (c<0)
  174. c = 0;
  175. if (c>disp_x_size)
  176. c = disp_x_size;
  177.  
  178. c = disp_x_size - c; // ROTATION HACK
  179. }
  180. else
  181. {
  182. if (_default_orientation == PORTRAIT)
  183. c = long(long(TP_X - touch_y_top) * (-disp_y_size)) / long(touch_y_bottom - touch_y_top) + long(disp_y_size);
  184. else
  185. c = long(long(TP_X - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
  186. if (c<0)
  187. c = 0;
  188. if (c>disp_y_size)
  189. c = disp_y_size;
  190. }
  191. return c;
  192. }
  193.  
  194. int16_t URTouch::getY()
  195. {
  196. int c;
  197.  
  198. if ((TP_X==-1) or (TP_Y==-1))
  199. return -1;
  200. if (orient == _default_orientation)
  201. {
  202. c = long(long(TP_Y - touch_y_top) * (disp_y_size)) / long(touch_y_bottom - touch_y_top);
  203. if (c<0)
  204. c = 0;
  205. if (c>disp_y_size)
  206. c = disp_y_size;
  207. }
  208. else
  209. {
  210. if (_default_orientation == PORTRAIT)
  211. c = long(long(TP_Y - touch_x_left) * (disp_x_size)) / long(touch_x_right - touch_x_left);
  212. else
  213. c = long(long(TP_Y - touch_x_left) * (-disp_x_size)) / long(touch_x_right - touch_x_left) + long(disp_x_size);
  214. if (c<0)
  215. c = 0;
  216. if (c>disp_x_size)
  217. c = disp_x_size;
  218.  
  219. c = disp_x_size - c; // ROTATION HACK
  220. }
  221. return c;
  222. }
  223.  
  224. void URTouch::setPrecision(byte precision)
  225. {
  226. switch (precision)
  227. {
  228. case PREC_LOW:
  229. prec=1; // DO NOT CHANGE!
  230. break;
  231. case PREC_MEDIUM:
  232. prec=12; // Iterations + 2
  233. break;
  234. case PREC_HI:
  235. prec=27; // Iterations + 2
  236. break;
  237. case PREC_EXTREME:
  238. prec=102; // Iterations + 2
  239. break;
  240. default:
  241. prec=12; // Iterations + 2
  242. break;
  243. }
  244. }
  245.  
  246. void URTouch::calibrateRead()
  247. {
  248. unsigned long tx=0;
  249. unsigned long ty=0;
  250.  
  251. cbi(P_CS, B_CS);
  252.  
  253. touch_WriteData(0x90);
  254. pulse_high(P_CLK, B_CLK);
  255. tx=touch_ReadData();
  256.  
  257. touch_WriteData(0xD0);
  258. pulse_high(P_CLK, B_CLK);
  259. ty=touch_ReadData();
  260.  
  261. sbi(P_CS, B_CS);
  262.  
  263. TP_X=ty;
  264. TP_Y=tx;
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement