![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
DuckHunt game using IMU sensor and uLCD display
Dependencies: 4DGL-uLCD-SE LSM9DS1_Library_cal SDFileSystem mbed wave_player
Fork of uLCD144G2_demo by
main.cpp@8:31e63caf37e2, 2013-11-30 (annotated)
- Committer:
- 4180_1
- Date:
- Sat Nov 30 02:06:03 2013 +0000
- Revision:
- 8:31e63caf37e2
- Parent:
- 7:7bd7397ab89f
- Child:
- 10:c19a945d9dca
ver1.5
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
4180_1 | 8:31e63caf37e2 | 1 | // uLCD-144-G2 demo program for uLCD-4GL LCD driver library |
4180_1 | 0:cfcf73272647 | 2 | // |
4180_1 | 0:cfcf73272647 | 3 | #include "mbed.h" |
4180_1 | 2:75727e89a717 | 4 | #include "uLCD_4DGL.h" |
4180_1 | 0:cfcf73272647 | 5 | |
4180_1 | 2:75727e89a717 | 6 | uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; |
4180_1 | 0:cfcf73272647 | 7 | |
4180_1 | 2:75727e89a717 | 8 | int main() |
4180_1 | 2:75727e89a717 | 9 | { |
4180_1 | 3:454d1f4c8fd7 | 10 | // basic printf demo = 16 by 18 characters on screen |
4180_1 | 3:454d1f4c8fd7 | 11 | uLCD.printf("\nHello uLCD World\n"); //Default Green on black text |
4180_1 | 3:454d1f4c8fd7 | 12 | uLCD.printf("\n Starting Demo..."); |
4180_1 | 3:454d1f4c8fd7 | 13 | uLCD.text_width(4); //4X size text |
4180_1 | 3:454d1f4c8fd7 | 14 | uLCD.text_height(4); |
4180_1 | 3:454d1f4c8fd7 | 15 | uLCD.color(RED); |
4180_1 | 3:454d1f4c8fd7 | 16 | for (int i=10; i>=0; --i) { |
4180_1 | 3:454d1f4c8fd7 | 17 | uLCD.locate(1,2); |
4180_1 | 3:454d1f4c8fd7 | 18 | uLCD.printf("%2D",i); |
4180_1 | 3:454d1f4c8fd7 | 19 | wait(.5); |
4180_1 | 3:454d1f4c8fd7 | 20 | } |
4180_1 | 3:454d1f4c8fd7 | 21 | uLCD.cls(); |
4180_1 | 8:31e63caf37e2 | 22 | uLCD.printf("Change baudrate......"); |
4180_1 | 8:31e63caf37e2 | 23 | uLCD.baudrate(3000000); //jack up baud rate to max for fast display |
4180_1 | 6:f752accd632c | 24 | //if demo hangs here - try lower baud rates |
4180_1 | 8:31e63caf37e2 | 25 | // |
4180_1 | 7:7bd7397ab89f | 26 | // printf text only full screen mode demo |
4180_1 | 7:7bd7397ab89f | 27 | uLCD.background_color(BLUE); |
4180_1 | 7:7bd7397ab89f | 28 | uLCD.cls(); |
4180_1 | 7:7bd7397ab89f | 29 | uLCD.locate(0,0); |
4180_1 | 7:7bd7397ab89f | 30 | uLCD.color(WHITE); |
4180_1 | 7:7bd7397ab89f | 31 | uLCD.textbackground_color(BLUE); |
4180_1 | 7:7bd7397ab89f | 32 | uLCD.set_font(FONT_7X8); |
4180_1 | 7:7bd7397ab89f | 33 | uLCD.text_mode(OPAQUE); |
4180_1 | 7:7bd7397ab89f | 34 | int i=0; |
4180_1 | 7:7bd7397ab89f | 35 | while(i<64) { |
4180_1 | 7:7bd7397ab89f | 36 | if(i%16==0) uLCD.cls(); |
4180_1 | 7:7bd7397ab89f | 37 | uLCD.printf("TxtLine %2D Page %D\n",i%16,i/16 ); |
4180_1 | 7:7bd7397ab89f | 38 | i++; //16 lines with 18 charaters per line |
4180_1 | 7:7bd7397ab89f | 39 | } |
4180_1 | 7:7bd7397ab89f | 40 | wait(0.5); |
4180_1 | 6:f752accd632c | 41 | //demo graphics commands |
4180_1 | 7:7bd7397ab89f | 42 | uLCD.background_color(BLACK); |
4180_1 | 7:7bd7397ab89f | 43 | uLCD.cls(); |
4180_1 | 2:75727e89a717 | 44 | uLCD.background_color(DGREY); |
4180_1 | 8:31e63caf37e2 | 45 | uLCD.filled_circle(60, 50, 30, 0xFF00FF); |
4180_1 | 2:75727e89a717 | 46 | uLCD.triangle(120, 100, 40, 40, 10, 100, 0x0000FF); |
4180_1 | 2:75727e89a717 | 47 | uLCD.line(0, 0, 80, 60, 0xFF0000); |
4180_1 | 8:31e63caf37e2 | 48 | uLCD.filled_rectangle(50, 50, 100, 90, 0x00FF00); |
4180_1 | 2:75727e89a717 | 49 | uLCD.pixel(60, 60, BLACK); |
4180_1 | 2:75727e89a717 | 50 | uLCD.read_pixel(120, 70); |
4180_1 | 2:75727e89a717 | 51 | uLCD.circle(120, 60, 10, BLACK); |
4180_1 | 3:454d1f4c8fd7 | 52 | uLCD.set_font(FONT_7X8); |
4180_1 | 2:75727e89a717 | 53 | uLCD.text_mode(TRANSPARENT); |
4180_1 | 6:f752accd632c | 54 | uLCD.text_bold(ON); |
4180_1 | 2:75727e89a717 | 55 | uLCD.text_char('B', 9, 8, BLACK); |
4180_1 | 2:75727e89a717 | 56 | uLCD.text_char('I',10, 8, BLACK); |
4180_1 | 2:75727e89a717 | 57 | uLCD.text_char('G',11, 8, BLACK); |
4180_1 | 6:f752accd632c | 58 | uLCD.text_italic(ON); |
4180_1 | 3:454d1f4c8fd7 | 59 | uLCD.text_string("This is a test of string", 1, 4, FONT_7X8, WHITE); |
4180_1 | 3:454d1f4c8fd7 | 60 | wait(2); |
4180_1 | 7:7bd7397ab89f | 61 | |
4180_1 | 6:f752accd632c | 62 | //Bouncing Ball Demo |
4180_1 | 7:7bd7397ab89f | 63 | float fx=50.0,fy=21.0,vx=1.0,vy=0.4; |
4180_1 | 7:7bd7397ab89f | 64 | int x=50,y=21,radius=4; |
4180_1 | 4:25a266a74a4c | 65 | uLCD.background_color(BLACK); |
4180_1 | 4:25a266a74a4c | 66 | uLCD.cls(); |
4180_1 | 6:f752accd632c | 67 | //draw walls |
4180_1 | 6:f752accd632c | 68 | uLCD.line(0, 0, 127, 0, WHITE); |
4180_1 | 6:f752accd632c | 69 | uLCD.line(127, 0, 127, 127, WHITE); |
4180_1 | 6:f752accd632c | 70 | uLCD.line(127, 127, 0, 127, WHITE); |
4180_1 | 6:f752accd632c | 71 | uLCD.line(0, 127, 0, 0, WHITE); |
4180_1 | 7:7bd7397ab89f | 72 | for (int i=0; i<1500; i++) { |
4180_1 | 6:f752accd632c | 73 | //draw ball |
4180_1 | 8:31e63caf37e2 | 74 | uLCD.filled_circle(x, y, radius, RED); |
4180_1 | 7:7bd7397ab89f | 75 | //bounce off edge walls and slow down a bit? |
4180_1 | 7:7bd7397ab89f | 76 | if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx; |
4180_1 | 7:7bd7397ab89f | 77 | if ((y<=radius+1) || (y>=126-radius)) vy = -.90*vy; |
4180_1 | 6:f752accd632c | 78 | //erase old ball location |
4180_1 | 8:31e63caf37e2 | 79 | uLCD.filled_circle(x, y, radius, BLACK); |
4180_1 | 6:f752accd632c | 80 | //move ball |
4180_1 | 7:7bd7397ab89f | 81 | fx=fx+vx; |
4180_1 | 7:7bd7397ab89f | 82 | fy=fy+vy; |
4180_1 | 7:7bd7397ab89f | 83 | x=(int)fx; |
4180_1 | 7:7bd7397ab89f | 84 | y=(int)fy; |
4180_1 | 4:25a266a74a4c | 85 | } |
4180_1 | 7:7bd7397ab89f | 86 | wait(0.5); |
4180_1 | 3:454d1f4c8fd7 | 87 | //draw an image pixel by pixel |
4180_1 | 6:f752accd632c | 88 | int pixelcolors[50][50]; |
4180_1 | 3:454d1f4c8fd7 | 89 | uLCD.background_color(BLACK); |
4180_1 | 3:454d1f4c8fd7 | 90 | uLCD.cls(); |
4180_1 | 6:f752accd632c | 91 | //compute Mandelbrot set image for display |
4180_1 | 8:31e63caf37e2 | 92 | //image size in pixels |
4180_1 | 3:454d1f4c8fd7 | 93 | const unsigned ImageHeight=128; |
4180_1 | 3:454d1f4c8fd7 | 94 | const unsigned ImageWidth=128; |
4180_1 | 8:31e63caf37e2 | 95 | //"c" region to display |
4180_1 | 3:454d1f4c8fd7 | 96 | double MinRe = -0.75104; |
4180_1 | 3:454d1f4c8fd7 | 97 | double MaxRe = -0.7408; |
4180_1 | 3:454d1f4c8fd7 | 98 | double MinIm = 0.10511; |
4180_1 | 3:454d1f4c8fd7 | 99 | double MaxIm = MinIm+(MaxRe-MinRe)*ImageHeight/ImageWidth; |
4180_1 | 3:454d1f4c8fd7 | 100 | double Re_factor = (MaxRe-MinRe)/(ImageWidth-1); |
4180_1 | 3:454d1f4c8fd7 | 101 | double Im_factor = (MaxIm-MinIm)/(ImageHeight-1); |
4180_1 | 7:7bd7397ab89f | 102 | unsigned MaxIterations = 2048; |
4180_1 | 3:454d1f4c8fd7 | 103 | for(unsigned y=0; y<ImageHeight; ++y) { |
4180_1 | 3:454d1f4c8fd7 | 104 | double c_im = MaxIm - y*Im_factor; |
4180_1 | 3:454d1f4c8fd7 | 105 | for(unsigned x=0; x<ImageWidth; ++x) { |
4180_1 | 3:454d1f4c8fd7 | 106 | double c_re = MinRe + x*Re_factor; |
4180_1 | 3:454d1f4c8fd7 | 107 | double Z_re = c_re, Z_im = c_im; |
4180_1 | 3:454d1f4c8fd7 | 108 | int niterations=0; |
4180_1 | 3:454d1f4c8fd7 | 109 | for(unsigned n=0; n<MaxIterations; ++n) { |
4180_1 | 3:454d1f4c8fd7 | 110 | double Z_re2 = Z_re*Z_re, Z_im2 = Z_im*Z_im; |
4180_1 | 3:454d1f4c8fd7 | 111 | if(Z_re2 + Z_im2 > 4) { |
4180_1 | 3:454d1f4c8fd7 | 112 | niterations = n; |
4180_1 | 3:454d1f4c8fd7 | 113 | break; |
4180_1 | 3:454d1f4c8fd7 | 114 | } |
4180_1 | 3:454d1f4c8fd7 | 115 | Z_im = 2*Z_re*Z_im + c_im; |
4180_1 | 3:454d1f4c8fd7 | 116 | Z_re = Z_re2 - Z_im2 + c_re; |
4180_1 | 3:454d1f4c8fd7 | 117 | } |
4180_1 | 6:f752accd632c | 118 | if (niterations!=(MaxIterations-1)) |
4180_1 | 6:f752accd632c | 119 | uLCD.pixel(x,y,((niterations & 0xF00)<<12)+((niterations & 0xF0)<<8)+((niterations & 0x0F)<<4) ); |
4180_1 | 3:454d1f4c8fd7 | 120 | } |
4180_1 | 3:454d1f4c8fd7 | 121 | } |
4180_1 | 5:a1ef40ff0f78 | 122 | wait(5); |
4180_1 | 6:f752accd632c | 123 | // PLASMA wave BLIT animation |
4180_1 | 6:f752accd632c | 124 | //draw an image using BLIT (Block Image Transfer) fastest way to transfer pixel data |
4180_1 | 5:a1ef40ff0f78 | 125 | uLCD.cls(); |
4180_1 | 6:f752accd632c | 126 | int num_cols=50; |
4180_1 | 6:f752accd632c | 127 | int num_rows=50; |
4180_1 | 7:7bd7397ab89f | 128 | int frame=0; |
4180_1 | 5:a1ef40ff0f78 | 129 | double a,b,c=0.0; |
4180_1 | 8:31e63caf37e2 | 130 | while(frame<75) { |
4180_1 | 5:a1ef40ff0f78 | 131 | for (int k=0; k<num_cols; k++) { |
4180_1 | 5:a1ef40ff0f78 | 132 | b= (1+sin(3.14159*k*0.75/(num_cols-1.0)+c))*0.5; |
4180_1 | 5:a1ef40ff0f78 | 133 | for (int i=0; i<num_rows; i++) { |
4180_1 | 5:a1ef40ff0f78 | 134 | a= (1+sin(3.14159*i*0.75/(num_rows-1.0)+c))*0.5; |
4180_1 | 5:a1ef40ff0f78 | 135 | // a and b will be a sine wave output between 0 and 1 |
4180_1 | 5:a1ef40ff0f78 | 136 | // sine wave was scaled for nice effect across array |
4180_1 | 5:a1ef40ff0f78 | 137 | // uses a and b to compute pixel colors based on rol and col location in array |
4180_1 | 5:a1ef40ff0f78 | 138 | // also keeps colors at the same brightness level |
4180_1 | 5:a1ef40ff0f78 | 139 | if ((a+b) <.667) |
4180_1 | 6:f752accd632c | 140 | 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 | 141 | else if ((a+b)<1.333) |
4180_1 | 6:f752accd632c | 142 | 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 | 143 | else |
4180_1 | 6:f752accd632c | 144 | 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 | 145 | } |
4180_1 | 5:a1ef40ff0f78 | 146 | } |
4180_1 | 6:f752accd632c | 147 | uLCD.BLIT(39, 39, 50, 50, &pixelcolors[0][0]); |
4180_1 | 5:a1ef40ff0f78 | 148 | c = c + 0.0314159*3.0; |
4180_1 | 5:a1ef40ff0f78 | 149 | if (c > 6.2831) c = 0.0; |
4180_1 | 7:7bd7397ab89f | 150 | frame++; |
4180_1 | 7:7bd7397ab89f | 151 | } |
4180_1 | 8:31e63caf37e2 | 152 | //Load Image Demo |
4180_1 | 7:7bd7397ab89f | 153 | uLCD.cls(); |
4180_1 | 7:7bd7397ab89f | 154 | //SD card needed with image and video files for last two demos |
4180_1 | 7:7bd7397ab89f | 155 | uLCD.cls(); |
4180_1 | 7:7bd7397ab89f | 156 | uLCD.media_init(); |
4180_1 | 8:31e63caf37e2 | 157 | uLCD.printf("\n\nAn SD card is needed for image and video data"); |
4180_1 | 7:7bd7397ab89f | 158 | uLCD.set_sector_address(0x001D, 0x4C01); |
4180_1 | 7:7bd7397ab89f | 159 | uLCD.display_image(0,0); |
4180_1 | 7:7bd7397ab89f | 160 | wait(10); |
4180_1 | 8:31e63caf37e2 | 161 | //Play video demo |
4180_1 | 7:7bd7397ab89f | 162 | while(1) { |
4180_1 | 7:7bd7397ab89f | 163 | uLCD.cls(); |
4180_1 | 7:7bd7397ab89f | 164 | uLCD.media_init(); |
4180_1 | 7:7bd7397ab89f | 165 | uLCD.set_sector_address(0x001D, 0x4C42); |
4180_1 | 7:7bd7397ab89f | 166 | uLCD.display_video(0,0); |
4180_1 | 5:a1ef40ff0f78 | 167 | } |
4180_1 | 6:f752accd632c | 168 | } |
4180_1 | 7:7bd7397ab89f | 169 | |
4180_1 | 7:7bd7397ab89f | 170 | |
4180_1 | 8:31e63caf37e2 | 171 |