This class encapsulates all the algorithms required for the displaying strings and time dependent patterns on the lcd.

Dependents:   200943412_QuickClick

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Display.cpp Source File

Display.cpp

00001 #include "Display.h"
00002 #include "N5110.h"
00003 #include "Controller.h"
00004 #include <math.h>
00005 
00006 Display::Display()
00007 {
00008      _h = 0;
00009      _w = 0.2;
00010      _a = 0; 
00011      _x = 0;
00012     _y = 0; 
00013     _x0 = 0;
00014     _y0 = 0;
00015 }
00016 
00017 Display::~Display()
00018 {
00019 
00020 }
00021 void Display::init()
00022 {
00023 
00024      _h = 0;
00025      _w = 0.2;
00026      _a = 0;
00027      _x = 0; 
00028      _y = 0; 
00029      _x0 = 0;
00030     _y0 = 0; 
00031 }
00032 
00033 void Display::drawCircle(Controller &pad, N5110 &lcd)
00034 {  
00035     _arc_selector(pad, lcd);  
00036     //printf("Prev Wait:- %3.2f\n",_w);
00037     _calculateWait();  
00038     _h++;  
00039     //printf("Score:- %d\n",_h);
00040     lcd.clear(); 
00041     lcd.refresh();
00042     } 
00043 
00044 void Display::_calculateWait() 
00045 {  
00046    _w = 0.2*exp(-0.0231*_h);  //this equation models how much time the user has 
00047                             //to complete action with larger amounts of iterations. 
00048 } 
00049 
00050 
00051 void Display::put_wait(float w) 
00052 {  
00053    _w = w;
00054     
00055 } 
00056 
00057 float Display::get_wait() 
00058 {  
00059    return _w;
00060     
00061 } 
00062    
00063 void Display::display_instruction(N5110 &lcd, int ran) {   //printing a random instruction on the screen
00064     lcd.clear();
00065     if (ran == 1){
00066         lcd.printString("PRESS",28,2);
00067         lcd.printChar('A',40,3); } 
00068     else if (ran == 2){ 
00069         lcd.printString("PRESS",28,2);
00070         lcd.printChar('B',40,3); }  
00071     else if (ran == 3){ 
00072         lcd.printString("PRESS",28,2);
00073         lcd.printChar('X',40,3); }  
00074     else if (ran == 4){ 
00075         lcd.printString("PRESS",28,2);
00076         lcd.printChar('Y',40,3); } 
00077     else if (ran == 5){ 
00078         lcd.printString("FLICK",28,2);
00079         lcd.printChar('L',40,3); }  
00080     else if (ran == 6){ 
00081         lcd.printString("FLICK",28,2);
00082         lcd.printChar('R',40,3); }     
00083     else {
00084         lcd.printString("PRESS",28,2);
00085         lcd.printString("STICK",28,3); }  
00086     //printf("Random Instruction int:- %d\n", ran);
00087     lcd.refresh(); 
00088     } 
00089        
00090 // function to draw circle
00091 void Display::_arc_selector(Controller &ctrl ,N5110 &lcd)
00092 {
00093   for (int _a = 0; _a < 8; ++_a)
00094         {
00095     //printf("for loop (a):- %d\n",a);
00096     _drawArc(lcd, _a); //run through _drawArc function drawing octant specific to a value
00097     lcd.refresh();
00098     float Brightness = ctrl.pot_value(); //set brightness to petentiometer value
00099     lcd.setBrightness(0.0);         
00100     //printf("Brightness:- %3.2f\n",Brightness);
00101     //printf("Wait used:- %3.2f\n",_w);
00102     wait(_w); 
00103     lcd.setBrightness(Brightness); //Variable brightness will change with petentiometer value
00104     wait(_w); //changes after every circle is drawn to make the game more exciting.
00105          }
00106  
00107    }   
00108     
00109 void Display::_drawArc(N5110 &lcd, int _a) { 
00110     _x = 20;
00111     _y = 0;
00112     
00113     int a1;
00114     int b1;
00115     _radiusMod = 1-_x; 
00116     _x0 = 42; 
00117     _y0 = 24;
00118 
00119 //the algorithm below is a modified version of the midpoint circle algorithm from: 
00120 //https://en.wikipedia.org/wiki/Midpoint_circle_algorithm    
00121     
00122     while(_x >= _y) { 
00123     
00124     switch(_a) { 
00125     
00126         case 0 : {a1 = _y;           //each case statement draws a single octant by setting a1 and b1 accordingly
00127                   b1 = _x * -1;
00128                   break;
00129                 }            
00130         case 1 : {a1 = _x;
00131                   b1 = _y * -1;
00132                   break;
00133                 }           
00134         case 2 : {a1 = _x;
00135                   b1 = _y;
00136                   break;
00137                 }                       
00138         case 3 : {a1 = _y;
00139                   b1 = _x;
00140                   break;
00141                 }   
00142         case 4 : {a1 = _y * -1;
00143                   b1 = _x;
00144                   break;
00145                 } 
00146         case 5 : {a1 = _x * -1;
00147                   b1 = _y;
00148                   break;
00149                 }
00150         case 6 : {a1 = _x * -1;
00151                   b1 = _y * -1;
00152                   break;
00153                 }                
00154         case 7 : {a1 = _y * -1;
00155                   b1 = _x * -1;
00156                   break;
00157                 }               
00158         default : { a1 = _x;
00159                 b1 = _y * -1;
00160                 break;
00161                 } 
00162         }
00163                                   
00164                               
00165             lcd.setPixel( a1 + _x0,  b1 + _y0); //draws octant depending on a1 and b1
00166         
00167         _y++;   
00168 
00169    
00170         if (_radiusMod<0) {
00171             _radiusMod += 2*_y+1;
00172         } else {
00173             _x--;
00174             _radiusMod += 2*(_y-_x)+1;
00175         }
00176         //printf("_a1:- %d\n", _a1); 
00177         //printf("_b1:- %d\n", _b1); 
00178         //printf("_x:- %d\n", _x); 
00179         //printf("_y:- %d\n", _y);
00180         //printf("_radiusMod:- %d\n",_radiusMod);
00181     }
00182 }