Steven Rhodes / Mbed 2 deprecated DisplayTest

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Display Test
00002 #include "mbed.h"
00003 #include "KS0108.h"
00004 #include "calsolimage.h"
00005 #include "numbers.h"
00006 
00007 
00008 KS0108 display(p23,p21,p22,p20,p24,p25,p17,p18,p19,p30,p29,p28,p27,p26);
00009 DigitalIn topButton(p13);
00010 DigitalIn bottomButton(p12);
00011 Serial pc(USBTX,USBRX);
00012 Timer timer1;
00013 Timer timer2;
00014 
00015 void rules (unsigned char *);
00016 void random (unsigned char *);
00017 
00018 int main() {
00019   int dist = 20;
00020   int demo = 0;
00021   int i = 0;
00022   topButton.mode(PullUp);
00023   bottomButton.mode(PullUp);
00024   display.FullScreenBMP(pic);
00025   wait(0.2);
00026 
00027   timer1.start();
00028   timer2.start();
00029   while(1) {
00030     if(!topButton) {
00031       if(timer1.read_ms() > 200) {
00032         demo++;
00033         display.ClearScreen();
00034         if(demo > 3) {
00035           demo = 0;
00036         }
00037         timer1.reset();
00038       }
00039     }
00040     switch(demo) {
00041     case 0:
00042       if(timer2.read_ms() > 500) {
00043         rules(pic);
00044         display.FullScreenBMP(pic);
00045         timer2.reset();
00046       }
00047       break;
00048     case 1:
00049       display.ClearScreen();
00050       break;
00051     case 2:
00052       if(timer2.read_ms() > 100) {
00053         // display.PutString(3,2,"I can has mbed too");
00054        //  display.PrintFloat(123.23,4,5);
00055          display.VLine(6+i,47,56,BLACK);
00056          if (i < 10){dist = 30;}
00057          else if (i < 100){dist = 25;}
00058          else {dist = 20;}         
00059          display.PrintInteger(i,6,dist);
00060          i++;
00061          if(i > 100) {
00062            i = 0;
00063            display.ClearScreen();
00064          }
00065          timer2.reset();
00066       }
00067       break;
00068     case 3:
00069       if(timer2.read_ms() > 500) {
00070         rules(pic);
00071         display.FullScreenBMP(pic);
00072         timer2.reset();
00073       }
00074 k,    default:
00075       break;
00076     }
00077   }
00078 }
00079 
00080 void rules (unsigned char * array) {
00081   char neighborArray[128][64];
00082   // Get number of neighbors
00083   for (int x = 0; x < 128; x++) {
00084     for (int y = 0;y < 64; y++) {
00085       neighborArray[x][y] = 0;
00086     }
00087   }
00088   for (int y = 0;y < 64; y++) {
00089     for (int x = 0; x < 128; x++) {
00090       if (array[x + (y/8*8)*16] & (1<<(y%8))) {
00091         if (x != 0) {
00092           neighborArray[x-1][y] += 1;
00093           if (y != 0) {
00094             neighborArray[x-1][y-1] += 1;
00095           }
00096           if (y != 127) {
00097             neighborArray[x-1][y+1] += 1;
00098           }
00099         }
00100         if (x != 127) {
00101           neighborArray[x+1][y] += 1;
00102           if (y != 0) {
00103             neighborArray[x+1][y-1] += 1;
00104           }
00105           if (y != 127) {
00106             neighborArray[x+1][y+1] += 1;
00107           }
00108         }
00109         if (y != 0) {
00110           neighborArray[x][y-1] += 1;
00111         }
00112         if (y != 63){
00113           neighborArray[x][y+1] += 1;
00114         }
00115       }
00116     }
00117   }
00118   int arrayIndex;
00119   for (int y = 0;y < 64; y++) {
00120     for (int x = 0; x < 128; x++) {
00121       arrayIndex = x + (y/8*8)*16;
00122       if (neighborArray[x][y] == 3) {
00123         array[arrayIndex] |= (1<<(y%8));
00124       } else if (neighborArray[x][y] == 2) {
00125         continue;
00126       } else {
00127         array[arrayIndex] &= ~(1<<(y%8));
00128       }
00129     }
00130   }
00131 }
00132 
00133 void random (unsigned char * array) {
00134   for (int y = 0;y < 64; y += 8) {
00135     for (int x = 0; x < 128; x++) {
00136       array[x + y*16] = (unsigned char) rand();
00137     }
00138   }
00139 }