jepjaee
Dependents: MIDI_Interface_ver_1
Fork of Touch_tft by
touch_tft.cpp@4:cef5cdc775ea, 2014-01-16 (annotated)
- Committer:
- Vekotin
- Date:
- Thu Jan 16 13:05:42 2014 +0000
- Revision:
- 4:cef5cdc775ea
- Parent:
- 3:ef67f9068850
16.1.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 0:d78b00f167cb | 1 | /* mbed library for resistive touch pads |
dreschpe | 0:d78b00f167cb | 2 | * uses 4 pins - 2 IO and 2 Analog |
dreschpe | 0:d78b00f167cb | 3 | |
dreschpe | 0:d78b00f167cb | 4 | * c 2011 Peter Drescher - DC2PD |
dreschpe | 0:d78b00f167cb | 5 | * |
dreschpe | 0:d78b00f167cb | 6 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dreschpe | 0:d78b00f167cb | 7 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dreschpe | 0:d78b00f167cb | 8 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dreschpe | 0:d78b00f167cb | 9 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dreschpe | 0:d78b00f167cb | 10 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dreschpe | 0:d78b00f167cb | 11 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dreschpe | 0:d78b00f167cb | 12 | * THE SOFTWARE. |
dreschpe | 0:d78b00f167cb | 13 | */ |
dreschpe | 0:d78b00f167cb | 14 | |
dreschpe | 0:d78b00f167cb | 15 | |
dreschpe | 0:d78b00f167cb | 16 | #include "touch_tft.h" |
dreschpe | 0:d78b00f167cb | 17 | #include "mbed.h" |
dreschpe | 0:d78b00f167cb | 18 | |
dreschpe | 0:d78b00f167cb | 19 | #define threshold 0x2000 // threshold to detect pressed |
dreschpe | 0:d78b00f167cb | 20 | |
dreschpe | 0:d78b00f167cb | 21 | touch_tft::touch_tft(PinName xp, PinName xm, PinName yp, PinName ym, |
Vekotin | 3:ef67f9068850 | 22 | PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name): |
dreschpe | 0:d78b00f167cb | 23 | _xp(xp),_xm(xm),_yp(yp),_ym(ym),_ax(xp),_ay(yp), |
Vekotin | 3:ef67f9068850 | 24 | SPI_TFT_ILI9341(mosi,miso,sclk,cs,reset,dc,name) { |
dreschpe | 0:d78b00f167cb | 25 | xa = xp; |
Vekotin | 3:ef67f9068850 | 26 | ya = yp; } |
dreschpe | 0:d78b00f167cb | 27 | |
dreschpe | 0:d78b00f167cb | 28 | |
dreschpe | 0:d78b00f167cb | 29 | point touch_tft::get_touch() { |
dreschpe | 0:d78b00f167cb | 30 | unsigned short x1 = 0,x2 = 0, y1 = 0, y2 = 0; |
dreschpe | 0:d78b00f167cb | 31 | unsigned int s1 = 0,s2 = 0,d1 , d2; |
dreschpe | 0:d78b00f167cb | 32 | point p; |
dreschpe | 0:d78b00f167cb | 33 | |
dreschpe | 0:d78b00f167cb | 34 | do { |
dreschpe | 0:d78b00f167cb | 35 | // read y voltage |
dreschpe | 0:d78b00f167cb | 36 | _xp.output(); |
dreschpe | 0:d78b00f167cb | 37 | _xm.output(); |
dreschpe | 0:d78b00f167cb | 38 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 39 | case(0): |
dreschpe | 0:d78b00f167cb | 40 | case(3): |
dreschpe | 0:d78b00f167cb | 41 | _xp = 1; |
dreschpe | 0:d78b00f167cb | 42 | _xm = 0; |
dreschpe | 0:d78b00f167cb | 43 | break; |
dreschpe | 0:d78b00f167cb | 44 | case(1): |
dreschpe | 0:d78b00f167cb | 45 | case(2): |
dreschpe | 0:d78b00f167cb | 46 | _xp = 0; |
dreschpe | 0:d78b00f167cb | 47 | _xm = 1; |
dreschpe | 0:d78b00f167cb | 48 | break; |
dreschpe | 0:d78b00f167cb | 49 | } |
dreschpe | 0:d78b00f167cb | 50 | _ym.input(); // y- have to be passive |
dreschpe | 0:d78b00f167cb | 51 | AnalogIn Ay(ya); // we have to call the constructor to switch to analog mode |
dreschpe | 0:d78b00f167cb | 52 | wait_us(10); |
dreschpe | 0:d78b00f167cb | 53 | y1 = Ay.read_u16(); // get y voltage |
dreschpe | 0:d78b00f167cb | 54 | d1 = (y1 > y2)? (y1-y2) : (y2-y1); |
dreschpe | 0:d78b00f167cb | 55 | if (((y1 < 8000) && (d1 < 2000)) || ((y1 > 8000) && (d1 < 150))) s1 ++; |
dreschpe | 0:d78b00f167cb | 56 | else { |
dreschpe | 0:d78b00f167cb | 57 | if (s1 > 0) s1 --; |
dreschpe | 0:d78b00f167cb | 58 | } |
dreschpe | 0:d78b00f167cb | 59 | y2 = y1; |
dreschpe | 0:d78b00f167cb | 60 | // debug |
dreschpe | 0:d78b00f167cb | 61 | //locate(1,7); |
dreschpe | 0:d78b00f167cb | 62 | //printf("d: %4d y: %5d s1: %4d",d1,y1,s1); |
dreschpe | 0:d78b00f167cb | 63 | |
dreschpe | 0:d78b00f167cb | 64 | // read x voltage |
dreschpe | 0:d78b00f167cb | 65 | _yp.output(); |
dreschpe | 0:d78b00f167cb | 66 | _ym.output(); |
dreschpe | 0:d78b00f167cb | 67 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 68 | case(0): |
dreschpe | 0:d78b00f167cb | 69 | case(1): |
dreschpe | 0:d78b00f167cb | 70 | _yp = 1; |
dreschpe | 0:d78b00f167cb | 71 | _ym = 0; |
dreschpe | 0:d78b00f167cb | 72 | break; |
dreschpe | 0:d78b00f167cb | 73 | case(2): |
dreschpe | 0:d78b00f167cb | 74 | case(3): |
dreschpe | 0:d78b00f167cb | 75 | _yp = 0; |
dreschpe | 0:d78b00f167cb | 76 | _ym = 1; |
dreschpe | 0:d78b00f167cb | 77 | break; |
dreschpe | 0:d78b00f167cb | 78 | } |
dreschpe | 0:d78b00f167cb | 79 | _xm.input(); // x- have to be passive |
dreschpe | 0:d78b00f167cb | 80 | AnalogIn Ax(xa); // we have to call the constructor to switch to analog mode |
dreschpe | 0:d78b00f167cb | 81 | wait_us(10); |
dreschpe | 0:d78b00f167cb | 82 | x1 = Ax.read_u16(); // get x voltage |
dreschpe | 0:d78b00f167cb | 83 | d2 = (x1 > x2)? (x1-x2) : (x2-x1); |
dreschpe | 0:d78b00f167cb | 84 | if (((x1 < 8000) && (d2 < 2000)) || ((x1 > 8000) && (d2 < 150))) s2 ++; |
dreschpe | 0:d78b00f167cb | 85 | else { |
dreschpe | 0:d78b00f167cb | 86 | if (s2 > 0) s2 --; |
dreschpe | 0:d78b00f167cb | 87 | } |
dreschpe | 0:d78b00f167cb | 88 | x2 = x1; |
dreschpe | 0:d78b00f167cb | 89 | // debug |
dreschpe | 0:d78b00f167cb | 90 | //locate(1,8); |
dreschpe | 0:d78b00f167cb | 91 | //printf("d: %4d x: %5d s2: %4d",d2,x1,s2); |
dreschpe | 0:d78b00f167cb | 92 | |
dreschpe | 0:d78b00f167cb | 93 | } while (s1 < 3 || s2 < 3); // read until we have three samples close together |
dreschpe | 0:d78b00f167cb | 94 | switch (orientation) { |
dreschpe | 0:d78b00f167cb | 95 | case(0): |
dreschpe | 0:d78b00f167cb | 96 | case(2): |
dreschpe | 0:d78b00f167cb | 97 | p.y = (x1+x2) / 2; // average of two sample |
dreschpe | 0:d78b00f167cb | 98 | p.x = (y1+y2) / 2; |
dreschpe | 0:d78b00f167cb | 99 | break; |
dreschpe | 0:d78b00f167cb | 100 | case(1): |
dreschpe | 0:d78b00f167cb | 101 | case(3): |
dreschpe | 0:d78b00f167cb | 102 | p.x = (x1+x2) / 2; // average of two sample |
dreschpe | 0:d78b00f167cb | 103 | p.y = (y1+y2) / 2; |
dreschpe | 0:d78b00f167cb | 104 | break; |
dreschpe | 0:d78b00f167cb | 105 | } |
dreschpe | 0:d78b00f167cb | 106 | return(p); |
dreschpe | 0:d78b00f167cb | 107 | } |
dreschpe | 0:d78b00f167cb | 108 | |
dreschpe | 0:d78b00f167cb | 109 | void touch_tft::calibrate(void) { |
dreschpe | 0:d78b00f167cb | 110 | int i; |
dreschpe | 0:d78b00f167cb | 111 | int a = 0,b = 0,c = 0, d = 0; |
dreschpe | 2:ef7972c29c0e | 112 | int pos_x, pos_y; |
dreschpe | 0:d78b00f167cb | 113 | point p; |
dreschpe | 0:d78b00f167cb | 114 | |
dreschpe | 0:d78b00f167cb | 115 | cls(); |
dreschpe | 0:d78b00f167cb | 116 | line(0,3,6,3,White); |
dreschpe | 0:d78b00f167cb | 117 | line(3,0,3,6,White); |
dreschpe | 2:ef7972c29c0e | 118 | |
dreschpe | 2:ef7972c29c0e | 119 | // get the center of the screen |
dreschpe | 2:ef7972c29c0e | 120 | pos_x = columns() / 2 - 3; |
dreschpe | 2:ef7972c29c0e | 121 | pos_x = pos_x * font[1]; |
dreschpe | 2:ef7972c29c0e | 122 | pos_y = (rows() / 2) - 1; |
dreschpe | 2:ef7972c29c0e | 123 | pos_y = pos_y * font[2]; |
dreschpe | 2:ef7972c29c0e | 124 | |
dreschpe | 2:ef7972c29c0e | 125 | locate(pos_x,pos_y); |
Vekotin | 4:cef5cdc775ea | 126 | printf("KALIBROI"); |
dreschpe | 2:ef7972c29c0e | 127 | locate(pos_x,pos_y + font[2]); |
Vekotin | 4:cef5cdc775ea | 128 | printf("KALIBROI"); |
dreschpe | 0:d78b00f167cb | 129 | for (i=0; i<5; i++) { |
dreschpe | 0:d78b00f167cb | 130 | do { |
dreschpe | 0:d78b00f167cb | 131 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 132 | } while (p.x < 0x2000 | p.y < 0x2000); // wait for touch |
dreschpe | 0:d78b00f167cb | 133 | a += p.x; |
dreschpe | 0:d78b00f167cb | 134 | b += p.y; |
dreschpe | 0:d78b00f167cb | 135 | } |
dreschpe | 0:d78b00f167cb | 136 | a = a / 5; |
dreschpe | 0:d78b00f167cb | 137 | b = b / 5; |
dreschpe | 2:ef7972c29c0e | 138 | locate(pos_x,pos_y); |
dreschpe | 0:d78b00f167cb | 139 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 140 | do { |
dreschpe | 0:d78b00f167cb | 141 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 142 | } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch |
dreschpe | 0:d78b00f167cb | 143 | |
dreschpe | 0:d78b00f167cb | 144 | cls(); |
dreschpe | 0:d78b00f167cb | 145 | line(width() -5, height() - 8,width() - 5,height() -1,White); // paint cross |
dreschpe | 0:d78b00f167cb | 146 | line(width() - 8,height() - 5,width() - 1,height() - 5,White); |
dreschpe | 2:ef7972c29c0e | 147 | locate(pos_x,pos_y); |
Vekotin | 4:cef5cdc775ea | 148 | printf("KALIBROI"); |
dreschpe | 2:ef7972c29c0e | 149 | locate(pos_x,pos_y + font[2]); |
Vekotin | 4:cef5cdc775ea | 150 | printf("KALIBROI"); |
dreschpe | 0:d78b00f167cb | 151 | for (i=0; i<5; i++) { |
dreschpe | 0:d78b00f167cb | 152 | do { |
dreschpe | 0:d78b00f167cb | 153 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 154 | } while (p.y < 0x2000 | p.x < 0x2000); // wait for touch |
dreschpe | 0:d78b00f167cb | 155 | c+= p.x; |
dreschpe | 0:d78b00f167cb | 156 | d+= p.y; |
dreschpe | 0:d78b00f167cb | 157 | } |
dreschpe | 0:d78b00f167cb | 158 | c = c / 5; |
dreschpe | 0:d78b00f167cb | 159 | d = d / 5; |
dreschpe | 0:d78b00f167cb | 160 | |
dreschpe | 2:ef7972c29c0e | 161 | locate(pos_x, pos_y); |
dreschpe | 0:d78b00f167cb | 162 | printf("OK "); |
dreschpe | 0:d78b00f167cb | 163 | do { |
dreschpe | 0:d78b00f167cb | 164 | p = get_touch(); |
dreschpe | 0:d78b00f167cb | 165 | } while (p.y > 0x2000 | p.x > 0x2000); // wait for no touch |
dreschpe | 0:d78b00f167cb | 166 | |
dreschpe | 0:d78b00f167cb | 167 | cls(); |
dreschpe | 0:d78b00f167cb | 168 | |
dreschpe | 0:d78b00f167cb | 169 | x_off = a; |
dreschpe | 0:d78b00f167cb | 170 | y_off = b; |
dreschpe | 0:d78b00f167cb | 171 | |
dreschpe | 0:d78b00f167cb | 172 | i = c-a; // delta x |
dreschpe | 0:d78b00f167cb | 173 | pp_tx = i / (width() - 6); |
dreschpe | 0:d78b00f167cb | 174 | |
dreschpe | 0:d78b00f167cb | 175 | i = d-b; // delta y |
dreschpe | 0:d78b00f167cb | 176 | pp_ty = i / (height() - 6); |
dreschpe | 0:d78b00f167cb | 177 | } |
dreschpe | 0:d78b00f167cb | 178 | |
dreschpe | 0:d78b00f167cb | 179 | |
dreschpe | 0:d78b00f167cb | 180 | point touch_tft::to_pixel(point a_point) { |
dreschpe | 0:d78b00f167cb | 181 | point p; |
dreschpe | 0:d78b00f167cb | 182 | |
dreschpe | 0:d78b00f167cb | 183 | p.x = (a_point.x - x_off) / pp_tx; |
dreschpe | 2:ef7972c29c0e | 184 | if (p.x > width()) p.x = width(); |
dreschpe | 0:d78b00f167cb | 185 | p.y = (a_point.y - y_off) / pp_ty; |
dreschpe | 2:ef7972c29c0e | 186 | if (p.y > height()) p.y = height(); |
dreschpe | 0:d78b00f167cb | 187 | return (p); |
dreschpe | 0:d78b00f167cb | 188 | } |
dreschpe | 0:d78b00f167cb | 189 | |
dreschpe | 2:ef7972c29c0e | 190 | bool touch_tft::is_touched(point a) { |
dreschpe | 2:ef7972c29c0e | 191 | if (a.x > threshold & a.y > threshold) return(true); |
dreschpe | 2:ef7972c29c0e | 192 | else return(false); |
dreschpe | 0:d78b00f167cb | 193 | } |