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 #include "Arial12.h"
00007 #include "Arial14.h"
00008 #include "arial_bold_14.h"
00009 #include "ArialB40.h"
00010 #include "SystemFont5x7.h"
00011 #include "HelveticaB35.h"
00012 #include "CourierNew_B70.h"
00013 
00014 // Declare each of the things you're going to use
00015 // The display
00016 KS0108 display(p23,p21,p22,p20,p24,p25,p17,p18,p19,p30,p29,p28,p27,p26);
00017 // The button on the breadboard
00018 DigitalIn topButton(p13);
00019 // For printing things to the computer
00020 Serial pc(USBTX,USBRX);
00021 // Timers
00022 Timer timer1;
00023 Timer timer2;
00024 Timer testTimer;
00025 
00026 // Declare methods, the actual body is down there.
00027 // You have to do this for C
00028 void rules (unsigned char *);
00029 void random (unsigned char *);
00030 
00031 // This is where your programs starts running
00032 int main() {
00033   // Initialize variables
00034   int dist = 20;
00035   int demo = 0;
00036   int i = 0;
00037   topButton.mode(PullUp);
00038   // Display CalSol logo
00039   display.FullScreenBMP(pic);
00040   wait(0.2);
00041 
00042   timer1.start();  // For button stuff
00043   timer2.start();
00044   
00045   int percentage = 0; //integer value from 0 to 100 (battery)
00046   int speed = 150;          //integer value from 0 to 999
00047   double current = 15.2;    //double  value from 0 to 99.9
00048   
00049   while(1) {
00050     if(!topButton) {
00051       if(timer1.read_ms() > 200) {
00052         demo++;
00053         display.ClearScreen();
00054         if(demo > 3) {
00055           demo = 0;
00056         }
00057         timer1.reset();
00058       }
00059     }
00060     switch(demo) {
00061     case 0:
00062       if(timer2.read_ms() > 500) {
00063         rules(pic);
00064         display.FullScreenBMP(pic);
00065         timer2.reset();
00066       }
00067       break;
00068       
00069     case 1:
00070    
00071     //GRID:
00072         //speed:   70x64, ( 0,  0)
00073         //current: 57x23, (71,  0)
00074         //battery: 57x40, (71, 23)
00075     display.HLineShort(71, 64-24, 57, BLACK);
00076     display.VLineShort(71, 0, 64, BLACK);
00077  
00078  
00079  /*
00080     /*
00081      //CURRENT
00082      display.SelectFont(Arial_Bold_14, WHITE, ReadData);
00083      display.GotoXY(78, 8);
00084      display.PrintNumber(current); // this doesn't work with doubles
00085      display.SelectFont(System5x7, WHITE, ReadData);
00086      display.GotoXY(110, 8);
00087      display.PrintString("Amp");
00088      */
00089      
00090     
00091     //BATTERY
00092     display.RoundRectangle(82, 2, 40, 24, 4, BLACK); //body: 40x24
00093     display.EmptyRectangle(78, 10, 78+4, 10+8, BLACK);//head:  4x8
00094     display.SelectFont(System5x7, WHITE, ReadData);
00095     display.GotoXY(108, 24+8);
00096     display.PrintString("%");   
00097     
00098     
00099     //PERCENTAGE (reasonable update rate: testTimer.read_ms()/700)
00100     /*
00101     int INITIAL_PERCENTAGE = 100;
00102     int percentage = INITIAL_PERCENTAGE; // values 0-100
00103     int newPercentage = percentage; // values 0-100
00104     int barPosition = 86+(int)(((double)(100-percentage)/100.0)*32);
00105     int newBarPosition = 86+(int)(((double)(100-newPercentage)/100.0)*32); //x-coordinates
00106     
00107     display.SelectFont(System5x7, BLACK, ReadData);
00108     
00109     //draw initial box
00110     display.FullRectangle(barPosition, 2+4, 86+32, 2+24-4, BLACK); //inside: 32x16
00111     int y = 32; //y coordinate for percenage. for testing/fixing font
00112     
00113     testTimer.start();
00114    
00115     while (newPercentage <= 100 && newPercentage >= 0) //testing update rate/centering
00116     {
00117         if (testTimer.read_ms()%700 == 0)
00118         {
00119             //account for number of digits
00120             
00121             if (newPercentage > 99)
00122                 display.GotoXY(88, y);
00123             else if (newPercentage >= 10)
00124             {
00125                 if (percentage > 99)
00126                     display.FullRectangle(88, y, 88+5, y+7, WHITE);
00127                 display.GotoXY(94, y);
00128             }
00129             else
00130             {
00131                 if (percentage >= 10)
00132                     display.FullRectangle(88, y, 88+5*2+1, y+7, WHITE);
00133                 display.GotoXY(100,y);
00134             }
00135             
00136             
00137             display.PrintNumber(newPercentage);
00138             
00139             
00140             
00141             barPosition = 86+(int)(((double)(100-percentage)/100.0)*32);
00142             newBarPosition = 86+(int)(((double)(100-newPercentage)/100.0)*32);
00143             
00144             //draw increase or decrease in percentage
00145             if (newBarPosition < barPosition) //percentage increase
00146                 display.FullRectangle(newBarPosition, 2+4, barPosition, 2+24-4, BLACK);
00147             else if (newBarPosition > barPosition) //percentage decrease
00148                 display.FullRectangle(barPosition, 2+4, newBarPosition, 2+24-4, WHITE);
00149                 
00150                 
00151             //update percentages
00152             percentage = newPercentage;
00153             //newPercentage = testTimer.read_ms()/700; //slow increase
00154             newPercentage = INITIAL_PERCENTAGE-(testTimer.read_ms()/700); //rapid decrease
00155             
00156         }
00157    }*/
00158    
00159    //SPEED
00160    
00161    display.SelectFont(System5x7, WHITE, ReadData);
00162    display.GotoXY(26, 48);
00163    display.PrintString("MPH");
00164    testTimer.start();
00165      
00166     display.SelectFont(CourierNew_B70, WHITE, ReadData); 
00167     /* while (true){
00168          if (testTimer.read_ms()%700 == 1)
00169          {*/  // for testing
00170              //speed = testTimer.read_ms()/1000;
00171              speed += (int)((rand()%3)-2);
00172              
00173              //display.FullRectangle(0, 16, 70, 44, WHITE); //clears space too slowly. find alternative
00174              
00175              if (speed > 99)       // 3digits
00176                 display.GotoXY(1, 16);
00177              else if (speed >= 10) // 2digits
00178                 display.GotoXY(3+10, 16);
00179              else                  //1digit
00180                 display.GotoXY(3+10*2, 16);
00181              
00182              display.PrintNumber(speed); 
00183          /*}
00184      }*/
00185    */
00186       break;
00187     case 2:
00188       // This block does the bar graph stuff.
00189       if(timer2.read_ms() > 100) {
00190         // display.PutString(3,2,"I can has mbed too");
00191        //  display.PrintFloat(123.23,4,5);
00192          display.VLine(6+i,47,56,BLACK);
00193          if (i < 10){dist = 30;}
00194          else if (i < 100){dist = 25;}
00195          else {dist = 20;}         
00196          display.PrintInteger(i,6,dist);
00197          i++;
00198          if(i > 100) {
00199            i = 0;
00200            display.ClearScreen();
00201          }
00202          timer2.reset();
00203       }
00204       break;
00205     case 3:
00206       if(timer2.read_ms() > 500) {
00207         rules(pic);
00208         display.FullScreenBMP(pic);
00209         timer2.reset();
00210       }
00211       default:
00212       break;
00213     }
00214   }
00215 }
00216 
00217 //For Conway's Game of Life
00218 void rules (unsigned char * array) {
00219   char neighborArray[128][64];
00220   // Get number of neighbors
00221   for (int x = 0; x < 128; x++) {
00222     for (int y = 0;y < 64; y++) {
00223       neighborArray[x][y] = 0;
00224     }
00225   }
00226   for (int y = 0;y < 64; y++) {
00227     for (int x = 0; x < 128; x++) {
00228       if (array[x + (y/8*8)*16] & (1<<(y%8))) {
00229         if (x != 0) {
00230           neighborArray[x-1][y] += 1;
00231           if (y != 0) {
00232             neighborArray[x-1][y-1] += 1;
00233           }
00234           if (y != 127) {
00235             neighborArray[x-1][y+1] += 1;
00236           }
00237         }
00238         if (x != 127) {
00239           neighborArray[x+1][y] += 1;
00240           if (y != 0) {
00241             neighborArray[x+1][y-1] += 1;
00242           }
00243           if (y != 127) {
00244             neighborArray[x+1][y+1] += 1;
00245           }
00246         }
00247         if (y != 0) {
00248           neighborArray[x][y-1] += 1;
00249         }
00250         if (y != 63){
00251           neighborArray[x][y+1] += 1;
00252         }
00253       }
00254     }
00255   }
00256   int arrayIndex;
00257   for (int y = 0;y < 64; y++) {
00258     for (int x = 0; x < 128; x++) {
00259       arrayIndex = x + (y/8*8)*16;
00260       if (neighborArray[x][y] == 3) {
00261         array[arrayIndex] |= (1<<(y%8));
00262       } else if (neighborArray[x][y] == 2) {
00263         continue;
00264       } else {
00265         array[arrayIndex] &= ~(1<<(y%8));
00266       }
00267     }
00268   }
00269 }
00270 
00271 //Is this used anywhere?
00272 void random (unsigned char * array) {
00273   for (int y = 0;y < 64; y += 8) {
00274     for (int x = 0; x < 128; x++) {
00275       array[x + y*16] = (unsigned char) rand();
00276     }
00277   }
00278 }