LIB for resistiv touchscreen connected to 4 mbed pins Use SPI_TFT lib
Fork of Touch_tft by
touch_tft.h@4:330f2f16dabb, 2013-06-26 (annotated)
- Committer:
- leihen
- Date:
- Wed Jun 26 21:55:37 2013 +0000
- Revision:
- 4:330f2f16dabb
- Parent:
- 3:1c2df48aa2eb
No Change
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dreschpe | 1:1745fdf054b5 | 1 | /* mbed library for touchscreen connected to 4 mbed pins |
dreschpe | 1:1745fdf054b5 | 2 | * derive from SPI_TFT lib |
dreschpe | 1:1745fdf054b5 | 3 | * Copyright (c) 2011 Peter Drescher - DC2PD |
dreschpe | 1:1745fdf054b5 | 4 | * |
dreschpe | 1:1745fdf054b5 | 5 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
dreschpe | 1:1745fdf054b5 | 6 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
dreschpe | 1:1745fdf054b5 | 7 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
dreschpe | 1:1745fdf054b5 | 8 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
dreschpe | 1:1745fdf054b5 | 9 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
dreschpe | 1:1745fdf054b5 | 10 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
dreschpe | 1:1745fdf054b5 | 11 | * THE SOFTWARE. |
dreschpe | 1:1745fdf054b5 | 12 | */ |
dreschpe | 1:1745fdf054b5 | 13 | |
dreschpe | 0:d78b00f167cb | 14 | #ifndef MBED_TOUCH_H |
dreschpe | 0:d78b00f167cb | 15 | #define MBED_TOUCH_H |
dreschpe | 0:d78b00f167cb | 16 | |
dreschpe | 0:d78b00f167cb | 17 | #include "mbed.h" |
dreschpe | 0:d78b00f167cb | 18 | #include "SPI_TFT.h" |
dreschpe | 0:d78b00f167cb | 19 | |
dreschpe | 0:d78b00f167cb | 20 | struct point{ |
dreschpe | 0:d78b00f167cb | 21 | unsigned short x; |
dreschpe | 0:d78b00f167cb | 22 | unsigned short y; |
dreschpe | 0:d78b00f167cb | 23 | }; |
dreschpe | 0:d78b00f167cb | 24 | |
dreschpe | 1:1745fdf054b5 | 25 | |
dreschpe | 1:1745fdf054b5 | 26 | /** touchscreen control class, based on SPI_TFT |
dreschpe | 1:1745fdf054b5 | 27 | * |
dreschpe | 1:1745fdf054b5 | 28 | * Example: |
dreschpe | 1:1745fdf054b5 | 29 | * @code |
dreschpe | 1:1745fdf054b5 | 30 | * |
dreschpe | 1:1745fdf054b5 | 31 | * #include "mbed.h" |
dreschpe | 1:1745fdf054b5 | 32 | * #include "SPI_TFT.h" |
dreschpe | 1:1745fdf054b5 | 33 | * #include "Arial12x12.h" |
dreschpe | 1:1745fdf054b5 | 34 | * #include "Arial28x28.h" |
dreschpe | 1:1745fdf054b5 | 35 | * #include "touch_tft.h" |
dreschpe | 1:1745fdf054b5 | 36 | * // the TFT is connected to SPI pin 5-7 |
dreschpe | 1:1745fdf054b5 | 37 | * // the touch is connected to 19,20,16,17 |
dreschpe | 1:1745fdf054b5 | 38 | * |
dreschpe | 1:1745fdf054b5 | 39 | * touch_tft tt(p19,p20,p16,p17,p5, p6, p7, p8, p15,"TFT"); // x+,x-,y+,y-,mosi, miso, sclk, cs, reset |
dreschpe | 1:1745fdf054b5 | 40 | * |
dreschpe | 1:1745fdf054b5 | 41 | * int main() { |
dreschpe | 1:1745fdf054b5 | 42 | * point p; |
dreschpe | 1:1745fdf054b5 | 43 | * |
dreschpe | 1:1745fdf054b5 | 44 | * tt.claim(stdout); // send stdout to the TFT display |
dreschpe | 1:1745fdf054b5 | 45 | * tt.background(Black); // set background to black |
dreschpe | 1:1745fdf054b5 | 46 | * tt.foreground(White); // set chars to white |
dreschpe | 1:1745fdf054b5 | 47 | * tt.cls(); // clear the screen |
dreschpe | 1:1745fdf054b5 | 48 | * tt.set_font((unsigned char*) Arial12x12); // select the font |
dreschpe | 1:1745fdf054b5 | 49 | * tt.set_orientation(1); |
dreschpe | 1:1745fdf054b5 | 50 | * |
dreschpe | 1:1745fdf054b5 | 51 | * tt.calibrate(); // calibrate the touch |
dreschpe | 1:1745fdf054b5 | 52 | * while (1) { |
dreschpe | 1:1745fdf054b5 | 53 | * p = tt.get_touch(); // read analog pos. |
dreschpe | 1:1745fdf054b5 | 54 | * if (tt.is_touched(p)) { // test if touched |
dreschpe | 1:1745fdf054b5 | 55 | * p = tt.to_pixel(p); // convert to pixel pos |
dreschpe | 1:1745fdf054b5 | 56 | * tt.fillcircle(p.x,p.y,3,Blue); // print a blue dot on the screen |
dreschpe | 1:1745fdf054b5 | 57 | * } |
dreschpe | 1:1745fdf054b5 | 58 | * } |
dreschpe | 1:1745fdf054b5 | 59 | * @endcode |
dreschpe | 1:1745fdf054b5 | 60 | */ |
dreschpe | 0:d78b00f167cb | 61 | class touch_tft : public SPI_TFT{ |
dreschpe | 0:d78b00f167cb | 62 | public: |
dreschpe | 1:1745fdf054b5 | 63 | /** create a TFT with touch object connected to the pins: |
dreschpe | 0:d78b00f167cb | 64 | * |
dreschpe | 0:d78b00f167cb | 65 | * @param pin xp resistiv touch x+ |
dreschpe | 0:d78b00f167cb | 66 | * @param pin xm resistiv touch x- |
dreschpe | 0:d78b00f167cb | 67 | * @param pin yp resistiv touch y+ |
dreschpe | 0:d78b00f167cb | 68 | * @param pin ym resistiv touch y- |
dreschpe | 0:d78b00f167cb | 69 | * @param mosi,miso,sclk SPI connection to TFT |
dreschpe | 0:d78b00f167cb | 70 | * @param cs pin connected to CS of display |
dreschpe | 0:d78b00f167cb | 71 | * @param reset pin connected to RESET of display |
dreschpe | 0:d78b00f167cb | 72 | * based on my SPI_TFT lib |
dreschpe | 0:d78b00f167cb | 73 | */ |
dreschpe | 0:d78b00f167cb | 74 | touch_tft(PinName xp, PinName xm, PinName yp, PinName ym,PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset,const char* name ="TFT"); |
dreschpe | 0:d78b00f167cb | 75 | |
dreschpe | 0:d78b00f167cb | 76 | /** calibrate the touch display |
dreschpe | 0:d78b00f167cb | 77 | * |
dreschpe | 1:1745fdf054b5 | 78 | * User is asked to touch on two points on the screen |
dreschpe | 0:d78b00f167cb | 79 | */ |
dreschpe | 0:d78b00f167cb | 80 | void calibrate(void); |
dreschpe | 0:d78b00f167cb | 81 | |
dreschpe | 0:d78b00f167cb | 82 | /** read x and y analog samples |
dreschpe | 0:d78b00f167cb | 83 | * |
dreschpe | 0:d78b00f167cb | 84 | * @returns point(x,y) |
dreschpe | 0:d78b00f167cb | 85 | */ |
dreschpe | 0:d78b00f167cb | 86 | point get_touch(void); |
dreschpe | 0:d78b00f167cb | 87 | |
dreschpe | 0:d78b00f167cb | 88 | /** calculate coord on screen |
dreschpe | 0:d78b00f167cb | 89 | * |
dreschpe | 0:d78b00f167cb | 90 | * @param a_point point(analog x, analog y) |
dreschpe | 0:d78b00f167cb | 91 | * @returns point(pixel x, pixel y) |
dreschpe | 0:d78b00f167cb | 92 | * |
dreschpe | 0:d78b00f167cb | 93 | */ |
dreschpe | 0:d78b00f167cb | 94 | point to_pixel(point a_point); |
dreschpe | 0:d78b00f167cb | 95 | |
dreschpe | 0:d78b00f167cb | 96 | /** test if screen is touched |
dreschpe | 0:d78b00f167cb | 97 | * |
dreschpe | 0:d78b00f167cb | 98 | * @param point analog x,y |
dreschpe | 0:d78b00f167cb | 99 | * @returns true is touched |
dreschpe | 0:d78b00f167cb | 100 | * |
dreschpe | 0:d78b00f167cb | 101 | */ |
dreschpe | 0:d78b00f167cb | 102 | bool is_touched(point a); |
dreschpe | 0:d78b00f167cb | 103 | |
leihen | 3:1c2df48aa2eb | 104 | void printf(const char* txt) { while(*txt) { _putc(*txt++); } } |
leihen | 3:1c2df48aa2eb | 105 | |
dreschpe | 0:d78b00f167cb | 106 | protected: |
dreschpe | 0:d78b00f167cb | 107 | DigitalInOut _xp; |
dreschpe | 0:d78b00f167cb | 108 | DigitalInOut _xm; |
dreschpe | 0:d78b00f167cb | 109 | DigitalInOut _yp; |
dreschpe | 0:d78b00f167cb | 110 | DigitalInOut _ym; |
dreschpe | 0:d78b00f167cb | 111 | AnalogIn _ax; |
dreschpe | 0:d78b00f167cb | 112 | AnalogIn _ay; |
leihen | 3:1c2df48aa2eb | 113 | PinName xa, xb; |
leihen | 3:1c2df48aa2eb | 114 | PinName ya, yb; |
dreschpe | 0:d78b00f167cb | 115 | |
dreschpe | 0:d78b00f167cb | 116 | |
dreschpe | 0:d78b00f167cb | 117 | unsigned short x_a,y_a; |
dreschpe | 0:d78b00f167cb | 118 | unsigned short x_off,y_off; |
dreschpe | 0:d78b00f167cb | 119 | unsigned short pp_tx,pp_ty; |
dreschpe | 0:d78b00f167cb | 120 | |
dreschpe | 0:d78b00f167cb | 121 | |
dreschpe | 0:d78b00f167cb | 122 | |
dreschpe | 0:d78b00f167cb | 123 | }; |
dreschpe | 0:d78b00f167cb | 124 | |
dreschpe | 0:d78b00f167cb | 125 | #endif |