J.P. Armstrong / Mbed 2 deprecated FourInARow_NokiaLCD

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Hello World! for Nokia LCD, sford
00002 // - LCD6610 is for newest Sparkfun breakout
00003 
00004 #include "mbed.h"
00005 #include "NokiaLCD.h"
00006 #define PIECE_GAP 18
00007 #define PIECE_SIZE 6
00008 #define PI 3.1415
00009 #define RED 0xFF0000
00010 #define GREEN 0x00FF00
00011 
00012 InterruptIn button1(p21);
00013 InterruptIn button2(p22);
00014 InterruptIn button3(p23);
00015 
00016 DigitalOut ledRed(p24);
00017 DigitalOut ledGreen(p25);
00018 
00019 void refreshMovingPiece();
00020 void displaySplash();
00021 bool scanForWinner();
00022 void lcdAlert(const char*);
00023 
00024 void drawCircle(int,int,int,int);
00025 void fillCircle(int,int,int,int);
00026 void drawLine(int,int,int,int,int);
00027 
00028 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
00029 int board[6][7];
00030 int board_stop_row[7] = {5,5,5,5,5,5,5};
00031 
00032 int player_active = RED;
00033 int player_position = 3;
00034 int next_step = false;
00035 
00036 
00037 void buttonMoveRight() {
00038     player_position = (++player_position % 7);
00039     refreshMovingPiece();
00040     wait(0.2);
00041 }
00042 
00043 void buttonMoveLeft() {
00044     player_position = (--player_position % 7);
00045     refreshMovingPiece();
00046     wait(0.2);
00047 }
00048 
00049 void refreshMovingPiece() {
00050 
00051        for(int i = 0; i < 7; i++)
00052             fillCircle(10 + PIECE_GAP * i, 8, PIECE_SIZE, 0x000000);
00053             
00054        fillCircle(10 + PIECE_GAP * player_position, 8, PIECE_SIZE, player_active);
00055 }
00056 
00057 void buttonSelect() {
00058 
00059     // PLACE CHIP WHERE IT'S MEANT TO BE
00060 
00061     if (board_stop_row[player_position] < 0) {
00062         lcdAlert("Illegal Move");
00063         return;
00064     }
00065         
00066     board[board_stop_row[player_position]][player_position] = player_active;
00067     board_stop_row[player_position]--;
00068     
00069     // RESET FOR NEXT PLAYER
00070     player_active = ( player_active == RED ) ? GREEN : RED;
00071     player_position = 3;
00072     refreshMovingPiece();
00073     next_step = true;
00074     
00075     wait(0.2);
00076 }
00077 
00078 int main() {
00079 
00080     displaySplash();
00081     
00082     button3.rise(&buttonMoveLeft);
00083     button2.rise(&buttonMoveRight);
00084     button1.rise(&buttonSelect);
00085  
00086     lcd.cls();
00087     lcd.background(0x000000);
00088     lcd.fill(2, 16, 126, 110, 0xFFFF00);
00089         
00090     player_position = 3;
00091     refreshMovingPiece();
00092     
00093     int cnt = 0;
00094     int color = 0x0;
00095     
00096     do {
00097 
00098         for(cnt = 0; cnt < 42; cnt++) {
00099            color = board[(cnt / 7)][(cnt % 7)];
00100            fillCircle(10 + PIECE_GAP * (cnt % 7), 24 + PIECE_GAP * (cnt / 7), PIECE_SIZE, color);
00101         }
00102 /*        
00103         if (scanForWinner()) {
00104              for(cnt = 0; cnt < 42; cnt++) {
00105                 board[(cnt / 7)][(cnt % 7)] = NULL;
00106              }
00107         }
00108 */       
00109         while (true) {
00110             if (next_step) break;    
00111             wait(0.25);
00112         }
00113 
00114         next_step = false;
00115         
00116    }     
00117    while(true);
00118 }
00119 
00120 
00121 bool scanForWinner() {
00122     
00123     int winner = 0;
00124     
00125     for (int y = 0; y < 6; y++) {
00126         
00127         int lead = 0;
00128         int count = 0;
00129         
00130         for (int x = 0; x < 7; x++) {
00131         
00132             if (board[y][x] == 0)
00133                 break;
00134                 
00135             if (board[y][x] != lead) {
00136                 lead = board[y][x];
00137                 count = 0;
00138             }
00139             
00140             count++;
00141             
00142             if (count == 4) {
00143                 winner = lead;
00144                 goto someone_won;
00145             }
00146         }
00147         
00148         for (int x = 6; x >= 0; x--) {
00149 
00150             if (board[x][y] == 0)
00151                 break;
00152                 
00153             if (board[x][y] != lead) {
00154                 lead = board[x][y];
00155                 count = 0;
00156             }
00157             
00158             count++;
00159             
00160             if (count == 4)
00161                 goto someone_won;
00162                  
00163         }
00164     }
00165 
00166     return false;
00167     
00168 someone_won:
00169     
00170     if (winner == RED)
00171         lcdAlert("RED WON!");
00172     else
00173         lcdAlert("GREEN WON!");
00174     
00175     return true;
00176 }
00177 void lcdAlert(const char* msg) {
00178 
00179         lcd.fill(7, 49, 112, 62, 0xFF0000);
00180         lcd.fill(8, 50, 110, 60, 0x0);
00181         lcd.locate(2,9);
00182         lcd.printf(msg);
00183         wait(2);
00184         
00185         lcd.cls();
00186         lcd.background(0x000000);
00187         lcd.fill(2, 16, 126, 110, 0xFFFF00);
00188 
00189         refreshMovingPiece();
00190         next_step = true;
00191         
00192 }
00193 void displaySplash() {
00194     lcd.cls();
00195     lcd.background(0x000000);
00196     
00197     lcd.foreground(0xFF0000);
00198     lcd.locate(2, 4);
00199     lcd.printf("FOUR");
00200     
00201     lcd.foreground(0x00FF00);
00202     lcd.locate(6, 5);
00203     lcd.printf("IN A");
00204     
00205     lcd.foreground(0x0000FF);
00206     lcd.locate(10, 6);
00207     lcd.printf("ROW!");
00208     
00209     lcd.foreground(0xFFFFFF);
00210     lcd.locate(2, 10);
00211     lcd.printf("JP ARMSTRONG");
00212     lcd.locate(1, 12);
00213     lcd.printf("ARMTRONICS.COM");
00214     wait(3);
00215 }
00216 
00217 /**********************************************************************************/
00218 
00219 // From: http://free.pages.at/easyfilter/bresenham.html
00220 void drawCircle(int xm, int ym, int r, int color)
00221 {
00222    int x = -r, y = 0, err = 2-2*r; /* II. Quadrant */ 
00223    do {
00224       lcd.pixel(xm-x, ym+y, color); /*   I. Quadrant */
00225       lcd.pixel(xm-y, ym-x, color); /*  II. Quadrant */
00226       lcd.pixel(xm+x, ym-y, color); /* III. Quadrant */
00227       lcd.pixel(xm+y, ym+x, color); /*  IV. Quadrant */
00228      
00229       r = err;
00230       if (r >  x) err += ++x*2+1; /* e_xy+e_x > 0 */
00231       if (r <= y) err += ++y*2+1; /* e_xy+e_y < 0 */
00232    } while (x < 0);
00233    
00234 }
00235 
00236 void fillCircle(int xm, int ym, int r, int color)
00237 {
00238    int x = -r, y = 0, err = 2-2*r; // II. Quadrant
00239    do {
00240       drawLine(xm-x, ym+y, xm+x, ym-y, color);
00241       drawLine(xm-y, ym-x, xm+y, ym+x, color);
00242       
00243       r = err;
00244       if (r >  x) err += ++x*2+1; /* e_xy+e_x > 0 */
00245       if (r <= y) err += ++y*2+1; /* e_xy+e_y < 0 */
00246    } while (x < 0);
00247    
00248 }
00249 
00250 // From http://rosettacode.org/wiki/Bitmap/Bresenham%27s_line_algorithm#C.2B.2B
00251 void drawLine(int x0, int y0, int x1, int y1, int color) {
00252  
00253   int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
00254   int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1; 
00255   int err = (dx>dy ? dx : -dy)/2, e2;
00256  
00257   for(;;){
00258     lcd.pixel(x0,y0, color);
00259     if (x0==x1 && y0==y1) break;
00260     e2 = err;
00261     if (e2 >-dx) { err -= dy; x0 += sx; }
00262     if (e2 < dy) { err += dx; y0 += sy; }
00263   }
00264 }
00265 
00266 /*
00267 void drawCircle(int x, int y, int ox, int oy, int color) {
00268 
00269     int lx, rx, ly, ry;
00270     
00271     for(int i = 0; i < ox; i++) {
00272         lcd.pixel(x + ox * cos((i*2*PI)/ox) ,y + oy * sin((i*2*PI)/oy), color);
00273     }
00274 }
00275 */