pong game

Dependencies:   4DGL-uLCD-SE mbed

Fork of uLCD144G2_demo by jim hamblen

Committer:
4180_1
Date:
Mon Nov 25 04:25:16 2013 +0000
Revision:
7:7bd7397ab89f
Parent:
6:f752accd632c
Child:
8:31e63caf37e2
ver1.4

Who changed what in which revision?

UserRevisionLine numberNew contents of line
4180_1 0:cfcf73272647 1 //
4180_1 7:7bd7397ab89f 2 // TFT_4DGL is a class to drive 4D Systems LCD screens
4180_1 0:cfcf73272647 3 //
4180_1 0:cfcf73272647 4 // Copyright (C) <2010> Stephane ROCHON <stephane.rochon at free.fr>
4180_1 0:cfcf73272647 5 //
4180_1 0:cfcf73272647 6 // TFT_4DGL is free software: you can redistribute it and/or modify
4180_1 0:cfcf73272647 7 // it under the terms of the GNU General Public License as published by
4180_1 0:cfcf73272647 8 // the Free Software Foundation, either version 3 of the License, or
4180_1 0:cfcf73272647 9 // (at your option) any later version.
4180_1 0:cfcf73272647 10 //
4180_1 0:cfcf73272647 11 // TFT_4DGL is distributed in the hope that it will be useful,
4180_1 0:cfcf73272647 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
4180_1 0:cfcf73272647 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4180_1 0:cfcf73272647 14 // GNU General Public License for more details.
4180_1 0:cfcf73272647 15 //
4180_1 0:cfcf73272647 16 // You should have received a copy of the GNU General Public License
4180_1 0:cfcf73272647 17 // along with TFT_4DGL. If not, see <http://www.gnu.org/licenses/>.
4180_1 0:cfcf73272647 18
4180_1 0:cfcf73272647 19 #include "mbed.h"
4180_1 2:75727e89a717 20 #include "uLCD_4DGL.h"
4180_1 0:cfcf73272647 21
4180_1 6:f752accd632c 22 //#define SIZE_X 128
4180_1 6:f752accd632c 23 //#define SIZE_Y 128
4180_1 1:38ef731c7bdf 24 //
4180_1 1:38ef731c7bdf 25
4180_1 2:75727e89a717 26 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
4180_1 0:cfcf73272647 27
4180_1 2:75727e89a717 28 int main()
4180_1 2:75727e89a717 29 {
4180_1 6:f752accd632c 30
4180_1 3:454d1f4c8fd7 31 // basic printf demo = 16 by 18 characters on screen
4180_1 3:454d1f4c8fd7 32 uLCD.printf("\nHello uLCD World\n"); //Default Green on black text
4180_1 3:454d1f4c8fd7 33 uLCD.printf("\n Starting Demo...");
4180_1 3:454d1f4c8fd7 34 uLCD.text_width(4); //4X size text
4180_1 3:454d1f4c8fd7 35 uLCD.text_height(4);
4180_1 3:454d1f4c8fd7 36 uLCD.color(RED);
4180_1 3:454d1f4c8fd7 37 for (int i=10; i>=0; --i) {
4180_1 3:454d1f4c8fd7 38 uLCD.locate(1,2);
4180_1 3:454d1f4c8fd7 39 uLCD.printf("%2D",i);
4180_1 3:454d1f4c8fd7 40 wait(.5);
4180_1 3:454d1f4c8fd7 41 }
4180_1 7:7bd7397ab89f 42
4180_1 3:454d1f4c8fd7 43 uLCD.cls();
4180_1 7:7bd7397ab89f 44
4180_1 7:7bd7397ab89f 45 uLCD.printf("Change baud rate......");
4180_1 6:f752accd632c 46 uLCD.baudrate(600000); //jack up baud rate to max for fast display
4180_1 6:f752accd632c 47 //if demo hangs here - try lower baud rates
4180_1 7:7bd7397ab89f 48 // printf text only full screen mode demo
4180_1 7:7bd7397ab89f 49 uLCD.background_color(BLUE);
4180_1 7:7bd7397ab89f 50 uLCD.cls();
4180_1 7:7bd7397ab89f 51 uLCD.locate(0,0);
4180_1 7:7bd7397ab89f 52 uLCD.color(WHITE);
4180_1 7:7bd7397ab89f 53 uLCD.textbackground_color(BLUE);
4180_1 7:7bd7397ab89f 54 uLCD.set_font(FONT_7X8);
4180_1 7:7bd7397ab89f 55 uLCD.text_mode(OPAQUE);
4180_1 7:7bd7397ab89f 56 int i=0;
4180_1 7:7bd7397ab89f 57 while(i<64) {
4180_1 7:7bd7397ab89f 58 if(i%16==0) uLCD.cls();
4180_1 7:7bd7397ab89f 59 uLCD.printf("TxtLine %2D Page %D\n",i%16,i/16 );
4180_1 7:7bd7397ab89f 60 i++; //16 lines with 18 charaters per line
4180_1 7:7bd7397ab89f 61 }
4180_1 7:7bd7397ab89f 62 wait(0.5);
4180_1 6:f752accd632c 63 //demo graphics commands
4180_1 7:7bd7397ab89f 64 uLCD.background_color(BLACK);
4180_1 7:7bd7397ab89f 65 uLCD.cls();
4180_1 2:75727e89a717 66 uLCD.background_color(DGREY);
4180_1 2:75727e89a717 67 uLCD.circle(60, 50, 30, 0xFF00FF);
4180_1 2:75727e89a717 68 uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF);
4180_1 2:75727e89a717 69 uLCD.line(0, 0, 80, 60, 0xFF0000);
4180_1 2:75727e89a717 70 uLCD.rectangle(50, 50, 100, 90, 0x00FF00);
4180_1 2:75727e89a717 71 uLCD.pixel(60, 60, BLACK);
4180_1 2:75727e89a717 72 uLCD.read_pixel(120, 70);
4180_1 2:75727e89a717 73 uLCD.circle(120, 60, 10, BLACK);
4180_1 3:454d1f4c8fd7 74 uLCD.set_font(FONT_7X8);
4180_1 2:75727e89a717 75 uLCD.text_mode(TRANSPARENT);
4180_1 6:f752accd632c 76 uLCD.text_bold(ON);
4180_1 2:75727e89a717 77 uLCD.text_char('B', 9, 8, BLACK);
4180_1 2:75727e89a717 78 uLCD.text_char('I',10, 8, BLACK);
4180_1 2:75727e89a717 79 uLCD.text_char('G',11, 8, BLACK);
4180_1 6:f752accd632c 80 uLCD.text_italic(ON);
4180_1 3:454d1f4c8fd7 81 uLCD.text_string("This is a test of string", 1, 4, FONT_7X8, WHITE);
4180_1 3:454d1f4c8fd7 82 wait(2);
4180_1 7:7bd7397ab89f 83
4180_1 6:f752accd632c 84 //Bouncing Ball Demo
4180_1 7:7bd7397ab89f 85 float fx=50.0,fy=21.0,vx=1.0,vy=0.4;
4180_1 7:7bd7397ab89f 86 int x=50,y=21,radius=4;
4180_1 4:25a266a74a4c 87 uLCD.background_color(BLACK);
4180_1 4:25a266a74a4c 88 uLCD.cls();
4180_1 6:f752accd632c 89 //draw walls
4180_1 6:f752accd632c 90 uLCD.line(0, 0, 127, 0, WHITE);
4180_1 6:f752accd632c 91 uLCD.line(127, 0, 127, 127, WHITE);
4180_1 6:f752accd632c 92 uLCD.line(127, 127, 0, 127, WHITE);
4180_1 6:f752accd632c 93 uLCD.line(0, 127, 0, 0, WHITE);
4180_1 7:7bd7397ab89f 94 for (int i=0; i<1500; i++) {
4180_1 6:f752accd632c 95 //draw ball
4180_1 6:f752accd632c 96 uLCD.circle(x, y, radius, RED);
4180_1 7:7bd7397ab89f 97 //bounce off edge walls and slow down a bit?
4180_1 7:7bd7397ab89f 98 if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx;
4180_1 7:7bd7397ab89f 99 if ((y<=radius+1) || (y>=126-radius)) vy = -.90*vy;
4180_1 6:f752accd632c 100 //erase old ball location
4180_1 6:f752accd632c 101 uLCD.circle(x, y, radius, BLACK);
4180_1 6:f752accd632c 102 //move ball
4180_1 7:7bd7397ab89f 103 fx=fx+vx;
4180_1 7:7bd7397ab89f 104 fy=fy+vy;
4180_1 7:7bd7397ab89f 105 x=(int)fx;
4180_1 7:7bd7397ab89f 106 y=(int)fy;
4180_1 4:25a266a74a4c 107 }
4180_1 7:7bd7397ab89f 108 wait(0.5);
4180_1 3:454d1f4c8fd7 109 //draw an image pixel by pixel
4180_1 6:f752accd632c 110 int pixelcolors[50][50];
4180_1 3:454d1f4c8fd7 111 uLCD.background_color(BLACK);
4180_1 3:454d1f4c8fd7 112 uLCD.cls();
4180_1 6:f752accd632c 113 //compute Mandelbrot set image for display
4180_1 3:454d1f4c8fd7 114 //image size in pixels and setup
4180_1 3:454d1f4c8fd7 115 const unsigned ImageHeight=128;
4180_1 3:454d1f4c8fd7 116 const unsigned ImageWidth=128;
4180_1 6:f752accd632c 117 //region to display
4180_1 3:454d1f4c8fd7 118 double MinRe = -0.75104;
4180_1 3:454d1f4c8fd7 119 double MaxRe = -0.7408;
4180_1 3:454d1f4c8fd7 120 double MinIm = 0.10511;
4180_1 3:454d1f4c8fd7 121 double MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth;
4180_1 3:454d1f4c8fd7 122 double Re_factor = (MaxRe-MinRe)/(ImageWidth-1);
4180_1 3:454d1f4c8fd7 123 double Im_factor = (MaxIm-MinIm)/(ImageHeight-1);
4180_1 7:7bd7397ab89f 124 unsigned MaxIterations = 2048;
4180_1 3:454d1f4c8fd7 125 for(unsigned y=0; y<ImageHeight; ++y) {
4180_1 3:454d1f4c8fd7 126 double c_im = MaxIm - y*Im_factor;
4180_1 3:454d1f4c8fd7 127 for(unsigned x=0; x<ImageWidth; ++x) {
4180_1 3:454d1f4c8fd7 128 double c_re = MinRe + x*Re_factor;
4180_1 3:454d1f4c8fd7 129 double Z_re = c_re, Z_im = c_im;
4180_1 3:454d1f4c8fd7 130 int niterations=0;
4180_1 3:454d1f4c8fd7 131 for(unsigned n=0; n<MaxIterations; ++n) {
4180_1 3:454d1f4c8fd7 132 double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im;
4180_1 3:454d1f4c8fd7 133 if(Z_re2 + Z_im2 > 4) {
4180_1 3:454d1f4c8fd7 134 niterations = n;
4180_1 3:454d1f4c8fd7 135 break;
4180_1 3:454d1f4c8fd7 136 }
4180_1 3:454d1f4c8fd7 137 Z_im = 2*Z_re*Z_im + c_im;
4180_1 3:454d1f4c8fd7 138 Z_re = Z_re2 - Z_im2 + c_re;
4180_1 3:454d1f4c8fd7 139 }
4180_1 6:f752accd632c 140 if (niterations!=(MaxIterations-1))
4180_1 6:f752accd632c 141 uLCD.pixel(x,y,((niterations & 0xF00)<<12)+((niterations & 0xF0)<<8)+((niterations & 0x0F)<<4) );
4180_1 3:454d1f4c8fd7 142 }
4180_1 3:454d1f4c8fd7 143 }
4180_1 5:a1ef40ff0f78 144 wait(5);
4180_1 6:f752accd632c 145 // PLASMA wave BLIT animation
4180_1 6:f752accd632c 146 //draw an image using BLIT (Block Image Transfer) fastest way to transfer pixel data
4180_1 5:a1ef40ff0f78 147 uLCD.cls();
4180_1 6:f752accd632c 148 int num_cols=50;
4180_1 6:f752accd632c 149 int num_rows=50;
4180_1 7:7bd7397ab89f 150 int frame=0;
4180_1 5:a1ef40ff0f78 151 double a,b,c=0.0;
4180_1 7:7bd7397ab89f 152 while(frame<50) {
4180_1 5:a1ef40ff0f78 153 for (int k=0; k<num_cols; k++) {
4180_1 5:a1ef40ff0f78 154 b= (1+sin(3.14159*k*0.75/(num_cols-1.0)+c))*0.5;
4180_1 5:a1ef40ff0f78 155 for (int i=0; i<num_rows; i++) {
4180_1 5:a1ef40ff0f78 156 a= (1+sin(3.14159*i*0.75/(num_rows-1.0)+c))*0.5;
4180_1 5:a1ef40ff0f78 157 // a and b will be a sine wave output between 0 and 1
4180_1 5:a1ef40ff0f78 158 // sine wave was scaled for nice effect across array
4180_1 5:a1ef40ff0f78 159 // uses a and b to compute pixel colors based on rol and col location in array
4180_1 5:a1ef40ff0f78 160 // also keeps colors at the same brightness level
4180_1 5:a1ef40ff0f78 161 if ((a+b) <.667)
4180_1 6:f752accd632c 162 pixelcolors[i][k] = (255-(int(254.0*((a+b)/0.667)))<<16) | (int(254.0*((a+b)/0.667))<<8) | 0;
4180_1 5:a1ef40ff0f78 163 else if ((a+b)<1.333)
4180_1 6:f752accd632c 164 pixelcolors[i][k] = (0 <<16) | (255-(int (254.0*((a+b-0.667)/0.667)))<<8) | int(254.0*((a+b-0.667)/0.667));
4180_1 5:a1ef40ff0f78 165 else
4180_1 6:f752accd632c 166 pixelcolors[i][k] = (int(255*((a+b-1.333)/0.667))<<16) | (0<<8) | (255-(int (254.0*((a+b-1.333)/0.667))));
4180_1 5:a1ef40ff0f78 167 }
4180_1 5:a1ef40ff0f78 168 }
4180_1 6:f752accd632c 169 uLCD.BLIT(39, 39, 50, 50, &pixelcolors[0][0]);
4180_1 5:a1ef40ff0f78 170 c = c + 0.0314159*3.0;
4180_1 5:a1ef40ff0f78 171 if (c > 6.2831) c = 0.0;
4180_1 7:7bd7397ab89f 172 frame++;
4180_1 7:7bd7397ab89f 173 }
4180_1 7:7bd7397ab89f 174 uLCD.cls();
4180_1 7:7bd7397ab89f 175 //SD card needed with image and video files for last two demos
4180_1 7:7bd7397ab89f 176 while(1){} //remove line, if SD card is used
4180_1 7:7bd7397ab89f 177 uLCD.cls();
4180_1 7:7bd7397ab89f 178 uLCD.media_init();
4180_1 7:7bd7397ab89f 179 uLCD.set_sector_address(0x001D, 0x4C01);
4180_1 7:7bd7397ab89f 180 uLCD.display_image(0,0);
4180_1 7:7bd7397ab89f 181 wait(10);
4180_1 7:7bd7397ab89f 182 while(1) {
4180_1 7:7bd7397ab89f 183 uLCD.cls();
4180_1 7:7bd7397ab89f 184 uLCD.media_init();
4180_1 7:7bd7397ab89f 185 uLCD.set_sector_address(0x001D, 0x4C42);
4180_1 7:7bd7397ab89f 186 uLCD.display_video(0,0);
4180_1 5:a1ef40ff0f78 187 }
4180_1 6:f752accd632c 188 }
4180_1 7:7bd7397ab89f 189
4180_1 7:7bd7397ab89f 190