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

Dependencies:   ST7735_TFT mbed

Fork of SPI18TFT by Jonne Valola

Committer:
yramesh
Date:
Wed Jan 24 02:30:27 2018 +0000
Revision:
2:1b8fe035d67e
Parent:
1:3486dcc20991
Child:
3:feffd5c100c9
microbit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
smultron1977 0:309c546f048d 1 #include "stdio.h"
smultron1977 0:309c546f048d 2 #include "stdlib.h"
smultron1977 0:309c546f048d 3 #include "math.h"
smultron1977 0:309c546f048d 4 #include "mbed.h"
smultron1977 0:309c546f048d 5 #include "ST7735_TFT.h"
smultron1977 0:309c546f048d 6 #include "string"
smultron1977 0:309c546f048d 7 #include "Arial12x12.h"
smultron1977 0:309c546f048d 8 #include "Arial24x23.h"
smultron1977 0:309c546f048d 9 #include "Arial28x28.h"
yramesh 1:3486dcc20991 10 //#defines for each edge connector pin
yramesh 1:3486dcc20991 11 #define MICROBIT_PIN_P0 P0_3 //P0 is the left most pad (ANALOG/DIGITAL) used to be P0_3 on green board
yramesh 1:3486dcc20991 12 #define MICROBIT_PIN_P1 P0_2 //P1 is the middle pad (ANALOG/DIGITAL)
yramesh 1:3486dcc20991 13 #define MICROBIT_PIN_P2 P0_1 //P2 is the right most pad (ANALOG/DIGITAL) used to be P0_1 on green board
yramesh 1:3486dcc20991 14 #define MICROBIT_PIN_P3 P0_4 //COL1 (ANALOG/DIGITAL)
yramesh 1:3486dcc20991 15 #define MICROBIT_PIN_P4 P0_5 //COL2 (ANALOG/DIGITAL)
yramesh 1:3486dcc20991 16 #define MICROBIT_PIN_P5 P0_17 //BTN_A
yramesh 1:3486dcc20991 17 #define MICROBIT_PIN_P6 P0_12 //COL9
yramesh 1:3486dcc20991 18 #define MICROBIT_PIN_P7 P0_11 //COL8
yramesh 1:3486dcc20991 19 #define MICROBIT_PIN_P8 P0_18 //PIN 18
yramesh 1:3486dcc20991 20 #define MICROBIT_PIN_P9 P0_10 //COL7
yramesh 1:3486dcc20991 21 #define MICROBIT_PIN_P10 P0_6 //COL3 (ANALOG/DIGITAL)
yramesh 1:3486dcc20991 22 #define MICROBIT_PIN_P11 P0_26 //BTN_B
yramesh 1:3486dcc20991 23 #define MICROBIT_PIN_P12 P0_20 //PIN 20
yramesh 1:3486dcc20991 24 #define MICROBIT_PIN_P13 P0_23 //SCK
yramesh 1:3486dcc20991 25 #define MICROBIT_PIN_P14 P0_22 //MISO
yramesh 1:3486dcc20991 26 #define MICROBIT_PIN_P15 P0_21 //MOSI
yramesh 1:3486dcc20991 27 #define MICROBIT_PIN_P16 P0_16 //PIN 16
yramesh 1:3486dcc20991 28 #define MICROBIT_PIN_P19 P0_0 //SCL
yramesh 1:3486dcc20991 29 #define MICROBIT_PIN_P20 P0_30 //SDA
smultron1977 0:309c546f048d 30
smultron1977 0:309c546f048d 31 #define NUMBER_OF_STARS 300
smultron1977 0:309c546f048d 32 #define SCREEN_WIDTH 128
smultron1977 0:309c546f048d 33 #define SCREEN_HEIGHT 160
smultron1977 0:309c546f048d 34
smultron1977 0:309c546f048d 35 /*star struct*/
smultron1977 0:309c546f048d 36 typedef struct
smultron1977 0:309c546f048d 37 {
smultron1977 0:309c546f048d 38 float xpos, ypos;
smultron1977 0:309c546f048d 39 short zpos, speed;
smultron1977 0:309c546f048d 40 unsigned int color;
smultron1977 0:309c546f048d 41 } STAR;
smultron1977 0:309c546f048d 42
smultron1977 0:309c546f048d 43 static STAR stars[NUMBER_OF_STARS];
smultron1977 0:309c546f048d 44
smultron1977 0:309c546f048d 45
smultron1977 0:309c546f048d 46 void init_star(STAR* star, int i)
smultron1977 0:309c546f048d 47 {
smultron1977 0:309c546f048d 48 /* randomly init stars, generate them around the center of the screen */
smultron1977 0:309c546f048d 49
smultron1977 0:309c546f048d 50 star->xpos = -10.0 + (20.0 * (rand()/(RAND_MAX+1.0)));
smultron1977 0:309c546f048d 51 star->ypos = -10.0 + (20.0 * (rand()/(RAND_MAX+1.0)));
smultron1977 0:309c546f048d 52
smultron1977 0:309c546f048d 53 star->xpos *= 3072.0; /*change viewpoint */
smultron1977 0:309c546f048d 54 star->ypos *= 3072.0;
smultron1977 0:309c546f048d 55
smultron1977 0:309c546f048d 56 star->zpos = i;
smultron1977 0:309c546f048d 57 star->speed = 2 + (int)(2.0 * (rand()/(RAND_MAX+1.0)));
smultron1977 0:309c546f048d 58
smultron1977 0:309c546f048d 59 star->color = i*Cyan >> 2; /*the closer to the viewer the brighter*/
smultron1977 0:309c546f048d 60 }
smultron1977 0:309c546f048d 61
smultron1977 0:309c546f048d 62
smultron1977 0:309c546f048d 63 void init()
smultron1977 0:309c546f048d 64 {
smultron1977 0:309c546f048d 65 int i;
smultron1977 0:309c546f048d 66
smultron1977 0:309c546f048d 67 for (i = 0; i < NUMBER_OF_STARS; i++)
smultron1977 0:309c546f048d 68 {
smultron1977 0:309c546f048d 69 init_star(stars + i, i + 1);
smultron1977 0:309c546f048d 70 }
smultron1977 0:309c546f048d 71 }
smultron1977 0:309c546f048d 72
smultron1977 0:309c546f048d 73
smultron1977 0:309c546f048d 74 // the TFT is connected to SPI pin 5-7, CS is p8, RS is p11, reset is p15
yramesh 2:1b8fe035d67e 75 ST7735_TFT TFT(P0_21, P0_22, P0_23, P0_3, P0_2, P0_1,"TFT"); // mosi, miso, sclk, cs, rs, reset
smultron1977 0:309c546f048d 76
smultron1977 0:309c546f048d 77 Serial pc(USBTX, USBRX); // tx, rx
smultron1977 0:309c546f048d 78 Timer t;
smultron1977 0:309c546f048d 79
yramesh 1:3486dcc20991 80 //extern unsigned char p1[]; // the mbed logo
smultron1977 0:309c546f048d 81
smultron1977 0:309c546f048d 82 int main() {
smultron1977 0:309c546f048d 83
smultron1977 0:309c546f048d 84 unsigned int centerx, centery;
smultron1977 0:309c546f048d 85 int i, j, tempx, tempy;
smultron1977 0:309c546f048d 86 init();
smultron1977 0:309c546f048d 87 TFT.set_orientation(1);
smultron1977 0:309c546f048d 88 centerx = TFT.width() >> 1;
smultron1977 0:309c546f048d 89 centery = TFT.height() >> 1;
smultron1977 0:309c546f048d 90
smultron1977 0:309c546f048d 91
smultron1977 0:309c546f048d 92 TFT.claim(stdout); // send stdout to the TFT display
smultron1977 0:309c546f048d 93 //TFT.claim(stderr); // send stderr to the TFT display
smultron1977 0:309c546f048d 94
yramesh 1:3486dcc20991 95 TFT.background(White); // set background to black
yramesh 1:3486dcc20991 96 TFT.foreground(Black); // set chars to white
smultron1977 0:309c546f048d 97
smultron1977 0:309c546f048d 98 TFT.cls();
smultron1977 0:309c546f048d 99 TFT.set_font((unsigned char*) Arial24x23); // select the font
smultron1977 0:309c546f048d 100
smultron1977 0:309c546f048d 101 t.start();
smultron1977 0:309c546f048d 102
yramesh 1:3486dcc20991 103 while(1){
yramesh 1:3486dcc20991 104 TFT.fillrect(20,20,100,100,Cyan);
yramesh 1:3486dcc20991 105 }
smultron1977 0:309c546f048d 106 ////// demo start
smultron1977 0:309c546f048d 107
smultron1977 0:309c546f048d 108 for ( j = 0 ; j < 10000; j++ )
smultron1977 0:309c546f048d 109 {
smultron1977 0:309c546f048d 110
smultron1977 0:309c546f048d 111 /* move and draw stars */
smultron1977 0:309c546f048d 112
smultron1977 0:309c546f048d 113 for (i = 0; i < NUMBER_OF_STARS; i++)
smultron1977 0:309c546f048d 114 {
smultron1977 0:309c546f048d 115 tempx = (stars[i].xpos / stars[i].zpos) + centerx;
smultron1977 0:309c546f048d 116 tempy = (stars[i].ypos / stars[i].zpos) + centery;
smultron1977 0:309c546f048d 117 TFT.pixel(tempx,tempy,Black);
smultron1977 0:309c546f048d 118
smultron1977 0:309c546f048d 119
smultron1977 0:309c546f048d 120 stars[i].zpos -= stars[i].speed;
smultron1977 0:309c546f048d 121
smultron1977 0:309c546f048d 122 if (stars[i].zpos <= 0)
smultron1977 0:309c546f048d 123 {
smultron1977 0:309c546f048d 124 init_star(stars + i, i + 1);
smultron1977 0:309c546f048d 125 }
smultron1977 0:309c546f048d 126
smultron1977 0:309c546f048d 127 //compute 3D position
smultron1977 0:309c546f048d 128 tempx = (stars[i].xpos / stars[i].zpos) + centerx;
smultron1977 0:309c546f048d 129 tempy = (stars[i].ypos / stars[i].zpos) + centery;
smultron1977 0:309c546f048d 130
smultron1977 0:309c546f048d 131 if (tempx < 0 || tempx > TFT.width() - 1 || tempy < 0 || tempy > TFT.height() - 1) //check if a star leaves the screen
smultron1977 0:309c546f048d 132 {
smultron1977 0:309c546f048d 133 init_star(stars + i, i + 1);
smultron1977 0:309c546f048d 134 continue;
smultron1977 0:309c546f048d 135 }
smultron1977 0:309c546f048d 136
smultron1977 0:309c546f048d 137 TFT.pixel(tempx,tempy,stars[i].color);
smultron1977 0:309c546f048d 138
smultron1977 0:309c546f048d 139 }
yramesh 1:3486dcc20991 140 // TFT.Bitmap(centerx-60,centery-19,120,38,p1);
smultron1977 0:309c546f048d 141 }
smultron1977 0:309c546f048d 142
smultron1977 0:309c546f048d 143 ///// demo stop
smultron1977 0:309c546f048d 144
yramesh 1:3486dcc20991 145 t.stop();
smultron1977 0:309c546f048d 146 TFT.locate(0,10);
smultron1977 0:309c546f048d 147 TFT.set_font((unsigned char*) Arial12x12); // select the font
smultron1977 0:309c546f048d 148 printf("Time %f s\n", t.read());
smultron1977 0:309c546f048d 149 }