This is a fork of a functional ILI9341 display with a functional Seeed touch screen library.

Dependencies:   BMP180 UniGraphic mbed BNO055_fusionI_fixed HTU21D GPSISR Compass Fonts uGUI

Fork of TFT_test_NUCLEO-F411RE by Motoo Tanaka

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SeeedStudioTFTv2.cpp Source File

SeeedStudioTFTv2.cpp

00001 /* mbed library for resistive touch pads
00002  * uses 4 pins - 2 IO and 2 Analog
00003 
00004  * c 2011 Peter Drescher - DC2PD
00005  *
00006  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00007  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00008  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00009  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00010  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00011  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00012  * THE SOFTWARE.
00013  */
00014 
00015 
00016 #include "mbed.h"
00017 #include "SeeedStudioTFTv2.h"
00018 
00019 //Serial pc(USBTX,USBRX,19200);
00020 
00021 TouchScreen::TouchScreen(PinName xp, PinName xm, PinName yp, PinName ym)
00022 {
00023     //font = NULL;
00024     // touch screen pins
00025     _xp = xp;
00026     _yp = yp;
00027     _xm = xm;
00028     _ym = ym;
00029     // default touch calibration
00030     // orientation     //      0      1      2      3
00031     x_off = 108000;  //  17252  16605 108755 108000
00032     y_off =  22000;  //  22330 105819  97167  22000
00033     pp_tx =   -291;  //    378    289   -390   -291
00034     pp_ty =    356;  //    261   -355   -239    356
00035 }
00036 
00037 int TouchScreen::readTouch(PinName p, PinName m, PinName a, PinName i)
00038 {
00039     int n, avg;
00040     // Setup analog ports to read X and Y touch values on display.
00041     DigitalOut _p(p);
00042     _p = 1;
00043     DigitalOut _m(m);
00044     _m = 0;
00045     AnalogIn   _a(a);
00046     AnalogIn   _i(i); // this pin has to be high Z (DigitalIn may also work)
00047     
00048     // Put averaging solution here. Take n samples then average.
00049     n=5000;
00050     for (int j=0; i<n; j++) {
00051         avg = avg + _a.read_u16();
00052     }
00053     avg=avg/n;
00054     
00055     wait_us(10);
00056     //return _a.read_u16();
00057     return avg;
00058 }
00059 
00060 void TouchScreen :: getTouch(point& p)
00061 {
00062     volatile int y2 = readTouch(_xp,_xm,_yp,_ym);
00063     volatile int x2 = readTouch(_yp,_ym,_xp,_xm);
00064     volatile int y1 = readTouch(_xp,_xm,_yp,_ym);
00065     volatile int x1 = readTouch(_yp,_ym,_xp,_xm);
00066     int xd = x1 - x2;
00067     int yd = y1 - y2;
00068     xd = (xd > 0) ? xd : -xd;
00069     yd = (yd > 0) ? xd : -xd;
00070     p.y = x1 + x2;
00071     p.x = y1 + y2;
00072          
00073     int z1          =  _xm;
00074     int z2          =  _yp;
00075     float rtouch    =  0;
00076 
00077     rtouch  = z2;
00078     rtouch /= z1;
00079     rtouch -= 1;
00080     //Constants from origional reference. 
00081     //12 bit ADC
00082     rtouch *= (4094-p.x)/2;
00083     rtouch *= RXPLATE;
00084     rtouch /= 2048;
00085         p.x = map(p.x, TS_MINX, TS_MAXX, 0, 240);
00086         p.y = map(p.y, TS_MINY, TS_MAXY, 0, 320);
00087         p.z = abs(rtouch);
00088  }
00089 
00090  long TouchScreen :: map(long x, long in_min, long in_max, long out_min, long out_max)
00091 {
00092   return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
00093 }