Veikko Kero / Touch_tft

Dependents:   MIDI_Interface_ver_1

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