![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
clone
Dependencies: UniGraphic mbed-STM32F103C8T6
Fork of STM32F103C8T6_ILI9341 by
main.cpp@4:6a2023014a95, 2017-11-29 (annotated)
- Committer:
- tulanthoar
- Date:
- Wed Nov 29 12:02:53 2017 +0000
- Revision:
- 4:6a2023014a95
- Parent:
- 2:7f4c1caee2c7
updae libs
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hudakz | 0:7136ca870582 | 1 | #include "stm32f103c8t6.h" |
hudakz | 0:7136ca870582 | 2 | #include "mbed.h" |
hudakz | 0:7136ca870582 | 3 | #include "Arial12x12.h" |
hudakz | 0:7136ca870582 | 4 | #include "Arial24x23.h" |
hudakz | 0:7136ca870582 | 5 | #include "Arial43x48_numb.h" |
hudakz | 0:7136ca870582 | 6 | #include "pict.h" |
hudakz | 0:7136ca870582 | 7 | #include "pavement_48x34.h" |
hudakz | 0:7136ca870582 | 8 | #include "ILI9341.h" |
hudakz | 0:7136ca870582 | 9 | |
hudakz | 0:7136ca870582 | 10 | const unsigned short FOREGROUND_COLORS[] = {White, Cyan, Red, Magenta, Yellow, Orange, GreenYellow}; |
hudakz | 0:7136ca870582 | 11 | const unsigned short BACKGROUND_COLORS[] = {Black, Navy, DarkGreen, DarkCyan, Maroon}; |
hudakz | 0:7136ca870582 | 12 | |
hudakz | 0:7136ca870582 | 13 | Serial pc(PA_2, PA_3); // serial interface with PC |
hudakz | 0:7136ca870582 | 14 | ILI9341* tft; // ILI9341 driver |
hudakz | 0:7136ca870582 | 15 | Timer t; |
hudakz | 0:7136ca870582 | 16 | unsigned short backgroundColor; |
hudakz | 0:7136ca870582 | 17 | unsigned short foregroundColor; |
hudakz | 0:7136ca870582 | 18 | unsigned short colorIndex = 0; |
hudakz | 0:7136ca870582 | 19 | char orient = 3; |
hudakz | 0:7136ca870582 | 20 | |
hudakz | 0:7136ca870582 | 21 | |
hudakz | 0:7136ca870582 | 22 | |
hudakz | 0:7136ca870582 | 23 | int main() |
hudakz | 0:7136ca870582 | 24 | { |
hudakz | 0:7136ca870582 | 25 | confSysClock(); //Configure system clock (72MHz HSE clock, 48MHz USB clock) |
hudakz | 0:7136ca870582 | 26 | |
hudakz | 0:7136ca870582 | 27 | tft = new ILI9341(SPI_8, 20000000, PB_5, PB_4, PB_3, PA_15, PA_12, PA_11, "tft"); // SPI type, SPI speed, mosi, miso, sclk, cs, reset, dc |
tulanthoar | 2:7f4c1caee2c7 | 28 | tft->idle_on(); |
hudakz | 0:7136ca870582 | 29 | tft->set_orientation(orient); |
hudakz | 0:7136ca870582 | 30 | int time, time2; |
hudakz | 0:7136ca870582 | 31 | pc.baud (115200); |
hudakz | 0:7136ca870582 | 32 | pc.printf("\n\nSystem Core Clock = %.3f MHZ\r\n",(float)SystemCoreClock/1000000); |
hudakz | 0:7136ca870582 | 33 | t.start(); |
hudakz | 0:7136ca870582 | 34 | |
hudakz | 0:7136ca870582 | 35 | while(1) { |
hudakz | 0:7136ca870582 | 36 | foregroundColor = FOREGROUND_COLORS[colorIndex % 7]; |
hudakz | 0:7136ca870582 | 37 | tft->foreground(foregroundColor); // set chars to white |
hudakz | 0:7136ca870582 | 38 | backgroundColor = BACKGROUND_COLORS[colorIndex % 5]; |
hudakz | 0:7136ca870582 | 39 | colorIndex++; |
hudakz | 0:7136ca870582 | 40 | tft->background(backgroundColor); // set background to black |
hudakz | 0:7136ca870582 | 41 | tft->set_orientation((orient++)%4); |
hudakz | 0:7136ca870582 | 42 | tft->cls(); // clear the screen |
hudakz | 0:7136ca870582 | 43 | tft->locate(0,30); |
hudakz | 0:7136ca870582 | 44 | tft->printf("Display ID: %.8X\r\n", tft->tftID); |
hudakz | 0:7136ca870582 | 45 | pc.printf("Display ID: %.8X\r\n", tft->tftID); |
hudakz | 0:7136ca870582 | 46 | // mem write/read test |
hudakz | 0:7136ca870582 | 47 | unsigned short readback; |
hudakz | 0:7136ca870582 | 48 | unsigned short colorstep = (0x10000/tft->width()); |
hudakz | 0:7136ca870582 | 49 | for(unsigned short i=0; i<tft->width(); i++) { |
hudakz | 0:7136ca870582 | 50 | tft->pixel(i,0,i*colorstep); // write line |
hudakz | 0:7136ca870582 | 51 | } |
hudakz | 0:7136ca870582 | 52 | bool readerror=false; |
hudakz | 0:7136ca870582 | 53 | for(unsigned short i=0; i<tft->width(); i++) { // verify line |
hudakz | 0:7136ca870582 | 54 | readback = tft->pixelread(i,0); |
hudakz | 0:7136ca870582 | 55 | if(readback!=i*colorstep) { |
hudakz | 0:7136ca870582 | 56 | readerror=true; |
hudakz | 0:7136ca870582 | 57 | pc.printf("pix %.4X readback %.4X\r\n", i*colorstep, readback); |
hudakz | 0:7136ca870582 | 58 | } |
hudakz | 0:7136ca870582 | 59 | } |
hudakz | 0:7136ca870582 | 60 | tft->locate(0,10); |
hudakz | 0:7136ca870582 | 61 | tft->printf("pixelread test %s\r\n", readerror ? "FAIL":"PASS"); |
hudakz | 0:7136ca870582 | 62 | wait(2); |
hudakz | 0:7136ca870582 | 63 | |
hudakz | 0:7136ca870582 | 64 | tft->cls(); |
hudakz | 0:7136ca870582 | 65 | tft->set_font((unsigned char*) Terminal6x8,32,127,false); //variable width disabled |
hudakz | 0:7136ca870582 | 66 | tft->locate(0,0); |
hudakz | 0:7136ca870582 | 67 | tft->printf("Display Test\r\nSome text just to see if auto carriage return works correctly"); |
hudakz | 0:7136ca870582 | 68 | tft->set_font((unsigned char*) Terminal6x8); |
hudakz | 0:7136ca870582 | 69 | tft->printf("\r\nDisplay Test\r\nSome text just to see if auto carriage return works correctly"); |
hudakz | 0:7136ca870582 | 70 | pc.printf(" Display Test \r\n"); |
hudakz | 0:7136ca870582 | 71 | wait(3); |
hudakz | 0:7136ca870582 | 72 | t.reset(); |
hudakz | 0:7136ca870582 | 73 | tft->cls(); |
hudakz | 0:7136ca870582 | 74 | time=t.read_us(); |
hudakz | 0:7136ca870582 | 75 | tft->locate(2,55); |
hudakz | 0:7136ca870582 | 76 | tft->printf("cls: %.3fms", (float)time/1000); |
hudakz | 0:7136ca870582 | 77 | pc.printf("cls: %.3fms\r\n", (float)time/1000); |
hudakz | 0:7136ca870582 | 78 | wait(3); |
hudakz | 0:7136ca870582 | 79 | |
hudakz | 0:7136ca870582 | 80 | tft->cls(); |
hudakz | 0:7136ca870582 | 81 | t.reset(); |
hudakz | 0:7136ca870582 | 82 | // draw some graphics |
hudakz | 0:7136ca870582 | 83 | tft->set_font((unsigned char*) Arial24x23); |
hudakz | 0:7136ca870582 | 84 | tft->locate(10,10); |
hudakz | 0:7136ca870582 | 85 | tft->printf("Test"); |
hudakz | 0:7136ca870582 | 86 | |
hudakz | 0:7136ca870582 | 87 | tft->line(0,0,tft->width()-1,0,foregroundColor); |
hudakz | 0:7136ca870582 | 88 | tft->line(0,0,0,tft->height()-1,foregroundColor); |
hudakz | 0:7136ca870582 | 89 | tft->line(0,0,tft->width()-1,tft->height()-1,foregroundColor); |
hudakz | 0:7136ca870582 | 90 | |
hudakz | 0:7136ca870582 | 91 | tft->rect(10,30,50,40,foregroundColor); |
hudakz | 0:7136ca870582 | 92 | tft->fillrect(60,30,100,40,foregroundColor); |
hudakz | 0:7136ca870582 | 93 | |
hudakz | 0:7136ca870582 | 94 | tft->circle(150,32,30,foregroundColor); |
hudakz | 0:7136ca870582 | 95 | tft->fillcircle(140,20,10,foregroundColor); |
hudakz | 0:7136ca870582 | 96 | |
hudakz | 0:7136ca870582 | 97 | double s; |
hudakz | 0:7136ca870582 | 98 | |
hudakz | 0:7136ca870582 | 99 | for (unsigned short i=0; i<tft->width(); i++) { |
hudakz | 0:7136ca870582 | 100 | s =10 * sin((long double) i / 10 ); |
hudakz | 0:7136ca870582 | 101 | tft->pixel(i,40 + (int)s ,foregroundColor); |
hudakz | 0:7136ca870582 | 102 | } |
hudakz | 0:7136ca870582 | 103 | |
hudakz | 0:7136ca870582 | 104 | |
hudakz | 0:7136ca870582 | 105 | time=t.read_us(); |
hudakz | 0:7136ca870582 | 106 | tft->locate(2,55); |
hudakz | 0:7136ca870582 | 107 | tft->set_font((unsigned char*) Terminal6x8); |
hudakz | 0:7136ca870582 | 108 | tft->printf("plot: %.3fms", (float)time/1000); |
hudakz | 0:7136ca870582 | 109 | pc.printf("plot: %.3fms\r\n", (float)time/1000); |
hudakz | 0:7136ca870582 | 110 | wait(3); |
hudakz | 0:7136ca870582 | 111 | tft->cls(); |
hudakz | 0:7136ca870582 | 112 | t.reset(); |
hudakz | 0:7136ca870582 | 113 | Bitmap_s pic = { |
hudakz | 0:7136ca870582 | 114 | 64, // XSize |
hudakz | 0:7136ca870582 | 115 | 64, // YSize |
hudakz | 0:7136ca870582 | 116 | 8, // Bytes in Line |
hudakz | 0:7136ca870582 | 117 | burp, // Pointer to picture data |
hudakz | 0:7136ca870582 | 118 | }; |
hudakz | 0:7136ca870582 | 119 | tft->Bitmap_BW(pic,tft->width()-64,0); |
hudakz | 0:7136ca870582 | 120 | time=t.read_us(); |
hudakz | 0:7136ca870582 | 121 | tft->locate(2,55); |
hudakz | 0:7136ca870582 | 122 | tft->printf("bmp: %.3fms", (float)time/1000); |
hudakz | 0:7136ca870582 | 123 | pc.printf("bmp: %.3fms\r\n", (float)time/1000); |
hudakz | 0:7136ca870582 | 124 | wait(3); |
hudakz | 0:7136ca870582 | 125 | tft->cls(); |
hudakz | 0:7136ca870582 | 126 | tft->set_font((unsigned char*) Arial43x48_numb, 46, 58, false); //only numbers, variable-width disabled |
hudakz | 0:7136ca870582 | 127 | t.reset(); |
hudakz | 0:7136ca870582 | 128 | tft->locate(0,0); |
hudakz | 0:7136ca870582 | 129 | tft->printf("%d", 12345); |
hudakz | 0:7136ca870582 | 130 | time=t.read_us(); |
hudakz | 0:7136ca870582 | 131 | tft->locate(2,55); |
hudakz | 0:7136ca870582 | 132 | tft->set_font((unsigned char*) Terminal6x8); |
hudakz | 0:7136ca870582 | 133 | tft->printf("Big Font: %.3fms", (float)time/1000); |
hudakz | 0:7136ca870582 | 134 | pc.printf("Big Font: %.3fms\r\n", (float)time/1000); |
hudakz | 0:7136ca870582 | 135 | wait(3); |
hudakz | 0:7136ca870582 | 136 | // sparse pixels test |
hudakz | 0:7136ca870582 | 137 | tft->cls(); |
hudakz | 0:7136ca870582 | 138 | tft->FastWindow(true); |
hudakz | 0:7136ca870582 | 139 | t.reset(); |
hudakz | 0:7136ca870582 | 140 | for(unsigned int i=0; i<20000; i++) { |
hudakz | 0:7136ca870582 | 141 | tft->pixel((i+(i*89)%tft->width()), (i+(i*61)%tft->height()), White); |
hudakz | 0:7136ca870582 | 142 | } |
hudakz | 0:7136ca870582 | 143 | tft->copy_to_lcd(); |
hudakz | 0:7136ca870582 | 144 | time=t.read_us(); |
hudakz | 0:7136ca870582 | 145 | tft->cls(); |
hudakz | 0:7136ca870582 | 146 | tft->FastWindow(false); |
hudakz | 0:7136ca870582 | 147 | t.reset(); |
hudakz | 0:7136ca870582 | 148 | for(unsigned int i=0; i<20000; i++) { |
hudakz | 0:7136ca870582 | 149 | tft->pixel((i+(i*89)%tft->width()), (i+(i*61)%tft->height()), White); |
hudakz | 0:7136ca870582 | 150 | } |
hudakz | 0:7136ca870582 | 151 | tft->copy_to_lcd(); |
hudakz | 0:7136ca870582 | 152 | time2=t.read_us(); |
hudakz | 0:7136ca870582 | 153 | tft->locate(2,55); |
hudakz | 0:7136ca870582 | 154 | tft->printf("std:%.3fms fastw:%.3fms", (float)time2/1000, (float)time/1000); |
hudakz | 0:7136ca870582 | 155 | pc.printf("std: %.3fms fastw: %.3fms\r\n", (float)time2/1000, (float)time/1000); |
hudakz | 0:7136ca870582 | 156 | wait(3); |
hudakz | 0:7136ca870582 | 157 | // scroll test, only for TFT |
hudakz | 0:7136ca870582 | 158 | tft->cls(); |
hudakz | 0:7136ca870582 | 159 | tft->set_font((unsigned char*) Arial24x23); |
hudakz | 0:7136ca870582 | 160 | tft->locate(2,10); |
hudakz | 0:7136ca870582 | 161 | tft->printf("Scrolling"); |
hudakz | 0:7136ca870582 | 162 | tft->rect(0,0,tft->width()-1,tft->height()-1,White); |
hudakz | 0:7136ca870582 | 163 | tft->rect(1,1,tft->width()-2,tft->height()-2,Blue); |
hudakz | 0:7136ca870582 | 164 | tft->setscrollarea(0,tft->sizeY()); |
hudakz | 0:7136ca870582 | 165 | wait(1); |
hudakz | 0:7136ca870582 | 166 | tft->scroll(1); //up 1 |
hudakz | 0:7136ca870582 | 167 | wait(1); |
hudakz | 0:7136ca870582 | 168 | tft->scroll(0); //center |
hudakz | 0:7136ca870582 | 169 | wait(1); |
hudakz | 0:7136ca870582 | 170 | tft->scroll(tft->sizeY()-1); //down 1 |
hudakz | 0:7136ca870582 | 171 | wait(1); |
hudakz | 0:7136ca870582 | 172 | tft->scroll(tft->sizeY()); // same as 0, center |
hudakz | 0:7136ca870582 | 173 | wait(1); |
hudakz | 0:7136ca870582 | 174 | tft->scroll(tft->sizeY()>>1); // half screen |
hudakz | 0:7136ca870582 | 175 | wait(1); |
hudakz | 0:7136ca870582 | 176 | tft->scrollreset(); // center |
hudakz | 0:7136ca870582 | 177 | wait(1); |
hudakz | 0:7136ca870582 | 178 | for(unsigned short i=1; i<=tft->sizeY(); i++) { |
hudakz | 0:7136ca870582 | 179 | tft->scroll(i); |
hudakz | 0:7136ca870582 | 180 | wait_ms(2); |
hudakz | 0:7136ca870582 | 181 | } |
hudakz | 0:7136ca870582 | 182 | wait(2); |
hudakz | 0:7136ca870582 | 183 | // color inversion |
hudakz | 0:7136ca870582 | 184 | for(unsigned short i=0; i<=8; i++) { |
hudakz | 0:7136ca870582 | 185 | tft->invert(i&1); |
hudakz | 0:7136ca870582 | 186 | wait_ms(200); |
hudakz | 0:7136ca870582 | 187 | } |
hudakz | 0:7136ca870582 | 188 | wait(2); |
hudakz | 0:7136ca870582 | 189 | // bmp 16bit test |
hudakz | 0:7136ca870582 | 190 | tft->cls(); |
hudakz | 0:7136ca870582 | 191 | t.reset(); |
hudakz | 0:7136ca870582 | 192 | for(int y=0; y<tft->height(); y+=34) { |
hudakz | 0:7136ca870582 | 193 | for(int x=0; x<tft->width(); x+=48) tft->Bitmap(x,y,48,34,(unsigned char *)pavement_48x34); |
hudakz | 0:7136ca870582 | 194 | } |
hudakz | 0:7136ca870582 | 195 | time=t.read_us(); |
hudakz | 0:7136ca870582 | 196 | tft->locate(2,55); |
hudakz | 0:7136ca870582 | 197 | tft->set_font((unsigned char*) Terminal6x8); |
hudakz | 0:7136ca870582 | 198 | tft->printf("Bmp speed: %.3fms", (float)time/1000); |
hudakz | 0:7136ca870582 | 199 | pc.printf("Bmp speed: %.3fms\r\n", (float)time/1000); |
hudakz | 0:7136ca870582 | 200 | wait(2); |
hudakz | 0:7136ca870582 | 201 | } |
hudakz | 0:7136ca870582 | 202 | } |
hudakz | 0:7136ca870582 | 203 | |
hudakz | 0:7136ca870582 | 204 |