1.8" ST7735 SPI TFT Starfield demo for the SPI ST7735 library http://mbed.org/users/smultron1977/libraries/ST7735_TFT/m20nw8

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  #include "stdio.h"
00002  #include "stdlib.h"
00003  #include "math.h"
00004  #include "mbed.h"
00005  #include "ST7735_TFT.h"
00006  #include "string"
00007  #include "Arial12x12.h"
00008  #include "Arial24x23.h"
00009  #include "Arial28x28.h"
00010 
00011  #define NUMBER_OF_STARS 300
00012  #define SCREEN_WIDTH 128
00013  #define SCREEN_HEIGHT 160
00014  
00015  /*star struct*/
00016 typedef struct 
00017 {
00018   float xpos, ypos;
00019   short zpos, speed;
00020   unsigned int color;
00021 } STAR;
00022 
00023 static STAR stars[NUMBER_OF_STARS];
00024 
00025 
00026 void init_star(STAR* star, int i)
00027 {
00028   /* randomly init stars, generate them around the center of the screen */
00029   
00030   star->xpos =  -10.0 + (20.0 * (rand()/(RAND_MAX+1.0)));
00031   star->ypos =  -10.0 + (20.0 * (rand()/(RAND_MAX+1.0)));
00032   
00033   star->xpos *= 3072.0; /*change viewpoint */
00034   star->ypos *= 3072.0;
00035 
00036   star->zpos =  i;
00037   star->speed =  2 + (int)(2.0 * (rand()/(RAND_MAX+1.0)));
00038 
00039   star->color = i*Cyan >> 2; /*the closer to the viewer the brighter*/
00040 }
00041 
00042 
00043 void init()
00044 {
00045   int i;
00046 
00047   for (i = 0; i < NUMBER_OF_STARS; i++)
00048     {
00049       init_star(stars + i, i + 1);
00050     }
00051 }
00052  
00053   
00054  // the TFT is connected to SPI pin 5-7, CS is p8, RS is p11, reset is p15 
00055  ST7735_TFT TFT(p5, p6, p7, p8, p11, p15,"TFT"); // mosi, miso, sclk, cs, rs, reset
00056  
00057  Serial pc(USBTX, USBRX); // tx, rx
00058  Timer t;
00059 
00060 extern unsigned char p1[];  // the mbed logo
00061  
00062  int main() {
00063     
00064     unsigned int centerx, centery;
00065     int i, j, tempx, tempy;  
00066     init();
00067     TFT.set_orientation(1);
00068     centerx = TFT.width() >> 1;
00069     centery = TFT.height() >> 1; 
00070     
00071      
00072     TFT.claim(stdout);      // send stdout to the TFT display 
00073     //TFT.claim(stderr);      // send stderr to the TFT display
00074 
00075     TFT.background(Black);    // set background to black
00076     TFT.foreground(White);    // set chars to white
00077     
00078     TFT.cls();
00079     TFT.set_font((unsigned char*) Arial24x23);  // select the font
00080         
00081     t.start();
00082 
00083     ////// demo start
00084     
00085     for ( j = 0 ; j < 10000; j++ ) 
00086     {
00087      
00088       /* move and draw stars */
00089        
00090       for (i = 0; i < NUMBER_OF_STARS; i++)
00091     {
00092       tempx = (stars[i].xpos / stars[i].zpos) + centerx;
00093       tempy = (stars[i].ypos / stars[i].zpos) + centery;
00094       TFT.pixel(tempx,tempy,Black);
00095       
00096         
00097       stars[i].zpos -= stars[i].speed;
00098       
00099       if (stars[i].zpos <= 0)
00100         {
00101           init_star(stars + i, i + 1);
00102         }
00103 
00104       //compute 3D position
00105       tempx = (stars[i].xpos / stars[i].zpos) + centerx;
00106       tempy = (stars[i].ypos / stars[i].zpos) + centery;
00107 
00108       if (tempx < 0 || tempx > TFT.width() - 1 || tempy < 0 || tempy > TFT.height() - 1) //check if a star leaves the screen
00109         {
00110           init_star(stars + i, i + 1);
00111           continue;
00112         }
00113       
00114       TFT.pixel(tempx,tempy,stars[i].color);
00115         
00116     }
00117      TFT.Bitmap(centerx-60,centery-19,120,38,p1);
00118     }
00119     
00120     ///// demo stop
00121 
00122     t.stop();
00123     TFT.locate(0,10);
00124     TFT.set_font((unsigned char*) Arial12x12);  // select the font
00125     printf("Time %f s\n", t.read());
00126   }