Fork with support for ILI9341 controller
Dependents: CANary_9341 CANary
Fork of TOUCH_TFTx2 by
TOUCH_TFTx2.cpp
- Committer:
- TickTock
- Date:
- 2013-02-11
- Revision:
- 4:a3cd26c97b76
- Parent:
- 3:3db7309b6146
- Child:
- 5:a9890c586a64
File content as of revision 4:a3cd26c97b76:
/* mbed library for resistive touch pads * uses 4 pins - 2 IO and 2 Analog * c 2011 Peter Drescher - DC2PD * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #include "TOUCH_TFTx2.h" #include "mbed.h" #define threshold 0x1000 // threshold to detect pressed TOUCH_TFTx2::TOUCH_TFTx2(PinName xp, PinName xm, PinName yp, PinName ym, PinName mosi, PinName miso, PinName sclk, PinName cs0, PinName cs1, PinName reset,const char* name): _xp(xp),_xm(xm),_yp(yp),_ym(ym),_ax(xp),_ay(yp), SPI_TFTx2(mosi,miso,sclk,cs0,cs1,reset,name) { xa = xp; ya = yp; } point TOUCH_TFTx2::get_touch() { unsigned short x1, x2, y1, y2; unsigned int s1 = 0,s2 = 0,d1 , d2; point p; do { // read y voltage _ym.input(); //LAJ y- has to be passive _yp.input(); //LAJ y+ has to be passive _xp.output(); _xm.output(); switch (orientation) { case(0): case(3): _xp = 1; _xm = 0; break; case(1): case(2): _xp = 0; _xm = 1; break; } AnalogIn Ay(ya); // we have to call the constructor to switch to analog mode wait_us(10); y1 = Ay.read_u16(); // get y voltage d1 = (y1 > y2)? (y1-y2) : (y2-y1); if (d1 < 3000) s1 ++; else { s1 = 0; } y2 = y1; // debug //locate(1,80); //printf("d: %4d y: %5d s1: %4d",d1,y1,s1); // read x voltage _xm.input(); //LAJ x- has to be passive _xp.input(); //LAJ x+ has to be passive _yp.output(); _ym.output(); switch (orientation) { case(0): case(1): _yp = 1; _ym = 0; break; case(2): case(3): _yp = 0; _ym = 1; break; } AnalogIn Ax(xa); // we have to call the constructor to switch to analog mode wait_us(10); x1 = Ax.read_u16(); // get x voltage d2 = (x1 > x2)? (x1-x2) : (x2-x1); if (d2 < 3000) s2 ++; else { s2 = 0; } x2 = x1; // debug //locate(1,100); //printf("d: %4d x: %5d s2: %4d",d2,x1,s2); //wait(0.25); } while (s1 < 3 || s2 < 3); // read until we have three samples close together switch (orientation) { case(0): case(2): p.y = (x1+x2) / 2; // average of two sample p.x = (y1+y2) / 2; break; case(1): case(3): p.x = (x1+x2) / 2; // average of two sample p.y = (y1+y2) / 2; break; } return(p); } void TOUCH_TFTx2::calibrate(void) { int i; int a = 0,b = 0,c = 0, d = 0; int pos_x, pos_y; point p; seldisp=1; // select right display cls(); line(0,3,6,3,White); line(3,0,3,6,White); // get the center of the screen pos_x = columns() / 2 - 3; pos_x = pos_x * font[1]; pos_y = (rows() / 2) - 1; pos_y = pos_y * font[2]; locate(pos_x,pos_y); printf("press cross"); locate(pos_x,pos_y + font[2]); printf("to calibrate"); do { wait(0.1); } while (!is_touched()); //Wait for touch for (i=0; i<5; i++) { p = get_touch(); b += p.y; } b = b / 5; locate(pos_x,pos_y); printf("OK "); do { wait(0.1); } while (is_touched()); //Wait for no touch cls(); line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross line(width() - 8,height() - 5,width() - 1,height() - 5,White); locate(pos_x,pos_y); printf("press cross"); locate(pos_x,pos_y + font[2]); printf("to calibrate"); do { wait(0.1); } while (!is_touched()); //Wait for touch for (i=0; i<5; i++) { p = get_touch(); c+= p.x; } c = c / 5; locate(pos_x, pos_y); printf("OK "); do { wait(0.1); } while (is_touched()); //Wait for no touch cls(); seldisp=0; // select left display cls(); line(0,3,6,3,White); line(3,0,3,6,White); // get the center of the screen pos_x = columns() / 2 - 3; pos_x = pos_x * font[1]; pos_y = (rows() / 2) - 1; pos_y = pos_y * font[2]; locate(pos_x,pos_y); printf("press cross"); locate(pos_x,pos_y + font[2]); printf("to calibrate"); do { wait(0.1); } while (!is_touched()); //Wait for touch for (i=0; i<5; i++) { p = get_touch(); a += p.x; } a = a / 5; locate(pos_x,pos_y); printf("OK "); do { wait(0.1); } while (is_touched()); //Wait for no touch cls(); line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross line(width() - 8,height() - 5,width() - 1,height() - 5,White); locate(pos_x,pos_y); printf("press cross"); locate(pos_x,pos_y + font[2]); printf("to calibrate"); do { wait(0.1); } while (!is_touched()); //Wait for touch for (i=0; i<5; i++) { p = get_touch(); d+= p.y; } d = d / 5; locate(pos_x, pos_y); printf("OK "); do { wait(0.1); } while (is_touched()); //Wait for no touch cls(); x_off = a; y_off = b; i = c-a; // delta x pp_tx = i / (width() - 6); i = d-b; // delta y pp_ty = i / (height() - 6); } point TOUCH_TFTx2::to_pixel(point a_point) { point p; p.x = (a_point.x - x_off) / pp_tx; if (p.x > width()) p.x = width(); p.y = (a_point.y - y_off) / pp_ty; if (p.y > height()) p.y = height(); //locate(1,60); //debug //printf("x: %d\ty: %d",p.x,p.y); return (p); } bool TOUCH_TFTx2::is_touched(void) { unsigned short y1; // read y voltage _ym.input(); //y- has to be passive _yp.input(); //y+ has to be passive _ym.mode(PullUp); _yp.mode(PullUp); _xp.output(); _xm.output(); _xp = 0; //drive x+ low _xm = 0; //drive x- low AnalogIn Ay(ya); // we have to call the constructor to switch to analog mode wait_us(10); y1 = Ay.read_u16(); // get y voltage return (y1<threshold); }