![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
ST7735 (Red Tab) working on the BBC Microbit (originally by user smultron1977)
Fork of SPI18TFT by
main.cpp@0:309c546f048d, 2011-12-11 (annotated)
- Committer:
- smultron1977
- Date:
- Sun Dec 11 21:14:33 2011 +0000
- Revision:
- 0:309c546f048d
- Child:
- 1:3486dcc20991
SPI 1.8" TFT Starfield demo, Work in Progress
Who changed what in which revision?
User | Revision | Line number | New 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" |
smultron1977 | 0:309c546f048d | 10 | |
smultron1977 | 0:309c546f048d | 11 | #define NUMBER_OF_STARS 300 |
smultron1977 | 0:309c546f048d | 12 | #define SCREEN_WIDTH 128 |
smultron1977 | 0:309c546f048d | 13 | #define SCREEN_HEIGHT 160 |
smultron1977 | 0:309c546f048d | 14 | |
smultron1977 | 0:309c546f048d | 15 | /*star struct*/ |
smultron1977 | 0:309c546f048d | 16 | typedef struct |
smultron1977 | 0:309c546f048d | 17 | { |
smultron1977 | 0:309c546f048d | 18 | float xpos, ypos; |
smultron1977 | 0:309c546f048d | 19 | short zpos, speed; |
smultron1977 | 0:309c546f048d | 20 | unsigned int color; |
smultron1977 | 0:309c546f048d | 21 | } STAR; |
smultron1977 | 0:309c546f048d | 22 | |
smultron1977 | 0:309c546f048d | 23 | static STAR stars[NUMBER_OF_STARS]; |
smultron1977 | 0:309c546f048d | 24 | |
smultron1977 | 0:309c546f048d | 25 | |
smultron1977 | 0:309c546f048d | 26 | void init_star(STAR* star, int i) |
smultron1977 | 0:309c546f048d | 27 | { |
smultron1977 | 0:309c546f048d | 28 | /* randomly init stars, generate them around the center of the screen */ |
smultron1977 | 0:309c546f048d | 29 | |
smultron1977 | 0:309c546f048d | 30 | star->xpos = -10.0 + (20.0 * (rand()/(RAND_MAX+1.0))); |
smultron1977 | 0:309c546f048d | 31 | star->ypos = -10.0 + (20.0 * (rand()/(RAND_MAX+1.0))); |
smultron1977 | 0:309c546f048d | 32 | |
smultron1977 | 0:309c546f048d | 33 | star->xpos *= 3072.0; /*change viewpoint */ |
smultron1977 | 0:309c546f048d | 34 | star->ypos *= 3072.0; |
smultron1977 | 0:309c546f048d | 35 | |
smultron1977 | 0:309c546f048d | 36 | star->zpos = i; |
smultron1977 | 0:309c546f048d | 37 | star->speed = 2 + (int)(2.0 * (rand()/(RAND_MAX+1.0))); |
smultron1977 | 0:309c546f048d | 38 | |
smultron1977 | 0:309c546f048d | 39 | star->color = i*Cyan >> 2; /*the closer to the viewer the brighter*/ |
smultron1977 | 0:309c546f048d | 40 | } |
smultron1977 | 0:309c546f048d | 41 | |
smultron1977 | 0:309c546f048d | 42 | |
smultron1977 | 0:309c546f048d | 43 | void init() |
smultron1977 | 0:309c546f048d | 44 | { |
smultron1977 | 0:309c546f048d | 45 | int i; |
smultron1977 | 0:309c546f048d | 46 | |
smultron1977 | 0:309c546f048d | 47 | for (i = 0; i < NUMBER_OF_STARS; i++) |
smultron1977 | 0:309c546f048d | 48 | { |
smultron1977 | 0:309c546f048d | 49 | init_star(stars + i, i + 1); |
smultron1977 | 0:309c546f048d | 50 | } |
smultron1977 | 0:309c546f048d | 51 | } |
smultron1977 | 0:309c546f048d | 52 | |
smultron1977 | 0:309c546f048d | 53 | |
smultron1977 | 0:309c546f048d | 54 | // the TFT is connected to SPI pin 5-7, CS is p8, RS is p11, reset is p15 |
smultron1977 | 0:309c546f048d | 55 | ST7735_TFT TFT(p5, p6, p7, p8, p11, p15,"TFT"); // mosi, miso, sclk, cs, rs, reset |
smultron1977 | 0:309c546f048d | 56 | |
smultron1977 | 0:309c546f048d | 57 | Serial pc(USBTX, USBRX); // tx, rx |
smultron1977 | 0:309c546f048d | 58 | Timer t; |
smultron1977 | 0:309c546f048d | 59 | |
smultron1977 | 0:309c546f048d | 60 | extern unsigned char p1[]; // the mbed logo |
smultron1977 | 0:309c546f048d | 61 | |
smultron1977 | 0:309c546f048d | 62 | int main() { |
smultron1977 | 0:309c546f048d | 63 | |
smultron1977 | 0:309c546f048d | 64 | unsigned int centerx, centery; |
smultron1977 | 0:309c546f048d | 65 | int i, j, tempx, tempy; |
smultron1977 | 0:309c546f048d | 66 | init(); |
smultron1977 | 0:309c546f048d | 67 | TFT.set_orientation(1); |
smultron1977 | 0:309c546f048d | 68 | centerx = TFT.width() >> 1; |
smultron1977 | 0:309c546f048d | 69 | centery = TFT.height() >> 1; |
smultron1977 | 0:309c546f048d | 70 | |
smultron1977 | 0:309c546f048d | 71 | |
smultron1977 | 0:309c546f048d | 72 | TFT.claim(stdout); // send stdout to the TFT display |
smultron1977 | 0:309c546f048d | 73 | //TFT.claim(stderr); // send stderr to the TFT display |
smultron1977 | 0:309c546f048d | 74 | |
smultron1977 | 0:309c546f048d | 75 | TFT.background(Black); // set background to black |
smultron1977 | 0:309c546f048d | 76 | TFT.foreground(White); // set chars to white |
smultron1977 | 0:309c546f048d | 77 | |
smultron1977 | 0:309c546f048d | 78 | TFT.cls(); |
smultron1977 | 0:309c546f048d | 79 | TFT.set_font((unsigned char*) Arial24x23); // select the font |
smultron1977 | 0:309c546f048d | 80 | |
smultron1977 | 0:309c546f048d | 81 | t.start(); |
smultron1977 | 0:309c546f048d | 82 | |
smultron1977 | 0:309c546f048d | 83 | ////// demo start |
smultron1977 | 0:309c546f048d | 84 | |
smultron1977 | 0:309c546f048d | 85 | for ( j = 0 ; j < 10000; j++ ) |
smultron1977 | 0:309c546f048d | 86 | { |
smultron1977 | 0:309c546f048d | 87 | |
smultron1977 | 0:309c546f048d | 88 | /* move and draw stars */ |
smultron1977 | 0:309c546f048d | 89 | |
smultron1977 | 0:309c546f048d | 90 | for (i = 0; i < NUMBER_OF_STARS; i++) |
smultron1977 | 0:309c546f048d | 91 | { |
smultron1977 | 0:309c546f048d | 92 | tempx = (stars[i].xpos / stars[i].zpos) + centerx; |
smultron1977 | 0:309c546f048d | 93 | tempy = (stars[i].ypos / stars[i].zpos) + centery; |
smultron1977 | 0:309c546f048d | 94 | TFT.pixel(tempx,tempy,Black); |
smultron1977 | 0:309c546f048d | 95 | |
smultron1977 | 0:309c546f048d | 96 | |
smultron1977 | 0:309c546f048d | 97 | stars[i].zpos -= stars[i].speed; |
smultron1977 | 0:309c546f048d | 98 | |
smultron1977 | 0:309c546f048d | 99 | if (stars[i].zpos <= 0) |
smultron1977 | 0:309c546f048d | 100 | { |
smultron1977 | 0:309c546f048d | 101 | init_star(stars + i, i + 1); |
smultron1977 | 0:309c546f048d | 102 | } |
smultron1977 | 0:309c546f048d | 103 | |
smultron1977 | 0:309c546f048d | 104 | //compute 3D position |
smultron1977 | 0:309c546f048d | 105 | tempx = (stars[i].xpos / stars[i].zpos) + centerx; |
smultron1977 | 0:309c546f048d | 106 | tempy = (stars[i].ypos / stars[i].zpos) + centery; |
smultron1977 | 0:309c546f048d | 107 | |
smultron1977 | 0:309c546f048d | 108 | if (tempx < 0 || tempx > TFT.width() - 1 || tempy < 0 || tempy > TFT.height() - 1) //check if a star leaves the screen |
smultron1977 | 0:309c546f048d | 109 | { |
smultron1977 | 0:309c546f048d | 110 | init_star(stars + i, i + 1); |
smultron1977 | 0:309c546f048d | 111 | continue; |
smultron1977 | 0:309c546f048d | 112 | } |
smultron1977 | 0:309c546f048d | 113 | |
smultron1977 | 0:309c546f048d | 114 | TFT.pixel(tempx,tempy,stars[i].color); |
smultron1977 | 0:309c546f048d | 115 | |
smultron1977 | 0:309c546f048d | 116 | } |
smultron1977 | 0:309c546f048d | 117 | TFT.Bitmap(centerx-60,centery-19,120,38,p1); |
smultron1977 | 0:309c546f048d | 118 | } |
smultron1977 | 0:309c546f048d | 119 | |
smultron1977 | 0:309c546f048d | 120 | ///// demo stop |
smultron1977 | 0:309c546f048d | 121 | |
smultron1977 | 0:309c546f048d | 122 | t.stop(); |
smultron1977 | 0:309c546f048d | 123 | TFT.locate(0,10); |
smultron1977 | 0:309c546f048d | 124 | TFT.set_font((unsigned char*) Arial12x12); // select the font |
smultron1977 | 0:309c546f048d | 125 | printf("Time %f s\n", t.read()); |
smultron1977 | 0:309c546f048d | 126 | } |