Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of TFT_test_NUCLEO-F411RE by
main.cpp@0:cd5e3d371b54, 2015-11-19 (annotated)
- Committer:
- Rhyme
- Date:
- Thu Nov 19 13:34:57 2015 +0000
- Revision:
- 0:cd5e3d371b54
- Child:
- 1:0a7005226e61
First working version with NUCLEO F411RE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:cd5e3d371b54 | 1 | /* mbed main.cpp to test adafruit 2.8" TFT LCD shiled w Touchscreen |
Rhyme | 0:cd5e3d371b54 | 2 | * Copyright (c) 2014, 2015 Motoo Tanaka @ Design Methodology Lab |
Rhyme | 0:cd5e3d371b54 | 3 | * |
Rhyme | 0:cd5e3d371b54 | 4 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
Rhyme | 0:cd5e3d371b54 | 5 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
Rhyme | 0:cd5e3d371b54 | 6 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
Rhyme | 0:cd5e3d371b54 | 7 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
Rhyme | 0:cd5e3d371b54 | 8 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
Rhyme | 0:cd5e3d371b54 | 9 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
Rhyme | 0:cd5e3d371b54 | 10 | * THE SOFTWARE. |
Rhyme | 0:cd5e3d371b54 | 11 | */ |
Rhyme | 0:cd5e3d371b54 | 12 | |
Rhyme | 0:cd5e3d371b54 | 13 | /* |
Rhyme | 0:cd5e3d371b54 | 14 | * Note: This program is derived from the SeeeStudioTFTv2 program. |
Rhyme | 0:cd5e3d371b54 | 15 | * Although both program share same ILI9341 TFT driver, |
Rhyme | 0:cd5e3d371b54 | 16 | * the touch sensor was not same with the Display I purchased from Akizuki. |
Rhyme | 0:cd5e3d371b54 | 17 | * http://akizukidenshi.com/catalog/g/gM-07747/ |
Rhyme | 0:cd5e3d371b54 | 18 | * The touch sensor on the display is STMPE610, |
Rhyme | 0:cd5e3d371b54 | 19 | * so I hacked the minimum spi driver for it (polling mode only). |
Rhyme | 0:cd5e3d371b54 | 20 | */ |
Rhyme | 0:cd5e3d371b54 | 21 | |
Rhyme | 0:cd5e3d371b54 | 22 | #include "mbed.h" |
Rhyme | 0:cd5e3d371b54 | 23 | #include <math.h> |
Rhyme | 0:cd5e3d371b54 | 24 | #include "ILI9341.h" |
Rhyme | 0:cd5e3d371b54 | 25 | #include "SPI_STMPE610.h" |
Rhyme | 0:cd5e3d371b54 | 26 | #include "Arial12x12.h" |
Rhyme | 0:cd5e3d371b54 | 27 | #include "Arial24x23.h" |
Rhyme | 0:cd5e3d371b54 | 28 | #include "Arial28x28.h" |
Rhyme | 0:cd5e3d371b54 | 29 | #include "Arial43x48_numb.h" |
Rhyme | 0:cd5e3d371b54 | 30 | |
Rhyme | 0:cd5e3d371b54 | 31 | #define PIN_MOSI PA_7 |
Rhyme | 0:cd5e3d371b54 | 32 | #define PIN_MISO PA_6 |
Rhyme | 0:cd5e3d371b54 | 33 | #define PIN_SCLK PA_5 |
Rhyme | 0:cd5e3d371b54 | 34 | #define PIN_CS_TFT PB_6 |
Rhyme | 0:cd5e3d371b54 | 35 | #define PIN_DC_TFT PC_7 |
Rhyme | 0:cd5e3d371b54 | 36 | #define PIN_BL_TFT PA_8 |
Rhyme | 0:cd5e3d371b54 | 37 | #define PIN_CS_SD PB_5 |
Rhyme | 0:cd5e3d371b54 | 38 | #define PIN_CS_TSC PA_9 |
Rhyme | 0:cd5e3d371b54 | 39 | #define PIN_TSC_INTR PA_8 |
Rhyme | 0:cd5e3d371b54 | 40 | #define PIN_RESET_TFT PC_13 /* place holder */ |
Rhyme | 0:cd5e3d371b54 | 41 | #define DEVICE_NAME "F411RE" |
Rhyme | 0:cd5e3d371b54 | 42 | |
Rhyme | 0:cd5e3d371b54 | 43 | #ifndef TARGET_NECLEO_F411RE |
Rhyme | 0:cd5e3d371b54 | 44 | #define TARGET_NECLEO_F411RE |
Rhyme | 0:cd5e3d371b54 | 45 | #endif |
Rhyme | 0:cd5e3d371b54 | 46 | |
Rhyme | 0:cd5e3d371b54 | 47 | |
Rhyme | 0:cd5e3d371b54 | 48 | DigitalOut backlight(PB_3) ; |
Rhyme | 0:cd5e3d371b54 | 49 | DigitalOut tsc_cs(PA_9, 1) ; |
Rhyme | 0:cd5e3d371b54 | 50 | DigitalOut tft_cs(PB_6, 1) ; |
Rhyme | 0:cd5e3d371b54 | 51 | |
Rhyme | 0:cd5e3d371b54 | 52 | ILI9341 TFT(SPI_8, 10000000, |
Rhyme | 0:cd5e3d371b54 | 53 | PIN_MOSI, PIN_MISO, PIN_SCLK, |
Rhyme | 0:cd5e3d371b54 | 54 | PIN_CS_TFT, PIN_RESET_TFT, PIN_DC_TFT, "Adafruit2.8") ; |
Rhyme | 0:cd5e3d371b54 | 55 | SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ; |
Rhyme | 0:cd5e3d371b54 | 56 | |
Rhyme | 0:cd5e3d371b54 | 57 | int page = 0 ; |
Rhyme | 0:cd5e3d371b54 | 58 | int numPage = 2 ; |
Rhyme | 0:cd5e3d371b54 | 59 | |
Rhyme | 0:cd5e3d371b54 | 60 | void initTFT(void) |
Rhyme | 0:cd5e3d371b54 | 61 | { |
Rhyme | 0:cd5e3d371b54 | 62 | //Configure the display driver |
Rhyme | 0:cd5e3d371b54 | 63 | TFT.BusEnable(true) ; |
Rhyme | 0:cd5e3d371b54 | 64 | TFT.FastWindow(true) ; |
Rhyme | 0:cd5e3d371b54 | 65 | TFT.background(Black); |
Rhyme | 0:cd5e3d371b54 | 66 | TFT.foreground(White); |
Rhyme | 0:cd5e3d371b54 | 67 | wait(0.01) ; |
Rhyme | 0:cd5e3d371b54 | 68 | TFT.cls(); |
Rhyme | 0:cd5e3d371b54 | 69 | TFT.BusEnable(false) ; |
Rhyme | 0:cd5e3d371b54 | 70 | } |
Rhyme | 0:cd5e3d371b54 | 71 | |
Rhyme | 0:cd5e3d371b54 | 72 | void screen1(void) // Welcome Screen |
Rhyme | 0:cd5e3d371b54 | 73 | { |
Rhyme | 0:cd5e3d371b54 | 74 | TFT.BusEnable(true) ; |
Rhyme | 0:cd5e3d371b54 | 75 | backlight = 0 ; |
Rhyme | 0:cd5e3d371b54 | 76 | TFT.background(White) ; |
Rhyme | 0:cd5e3d371b54 | 77 | wait(0.1) ; |
Rhyme | 0:cd5e3d371b54 | 78 | TFT.cls() ; |
Rhyme | 0:cd5e3d371b54 | 79 | wait(0.1) ; |
Rhyme | 0:cd5e3d371b54 | 80 | |
Rhyme | 0:cd5e3d371b54 | 81 | TFT.set_font((unsigned char*) Arial24x23); |
Rhyme | 0:cd5e3d371b54 | 82 | TFT.foreground(Red) ; |
Rhyme | 0:cd5e3d371b54 | 83 | TFT.locate(80, 40) ; |
Rhyme | 0:cd5e3d371b54 | 84 | TFT.printf("MBED") ; |
Rhyme | 0:cd5e3d371b54 | 85 | TFT.foreground(Blue); |
Rhyme | 0:cd5e3d371b54 | 86 | TFT.locate(60, 80) ; |
Rhyme | 0:cd5e3d371b54 | 87 | TFT.printf("2.8\"TFT") ; |
Rhyme | 0:cd5e3d371b54 | 88 | TFT.locate(40, 120) ; |
Rhyme | 0:cd5e3d371b54 | 89 | TFT.printf("with touch") ; |
Rhyme | 0:cd5e3d371b54 | 90 | TFT.foreground(Black); |
Rhyme | 0:cd5e3d371b54 | 91 | TFT.set_font((unsigned char*) Arial12x12); |
Rhyme | 0:cd5e3d371b54 | 92 | TFT.foreground(Blue) ; |
Rhyme | 0:cd5e3d371b54 | 93 | TFT.locate(30, 180) ; |
Rhyme | 0:cd5e3d371b54 | 94 | TFT.printf("This program is running on") ; |
Rhyme | 0:cd5e3d371b54 | 95 | TFT.locate(30, 200) ; |
Rhyme | 0:cd5e3d371b54 | 96 | TFT.printf("ST Nucleo F411RE with") ; |
Rhyme | 0:cd5e3d371b54 | 97 | TFT.locate(30, 220) ; |
Rhyme | 0:cd5e3d371b54 | 98 | TFT.printf("a program developed in mbed") ; |
Rhyme | 0:cd5e3d371b54 | 99 | TFT.foreground(Green) ; |
Rhyme | 0:cd5e3d371b54 | 100 | TFT.locate(30, 260) ; |
Rhyme | 0:cd5e3d371b54 | 101 | TFT.printf("To advance demo page, touch") ; |
Rhyme | 0:cd5e3d371b54 | 102 | TFT.locate(30, 280) ; |
Rhyme | 0:cd5e3d371b54 | 103 | TFT.printf("and hold right side of screen") ; |
Rhyme | 0:cd5e3d371b54 | 104 | TFT.locate(30, 300) ; |
Rhyme | 0:cd5e3d371b54 | 105 | TFT.printf("until the next screen starts") ; |
Rhyme | 0:cd5e3d371b54 | 106 | TFT.BusEnable(false) ; |
Rhyme | 0:cd5e3d371b54 | 107 | backlight = 1 ; |
Rhyme | 0:cd5e3d371b54 | 108 | } |
Rhyme | 0:cd5e3d371b54 | 109 | |
Rhyme | 0:cd5e3d371b54 | 110 | void screen2(void) // Graphics |
Rhyme | 0:cd5e3d371b54 | 111 | { |
Rhyme | 0:cd5e3d371b54 | 112 | //Draw some graphics |
Rhyme | 0:cd5e3d371b54 | 113 | int i, x[2], y[2] ; |
Rhyme | 0:cd5e3d371b54 | 114 | backlight = 0 ; |
Rhyme | 0:cd5e3d371b54 | 115 | TFT.BusEnable(true) ; |
Rhyme | 0:cd5e3d371b54 | 116 | TFT.background(Black); |
Rhyme | 0:cd5e3d371b54 | 117 | wait(0.1) ; |
Rhyme | 0:cd5e3d371b54 | 118 | TFT.foreground(White); |
Rhyme | 0:cd5e3d371b54 | 119 | wait(0.1) ; |
Rhyme | 0:cd5e3d371b54 | 120 | TFT.cls() ; |
Rhyme | 0:cd5e3d371b54 | 121 | wait(0.1) ; |
Rhyme | 0:cd5e3d371b54 | 122 | TFT.set_font((unsigned char*) Arial12x12); |
Rhyme | 0:cd5e3d371b54 | 123 | TFT.locate(90,0); |
Rhyme | 0:cd5e3d371b54 | 124 | TFT.printf("Graphics"); |
Rhyme | 0:cd5e3d371b54 | 125 | |
Rhyme | 0:cd5e3d371b54 | 126 | x[0] = 25 ; x[1] = 224 ; |
Rhyme | 0:cd5e3d371b54 | 127 | y[0] = 20 ; y[1] = 219 ; |
Rhyme | 0:cd5e3d371b54 | 128 | for (i = 20 ; i < 220 ; i += 10) { |
Rhyme | 0:cd5e3d371b54 | 129 | TFT.line(i+5, y[0], i+5, y[1], Blue) ; |
Rhyme | 0:cd5e3d371b54 | 130 | TFT.line(x[0], i, x[1], i, Blue) ; |
Rhyme | 0:cd5e3d371b54 | 131 | } |
Rhyme | 0:cd5e3d371b54 | 132 | TFT.line(125, y[0], 125, y[1], Green) ; |
Rhyme | 0:cd5e3d371b54 | 133 | TFT.line(x[0], 120, x[1], 120, Green) ; |
Rhyme | 0:cd5e3d371b54 | 134 | TFT.rect(x[0],y[0], x[1], y[1], Green) ; |
Rhyme | 0:cd5e3d371b54 | 135 | TFT.locate(10, 20) ; |
Rhyme | 0:cd5e3d371b54 | 136 | TFT.printf("V") ; |
Rhyme | 0:cd5e3d371b54 | 137 | TFT.locate(0, 115) ; |
Rhyme | 0:cd5e3d371b54 | 138 | TFT.printf("0.0") ; |
Rhyme | 0:cd5e3d371b54 | 139 | TFT.locate(115, 225) ; |
Rhyme | 0:cd5e3d371b54 | 140 | TFT.printf("0.0") ; |
Rhyme | 0:cd5e3d371b54 | 141 | TFT.locate(215, 225) ; |
Rhyme | 0:cd5e3d371b54 | 142 | TFT.printf("T") ; |
Rhyme | 0:cd5e3d371b54 | 143 | |
Rhyme | 0:cd5e3d371b54 | 144 | double s; |
Rhyme | 0:cd5e3d371b54 | 145 | for (int i = x[0]; i < 225; i++) { |
Rhyme | 0:cd5e3d371b54 | 146 | s = 40 * sin((long double)i / 20); |
Rhyme | 0:cd5e3d371b54 | 147 | TFT.pixel(i, 120 + (int)s, White); |
Rhyme | 0:cd5e3d371b54 | 148 | } |
Rhyme | 0:cd5e3d371b54 | 149 | |
Rhyme | 0:cd5e3d371b54 | 150 | TFT.fillrect(10, 240, 229, 309, White) ; |
Rhyme | 0:cd5e3d371b54 | 151 | TFT.rect(10, 240, 229, 309, Red) ; |
Rhyme | 0:cd5e3d371b54 | 152 | TFT.rect(11, 241, 228, 308, Red) ; |
Rhyme | 0:cd5e3d371b54 | 153 | |
Rhyme | 0:cd5e3d371b54 | 154 | TFT.background(White) ; |
Rhyme | 0:cd5e3d371b54 | 155 | TFT.foreground(Black) ; |
Rhyme | 0:cd5e3d371b54 | 156 | TFT.locate(20, 250) ; |
Rhyme | 0:cd5e3d371b54 | 157 | TFT.printf("With QVGA resolution") ; |
Rhyme | 0:cd5e3d371b54 | 158 | TFT.locate(20, 270) ; |
Rhyme | 0:cd5e3d371b54 | 159 | TFT.printf("simple graphics drawing") ; |
Rhyme | 0:cd5e3d371b54 | 160 | TFT.locate(20, 290) ; |
Rhyme | 0:cd5e3d371b54 | 161 | TFT.printf("capability is provided") ; |
Rhyme | 0:cd5e3d371b54 | 162 | TFT.BusEnable(false) ; |
Rhyme | 0:cd5e3d371b54 | 163 | backlight = 1 ; |
Rhyme | 0:cd5e3d371b54 | 164 | } |
Rhyme | 0:cd5e3d371b54 | 165 | |
Rhyme | 0:cd5e3d371b54 | 166 | double clip(double src) |
Rhyme | 0:cd5e3d371b54 | 167 | { |
Rhyme | 0:cd5e3d371b54 | 168 | double value ; |
Rhyme | 0:cd5e3d371b54 | 169 | value = src ; |
Rhyme | 0:cd5e3d371b54 | 170 | if (value < 0.0) { |
Rhyme | 0:cd5e3d371b54 | 171 | value = 0.0 ; |
Rhyme | 0:cd5e3d371b54 | 172 | } else if (value > 2.0) { |
Rhyme | 0:cd5e3d371b54 | 173 | value = 2.0 ; |
Rhyme | 0:cd5e3d371b54 | 174 | } |
Rhyme | 0:cd5e3d371b54 | 175 | return( value ) ; |
Rhyme | 0:cd5e3d371b54 | 176 | } |
Rhyme | 0:cd5e3d371b54 | 177 | |
Rhyme | 0:cd5e3d371b54 | 178 | void incPage(void) |
Rhyme | 0:cd5e3d371b54 | 179 | { |
Rhyme | 0:cd5e3d371b54 | 180 | page++ ; |
Rhyme | 0:cd5e3d371b54 | 181 | if (page >= numPage) { |
Rhyme | 0:cd5e3d371b54 | 182 | page = 0 ; |
Rhyme | 0:cd5e3d371b54 | 183 | } |
Rhyme | 0:cd5e3d371b54 | 184 | } |
Rhyme | 0:cd5e3d371b54 | 185 | |
Rhyme | 0:cd5e3d371b54 | 186 | void decPage(void) |
Rhyme | 0:cd5e3d371b54 | 187 | { |
Rhyme | 0:cd5e3d371b54 | 188 | page-- ; |
Rhyme | 0:cd5e3d371b54 | 189 | if (page < 0) { |
Rhyme | 0:cd5e3d371b54 | 190 | page = numPage - 1 ; |
Rhyme | 0:cd5e3d371b54 | 191 | } |
Rhyme | 0:cd5e3d371b54 | 192 | } |
Rhyme | 0:cd5e3d371b54 | 193 | |
Rhyme | 0:cd5e3d371b54 | 194 | int main() |
Rhyme | 0:cd5e3d371b54 | 195 | { |
Rhyme | 0:cd5e3d371b54 | 196 | uint16_t x, y, z ; |
Rhyme | 0:cd5e3d371b54 | 197 | int prevPage = 99 ; |
Rhyme | 0:cd5e3d371b54 | 198 | bool waitTouch = false ; |
Rhyme | 0:cd5e3d371b54 | 199 | |
Rhyme | 0:cd5e3d371b54 | 200 | printf("Hello World\n") ; |
Rhyme | 0:cd5e3d371b54 | 201 | |
Rhyme | 0:cd5e3d371b54 | 202 | tsc_cs = 1 ; |
Rhyme | 0:cd5e3d371b54 | 203 | tft_cs = 0 ; |
Rhyme | 0:cd5e3d371b54 | 204 | initTFT() ; |
Rhyme | 0:cd5e3d371b54 | 205 | tft_cs = 1 ; |
Rhyme | 0:cd5e3d371b54 | 206 | |
Rhyme | 0:cd5e3d371b54 | 207 | tsc_cs = 0 ; |
Rhyme | 0:cd5e3d371b54 | 208 | TSC.spi_format(8, 1) ; // for Nucleo F411RE |
Rhyme | 0:cd5e3d371b54 | 209 | tsc_cs = 1 ; |
Rhyme | 0:cd5e3d371b54 | 210 | |
Rhyme | 0:cd5e3d371b54 | 211 | // screen0() ; |
Rhyme | 0:cd5e3d371b54 | 212 | |
Rhyme | 0:cd5e3d371b54 | 213 | printf("Program Started!\n\r") ; |
Rhyme | 0:cd5e3d371b54 | 214 | |
Rhyme | 0:cd5e3d371b54 | 215 | for(;;) { |
Rhyme | 0:cd5e3d371b54 | 216 | // printf("TFT width = %d, height = %d\n\r", TFT.width(), TFT.height()) ; |
Rhyme | 0:cd5e3d371b54 | 217 | tft_cs = 0 ; |
Rhyme | 0:cd5e3d371b54 | 218 | switch(page) { |
Rhyme | 0:cd5e3d371b54 | 219 | case 0: |
Rhyme | 0:cd5e3d371b54 | 220 | if (prevPage != page) { |
Rhyme | 0:cd5e3d371b54 | 221 | screen1() ; |
Rhyme | 0:cd5e3d371b54 | 222 | } |
Rhyme | 0:cd5e3d371b54 | 223 | waitTouch = true ; |
Rhyme | 0:cd5e3d371b54 | 224 | break ; |
Rhyme | 0:cd5e3d371b54 | 225 | case 1: |
Rhyme | 0:cd5e3d371b54 | 226 | if (prevPage != page) { |
Rhyme | 0:cd5e3d371b54 | 227 | screen2() ; |
Rhyme | 0:cd5e3d371b54 | 228 | } |
Rhyme | 0:cd5e3d371b54 | 229 | waitTouch = true ; |
Rhyme | 0:cd5e3d371b54 | 230 | break ; |
Rhyme | 0:cd5e3d371b54 | 231 | default: |
Rhyme | 0:cd5e3d371b54 | 232 | page = 0 ; |
Rhyme | 0:cd5e3d371b54 | 233 | break ; |
Rhyme | 0:cd5e3d371b54 | 234 | } |
Rhyme | 0:cd5e3d371b54 | 235 | prevPage = page ; |
Rhyme | 0:cd5e3d371b54 | 236 | |
Rhyme | 0:cd5e3d371b54 | 237 | tft_cs = 1 ; |
Rhyme | 0:cd5e3d371b54 | 238 | do { |
Rhyme | 0:cd5e3d371b54 | 239 | tsc_cs = 0 ; |
Rhyme | 0:cd5e3d371b54 | 240 | TSC.getRAWPoint(&x, &y, &z) ; |
Rhyme | 0:cd5e3d371b54 | 241 | if ((x != 0)||(y != 0) || (z != 0)) { |
Rhyme | 0:cd5e3d371b54 | 242 | if (x < 1000) { // left |
Rhyme | 0:cd5e3d371b54 | 243 | decPage() ; |
Rhyme | 0:cd5e3d371b54 | 244 | } else if (x > 3000) { // right |
Rhyme | 0:cd5e3d371b54 | 245 | incPage() ; |
Rhyme | 0:cd5e3d371b54 | 246 | } |
Rhyme | 0:cd5e3d371b54 | 247 | waitTouch = false ; |
Rhyme | 0:cd5e3d371b54 | 248 | } |
Rhyme | 0:cd5e3d371b54 | 249 | tsc_cs = 1 ; |
Rhyme | 0:cd5e3d371b54 | 250 | } while(waitTouch != false) ; |
Rhyme | 0:cd5e3d371b54 | 251 | // wait(1) ; |
Rhyme | 0:cd5e3d371b54 | 252 | } |
Rhyme | 0:cd5e3d371b54 | 253 | } |