Hugo Hu / Mbed 2 deprecated BRAVEHEART

Dependencies:   mbed N5110 ShiftReg PinDetect

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002 * @file main.cpp
00003 * @brief BRAVEHEART -- 2D dungeon game in XJEL2645 Project
00004 * @author Yadong Hu
00005 * @date March, 2021
00006 */
00007 
00008 /// libs
00009 #include "mbed.h"
00010 #include "N5110.h"
00011 #include "PinDetect.h"
00012 #include "ShiftReg.h"
00013 #include <string>
00014 #include <sstream>
00015 #include <ctime>
00016 
00017 /// customize libs
00018 #include "StateManager.h "
00019 #include "State.h "
00020 #include "InputManager.h "
00021 #include "Music.h "
00022 
00023 /// pin specification
00024 #define JOY_H p19
00025 #define JOY_V p20
00026 #define JOY_BTN p17
00027 #define LIGHT_SENSOR p15
00028 #define BUTTON_A p29
00029 #define BUTTON_B p28
00030 #define BUTTON_C p27
00031 #define BUTTON_D p26
00032 
00033 /// global variables
00034 int seven_seg_array [] = {
00035     0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71
00036 };
00037 
00038 /// I/O specification
00039 // screen
00040 N5110 *lcd;
00041 
00042 // create ShiftReg object
00043 ShiftReg *shiftreg;  
00044 ShiftReg shift;
00045 
00046 // background music
00047 Sound *sound; 
00048 
00049 // inputs assemble
00050 InputManager *input;
00051 
00052 // light brightness detector
00053 AnalogIn light_sensor(LIGHT_SENSOR);
00054 
00055 // to generate rand seeds from noise
00056 AnalogIn randomPin(p16); 
00057 AnalogIn randomPin2(p17);
00058 
00059 /// Finite state machine
00060 StateManager* fsm;
00061 
00062 /// Access file system
00063 LocalFileSystem local("local");
00064 
00065 /// Initialize
00066 void init();
00067 
00068 /// Generate seeds
00069 unsigned int getRandSeed();
00070 
00071 /// Access the records file
00072 void readHighscoreFile();
00073 
00074 int main()
00075 {    
00076     srand(getRandSeed());
00077     init();
00078     Timer timer;
00079     timer.start();
00080     while(true)
00081     {
00082         while (timer.read() < 0.08)
00083             sleep();
00084 
00085         // update
00086         lcd->setBrightness(1.0 - light_sensor); // Update brightness of screen
00087         fsm->update(timer.read());
00088         input->joystick->update();
00089         
00090         // render
00091         lcd->clear();
00092         fsm->render(); 
00093         lcd->refresh();
00094         
00095         fsm->processRequest(); // Change the state if requested.
00096         
00097         timer.reset();
00098     }
00099     
00100 }
00101 
00102 void init()
00103 {
00104     // Init LCD
00105     lcd = new N5110(p14, p8, p9, p10, p11, p13, p21);
00106     lcd->init();
00107     lcd->normalMode();
00108     lcd->setBrightness(1.0 - light_sensor); // Update brightness of screen
00109     
00110     // write 0 to 7-seg to turn it off
00111     shift.write(0x00);
00112     
00113     // Input
00114     input = new InputManager(BUTTON_A, BUTTON_B, BUTTON_C, BUTTON_D, JOY_H, JOY_V, JOY_BTN);
00115 
00116     // Finite state machine
00117     fsm = new StateManager(lcd, input, sound, shiftreg, TITLE_SCREEN);
00118     
00119     readHighscoreFile(); // load highscores from file system
00120     sound = new Sound(p18);
00121     sound->playNote(SFX::RESTART);
00122 }
00123 
00124 void readHighscoreFile()
00125 {    
00126      FILE *fp = fopen("/local/highscores.txt", "r");    // read from local file
00127      
00128      if (!fp)       // if file is not created yet the highscores will default values as defined in Global.cpp
00129         return;
00130         
00131      char initials[4];
00132      char scoreStr[8];
00133 
00134      for (int i = 0; i < 3; ++i)
00135      {   
00136          fscanf(fp, "%s %s", initials, scoreStr);   // read from file
00137          
00138          int score = atoi(scoreStr);    // convert to int
00139          
00140          // update global highscore table
00141          Global::highscores[i].initials = initials;
00142          Global::highscores[i].score = score;
00143      }
00144         
00145      fclose(fp);
00146 }
00147 
00148 
00149 /** Get a random seed based on unconnected analog input pins
00150   * @see https://developer.mbed.org/questions/2886/Why-is-the-rand-function-not-the-least-b/
00151  */
00152 unsigned int getRandSeed()
00153 {
00154     unsigned int randNum = 0;
00155     unsigned int lsb; // least significant bit
00156     
00157     for (unsigned int i = 0; i < 16; ++i)
00158     {
00159         lsb = 1 & (randomPin.read_u16()); // Read least significant bit in randomInput - value depends on noise
00160         randNum |= (lsb << i);              // Set bit nr.i to lsb in randomInput
00161         
00162         lsb = 1 & (randomPin2.read_u16());
00163         randNum |= (lsb << (16+i));
00164     }
00165     
00166     return randNum;
00167 }