Last commit 09 Jul 2011
Description: The first video game for the mbed video game console. The code utilizes the SimpleLib package developed by thomas@soete.org. For more information about the project and if you'd like to download the schematics and PCB design visit http://www.mbedgc.com/
mbedgc.cpp
00001 #include "mbed.h" 00002 #include "colors.h" 00003 #include "timers.h" 00004 00005 #define fbW 53 00006 #define fbH 242 00007 00008 DigitalOut vSync(p29); 00009 DigitalOut hSync(p30); 00010 00011 DigitalOut myled(LED1); 00012 00013 00014 void vsync(); 00015 void hsync(); 00016 void setup(); 00017 void loop(); 00018 void blankFB(); 00019 void fill(); 00020 00021 unsigned int frame = 0; 00022 unsigned char framebuffer[fbH][fbW]; 00023 00024 int snake_x = 5; 00025 int snake_y = 50; 00026 int snake_direction = 0; 00027 unsigned char snake_color = 1; 00028 00029 00030 TIMER0_INTERRUPT_HANDLER(void) 00031 { 00032 TIMER0_CLEAR_INTERRUPT(MR0_INT); 00033 00034 myled = 1; 00035 00036 vsync(); 00037 for( int i = 0; i < fbH; i++ ) 00038 { 00039 hsync(); 00040 for( int j = 0; j < fbW; j++ ) 00041 { 00042 unsigned char bp = framebuffer[ i ][ j ]; 00043 COLOR_SET(bp); 00044 } 00045 } 00046 COLOR_SET(0); 00047 myled = 0; 00048 } 00049 00050 int main() { 00051 fill(); 00052 NVIC_SetPriority(TIMER0_IRQn, 0); 00053 00054 COLOR_INIT(); 00055 TIMER0_INIT(); 00056 TIMER0_SETPCLK(CCLK4); 00057 TIMER0_SETPRESCALE(96000); // 1ms = 96000 : 1 00058 TIMER0_SETMATCH(0, 1); 00059 TIMER0_SETMATCHCONTROL(0, MATCH_RESET | MATCH_INTERRUPT); // Reset and Interrupt each 100ms 00060 TIMER0_ENABLE_INTERRUPT(); 00061 TIMER0_START(); 00062 00063 setup(); 00064 00065 while (1) 00066 loop(); 00067 } 00068 00069 void setup() { 00070 00071 } 00072 00073 void drawRectangle(int x, int y, int width, int height, unsigned char color) { 00074 00075 if (x < 0 || y < 0) 00076 return; 00077 00078 for (int i = 0; i < height; i++) { 00079 00080 for (int j = 0; j < width; j++) { 00081 00082 framebuffer[ (y + i) % fbH ][ (x + j) % fbW ] = color; 00083 00084 } 00085 } 00086 } 00087 00088 00089 void loop() { 00090 00091 if (snake_y <= 0) 00092 snake_y = fbH - 1; 00093 00094 if (snake_x <= 0) 00095 snake_x = fbW - 1; 00096 00097 snake_y %= fbH; 00098 snake_x %= fbW; 00099 00100 if (frame % (10 + rand() % 20) == 0) { 00101 snake_direction++; 00102 snake_direction %= 4; 00103 00104 snake_color++; 00105 } 00106 00107 00108 switch (snake_direction) { 00109 case 0: 00110 snake_y += 4; 00111 break; 00112 case 2: 00113 snake_y -= 4; 00114 break; 00115 case 1: 00116 snake_x += 1; 00117 break; 00118 case 3: 00119 snake_x -= 1; 00120 break; 00121 } 00122 00123 drawRectangle(snake_x, snake_y, 1, 4, snake_color); 00124 wait_us(10000); 00125 } 00126 00127 void draw() 00128 { 00129 00130 } 00131 00132 00133 void hsync() 00134 { 00135 00136 COLOR_SET(0); 00137 hSync = 1; 00138 wait_us(4.8); 00139 hSync = 0; 00140 wait_us(4); 00141 } 00142 00143 void vsync() 00144 { 00145 vSync = 1; 00146 wait_us(1300);//650 00147 vSync = 0; 00148 frame++; 00149 00150 if (frame == 65535) 00151 frame = 0; 00152 } 00153 00154 00155 00156 void fill() { 00157 00158 for (int i = 0; i < fbH; i++) { 00159 00160 for (int j = 0; j < fbW; j++) { 00161 00162 if ( (i / 5) % 2 == 0 ) { 00163 00164 if ( (j / 2) % 2 == 0 ) 00165 framebuffer[i][j] = 3; 00166 else 00167 framebuffer[i][j] = 0; 00168 } 00169 else { 00170 if ( (j / 2) % 2 == 0 ) 00171 framebuffer[i][j] = 0; 00172 else 00173 framebuffer[i][j] = 28; 00174 } 00175 } 00176 } 00177 00178 } 00179 00180 void blankFB() { 00181 for (int i = 0; i < fbH; i++) { 00182 for (int j = 0; j < fbW; j++) { 00183 framebuffer[i][j] = 0; 00184 } 00185 } 00186 00187 }
