Henry Leinen / Touch_tft

Fork of Touch_tft by Peter Drescher

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers touch_tft.cpp Source File

touch_tft.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 "touch_tft.h"
00017 #include "mbed.h"
00018 
00019 #define threshold 0x2000  // threshold to detect pressed 
00020 
00021 touch_tft::touch_tft(PinName xp, PinName xm, PinName yp, PinName ym,
00022                      PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset,const char* name):
00023         _xp(xp),_xm(xm),_yp(yp),_ym(ym),_ax(xp),_ay(yp),
00024         SPI_TFT(mosi,miso,sclk,cs,reset,name) {
00025     xa = xp;
00026     xb = xm;
00027     ya = yp;
00028     yb = ym;
00029 
00030 }
00031 static char Buffer[30];
00032 
00033 point touch_tft::get_touch() {
00034     unsigned short x1 = 0,x2 = 0, y1 = 0, y2 = 0;
00035     int s1 = 0,s2 = 0,d1 , d2;
00036     point p;
00037 
00038     do {
00039         // read y voltage
00040 //        DigitalOut __xp(xa);
00041         _xp.output();
00042 //        DigitalOut __xm(xb);
00043         _xm.output();
00044         switch (orientation) {
00045             case(0):
00046             case(3):
00047                 _xp = 1;
00048                 _xm = 0;
00049 //                __xp = 1;
00050 //                __xm = 0;
00051                 break;
00052             case(1):
00053             case(2):
00054                 _xp = 0;
00055                 _xm = 1;
00056 //                __xp = 0;
00057 //                __xm = 1;
00058                 break;
00059         }
00060 //        DigitalIn __ym(yb);
00061 //        __ym.mode(PullNone);
00062         _ym.input();        // y- have to be passive
00063         _ym.mode(PullNone);
00064         AnalogIn Ay(ya);    // we have to call the constructor to switch to analog mode
00065         wait_us(10);
00066         y1 = Ay.read_u16(); // get y voltage
00067         y1 = Ay.read_u16();
00068         d1 = (y1 > y2)? (y1-y2) : (y2-y1);
00069         if (((y1 < 8000) && (d1 < 2000)) || ((y1 > 8000) && (d1 < 250))) s1 ++;
00070         else {
00071             if (s1 > 0) s1 --;
00072         }
00073         // debug
00074         locate(1,7);
00075 //        sprintf(Buffer, "d: %4d y: %5d s1: %4d",d1,y1,s1);
00076         sprintf(Buffer, "d: %5d y1: %5d y2: %5d s1: %5d",d1,y1,y2,s1);
00077         printf(Buffer);
00078         y2 = y1;
00079         
00080         // read x voltage
00081 //        DigitalOut __yp(ya);
00082 //        DigitalOut __ym1(yb);
00083         _yp.output();
00084         _ym.output();
00085         switch (orientation) {
00086             case(0):
00087             case(1):
00088                 _yp = 1;
00089                 _ym = 0;
00090 //                __yp = 1;
00091 //                __ym1 = 0;
00092                 break;
00093             case(2):
00094             case(3):
00095                 _yp = 0;
00096                 _ym = 1;
00097 //                __yp = 0;
00098 //                __ym1 = 1;
00099                 break;
00100         }
00101 //        DigitalIn   __ixm(xb);
00102         _xm.input();        // x- have to be passive
00103 //        _xm.mode(PullNone);
00104         AnalogIn Ax(xa);    // we have to call the constructor to switch to analog mode
00105         wait_us(10);
00106         x1 = Ax.read_u16(); // get x voltage
00107         x1 = Ax.read_u16();
00108         d2 = (x1 > x2)? (x1-x2) : (x2-x1);
00109         if (((x1 < 8000) && (d2 < 2000)) || ((x1 > 8000) && (d2 < 250))) s2 ++;
00110         else {
00111             if (s2 > 0) s2 --;
00112         }
00113         x2 = x1;
00114         // debug
00115         locate(1,20);
00116         sprintf(Buffer, "d: %5d x1: %5d x2: %5d s1: %4d",d2,x1,x2, s2);
00117         printf(Buffer);
00118           //printf("d: %4d x: %5d s2: %4d",d2,x1,s2);
00119 
00120     } while (s1 < 3 || s2 < 3); // read until we have three samples close together
00121     switch (orientation) {
00122         case(0):
00123         case(2):
00124             p.y = (x1+x2) / 2;  // average of two sample
00125             p.x = (y1+y2) / 2;
00126             break;
00127         case(1):
00128         case(3):
00129             p.x = (x1+x2) / 2;  // average of two sample
00130             p.y = (y1+y2) / 2;
00131             break;
00132     }
00133     return(p);
00134 }
00135 
00136 void touch_tft::calibrate(void) {
00137     int i;
00138     int a = 0,b = 0,c = 0, d = 0;
00139     int pos_x, pos_y;
00140     point p;
00141 
00142     cls();
00143     line(0,3,6,3,White);
00144     line(3,0,3,6,White);
00145 
00146     // get the center of the screen
00147     pos_x = columns() / 2 - 3;
00148     pos_x = pos_x * font[1];
00149     pos_y = (rows() / 2) - 1;
00150     pos_y = pos_y * font[2];
00151 
00152     locate(pos_x,pos_y);
00153     printf("press cross");
00154     locate(pos_x,pos_y + font[2]);
00155     printf("to calibrate");
00156     for (i=0; i<5; i++) {
00157         do {
00158             p = get_touch();
00159         } while (p.x < 0x2000 | p.y < 0x2000);  // wait for touch
00160         a += p.x;
00161         b += p.y;
00162     }
00163     a = a / 5;
00164     b = b / 5;
00165     locate(pos_x,pos_y);
00166     printf("OK         ");
00167     do {
00168         p = get_touch();
00169     } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch
00170 
00171     cls();
00172     line(width() -5, height() - 8,width() - 5,height() -1,White);   // paint cross
00173     line(width() - 8,height() - 5,width() - 1,height() - 5,White);
00174     locate(pos_x,pos_y);
00175     printf("press cross");
00176     locate(pos_x,pos_y + font[2]);
00177     printf("to calibrate");
00178     for (i=0; i<5; i++) {
00179         do {
00180             p  = get_touch();
00181         } while (p.y < 0x2000 | p.x < 0x2000);  // wait for touch
00182         c+= p.x;
00183         d+= p.y;
00184     }
00185     c = c / 5;
00186     d = d / 5;
00187 
00188     locate(pos_x, pos_y);
00189     printf("OK         ");
00190     do {
00191         p = get_touch();
00192     } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch
00193 
00194     cls();
00195 
00196     x_off = a;
00197     y_off = b;
00198 
00199     i = c-a;  // delta x
00200     pp_tx = i / (width() - 6);
00201 
00202     i = d-b;  // delta y
00203     pp_ty = i / (height() - 6);
00204 }
00205 
00206 
00207 point touch_tft::to_pixel(point a_point) {
00208     point p;
00209 
00210     p.x = (a_point.x - x_off) / pp_tx;
00211     if (p.x > width()) p.x = width();
00212     p.y = (a_point.y - y_off) / pp_ty;
00213     if (p.y > height()) p.y = height();
00214     return (p);
00215 }
00216 
00217 bool touch_tft::is_touched(point a) {
00218     if (a.x > threshold & a.y > threshold) return(true);
00219     else return(false);
00220 }