ST7735 (Red Tab) working on the BBC Microbit (originally by user smultron1977)

Dependencies:   ST7735_TFT mbed

Fork of SPI18TFT by Jonne Valola

main.cpp

Committer:
yramesh
Date:
2018-01-24
Revision:
1:3486dcc20991
Parent:
0:309c546f048d
Child:
2:1b8fe035d67e

File content as of revision 1:3486dcc20991:

 #include "stdio.h"
 #include "stdlib.h"
 #include "math.h"
 #include "mbed.h"
 #include "ST7735_TFT.h"
 #include "string"
 #include "Arial12x12.h"
 #include "Arial24x23.h"
 #include "Arial28x28.h"
//#defines for each edge connector pin
#define MICROBIT_PIN_P0                     P0_3        //P0 is the left most pad (ANALOG/DIGITAL) used to be P0_3 on green board
#define MICROBIT_PIN_P1                     P0_2        //P1 is the middle pad (ANALOG/DIGITAL)
#define MICROBIT_PIN_P2                     P0_1        //P2 is the right most pad (ANALOG/DIGITAL) used to be P0_1 on green board
#define MICROBIT_PIN_P3                     P0_4        //COL1 (ANALOG/DIGITAL)
#define MICROBIT_PIN_P4                     P0_5        //COL2 (ANALOG/DIGITAL)
#define MICROBIT_PIN_P5                     P0_17       //BTN_A
#define MICROBIT_PIN_P6                     P0_12       //COL9
#define MICROBIT_PIN_P7                     P0_11       //COL8
#define MICROBIT_PIN_P8                     P0_18       //PIN 18
#define MICROBIT_PIN_P9                     P0_10       //COL7
#define MICROBIT_PIN_P10                    P0_6        //COL3 (ANALOG/DIGITAL)
#define MICROBIT_PIN_P11                    P0_26       //BTN_B
#define MICROBIT_PIN_P12                    P0_20       //PIN 20
#define MICROBIT_PIN_P13                    P0_23       //SCK
#define MICROBIT_PIN_P14                    P0_22       //MISO
#define MICROBIT_PIN_P15                    P0_21       //MOSI
#define MICROBIT_PIN_P16                    P0_16       //PIN 16
#define MICROBIT_PIN_P19                    P0_0        //SCL
#define MICROBIT_PIN_P20 P0_30 //SDA

 #define NUMBER_OF_STARS 300
 #define SCREEN_WIDTH 128
 #define SCREEN_HEIGHT 160
 
 /*star struct*/
typedef struct 
{
  float xpos, ypos;
  short zpos, speed;
  unsigned int color;
} STAR;

static STAR stars[NUMBER_OF_STARS];


void init_star(STAR* star, int i)
{
  /* randomly init stars, generate them around the center of the screen */
  
  star->xpos =  -10.0 + (20.0 * (rand()/(RAND_MAX+1.0)));
  star->ypos =  -10.0 + (20.0 * (rand()/(RAND_MAX+1.0)));
  
  star->xpos *= 3072.0; /*change viewpoint */
  star->ypos *= 3072.0;

  star->zpos =  i;
  star->speed =  2 + (int)(2.0 * (rand()/(RAND_MAX+1.0)));

  star->color = i*Cyan >> 2; /*the closer to the viewer the brighter*/
}


void init()
{
  int i;

  for (i = 0; i < NUMBER_OF_STARS; i++)
    {
      init_star(stars + i, i + 1);
    }
}
 
  
 // the TFT is connected to SPI pin 5-7, CS is p8, RS is p11, reset is p15 
 ST7735_TFT TFT(P0_21, P0_22, P0_23, P0_3, P0_18, P0_16,"TFT"); // mosi, miso, sclk, cs, rs, reset
 
 Serial pc(USBTX, USBRX); // tx, rx
 Timer t;

//extern unsigned char p1[];  // the mbed logo
 
 int main() {
    
    unsigned int centerx, centery;
    int i, j, tempx, tempy;  
    init();
    TFT.set_orientation(1);
    centerx = TFT.width() >> 1;
    centery = TFT.height() >> 1; 
    
     
    TFT.claim(stdout);      // send stdout to the TFT display 
    //TFT.claim(stderr);      // send stderr to the TFT display

    TFT.background(White);    // set background to black
    TFT.foreground(Black);    // set chars to white
    
    TFT.cls();
    TFT.set_font((unsigned char*) Arial24x23);  // select the font
        
    t.start();

    while(1){
        TFT.fillrect(20,20,100,100,Cyan);
    }
    ////// demo start
    
    for ( j = 0 ; j < 10000; j++ ) 
    {
     
      /* move and draw stars */
       
      for (i = 0; i < NUMBER_OF_STARS; i++)
    {
      tempx = (stars[i].xpos / stars[i].zpos) + centerx;
      tempy = (stars[i].ypos / stars[i].zpos) + centery;
      TFT.pixel(tempx,tempy,Black);
      
        
      stars[i].zpos -= stars[i].speed;
      
      if (stars[i].zpos <= 0)
        {
          init_star(stars + i, i + 1);
        }

      //compute 3D position
      tempx = (stars[i].xpos / stars[i].zpos) + centerx;
      tempy = (stars[i].ypos / stars[i].zpos) + centery;

      if (tempx < 0 || tempx > TFT.width() - 1 || tempy < 0 || tempy > TFT.height() - 1) //check if a star leaves the screen
        {
          init_star(stars + i, i + 1);
          continue;
        }
      
      TFT.pixel(tempx,tempy,stars[i].color);
        
    }
//     TFT.Bitmap(centerx-60,centery-19,120,38,p1);
    }
    
    ///// demo stop

    t.stop(); 
    TFT.locate(0,10);
    TFT.set_font((unsigned char*) Arial12x12);  // select the font
    printf("Time %f s\n", t.read());
  }