Andreas Garmannslund / Mbed 2 deprecated SimplePlatformGame

Dependencies:   N5110 PinDetect PowerControl mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

Go to the documentation of this file.
00001 /**
00002 * @file main.cpp
00003 * @brief Simple platform game developed for ELEC2645 Embedded Systems Project at University of Leeds
00004 * @author Andreas Garmannslund
00005 * @date April/May 2015
00006 */
00007 
00008 #include "mbed.h"
00009 #include "N5110.h"
00010 #include "PowerControl/PowerControl.h"
00011 #include "PowerControl/EthernetPowerControl.h"
00012 #include "PinDetect.h"
00013 #include <string>
00014 #include <sstream>
00015 #include <ctime>
00016 
00017 #include "Joystick.h"
00018 #include "StateManager.h "
00019 #include "State.h "
00020 #include "InputManager.h "
00021 #include "Sound.h "
00022 
00023 // Redefine pin names for simple access.
00024 #define JOY_H p17
00025 #define JOY_V p16
00026 #define JOY_BTN p15
00027 
00028 #define LED_POT p20
00029 
00030 #define BUTTON_A p28
00031 #define BUTTON_B p27
00032 #define BUTTON_C p29
00033 
00034 // Input and Output
00035 /// Display
00036 N5110 *lcd;
00037 
00038 /// Sound: Piezo buzzer
00039 Sound *sound; 
00040 
00041 /// Responsible for managing user input
00042 InputManager *input;
00043 
00044 /// Brightness potentiometer
00045 AnalogIn ledPot(LED_POT);
00046 
00047 AnalogIn randomPin(p18); // Unconnected pin => noise, used for generating a random seed.
00048 AnalogIn randomPin2(p19); // Unconnected pin
00049 
00050 /// Set up initial variables
00051 void init();
00052 
00053 /// Frees remaining allocated memory
00054 void cleanUp();
00055 
00056 /// Finite state machine
00057 StateManager* fsm;
00058 
00059 /// Local file system
00060 LocalFileSystem local("local");
00061 
00062 /// Function for generating a random seed by using a unconnected analog input pin
00063 // Problem: Not trully random as pin is fairly stable. It is unfortunately connected to the ground layer (often returning 0). Would be better to connect it to a "loose wire"/bad connection-
00064 unsigned int getRandSeed();
00065 
00066 /// Read and load the high score file.
00067 void readHighscoreFile();
00068 
00069 int main()
00070 {    
00071     srand(getRandSeed());
00072     
00073     init();
00074     
00075     Timer timer;
00076     timer.start();
00077     
00078     while(true)
00079     {
00080         // We need to call Sleep() repeatedly, because periodic interrupts in InputManager will cause it to wake up every 20 ms.
00081         while (timer.read() < 0.08)
00082             Sleep();
00083         
00084         // update
00085         lcd->setBrightness(1.0 - ledPot); // Update brightness of screen
00086         fsm->update(timer.read());
00087         input->joystick->update();
00088         
00089         // render
00090         lcd->clear();
00091         fsm->render(); 
00092         lcd->refresh();
00093         
00094         fsm->processRequest(); // Change the state if requested.
00095         
00096         timer.reset();
00097     }
00098     
00099     cleanUp();  // Not really reached as the program never terminates. Added for completeness.
00100     
00101     return 0;
00102 }
00103 
00104 void init()
00105 {
00106     // Disable ethernet to save power
00107     PHY_PowerDown();
00108     
00109     // Init LCD
00110     lcd = new N5110(p7, p8, p9, p10, p11, p13, p26);
00111     lcd->init();
00112     lcd->normalMode();
00113     lcd->setBrightness(1.0 - ledPot); // Update brightness of screen
00114     
00115     // Input
00116     input = new InputManager(BUTTON_A, BUTTON_B, BUTTON_C, JOY_H, JOY_V, JOY_BTN);
00117     
00118     // Sound
00119     sound = new Sound(p21);
00120 
00121     // Finite state machine
00122     fsm = new StateManager(lcd, input, sound, TITLE_SCREEN);
00123     
00124     readHighscoreFile(); // load highscores from file system
00125     
00126     sound->playNote(SFX::RESTART);
00127 }
00128 
00129 void readHighscoreFile()
00130 {    
00131      FILE *fp = fopen("/local/highscores.txt", "r");    // read from local file
00132      
00133      if (!fp)       // if file is not created yet the highscores will default values as defined in Global.cpp
00134         return;
00135         
00136      char initials[4];
00137      char scoreStr[8];
00138 
00139      for (int i = 0; i < 3; ++i)
00140      {   
00141          fscanf(fp, "%s %s", initials, scoreStr);   // read from file
00142          
00143          int score = atoi(scoreStr);    // convert to int
00144          
00145          // update global highscore table
00146          Global::highscores[i].initials = initials;
00147          Global::highscores[i].score = score;
00148      }
00149         
00150      fclose(fp);
00151 }
00152 
00153 void cleanUp()
00154 {
00155     delete lcd;
00156     delete input;
00157     delete sound;
00158 }
00159 
00160 /** Get a random seed based on unconnected analog input pins
00161   * @see https://developer.mbed.org/questions/2886/Why-is-the-rand-function-not-the-least-b/
00162  */
00163 unsigned int getRandSeed()
00164 {
00165     unsigned int randNum = 0;
00166     unsigned int lsb; // least significant bit
00167     
00168     for (unsigned int i = 0; i < 16; ++i)
00169     {
00170         lsb = 1 & (randomPin.read_u16()); // Read least significant bit in randomInput - value depends on noise
00171         randNum |= (lsb << i);              // Set bit nr.i to lsb in randomInput
00172         
00173         lsb = 1 & (randomPin2.read_u16());
00174         randNum |= (lsb << (16+i));
00175     }
00176     
00177     return randNum;
00178 }