This sample program is for Arduino TFT Screen module.
Fork of SPI18TFT by
mbedとArduino TFT LCD Screenとの接続について
Arduino TFT LCD Screenswitch science webをmbed(LPC1768)で制御するsample programです。ベースにしたprogramはSPI18TFTです。
LCD moduleとmbedの接続はベースソフトと同じです。以下に接続図を示します。
配線完成の写真です
表示している様子
以上
main.cpp@1:0481ba500435, 2014-09-20 (annotated)
- Committer:
- suupen
- Date:
- Sat Sep 20 12:55:19 2014 +0000
- Revision:
- 1:0481ba500435
- Parent:
- 0:309c546f048d
This sample program is for Arduino TFT LCD Screen module.; http://www.switch-science.com/catalog/1390/
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
suupen | 1:0481ba500435 | 1 | #include "stdio.h" |
suupen | 1:0481ba500435 | 2 | #include "stdlib.h" |
suupen | 1:0481ba500435 | 3 | #include "math.h" |
suupen | 1:0481ba500435 | 4 | #include "mbed.h" |
suupen | 1:0481ba500435 | 5 | #include "ST7735_TFT.h" |
suupen | 1:0481ba500435 | 6 | #include "string" |
suupen | 1:0481ba500435 | 7 | #include "Arial12x12.h" |
suupen | 1:0481ba500435 | 8 | #include "Arial24x23.h" |
suupen | 1:0481ba500435 | 9 | #include "Arial28x28.h" |
smultron1977 | 0:309c546f048d | 10 | |
suupen | 1:0481ba500435 | 11 | #define NUMBER_OF_STARS 300 |
suupen | 1:0481ba500435 | 12 | #define SCREEN_WIDTH 128 |
suupen | 1:0481ba500435 | 13 | #define SCREEN_HEIGHT 160 |
suupen | 1:0481ba500435 | 14 | |
suupen | 1:0481ba500435 | 15 | /*star struct*/ |
suupen | 1:0481ba500435 | 16 | typedef struct { |
suupen | 1:0481ba500435 | 17 | float xpos, ypos; |
suupen | 1:0481ba500435 | 18 | short zpos, speed; |
suupen | 1:0481ba500435 | 19 | unsigned int color; |
smultron1977 | 0:309c546f048d | 20 | } STAR; |
smultron1977 | 0:309c546f048d | 21 | |
smultron1977 | 0:309c546f048d | 22 | static STAR stars[NUMBER_OF_STARS]; |
smultron1977 | 0:309c546f048d | 23 | |
smultron1977 | 0:309c546f048d | 24 | |
smultron1977 | 0:309c546f048d | 25 | void init_star(STAR* star, int i) |
smultron1977 | 0:309c546f048d | 26 | { |
suupen | 1:0481ba500435 | 27 | /* randomly init stars, generate them around the center of the screen */ |
suupen | 1:0481ba500435 | 28 | |
suupen | 1:0481ba500435 | 29 | star->xpos = -10.0 + (20.0 * (rand()/(RAND_MAX+1.0))); |
suupen | 1:0481ba500435 | 30 | star->ypos = -10.0 + (20.0 * (rand()/(RAND_MAX+1.0))); |
smultron1977 | 0:309c546f048d | 31 | |
suupen | 1:0481ba500435 | 32 | star->xpos *= 3072.0; /*change viewpoint */ |
suupen | 1:0481ba500435 | 33 | star->ypos *= 3072.0; |
smultron1977 | 0:309c546f048d | 34 | |
suupen | 1:0481ba500435 | 35 | star->zpos = i; |
suupen | 1:0481ba500435 | 36 | star->speed = 2 + (int)(2.0 * (rand()/(RAND_MAX+1.0))); |
suupen | 1:0481ba500435 | 37 | |
suupen | 1:0481ba500435 | 38 | star->color = i*Cyan >> 2; /*the closer to the viewer the brighter*/ |
smultron1977 | 0:309c546f048d | 39 | } |
smultron1977 | 0:309c546f048d | 40 | |
smultron1977 | 0:309c546f048d | 41 | |
smultron1977 | 0:309c546f048d | 42 | void init() |
smultron1977 | 0:309c546f048d | 43 | { |
suupen | 1:0481ba500435 | 44 | int i; |
smultron1977 | 0:309c546f048d | 45 | |
suupen | 1:0481ba500435 | 46 | for (i = 0; i < NUMBER_OF_STARS; i++) { |
suupen | 1:0481ba500435 | 47 | init_star(stars + i, i + 1); |
smultron1977 | 0:309c546f048d | 48 | } |
smultron1977 | 0:309c546f048d | 49 | } |
suupen | 1:0481ba500435 | 50 | |
suupen | 1:0481ba500435 | 51 | |
suupen | 1:0481ba500435 | 52 | // the TFT is connected to SPI pin 5-7, CS is p8, RS is p11, reset is p15 |
suupen | 1:0481ba500435 | 53 | ST7735_TFT TFT(p5, p6, p7, p8, p11, p15,"TFT"); // mosi, miso, sclk, cs, rs, reset |
suupen | 1:0481ba500435 | 54 | |
suupen | 1:0481ba500435 | 55 | Serial pc(USBTX, USBRX); // tx, rx |
suupen | 1:0481ba500435 | 56 | Timer t; |
smultron1977 | 0:309c546f048d | 57 | |
smultron1977 | 0:309c546f048d | 58 | extern unsigned char p1[]; // the mbed logo |
suupen | 1:0481ba500435 | 59 | |
suupen | 1:0481ba500435 | 60 | int main() |
suupen | 1:0481ba500435 | 61 | { |
suupen | 1:0481ba500435 | 62 | |
smultron1977 | 0:309c546f048d | 63 | unsigned int centerx, centery; |
suupen | 1:0481ba500435 | 64 | int i, j, tempx, tempy; |
smultron1977 | 0:309c546f048d | 65 | init(); |
suupen | 1:0481ba500435 | 66 | TFT.set_orientation(1); // original = 1 |
smultron1977 | 0:309c546f048d | 67 | centerx = TFT.width() >> 1; |
suupen | 1:0481ba500435 | 68 | centery = TFT.height() >> 1; |
suupen | 1:0481ba500435 | 69 | |
suupen | 1:0481ba500435 | 70 | |
suupen | 1:0481ba500435 | 71 | TFT.claim(stdout); // send stdout to the TFT display |
smultron1977 | 0:309c546f048d | 72 | //TFT.claim(stderr); // send stderr to the TFT display |
smultron1977 | 0:309c546f048d | 73 | |
smultron1977 | 0:309c546f048d | 74 | TFT.background(Black); // set background to black |
suupen | 1:0481ba500435 | 75 | |
smultron1977 | 0:309c546f048d | 76 | TFT.foreground(White); // set chars to white |
suupen | 1:0481ba500435 | 77 | |
smultron1977 | 0:309c546f048d | 78 | TFT.cls(); |
smultron1977 | 0:309c546f048d | 79 | TFT.set_font((unsigned char*) Arial24x23); // select the font |
suupen | 1:0481ba500435 | 80 | |
smultron1977 | 0:309c546f048d | 81 | t.start(); |
smultron1977 | 0:309c546f048d | 82 | |
suupen | 1:0481ba500435 | 83 | |
suupen | 1:0481ba500435 | 84 | //↓@ss demo |
suupen | 1:0481ba500435 | 85 | for( i=0; i < 1000; i++){ |
suupen | 1:0481ba500435 | 86 | TFT.circle(rand()%TFT.width(), rand()%TFT.height(), rand()%50, rand()%0xffff); |
suupen | 1:0481ba500435 | 87 | } |
suupen | 1:0481ba500435 | 88 | |
suupen | 1:0481ba500435 | 89 | for( i=0; i < 10000; i++){ |
suupen | 1:0481ba500435 | 90 | TFT.rect(rand()%TFT.width(), rand()%TFT.height(), rand()%TFT.width(), rand()%TFT.height(), rand()%0xffff); |
suupen | 1:0481ba500435 | 91 | } |
suupen | 1:0481ba500435 | 92 | |
suupen | 1:0481ba500435 | 93 | for( i=0; i < 1000; i++){ |
suupen | 1:0481ba500435 | 94 | TFT.fillcircle(rand()%TFT.width(), rand()%TFT.height(), rand()%50, rand()%0xffff); |
suupen | 1:0481ba500435 | 95 | |
suupen | 1:0481ba500435 | 96 | for( i=0; i < 1000; i++){ |
suupen | 1:0481ba500435 | 97 | TFT.fillrect(rand()%TFT.width(), rand()%TFT.height(), rand()%TFT.width(), rand()%TFT.height(), rand()%0xffff); |
suupen | 1:0481ba500435 | 98 | } |
suupen | 1:0481ba500435 | 99 | |
suupen | 1:0481ba500435 | 100 | |
suupen | 1:0481ba500435 | 101 | } |
suupen | 1:0481ba500435 | 102 | //↑@ss demo |
suupen | 1:0481ba500435 | 103 | |
suupen | 1:0481ba500435 | 104 | |
smultron1977 | 0:309c546f048d | 105 | ////// demo start |
suupen | 1:0481ba500435 | 106 | // *************************************************************** |
suupen | 1:0481ba500435 | 107 | for( j = 0 ; j < 10000; j++) |
suupen | 1:0481ba500435 | 108 | // for ( j = 0 ; j < 10000; j++ ) |
suupen | 1:0481ba500435 | 109 | { |
suupen | 1:0481ba500435 | 110 | |
suupen | 1:0481ba500435 | 111 | |
suupen | 1:0481ba500435 | 112 | // move and draw stars |
suupen | 1:0481ba500435 | 113 | |
suupen | 1:0481ba500435 | 114 | for (i = 0; i < NUMBER_OF_STARS; i++) |
smultron1977 | 0:309c546f048d | 115 | { |
suupen | 1:0481ba500435 | 116 | tempx = (stars[i].xpos / stars[i].zpos) + centerx; |
suupen | 1:0481ba500435 | 117 | tempy = (stars[i].ypos / stars[i].zpos) + centery; |
suupen | 1:0481ba500435 | 118 | TFT.pixel(tempx,tempy,Black); |
suupen | 1:0481ba500435 | 119 | |
suupen | 1:0481ba500435 | 120 | |
suupen | 1:0481ba500435 | 121 | stars[i].zpos -= stars[i].speed; |
suupen | 1:0481ba500435 | 122 | |
suupen | 1:0481ba500435 | 123 | if (stars[i].zpos <= 0) |
suupen | 1:0481ba500435 | 124 | { |
suupen | 1:0481ba500435 | 125 | init_star(stars + i, i + 1); |
suupen | 1:0481ba500435 | 126 | } |
suupen | 1:0481ba500435 | 127 | |
suupen | 1:0481ba500435 | 128 | //compute 3D position |
suupen | 1:0481ba500435 | 129 | tempx = (stars[i].xpos / stars[i].zpos) + centerx; |
suupen | 1:0481ba500435 | 130 | tempy = (stars[i].ypos / stars[i].zpos) + centery; |
suupen | 1:0481ba500435 | 131 | |
suupen | 1:0481ba500435 | 132 | if (tempx < 0 || tempx > TFT.width() - 1 || tempy < 0 || tempy > TFT.height() - 1) //check if a star leaves the screen |
suupen | 1:0481ba500435 | 133 | { |
suupen | 1:0481ba500435 | 134 | init_star(stars + i, i + 1); |
suupen | 1:0481ba500435 | 135 | continue; |
suupen | 1:0481ba500435 | 136 | } |
suupen | 1:0481ba500435 | 137 | |
suupen | 1:0481ba500435 | 138 | TFT.pixel(tempx,tempy,stars[i].color); |
suupen | 1:0481ba500435 | 139 | |
suupen | 1:0481ba500435 | 140 | } |
suupen | 1:0481ba500435 | 141 | TFT.Bitmap(centerx-60,centery-19,120,38,p1); |
smultron1977 | 0:309c546f048d | 142 | } |
smultron1977 | 0:309c546f048d | 143 | |
suupen | 1:0481ba500435 | 144 | ///// demo stop |
suupen | 1:0481ba500435 | 145 | |
suupen | 1:0481ba500435 | 146 | t.stop(); |
suupen | 1:0481ba500435 | 147 | // *******************************************************************/ |
suupen | 1:0481ba500435 | 148 | |
suupen | 1:0481ba500435 | 149 | |
suupen | 1:0481ba500435 | 150 | //↓@ss test |
suupen | 1:0481ba500435 | 151 | #ifndef 1 |
suupen | 1:0481ba500435 | 152 | TFT.set_font((unsigned char*) Arial12x12); // select the font |
suupen | 1:0481ba500435 | 153 | for(int x = 159; x >= 0; x--) { |
suupen | 1:0481ba500435 | 154 | // TFT.pixel(-2,-1,0x100*x+0xff); |
suupen | 1:0481ba500435 | 155 | TFT.rect(0, 0, x,(x-32), 0xffff); |
suupen | 1:0481ba500435 | 156 | TFT.locate(0,10); |
suupen | 1:0481ba500435 | 157 | printf("X=%d, y = %d",x,x-32); |
suupen | 1:0481ba500435 | 158 | wait(0.1); |
suupen | 1:0481ba500435 | 159 | TFT.rect(0, 0, x,(x-32), 0x0000); |
suupen | 1:0481ba500435 | 160 | } |
suupen | 1:0481ba500435 | 161 | #else |
suupen | 1:0481ba500435 | 162 | // TFT.locate(0,10); |
suupen | 1:0481ba500435 | 163 | TFT.set_font((unsigned char*) Arial12x12); // select the font |
suupen | 1:0481ba500435 | 164 | // printf("Time %f s\n", t.read()); |
suupen | 1:0481ba500435 | 165 | |
suupen | 1:0481ba500435 | 166 | // TFT.fillrect(-2, -1, 157, 126, 0); //clr |
smultron1977 | 0:309c546f048d | 167 | |
suupen | 1:0481ba500435 | 168 | TFT.set_font((unsigned char*) Arial12x12); |
suupen | 1:0481ba500435 | 169 | /* |
suupen | 1:0481ba500435 | 170 | TFT.rect(0,0,159,127,0xffff); |
suupen | 1:0481ba500435 | 171 | TFT.rect(10,10,20,20,0x0fff); |
suupen | 1:0481ba500435 | 172 | TFT.rect(158,126,20,20,0xffff); |
suupen | 1:0481ba500435 | 173 | TFT.line(0,0, 159,127,0xffff); |
suupen | 1:0481ba500435 | 174 | TFT.line(159,0,0,127,0xffff); |
suupen | 1:0481ba500435 | 175 | */ |
suupen | 1:0481ba500435 | 176 | for(int y = 0; y < 128; y+=1) { |
suupen | 1:0481ba500435 | 177 | for(int x = 0; x <160; x+=1) { |
suupen | 1:0481ba500435 | 178 | if(y%2== 0) { |
suupen | 1:0481ba500435 | 179 | if(x%2==0) { |
suupen | 1:0481ba500435 | 180 | TFT.pixel(x,y,0xffff); |
suupen | 1:0481ba500435 | 181 | } else { |
suupen | 1:0481ba500435 | 182 | TFT.pixel(x,y,0x0ff); |
suupen | 1:0481ba500435 | 183 | } |
suupen | 1:0481ba500435 | 184 | } else { |
suupen | 1:0481ba500435 | 185 | if(x%2==0) { |
suupen | 1:0481ba500435 | 186 | TFT.pixel(x,y,0xff); |
suupen | 1:0481ba500435 | 187 | } else { |
suupen | 1:0481ba500435 | 188 | TFT.pixel(x,y,0xffff); |
suupen | 1:0481ba500435 | 189 | } |
suupen | 1:0481ba500435 | 190 | } |
suupen | 1:0481ba500435 | 191 | //wait_ms(10); |
smultron1977 | 0:309c546f048d | 192 | } |
smultron1977 | 0:309c546f048d | 193 | } |
smultron1977 | 0:309c546f048d | 194 | |
suupen | 1:0481ba500435 | 195 | #endif |
suupen | 1:0481ba500435 | 196 | //↑@ss test |
suupen | 1:0481ba500435 | 197 | |
suupen | 1:0481ba500435 | 198 | } |