jyghj
Dependencies: Images SeeedStudioTFTv2 TFT_fonts inclinometer mbed
Fork of Seeed_TFT_Touch_Shield by
main.cpp@5:d7868e4dc7d1, 2018-04-17 (annotated)
- Committer:
- lindaako
- Date:
- Tue Apr 17 09:25:03 2018 +0000
- Revision:
- 5:d7868e4dc7d1
- Parent:
- 2:5c2f6ff36ff1
ll
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
lawliet | 1:b2794eb7c940 | 1 | /* |
lawliet | 1:b2794eb7c940 | 2 | main.cpp |
lawliet | 1:b2794eb7c940 | 3 | 2014 Copyright (c) Seeed Technology Inc. All right reserved. |
lawliet | 1:b2794eb7c940 | 4 | |
lawliet | 1:b2794eb7c940 | 5 | Author:lawliet zou(lawliet.zou@gmail.com) |
lawliet | 1:b2794eb7c940 | 6 | 2014-02-17 |
lawliet | 1:b2794eb7c940 | 7 | |
lawliet | 1:b2794eb7c940 | 8 | This library is free software; you can redistribute it and/or |
lawliet | 1:b2794eb7c940 | 9 | modify it under the terms of the GNU Lesser General Public |
lawliet | 1:b2794eb7c940 | 10 | License as published by the Free Software Foundation; either |
lawliet | 1:b2794eb7c940 | 11 | version 2.1 of the License, or (at your option) any later version. |
lawliet | 1:b2794eb7c940 | 12 | |
lawliet | 1:b2794eb7c940 | 13 | This library is distributed in the hope that it will be useful, |
lawliet | 1:b2794eb7c940 | 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
lawliet | 1:b2794eb7c940 | 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
lawliet | 1:b2794eb7c940 | 16 | Lesser General Public License for more details. |
lawliet | 1:b2794eb7c940 | 17 | |
lawliet | 1:b2794eb7c940 | 18 | You should have received a copy of the GNU Lesser General Public |
lawliet | 1:b2794eb7c940 | 19 | License along with this library; if not, write to the Free Software |
lawliet | 1:b2794eb7c940 | 20 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
lawliet | 1:b2794eb7c940 | 21 | */ |
lawliet | 1:b2794eb7c940 | 22 | |
neilt6 | 0:407dafce805d | 23 | #include "mbed.h" |
neilt6 | 0:407dafce805d | 24 | #include "SeeedStudioTFTv2.h" |
neilt6 | 0:407dafce805d | 25 | #include "Arial12x12.h" |
neilt6 | 0:407dafce805d | 26 | #include "Arial24x23.h" |
neilt6 | 0:407dafce805d | 27 | #include "Arial28x28.h" |
neilt6 | 0:407dafce805d | 28 | #include "font_big.h" |
lindaako | 5:d7868e4dc7d1 | 29 | #include "car.h" |
lindaako | 5:d7868e4dc7d1 | 30 | #include "DFRobot_LIS2DH12.h" |
neilt6 | 0:407dafce805d | 31 | |
lindaako | 5:d7868e4dc7d1 | 32 | #define PIN_XP p20 |
lindaako | 5:d7868e4dc7d1 | 33 | #define PIN_XM p19 |
lindaako | 5:d7868e4dc7d1 | 34 | #define PIN_YP p18 |
lindaako | 5:d7868e4dc7d1 | 35 | #define PIN_YM p17 |
lindaako | 5:d7868e4dc7d1 | 36 | #define PIN_MOSI p11 |
lindaako | 5:d7868e4dc7d1 | 37 | #define PIN_MISO p12 |
lindaako | 5:d7868e4dc7d1 | 38 | #define PIN_SCLK p13 |
lindaako | 5:d7868e4dc7d1 | 39 | #define PIN_CS_TFT p14 |
lindaako | 5:d7868e4dc7d1 | 40 | #define PIN_DC_TFT p21 |
lindaako | 5:d7868e4dc7d1 | 41 | #define PIN_BL_TFT p15 |
lindaako | 5:d7868e4dc7d1 | 42 | //#define PIN_CS_SD D4 |
lindaako | 5:d7868e4dc7d1 | 43 | |
lindaako | 5:d7868e4dc7d1 | 44 | SeeedStudioTFTv2 TFT(PIN_XP, PIN_XM, PIN_YP, PIN_YM, PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_DC_TFT, PIN_BL_TFT); |
lindaako | 5:d7868e4dc7d1 | 45 | |
lindaako | 5:d7868e4dc7d1 | 46 | // Serial connection between PC and MBED |
lindaako | 5:d7868e4dc7d1 | 47 | Serial pc(USBTX, USBRX); |
lindaako | 5:d7868e4dc7d1 | 48 | I2C i2c(p9, p10); |
lindaako | 5:d7868e4dc7d1 | 49 | DFRobot_LIS2DH12 LIS(&i2c,&pc); |
lindaako | 5:d7868e4dc7d1 | 50 | |
lindaako | 5:d7868e4dc7d1 | 51 | point p; // creates pointer p.x,p.y |
lindaako | 5:d7868e4dc7d1 | 52 | |
lindaako | 5:d7868e4dc7d1 | 53 | |
lindaako | 5:d7868e4dc7d1 | 54 | // sub-program |
lindaako | 5:d7868e4dc7d1 | 55 | // this program will wait until user touches screen |
lindaako | 5:d7868e4dc7d1 | 56 | // analog data of touch panel is converted trough ADC to digital value |
lindaako | 5:d7868e4dc7d1 | 57 | // getTouch will store that value |
lindaako | 5:d7868e4dc7d1 | 58 | //getPixel will then convert ADC value to 320*240 pixel coordinates |
lindaako | 5:d7868e4dc7d1 | 59 | // this touch program will be used in every code at this example |
lindaako | 5:d7868e4dc7d1 | 60 | |
lindaako | 5:d7868e4dc7d1 | 61 | void touch() |
lindaako | 5:d7868e4dc7d1 | 62 | { |
lindaako | 5:d7868e4dc7d1 | 63 | while(1) |
lindaako | 5:d7868e4dc7d1 | 64 | { |
lindaako | 5:d7868e4dc7d1 | 65 | p.x=0;p.y=0; |
lindaako | 5:d7868e4dc7d1 | 66 | if (TFT.getTouch(p)==TFT.YES) // read analog pos. |
lindaako | 5:d7868e4dc7d1 | 67 | { |
lindaako | 5:d7868e4dc7d1 | 68 | TFT.getTouch(p); |
lindaako | 5:d7868e4dc7d1 | 69 | TFT.getPixel(p); // convert to pixel pos |
lindaako | 5:d7868e4dc7d1 | 70 | pc.printf("\n\r Point x:%d, y:%d", p.x, p.y); // will print p.x and p.y valu to pc(putty/teraterm) |
lindaako | 5:d7868e4dc7d1 | 71 | wait_ms(150); // give some time for the touch |
lindaako | 5:d7868e4dc7d1 | 72 | break; |
lindaako | 5:d7868e4dc7d1 | 73 | } |
lindaako | 5:d7868e4dc7d1 | 74 | } |
lindaako | 5:d7868e4dc7d1 | 75 | } |
lawliet | 1:b2794eb7c940 | 76 | |
lindaako | 5:d7868e4dc7d1 | 77 | void DFRobot_LIS2DH12::getIncline(int16_t &a, int16_t &b, int16_t &c) |
lindaako | 5:d7868e4dc7d1 | 78 | { |
lindaako | 5:d7868e4dc7d1 | 79 | if((c > 900) && (c < 1100)&&(a > -250) && (a < 250)&&(b > -250) && (b < 250)) |
lindaako | 5:d7868e4dc7d1 | 80 | { |
lindaako | 5:d7868e4dc7d1 | 81 | TFT.printf("The sensor is level."); |
lindaako | 5:d7868e4dc7d1 | 82 | } |
lindaako | 5:d7868e4dc7d1 | 83 | else |
lindaako | 5:d7868e4dc7d1 | 84 | { |
lindaako | 5:d7868e4dc7d1 | 85 | TFT.printf("The sensor is tilted "); |
lindaako | 5:d7868e4dc7d1 | 86 | if(a > 250) |
lindaako | 5:d7868e4dc7d1 | 87 | { |
lindaako | 5:d7868e4dc7d1 | 88 | TFT.printf("left"); |
lindaako | 5:d7868e4dc7d1 | 89 | if(b > 250) |
lindaako | 5:d7868e4dc7d1 | 90 | { |
lindaako | 5:d7868e4dc7d1 | 91 | TFT.printf(" and backward."); |
lindaako | 5:d7868e4dc7d1 | 92 | } |
lindaako | 5:d7868e4dc7d1 | 93 | else if(b < -250) |
lindaako | 5:d7868e4dc7d1 | 94 | { |
lindaako | 5:d7868e4dc7d1 | 95 | TFT.printf(" and forward."); |
lindaako | 5:d7868e4dc7d1 | 96 | } |
lindaako | 5:d7868e4dc7d1 | 97 | else |
lindaako | 5:d7868e4dc7d1 | 98 | { |
lindaako | 5:d7868e4dc7d1 | 99 | TFT.printf("."); |
lindaako | 5:d7868e4dc7d1 | 100 | } |
lindaako | 5:d7868e4dc7d1 | 101 | } |
lindaako | 5:d7868e4dc7d1 | 102 | else if(a < -250) |
lindaako | 5:d7868e4dc7d1 | 103 | { |
lindaako | 5:d7868e4dc7d1 | 104 | TFT.printf("right"); |
lindaako | 5:d7868e4dc7d1 | 105 | if(b > 250) |
lindaako | 5:d7868e4dc7d1 | 106 | { |
lindaako | 5:d7868e4dc7d1 | 107 | TFT.printf(" and backward."); |
lindaako | 5:d7868e4dc7d1 | 108 | } |
lindaako | 5:d7868e4dc7d1 | 109 | else if(b < -250) |
lindaako | 5:d7868e4dc7d1 | 110 | { |
lindaako | 5:d7868e4dc7d1 | 111 | TFT.printf(" and forward."); |
lindaako | 5:d7868e4dc7d1 | 112 | } |
lindaako | 5:d7868e4dc7d1 | 113 | else |
lindaako | 5:d7868e4dc7d1 | 114 | { |
lindaako | 5:d7868e4dc7d1 | 115 | TFT.printf("."); |
lindaako | 5:d7868e4dc7d1 | 116 | } |
lindaako | 5:d7868e4dc7d1 | 117 | } |
lindaako | 5:d7868e4dc7d1 | 118 | else |
lindaako | 5:d7868e4dc7d1 | 119 | { |
lindaako | 5:d7868e4dc7d1 | 120 | if(b > 250) |
lindaako | 5:d7868e4dc7d1 | 121 | { |
lindaako | 5:d7868e4dc7d1 | 122 | TFT.printf("backward."); |
lindaako | 5:d7868e4dc7d1 | 123 | } |
lindaako | 5:d7868e4dc7d1 | 124 | else if(b < -250) |
lindaako | 5:d7868e4dc7d1 | 125 | { |
lindaako | 5:d7868e4dc7d1 | 126 | TFT.printf("forward."); |
lindaako | 5:d7868e4dc7d1 | 127 | } |
lindaako | 5:d7868e4dc7d1 | 128 | else |
lindaako | 5:d7868e4dc7d1 | 129 | { |
lindaako | 5:d7868e4dc7d1 | 130 | TFT.printf("upside down."); |
lindaako | 5:d7868e4dc7d1 | 131 | } |
lindaako | 5:d7868e4dc7d1 | 132 | } |
lindaako | 5:d7868e4dc7d1 | 133 | } |
lindaako | 5:d7868e4dc7d1 | 134 | TFT.printf("\n\r"); |
lindaako | 5:d7868e4dc7d1 | 135 | } |
neilt6 | 0:407dafce805d | 136 | |
neilt6 | 0:407dafce805d | 137 | int main() |
neilt6 | 0:407dafce805d | 138 | { |
lindaako | 5:d7868e4dc7d1 | 139 | while(1) |
lindaako | 5:d7868e4dc7d1 | 140 | { |
lindaako | 5:d7868e4dc7d1 | 141 | |
lindaako | 5:d7868e4dc7d1 | 142 | TFT.locate(0,0); // Set pixel location at screen |
lindaako | 5:d7868e4dc7d1 | 143 | int counter=0; |
lindaako | 5:d7868e4dc7d1 | 144 | //TFT initialize |
lindaako | 5:d7868e4dc7d1 | 145 | TFT.claim(stdout); // send stdout to the TFT display |
lindaako | 5:d7868e4dc7d1 | 146 | TFT.set_orientation(0); // set screen orientation to vertical |
lindaako | 5:d7868e4dc7d1 | 147 | // TFT.set_orientation(1); // set screen orientation to horizontal |
lindaako | 5:d7868e4dc7d1 | 148 | TFT.background(Black); // set background to black |
lindaako | 5:d7868e4dc7d1 | 149 | TFT.foreground(White); // set chars to white |
lindaako | 5:d7868e4dc7d1 | 150 | TFT.cls(); // clear the screen |
lindaako | 5:d7868e4dc7d1 | 151 | TFT.set_font((unsigned char*) Arial12x12); // set used txt font |
lindaako | 5:d7868e4dc7d1 | 152 | |
lindaako | 5:d7868e4dc7d1 | 153 | while(LIS.init(LIS2DH12_RANGE_8GA) != 0) |
lindaako | 5:d7868e4dc7d1 | 154 | { |
lindaako | 5:d7868e4dc7d1 | 155 | TFT.printf("No I2C devices found\n\r"); |
lindaako | 5:d7868e4dc7d1 | 156 | wait(1); |
lindaako | 5:d7868e4dc7d1 | 157 | } |
lindaako | 5:d7868e4dc7d1 | 158 | int16_t a, b, c; |
lindaako | 5:d7868e4dc7d1 | 159 | |
lindaako | 5:d7868e4dc7d1 | 160 | while (counter<12) |
lindaako | 5:d7868e4dc7d1 | 161 | { |
lindaako | 5:d7868e4dc7d1 | 162 | LIS.readXYZ(a, b, c); |
lindaako | 5:d7868e4dc7d1 | 163 | LIS.mgScale(a, b, c); |
lindaako | 5:d7868e4dc7d1 | 164 | LIS.getIncline(a,b,c); |
lindaako | 5:d7868e4dc7d1 | 165 | wait(2); // wait 2 seconds |
lindaako | 5:d7868e4dc7d1 | 166 | counter++; |
neilt6 | 0:407dafce805d | 167 | } |
lindaako | 5:d7868e4dc7d1 | 168 | } |
neilt6 | 0:407dafce805d | 169 | } |