TS library is for the 4-wire resistive Touch Screen (without an interface) which is usually included on TFT LCD like are MCUFriend displays.
TS.h@1:ca8fdb4d4afc, 2021-01-17 (annotated)
- Committer:
- JohnnyK
- Date:
- Sun Jan 17 15:58:15 2021 +0000
- Revision:
- 1:ca8fdb4d4afc
- Parent:
- 0:f2fda7f69e5a
Corrections - include guards was added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JohnnyK | 0:f2fda7f69e5a | 1 | /* TS library is for the 4-wire resistive Touch Screen (without an interface) |
JohnnyK | 0:f2fda7f69e5a | 2 | * which is usually included on TFT LCD like are MCUFriend displays. |
JohnnyK | 0:f2fda7f69e5a | 3 | * The pins of the Touch Screen are shared with display interface and it is |
JohnnyK | 0:f2fda7f69e5a | 4 | * not possible to read the TouchScreen and write to the display at the "same time". |
JohnnyK | 0:f2fda7f69e5a | 5 | * Also the TouchScreen coordinates must be read one by one and TouchScreen's pins must be |
JohnnyK | 0:f2fda7f69e5a | 6 | * configurted for reading for one Axis (X) and then for second one (Y). |
JohnnyK | 0:f2fda7f69e5a | 7 | * |
JohnnyK | 0:f2fda7f69e5a | 8 | * TS library mabe by Jan Kamidra 28.6.2020 under Mbed and its tools (Mbed Studio online). |
JohnnyK | 0:f2fda7f69e5a | 9 | */ |
JohnnyK | 0:f2fda7f69e5a | 10 | |
JohnnyK | 0:f2fda7f69e5a | 11 | /** Example for testing |
JohnnyK | 0:f2fda7f69e5a | 12 | * @code |
JohnnyK | 0:f2fda7f69e5a | 13 | |
JohnnyK | 0:f2fda7f69e5a | 14 | #include "mbed.h" |
JohnnyK | 0:f2fda7f69e5a | 15 | #include "TS.h" |
JohnnyK | 0:f2fda7f69e5a | 16 | |
JohnnyK | 0:f2fda7f69e5a | 17 | #define YP A3 |
JohnnyK | 0:f2fda7f69e5a | 18 | #define YM D9 |
JohnnyK | 0:f2fda7f69e5a | 19 | #define XM A2 |
JohnnyK | 0:f2fda7f69e5a | 20 | #define XP D8 |
JohnnyK | 0:f2fda7f69e5a | 21 | #define RD A0 |
JohnnyK | 0:f2fda7f69e5a | 22 | |
JohnnyK | 1:ca8fdb4d4afc | 23 | TS_config config = {2955,325,3070,470,240,320}; // that are minimum and maxim mV for all axis |
JohnnyK | 0:f2fda7f69e5a | 24 | TouchScreen ts(YP,YM,XP,XM,config); |
JohnnyK | 0:f2fda7f69e5a | 25 | |
JohnnyK | 0:f2fda7f69e5a | 26 | int main() |
JohnnyK | 0:f2fda7f69e5a | 27 | { |
JohnnyK | 0:f2fda7f69e5a | 28 | printf("Example starting..\n"); |
JohnnyK | 0:f2fda7f69e5a | 29 | DigitalInOut(A0,PIN_OUTPUT,PushPullNoPull,1); //this line is necessary only without display driver |
JohnnyK | 0:f2fda7f69e5a | 30 | DigitalOut led(LED1); |
JohnnyK | 0:f2fda7f69e5a | 31 | TS_point point; |
JohnnyK | 0:f2fda7f69e5a | 32 | //ts.setOrientation(TouchScreen::TS_PORTRAITE); |
JohnnyK | 0:f2fda7f69e5a | 33 | |
JohnnyK | 0:f2fda7f69e5a | 34 | while (true){ |
JohnnyK | 0:f2fda7f69e5a | 35 | if(ts.getPoint(&point)) printf("X: %d, Y: %d\n",point.x,point.y); |
JohnnyK | 0:f2fda7f69e5a | 36 | led = !led; |
JohnnyK | 0:f2fda7f69e5a | 37 | thread_sleep_for(1000); |
JohnnyK | 0:f2fda7f69e5a | 38 | } |
JohnnyK | 0:f2fda7f69e5a | 39 | } |
JohnnyK | 0:f2fda7f69e5a | 40 | * @/code |
JohnnyK | 0:f2fda7f69e5a | 41 | */ |
JohnnyK | 0:f2fda7f69e5a | 42 | |
JohnnyK | 1:ca8fdb4d4afc | 43 | #ifndef TS_H |
JohnnyK | 1:ca8fdb4d4afc | 44 | #define TS_H |
JohnnyK | 1:ca8fdb4d4afc | 45 | |
JohnnyK | 0:f2fda7f69e5a | 46 | #include "TS_porting.h" |
JohnnyK | 0:f2fda7f69e5a | 47 | |
JohnnyK | 0:f2fda7f69e5a | 48 | struct TS_point{ |
JohnnyK | 0:f2fda7f69e5a | 49 | /** Coordinates for XY axis calculated for selected orientation*/ |
JohnnyK | 0:f2fda7f69e5a | 50 | int x = 0, y = 0; |
JohnnyK | 0:f2fda7f69e5a | 51 | |
JohnnyK | 0:f2fda7f69e5a | 52 | /** Coordinates in mV for XY plates of the TouchScreen (orientation is not included)*/ |
JohnnyK | 0:f2fda7f69e5a | 53 | int xV = 0, yV = 0; |
JohnnyK | 0:f2fda7f69e5a | 54 | |
JohnnyK | 0:f2fda7f69e5a | 55 | /** Check if the current point is equal to another point |
JohnnyK | 0:f2fda7f69e5a | 56 | * |
JohnnyK | 0:f2fda7f69e5a | 57 | * @param p The other point being checked for equivalence |
JohnnyK | 0:f2fda7f69e5a | 58 | * @return return true according to whether the points are equal else false |
JohnnyK | 0:f2fda7f69e5a | 59 | */ |
JohnnyK | 0:f2fda7f69e5a | 60 | bool operator==(TS_point); |
JohnnyK | 0:f2fda7f69e5a | 61 | |
JohnnyK | 0:f2fda7f69e5a | 62 | /** Check if the current point is not equal to another point |
JohnnyK | 0:f2fda7f69e5a | 63 | * |
JohnnyK | 0:f2fda7f69e5a | 64 | * @param p The other point being checked for equivalence |
JohnnyK | 0:f2fda7f69e5a | 65 | * @return return false according to whether the points are not equal else true |
JohnnyK | 0:f2fda7f69e5a | 66 | */ |
JohnnyK | 0:f2fda7f69e5a | 67 | bool operator!=(TS_point); |
JohnnyK | 0:f2fda7f69e5a | 68 | }; |
JohnnyK | 0:f2fda7f69e5a | 69 | |
JohnnyK | 0:f2fda7f69e5a | 70 | |
JohnnyK | 1:ca8fdb4d4afc | 71 | |
JohnnyK | 0:f2fda7f69e5a | 72 | struct TS_config{ |
JohnnyK | 1:ca8fdb4d4afc | 73 | unsigned int TS_adc_Xmax, // max value for X in mV |
JohnnyK | 1:ca8fdb4d4afc | 74 | TS_adc_Xmin, // max value for X in mV |
JohnnyK | 1:ca8fdb4d4afc | 75 | TS_adc_Ymax, // max value for Y in mV |
JohnnyK | 1:ca8fdb4d4afc | 76 | TS_adc_Ymin, // min value for Y in mV |
JohnnyK | 1:ca8fdb4d4afc | 77 | TFTWIDTH, // Display width |
JohnnyK | 1:ca8fdb4d4afc | 78 | TFTHEIGHT; // Display height |
JohnnyK | 0:f2fda7f69e5a | 79 | }; |
JohnnyK | 0:f2fda7f69e5a | 80 | |
JohnnyK | 0:f2fda7f69e5a | 81 | |
JohnnyK | 0:f2fda7f69e5a | 82 | class TouchScreen: protected TouchScreenPort { |
JohnnyK | 0:f2fda7f69e5a | 83 | public: |
JohnnyK | 0:f2fda7f69e5a | 84 | enum TS_Orientation{ |
JohnnyK | 0:f2fda7f69e5a | 85 | TS_PORTRAITE = 0, |
JohnnyK | 0:f2fda7f69e5a | 86 | TS_LANDSCAPE = 1, |
JohnnyK | 0:f2fda7f69e5a | 87 | TS_PORTRAITE_REV = 2, |
JohnnyK | 0:f2fda7f69e5a | 88 | TS_LANDSCAPE_REV = 3 |
JohnnyK | 0:f2fda7f69e5a | 89 | }; |
JohnnyK | 0:f2fda7f69e5a | 90 | |
JohnnyK | 0:f2fda7f69e5a | 91 | /** Constructor of a new Touch Screen object |
JohnnyK | 0:f2fda7f69e5a | 92 | * |
JohnnyK | 0:f2fda7f69e5a | 93 | * @param pinYP Y+ pin of Y plate |
JohnnyK | 0:f2fda7f69e5a | 94 | * @param pinYM Y- pin of Y plate |
JohnnyK | 0:f2fda7f69e5a | 95 | * @param pinXP X+ pin of X plate |
JohnnyK | 0:f2fda7f69e5a | 96 | * @param pinXM M- pin of X plate |
JohnnyK | 0:f2fda7f69e5a | 97 | * @param config Necessary configuration via TS_config struct |
JohnnyK | 0:f2fda7f69e5a | 98 | */ |
JohnnyK | 0:f2fda7f69e5a | 99 | TouchScreen(int pinYP,int pinYM, int pinXP, int pinXM, TS_config configuration); |
JohnnyK | 0:f2fda7f69e5a | 100 | |
JohnnyK | 0:f2fda7f69e5a | 101 | /** Set the orientation |
JohnnyK | 0:f2fda7f69e5a | 102 | * |
JohnnyK | 1:ca8fdb4d4afc | 103 | * @param orientation It is set via the TS_Orientation enum |
JohnnyK | 0:f2fda7f69e5a | 104 | * TS_PORTRAITE(default), TS_LANDSCAPE,TS_PORTRAITE_REV and TS_LANDSCAPE_REV |
JohnnyK | 0:f2fda7f69e5a | 105 | */ |
JohnnyK | 0:f2fda7f69e5a | 106 | void setOrientation(TS_Orientation orientation); |
JohnnyK | 0:f2fda7f69e5a | 107 | |
JohnnyK | 0:f2fda7f69e5a | 108 | /** Get coordinates XY via struct TS_point |
JohnnyK | 0:f2fda7f69e5a | 109 | * |
JohnnyK | 0:f2fda7f69e5a | 110 | * @param point pointer to the TS_point where coordinates will be stored. |
JohnnyK | 0:f2fda7f69e5a | 111 | * @return return true if data are valid, else false. |
JohnnyK | 0:f2fda7f69e5a | 112 | * That mean if data are in range of TouchScren plates defined in TS_config. |
JohnnyK | 0:f2fda7f69e5a | 113 | */ |
JohnnyK | 0:f2fda7f69e5a | 114 | bool getPoint(TS_point *point); |
JohnnyK | 0:f2fda7f69e5a | 115 | |
JohnnyK | 0:f2fda7f69e5a | 116 | |
JohnnyK | 0:f2fda7f69e5a | 117 | private: |
JohnnyK | 0:f2fda7f69e5a | 118 | enum Axis{ |
JohnnyK | 0:f2fda7f69e5a | 119 | setX = 0, |
JohnnyK | 0:f2fda7f69e5a | 120 | setY = 1 |
JohnnyK | 0:f2fda7f69e5a | 121 | }; |
JohnnyK | 0:f2fda7f69e5a | 122 | enum Mode{ |
JohnnyK | 0:f2fda7f69e5a | 123 | pInput = 0, |
JohnnyK | 0:f2fda7f69e5a | 124 | pOutput = 1 |
JohnnyK | 0:f2fda7f69e5a | 125 | }; |
JohnnyK | 0:f2fda7f69e5a | 126 | int pYP, pYM, pXP, pXM; |
JohnnyK | 0:f2fda7f69e5a | 127 | TS_config conf; |
JohnnyK | 0:f2fda7f69e5a | 128 | TS_Orientation ts_orient = TS_PORTRAITE; |
JohnnyK | 0:f2fda7f69e5a | 129 | float pxX, pxY; |
JohnnyK | 0:f2fda7f69e5a | 130 | /** Pins setting for measuring on the plates (X/Y) or for releasing pins for further use.*/ |
JohnnyK | 0:f2fda7f69e5a | 131 | int getCoords(Axis axis); |
JohnnyK | 0:f2fda7f69e5a | 132 | /** ADC X value normalization*/ |
JohnnyK | 0:f2fda7f69e5a | 133 | float nrm_adcX(unsigned int x); |
JohnnyK | 0:f2fda7f69e5a | 134 | /** ADC Y value normalization*/ |
JohnnyK | 0:f2fda7f69e5a | 135 | float nrm_adcY(unsigned int y); |
JohnnyK | 0:f2fda7f69e5a | 136 | }; |
JohnnyK | 1:ca8fdb4d4afc | 137 | #endif |