jepjaee
Dependents: MIDI_Interface_ver_1
Fork of Touch_tft by
touch_tft.h@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 | 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" |
Vekotin | 3:ef67f9068850 | 18 | #include "SPI_TFT_ILI9341.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 | }; |
Vekotin | 4:cef5cdc775ea | 24 | |
dreschpe | 1:1745fdf054b5 | 25 | /** touchscreen control class, based on SPI_TFT |
dreschpe | 1:1745fdf054b5 | 26 | * |
dreschpe | 1:1745fdf054b5 | 27 | * Example: |
dreschpe | 1:1745fdf054b5 | 28 | * @code |
dreschpe | 1:1745fdf054b5 | 29 | * |
dreschpe | 1:1745fdf054b5 | 30 | * #include "mbed.h" |
dreschpe | 1:1745fdf054b5 | 31 | * #include "SPI_TFT.h" |
dreschpe | 1:1745fdf054b5 | 32 | * #include "Arial12x12.h" |
dreschpe | 1:1745fdf054b5 | 33 | * #include "Arial28x28.h" |
dreschpe | 1:1745fdf054b5 | 34 | * #include "touch_tft.h" |
dreschpe | 1:1745fdf054b5 | 35 | * // the TFT is connected to SPI pin 5-7 |
dreschpe | 1:1745fdf054b5 | 36 | * // the touch is connected to 19,20,16,17 |
dreschpe | 1:1745fdf054b5 | 37 | * |
dreschpe | 1:1745fdf054b5 | 38 | * 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 | 39 | * |
dreschpe | 1:1745fdf054b5 | 40 | * int main() { |
dreschpe | 1:1745fdf054b5 | 41 | * point p; |
dreschpe | 1:1745fdf054b5 | 42 | * |
dreschpe | 1:1745fdf054b5 | 43 | * tt.claim(stdout); // send stdout to the TFT display |
dreschpe | 1:1745fdf054b5 | 44 | * tt.background(Black); // set background to black |
dreschpe | 1:1745fdf054b5 | 45 | * tt.foreground(White); // set chars to white |
dreschpe | 1:1745fdf054b5 | 46 | * tt.cls(); // clear the screen |
dreschpe | 1:1745fdf054b5 | 47 | * tt.set_font((unsigned char*) Arial12x12); // select the font |
dreschpe | 1:1745fdf054b5 | 48 | * tt.set_orientation(1); |
dreschpe | 1:1745fdf054b5 | 49 | * |
dreschpe | 1:1745fdf054b5 | 50 | * tt.calibrate(); // calibrate the touch |
dreschpe | 1:1745fdf054b5 | 51 | * while (1) { |
dreschpe | 1:1745fdf054b5 | 52 | * p = tt.get_touch(); // read analog pos. |
dreschpe | 1:1745fdf054b5 | 53 | * if (tt.is_touched(p)) { // test if touched |
dreschpe | 1:1745fdf054b5 | 54 | * p = tt.to_pixel(p); // convert to pixel pos |
dreschpe | 1:1745fdf054b5 | 55 | * tt.fillcircle(p.x,p.y,3,Blue); // print a blue dot on the screen |
dreschpe | 1:1745fdf054b5 | 56 | * } |
dreschpe | 1:1745fdf054b5 | 57 | * } |
dreschpe | 1:1745fdf054b5 | 58 | * @endcode |
dreschpe | 1:1745fdf054b5 | 59 | */ |
Vekotin | 3:ef67f9068850 | 60 | class touch_tft : public SPI_TFT_ILI9341{ |
dreschpe | 0:d78b00f167cb | 61 | public: |
dreschpe | 1:1745fdf054b5 | 62 | /** create a TFT with touch object connected to the pins: |
dreschpe | 0:d78b00f167cb | 63 | * |
dreschpe | 0:d78b00f167cb | 64 | * @param pin xp resistiv touch x+ |
dreschpe | 0:d78b00f167cb | 65 | * @param pin xm resistiv touch x- |
dreschpe | 0:d78b00f167cb | 66 | * @param pin yp resistiv touch y+ |
dreschpe | 0:d78b00f167cb | 67 | * @param pin ym resistiv touch y- |
dreschpe | 0:d78b00f167cb | 68 | * @param mosi,miso,sclk SPI connection to TFT |
dreschpe | 0:d78b00f167cb | 69 | * @param cs pin connected to CS of display |
dreschpe | 0:d78b00f167cb | 70 | * @param reset pin connected to RESET of display |
dreschpe | 0:d78b00f167cb | 71 | * based on my SPI_TFT lib |
dreschpe | 0:d78b00f167cb | 72 | */ |
Vekotin | 3:ef67f9068850 | 73 | touch_tft(PinName xp, PinName xm, PinName yp, PinName ym,PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, PinName dc, const char* name ="TFT"); |
dreschpe | 0:d78b00f167cb | 74 | |
dreschpe | 0:d78b00f167cb | 75 | /** calibrate the touch display |
dreschpe | 0:d78b00f167cb | 76 | * |
dreschpe | 1:1745fdf054b5 | 77 | * User is asked to touch on two points on the screen |
dreschpe | 0:d78b00f167cb | 78 | */ |
dreschpe | 0:d78b00f167cb | 79 | void calibrate(void); |
dreschpe | 0:d78b00f167cb | 80 | |
dreschpe | 0:d78b00f167cb | 81 | /** read x and y analog samples |
dreschpe | 0:d78b00f167cb | 82 | * |
dreschpe | 0:d78b00f167cb | 83 | * @returns point(x,y) |
dreschpe | 0:d78b00f167cb | 84 | */ |
dreschpe | 0:d78b00f167cb | 85 | point get_touch(void); |
dreschpe | 0:d78b00f167cb | 86 | |
dreschpe | 0:d78b00f167cb | 87 | /** calculate coord on screen |
dreschpe | 0:d78b00f167cb | 88 | * |
dreschpe | 0:d78b00f167cb | 89 | * @param a_point point(analog x, analog y) |
dreschpe | 0:d78b00f167cb | 90 | * @returns point(pixel x, pixel y) |
dreschpe | 0:d78b00f167cb | 91 | * |
dreschpe | 0:d78b00f167cb | 92 | */ |
dreschpe | 0:d78b00f167cb | 93 | point to_pixel(point a_point); |
dreschpe | 0:d78b00f167cb | 94 | |
dreschpe | 0:d78b00f167cb | 95 | /** test if screen is touched |
dreschpe | 0:d78b00f167cb | 96 | * |
dreschpe | 0:d78b00f167cb | 97 | * @param point analog x,y |
dreschpe | 0:d78b00f167cb | 98 | * @returns true is touched |
dreschpe | 0:d78b00f167cb | 99 | * |
dreschpe | 0:d78b00f167cb | 100 | */ |
dreschpe | 0:d78b00f167cb | 101 | bool is_touched(point a); |
dreschpe | 0:d78b00f167cb | 102 | |
dreschpe | 0:d78b00f167cb | 103 | protected: |
dreschpe | 0:d78b00f167cb | 104 | DigitalInOut _xp; |
dreschpe | 0:d78b00f167cb | 105 | DigitalInOut _xm; |
dreschpe | 0:d78b00f167cb | 106 | DigitalInOut _yp; |
dreschpe | 0:d78b00f167cb | 107 | DigitalInOut _ym; |
dreschpe | 0:d78b00f167cb | 108 | AnalogIn _ax; |
dreschpe | 0:d78b00f167cb | 109 | AnalogIn _ay; |
dreschpe | 0:d78b00f167cb | 110 | PinName xa; |
dreschpe | 0:d78b00f167cb | 111 | PinName ya; |
dreschpe | 0:d78b00f167cb | 112 | |
dreschpe | 0:d78b00f167cb | 113 | |
dreschpe | 0:d78b00f167cb | 114 | unsigned short x_a,y_a; |
dreschpe | 0:d78b00f167cb | 115 | unsigned short x_off,y_off; |
dreschpe | 0:d78b00f167cb | 116 | unsigned short pp_tx,pp_ty; |
dreschpe | 0:d78b00f167cb | 117 | |
dreschpe | 0:d78b00f167cb | 118 | |
dreschpe | 0:d78b00f167cb | 119 | |
dreschpe | 0:d78b00f167cb | 120 | }; |
dreschpe | 0:d78b00f167cb | 121 | |
dreschpe | 0:d78b00f167cb | 122 | #endif |