ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18gs

Dependencies:   mbed

Revision:
13:3b2a4e14937b
Parent:
11:e89dbdb74df5
Child:
16:3b298bea3a70
diff -r 8666cd2c6201 -r 3b2a4e14937b main.cpp
--- a/main.cpp	Tue May 12 09:21:38 2020 +0000
+++ b/main.cpp	Tue May 12 16:34:47 2020 +0000
@@ -14,30 +14,14 @@
 #include "Gamepad.h"
 #include "N5110.h"
 #include "SDFileSystem.h"
+#include "gameEngine.h"
 #include "FX0S8700CQ.h"
 #include "Ghost.h"
 #include "Inventory.h"
+#include "tooling.h"
 #include <vector>
 #include <string>
 
-// Declare the string vector type
-typedef std::vector<std::string> stringvec;
-
-/**struct inven_state {
-    // UID, next{up,down}, type, attack, defense, level, xp, value, hp_max, hp
-    int uid;
-    int next[2];
-    std::string type;
-    std::string name;
-    int attack;
-    int defense;
-    int level;
-    int xp;
-    int value;
-    int hp_max;
-    int hp;
-};*/
-
 // objects
 Gamepad pad;
 N5110 lcd;
@@ -57,25 +41,8 @@
 BusOut right_LED(PTC11,PTC10,PTA1);
 
 // Function prototypes
-void welcome();
-int game_menu();
-void catch_ghosts();
-//void display_inventory(Inventory inventory, SDFileSystem &sd);
-std::vector<inven_state> gen_ghost_fsm(Inventory inventory);
-void settings();
-int** import_sprite(std::string file);
-void int_to_bin_digit(unsigned int in, int count, int* out);
-void listdir(void);
-bool hasEnding (std::string const &fullString, std::string const &ending);
-float cursor_transform(float OldMax,
-                       float OldMin,
-                       float NewMax,
-                       float NewMin,
-                       float OldValue);
-void adjustContrast();
-void buttonDelay();
-bool ghostHit( int xGhost, int yGhost, int xJoy, int yJoy);
-
+void setupISR();
+void zeroFlags();
 
 // Settings variables
 volatile int g_button_sesnsitivity = 250;
@@ -97,843 +64,62 @@
 volatile int g_buttonY_flag = 0;
 volatile int g_buttonStart_flag = 0;
 
-// struct for state
-struct State {
-    int output;  // output value
-    int nextState[2];  // array of next states
-};
-
 int main()
 {
-    sd.mount();
-    printf("Main function\n");
-    srand(time(NULL));
-    buttonX.mode(PullUp); // turn on internal pull-up resistor
-    buttonX.fall(&buttonX_isr);
-
-    buttonA.mode(PullUp); // turn on internal pull-up resistor
-    buttonA.fall(&buttonA_isr);
-
-    buttonB.mode(PullUp); // turn on internal pull-up resistor
-    buttonB.fall(&buttonB_isr);
-
-    buttonY.mode(PullUp); // turn on internal pull-up resistor
-    buttonY.fall(&buttonY_isr);
-
-    buttonStart.mode(PullUp); // turn on internal pull-up resistor
-    buttonStart.fall(&buttonStart_isr);
-
-    /*  Initialise the game
-    *   Create an object to hold the players ghosts in
-    *   Print the welcome message on the screen and wait for the player
-    */
-
+    gameEngine game(sd, pad, lcd);
+    setupISR(); // Hook all the ISR to their buttons
+    srand(time(NULL)); // Set the sed to millisecconds from start
     Inventory inventory(sd);
 
-    lcd.init(); // Initialise the screen
-    lcd.backLightOn();
+    game.welcome(sd, lcd, pad, g_buttonA_flag); // Display the welcome screen
 
-    welcome();
-    int choice = -1;
     while(1) {
-        choice = game_menu();
+        int choice = game.game_menu(lcd, g_buttonA_flag, g_buttonB_flag, g_buttonX_flag); // Display the game menu
         switch (choice) {
             case 0:
-                catch_ghosts();
-                inventory.regen(sd);
+                game.catch_ghosts(inventory, accel, sd, lcd, pad, g_buttonA_flag, left_LED, right_LED);
                 break;
             case 1:
-                inventory.display_inventory(sd, 
-                                            lcd, 
-                                            pad, 
-                                            g_buttonX_flag, 
-                                            g_buttonStart_flag);
+                inventory.display_inventory(sd, lcd, pad, g_buttonX_flag, g_buttonStart_flag);
                 break;
             case 2:
-                settings();
+                game.settings(lcd, pad, g_buttonA_flag, g_buttonStart_flag, g_buttonX_flag, g_buttonB_flag, g_buttonTesting, g_buttonSensitivityTest, g_button_sesnsitivity);
                 break;
         }
     }
 }
 
-void welcome()
-{
-    //printf("entered welcome screen\n");
-    int** welcome = import_sprite("/sd/assets/welcome.sprite");
-    lcd.clear();
-    for(int i = 0; i < 48; i++) {
-        for(int j = 0; j < 84; j++) {
-            lcd.setPixel(j,i, welcome[i][j]);
-        }
-    }
-    while(!g_buttonA_flag) {
-        // contrast should be bounded between 4 and 6
-        // contrast = 0.4 + contrast.read()/5
-        lcd.setContrast(0.4 + ((double) pad.read_pot1()/5));
-        lcd.refresh();
-        wait_ms(10);
-    }
-
-    g_buttonA_flag = 0;
-    //wait_ms(25);
-}
-
-
-int game_menu()
+void setupISR()
 {
-
-    State fsm[4] = {
-        //   line next{up, down}
-        {0,{2,1}}, // State: 0
-        {2,{0,2}}, // State: 1
-        {4,{1,0}}, // State: 2
-    };
-
-    int current_state = 0;
-
-    printf("entered game menu\n");
-    g_buttonX_flag = 0;
-    g_buttonA_flag = 0;
-    g_buttonB_flag = 0;
-
-    while(1) {
-        lcd.clear();
-        lcd.printString("Catch Ghosts",0,0);
-        lcd.printString("Inventory",0,2);
-        lcd.printString("Settings",0,4);
-
-        if(g_buttonA_flag) {
-            g_buttonA_flag = 0;
-            return current_state;
-        }
-
-        if(g_buttonX_flag) {
-            current_state = fsm[current_state].nextState[0];
-            g_buttonX_flag = 0;
-            g_buttonB_flag = 0;
-        } else if(g_buttonB_flag) {
-            current_state = fsm[current_state].nextState[1];
-            g_buttonX_flag = 0;
-            g_buttonB_flag = 0;
-        }
-
-        switch (fsm[current_state].output) {
-            case 0:
-                lcd.drawCircle(79, 3, 3, FILL_BLACK);
-                break;
-            case 2:
-                lcd.drawCircle(79, 20, 3, FILL_BLACK);
-                break;
-            case 4:
-                lcd.drawCircle(79, 35, 3, FILL_BLACK);
-                break;
-        }
-        lcd.refresh();
-    }
+#ifdef DEBUG
+    printf("Initialising ISR\n");
+#endif
+    buttonX.mode(PullUp); // turn on internal pull-up resistor
+    buttonX.fall(&buttonX_isr);
+    buttonA.mode(PullUp); // turn on internal pull-up resistor
+    buttonA.fall(&buttonA_isr);
+    buttonB.mode(PullUp); // turn on internal pull-up resistor
+    buttonB.fall(&buttonB_isr);
+    buttonY.mode(PullUp); // turn on internal pull-up resistor
+    buttonY.fall(&buttonY_isr);
+    buttonStart.mode(PullUp); // turn on internal pull-up resistor
+    buttonStart.fall(&buttonStart_isr);
 }
 
-void catch_ghosts()
+void zeroFlags()
 {
-    
-    float pitch = rand() % 46;
-    if(rand() & 1) {
-        pitch = pitch * -1;
-    }
-
-    float roll = rand() % 46;
-    if(rand() & 1) {
-        roll = roll * -1;
-    }
-
-    int count = 0;
-
-    lcd.clear();
-    lcd.printString("Align the",18,0);
-    lcd.printString("Gamepad until",4,1);
-    lcd.printString("all LEDs",20,2);
-    lcd.printString("turn on",22,3);
-    lcd.refresh();
-
-
-    while(1) {
-        accel.read_data();
-        bool focused = true;
-
-        if((double)accel.sensor.roll/roll > 0.75) {
-            left_LED = 0b000;
-        } else if ((double)accel.sensor.roll/roll > 0.3) {
-            focused = false;
-            left_LED = 0b001;
-        } else {
-            focused = false;
-            left_LED = 0b011;
-        }
-
-        if((double)accel.sensor.pitch/pitch > 0.75) {
-            right_LED = 0b000;
-        } else if ((double)accel.sensor.pitch/pitch > 0.3) {
-            focused = false;
-            right_LED = 0b001;
-        } else {
-            focused = false;
-            right_LED = 0b011;
-        }
-
-        if(focused) {
-            count++;
-        }
-
-        if(count > 500) {
-            break;
-        }
-        wait_ms(10);
-    }
-
-    Vector2D ideal = {rand() % 85,rand() % 49};
-    
-    int** ghost = import_sprite("/sd/assets/ghost.sprite");
-
-    while(1) {
-
-        lcd.clear();
-
-        Vector2D actual = {cursor_transform(2, 0, 84, 0, pad.get_coord().x),
-                           cursor_transform(2, 0, 48, 0, pad.get_coord().y * -1)
-                          };
-
-        //           x0 y0 x1 y1 type
-        lcd.drawLine(actual.x, 0, actual.x, 48, 2);
-        lcd.drawLine(0, actual.y, 84, actual.y, 2);
-
-        int col = 0;
-
-        for(int i = ideal.x - 8; i < ideal.x + 8; i++) { // Iterate Columns: x
-            int row = 0;
-            for(int j = ideal.y - 9; j < ideal.y + 9; j++) { // Iterate Rows: y
-                lcd.setPixel(i,j, ghost[row][col]);
-                row++;
-            }
-            col++;
-        }
-
-        if(ghostHit(ideal.x, ideal.y, actual.x, actual.y)) {
-            lcd.printString("Press A!",0,0);
-            if(g_buttonA_flag) {
-                break;
-            }
-        }
-
-        g_buttonA_flag = 0;
-
-        lcd.refresh();
-
-        int x_move = 0;
-        int y_move = 0;
-
-        if(rand() & 1) {
-            x_move = 1;
-        } else {
-            x_move = -1;
-        }
-
-        if(rand() & 1) {
-            y_move = 1;
-        } else {
-            y_move = -1;
-        }
-
-        ideal.x = ideal.x + x_move;
-        ideal.y = ideal.y + y_move;
-
-        if(ideal.x > 70) {
-            ideal.x = 70;
-        } else if(ideal.x < 13) {
-            ideal.x = 13;
-        }
-
-        if(ideal.y > 30) {
-            ideal.y = 30;
-        } else if(ideal.y < 12) {
-            ideal.y = 12;
-        }
-
-
-        wait_ms(10);
-    }
-
-    printf("Ghost caught\n");
-
-    Ghost caught_ghost(rand()%100, rand()%20, "/ghosts/", sd);
-    caught_ghost.save(sd);
-
-    lcd.clear();
-    lcd.printString("You caught:", 0, 0);
-    lcd.printString(caught_ghost.get_type_string().c_str(), 0, 1);
-    lcd.printString("Press A to", 0, 2);
-    lcd.printString("continue", 0, 3);
-    lcd.refresh();
-
-    //wait(2);
-
-    g_buttonA_flag = 0;
-
-    while(!g_buttonA_flag) {
-    }
-
-    g_buttonA_flag = 0;
-
-}
-
-bool ghostHit( int xGhost, int yGhost, int xJoy, int yJoy)
-{
-    int xDifference = abs(xGhost - xJoy);
-    int yDifference = abs(yGhost - yJoy);
-    
-    if(xDifference < 10 && yDifference <10){
-        return true;
-    } else {
-        return false;
-    }
-}
-
-void settings()
-{
-    // settings that can be edited:
-    // * contrast
-    // * button delay
-
-    // menu from FSM
-    State fsm[2] = {
-        //   line next{up, down}
-        {2,{1,1}}, // State: 0
-        {4,{0,0}}, // State: 1
-    };
-
-
-    int current_state = 0;
-
-    printf("entered game menu\n");
     g_buttonX_flag = 0;
     g_buttonA_flag = 0;
     g_buttonB_flag = 0;
-
-    while(1) {
-        lcd.clear();
-        lcd.printString("Settings",0,0);
-        lcd.printString("Contrast",0,2);
-        lcd.printString("Button delay",0,4);
-
-        if(g_buttonA_flag) {
-            g_buttonA_flag = 0;
-            switch (fsm[current_state].output) {
-                case 2:
-                    adjustContrast();
-                    break;
-                case 4:
-                    buttonDelay();
-                    break;
-            }
-        } else if (g_buttonStart_flag) {
-            g_buttonStart_flag = 0;
-            return;
-        }
-
-        if(g_buttonX_flag) {
-            current_state = fsm[current_state].nextState[0];
-            g_buttonX_flag = 0;
-            g_buttonB_flag = 0;
-        } else if(g_buttonB_flag) {
-            current_state = fsm[current_state].nextState[1];
-            g_buttonX_flag = 0;
-            g_buttonB_flag = 0;
-        }
-
-        switch (fsm[current_state].output) {
-            case 2:
-                lcd.drawCircle(79, 20, 3, FILL_BLACK);
-                break;
-            case 4:
-                lcd.drawCircle(79, 35, 3, FILL_BLACK);
-                break;
-        }
-
-        lcd.refresh();
-    }
-}
-
-void adjustContrast()
-{
-    while(1) {
-        lcd.clear();
-        // Print title
-
-        lcd.printString("Contrast", 0, 0);
-        lcd.printString("Press A to set", 0, 3);
-
-        float conSet = 0.4 + ((double) pad.read_pot1()/5);
-        float bar = cursor_transform(0.6,0.4,64,0,conSet);
-
-        printf("conSet: %f\n", conSet);
-        printf("bar: %f\n", bar);
-
-        // example of how to draw rectangles
-        //          origin x,y,width,height,type
-        lcd.drawRect(10,10,74,10,FILL_TRANSPARENT);    // transparent, just outline
-        lcd.drawRect(10,10,bar,10,FILL_BLACK);  // filled black rectangle
-        lcd.setContrast(conSet);
-
-        if(g_buttonA_flag) {
-            g_buttonA_flag = 0;
-            return;
-        }
-
-        lcd.refresh();
-    }
-}
-
-void buttonDelay()
-{
-    g_buttonTesting = true;
-
-    lcd.clear();
-
-    bool circle = false;
-
-    while(1) {
-        lcd.clear();
-        lcd.printString("Button Delay", 0, 0);
-        lcd.printString("Press X to test", 0, 4);
-        lcd.printString("Press A to set", 0, 5);
-
-        g_buttonSensitivityTest = cursor_transform(1,0,2,0,pad.read_pot2()) * 250;
-        int bar = cursor_transform(500,0,64,0,g_buttonSensitivityTest);
-
-        // example of how to draw rectangles
-        //          origin x,y,width,height,type
-        lcd.drawRect(10,10,74,10,FILL_TRANSPARENT);    // transparent, just outline
-        lcd.drawRect(10,10,bar,10,FILL_BLACK);  // filled black rectangle
-
-        if(circle) {
-            lcd.drawCircle(42, 25, 3, FILL_BLACK);
-        } else {
-            lcd.drawCircle(42, 25, 3, FILL_TRANSPARENT);
-        }
-
-        if(g_buttonX_flag) {
-            g_buttonX_flag = 0;
-            circle = !circle;
-        } else if(g_buttonA_flag) {
-            g_buttonA_flag = 0;
-            g_button_sesnsitivity = g_buttonSensitivityTest;
-            g_buttonTesting = false;
-            return;
-        } else if (g_buttonStart_flag) {
-            g_buttonStart_flag = 0;
-            g_buttonTesting = false;
-            return;
-        }
-
-        lcd.refresh();
-
-    }
-}
-
-
-float cursor_transform(float OldMax,
-                       float OldMin,
-                       float NewMax,
-                       float NewMin,
-                       float OldValue)
-{
-    float NewValue = -1;
-    float OldRange = (OldMax - OldMin);
-    if (OldRange == 0) {
-        NewValue = NewMin;
-    } else {
-        float NewRange = (NewMax - NewMin);
-        NewValue = (((OldValue - OldMin) * NewRange) / OldRange) + NewMin;
-    }
-    return NewValue;
+    g_buttonY_flag = 0;
+    g_buttonStart_flag = 0;
 }
 
-void display_inventory(Inventory inventory, SDFileSystem &sd)
-{
-    printf("viewing inventory\n");
-    int which_ghost_state = 0;
-    int which_view_state = 0;
-    int new_which_view_state = 0;
-
-    std::vector<inven_state> ghost_fsm;
-
-    bool regen_inven = true;
-    bool scroll_ghost = false;
-    bool update = true;
-
-    while(1) {
-        if(regen_inven) {
-            inventory.regen(sd);
-            ghost_fsm = gen_ghost_fsm(inventory);
-            regen_inven = false;
-            which_ghost_state = 0;
-            which_view_state = 0;
-        }
-
-        if(update) {
-            update = false;
-
-            if(scroll_ghost) {
-                wait_ms(750);
-                scroll_ghost = false;
-            }
-
-
-            lcd.clear();
-
-            lcd.printString("Inventory", 0, 0);
-            char buffer [64];
-
-            if(ghost_fsm.size() == 0) {
-                lcd.printString("No ghosts", 0, 1);
-                lcd.printString("found, exiting", 0, 2);
-                lcd.refresh();
-                wait(1);
-                return;
-            } else {
-
-                if(which_view_state <= 0) {
-                    sprintf(buffer, "%s", ghost_fsm[which_ghost_state].type.c_str());
-                    lcd.printString(buffer, 0, 1 - which_view_state);
-                }
-
-                if(which_view_state <= 1) {
-                    sprintf(buffer, "Name: %s", ghost_fsm[which_ghost_state].name.c_str());
-                    lcd.printString(buffer, 0, 2 - which_view_state);
-                }
-
-                if(which_view_state <= 2) {
-                    sprintf(buffer, "Attack: %d", ghost_fsm[which_ghost_state].attack);
-                    lcd.printString(buffer, 0, 3 - which_view_state);
-                }
-
-                if(which_view_state <= 3) {
-                    sprintf(buffer, "Defense: %d", ghost_fsm[which_ghost_state].defense);
-                    lcd.printString(buffer, 0, 4 - which_view_state);
-                }
-
-                if(which_view_state <= 4) {
-                    sprintf(buffer, "Level: %d", ghost_fsm[which_ghost_state].level);
-                    lcd.printString(buffer, 0, 5 - which_view_state);
-                }
-
-                if(which_view_state <= 5) {
-                    sprintf(buffer, "Value: %d", ghost_fsm[which_ghost_state].value);
-                    lcd.printString(buffer, 0, 6 - which_view_state);
-                }
-
-                if(which_view_state <= 6) {
-                    sprintf(buffer, "HP Max: %d", ghost_fsm[which_ghost_state].hp_max);
-                    lcd.printString(buffer, 0, 7 - which_view_state);
-                }
-
-                if(which_view_state <= 7) {
-                    sprintf(buffer, "HP: %d", ghost_fsm[which_ghost_state].hp);
-                    lcd.printString(buffer, 0, 7 - which_view_state);
-                }
-
-                lcd.refresh();
-            }
-
-        }
-
-        if(pad.read_pot2() <= (double) 0.33) {
-            new_which_view_state = 0;
-        } else if(pad.read_pot2() <= (double) 0.66) {
-            new_which_view_state = 1;
-        } else {
-            new_which_view_state = 2;
-        }
-
-        if(new_which_view_state != which_view_state) {
-            update = true;
-            which_view_state = new_which_view_state;
-        }
-
-        if(pad.read_pot1() < (double) 0.33) {
-            which_ghost_state = ghost_fsm[which_ghost_state].next[1];
-            scroll_ghost = true;
-        } else if(pad.read_pot1() > (double) 0.66) {
-            which_ghost_state = ghost_fsm[which_ghost_state].next[0];
-            scroll_ghost = true;
-        }
-
-        if(scroll_ghost) {
-            update = true;
-        }
-
-        if(g_buttonX_flag) {
-            printf("X button pressed\n");
-            update = true;
-            g_buttonX_flag = 0;
-            lcd.drawRect(0,16,84,16, FILL_WHITE);
-            lcd.printString("Press again",10,2);
-            lcd.printString("to sell",20,3);
-
-            //wait_ms(50);
-
-            Timer t;
-            t.start();
-
-            g_buttonX_flag = 0;
-            bool sell = false;
-
-            while(t.read() <= 5) {
-                int time = t.read();
-                bool changed = false;
-                if(time == 0) {
-                    lcd.printString("5",60,3);
-                    changed = true;
-                } else if (time == 1) {
-                    lcd.printString("4",60,3);
-                    changed = true;
-                } else if (time == 2) {
-                    lcd.printString("3",60,3);
-                    changed = true;
-                } else if (time == 3) {
-                    lcd.printString("2",60,3);
-                    changed = true;
-                } else if (time == 4) {
-                    lcd.printString("1",60,3);
-                    changed = true;
-                } else if (time == 5) {
-                    lcd.printString("0",60,3);
-                    changed = true;
-                }
-
-                if(g_buttonX_flag) {
-                    printf("button X pressed\n");
-                    g_buttonX_flag = 0;
-                    //wait_ms(50);
-                    sell = true;
-                }
-
-                if(changed) {
-                    lcd.refresh();
-                }
-
-                if(sell) {
-                    printf("Exiting to sell\n");
-                    break;
-                }
-
-            }
-
-            if(sell) {
-                printf("Running sell function\n");
-                inventory.sell_ghost_by_uid(ghost_fsm[which_ghost_state].uid, sd);
-                regen_inven = true;
-                update = true;
-                //wait_ms(500);
-            }
-
-        } else if(g_buttonStart_flag) {
-            g_buttonStart_flag = 0;
-            return;
-        }
-
-    }
-
-}
-
-std::vector<inven_state> gen_ghost_fsm(Inventory inventory)
-{
-    std::vector<inven_state> ghost_fsm;
-
-    std::vector<int> uids = inventory.list_ghost_uids();
-
-    sort(uids.begin(), uids.end());
-
-    for(int i = 0; i < uids.size(); i++) {
-        Ghost ghost_temp = inventory.get_ghost_by_uid(uids[i]);
-        inven_state temp;
-        temp.uid = ghost_temp.get_uid();
-        temp.name = ghost_temp.get_name();
-        temp.type = ghost_temp.get_type_string();
-        temp.attack = ghost_temp.get_attack();
-        temp.defense = ghost_temp.get_defense();
-        temp.level = ghost_temp.get_level();
-        temp.xp = ghost_temp.get_xp();
-        temp.value = ghost_temp.get_value();
-        temp.hp_max = ghost_temp.get_hp_max();
-        temp.hp = ghost_temp.get_hp();
-
-        ghost_fsm.push_back(temp);
-        //printf("Added Ghost UID %i to fsm\n", temp.uid);
-        printf("%s\n", temp.name.c_str());
-    }
-
-    for(int i = 0; i < ghost_fsm.size(); i++) {
-        if (i == 0) {
-            int next[2] = {1, ghost_fsm.size() - 1};
-            ghost_fsm[i].next[0] = next[0];
-            ghost_fsm[i].next[1] = next[1];
-        } else if (i == ghost_fsm.size() - 1) {
-            int next[2] = {0, ghost_fsm.size() - 2};
-            ghost_fsm[i].next[0] = next[0];
-            ghost_fsm[i].next[1] = next[1];
-        } else {
-            int next[2] = {i + 1, i - 1};
-            ghost_fsm[i].next[0] = next[0];
-            ghost_fsm[i].next[1] = next[1];
-        }
-    }
-
-    return ghost_fsm;
-}
-
-
-int** import_sprite(std::string file)
-{
-    FILE *fp; // this is our file pointer
-
-    // the previous example just read the values into variables and printed to
-    // serial, we'll now read files into an array.
-
-    // now open file for reading...note the 'r'
-    fp = fopen(file.c_str(), "r");
-
-
-    int n=0;  // going to store the number of lines in the file
-    stringvec values;
-
-    if (fp == NULL) {  // if it can't open the file then print error message
-        printf("Error! Unable to open file: %s\n", file.c_str());
-        listdir();
-    } else {
-        //Since we may not know the
-        // number of lines in the files ahead of time, we'll first count them
-        // * means scan but don't save
-        int max_len_tmp = 0;
-        int max_len = 0;
-
-        while (fscanf(fp, "%*i%n", &max_len_tmp) != EOF) {
-            n++;  // increment counter when read a line
-            if(max_len_tmp > max_len) {
-                max_len = max_len_tmp;
-            }
-        }
-
-        max_len--;
-
-        rewind(fp); // 'scrolled' to end of file, so go back to beginning
-
-        char buffer[n][max_len];
-
-        for(int i = 0; i < n; i++) {
-            fscanf(fp, "%s", &buffer[i][0]);
-        }
-
-        int** sprite = 0;
-        sprite = new int*[n];
-        for(int i = 0; i < n; i++) {
-            sprite[i] = new int[max_len];
-        }
-
-        for(int i = 0; i < n; i++) {
-            //printf("%s\n", buffer[i]);
-            for(int j = 0; j < max_len; j++) {
-                if((int)buffer[i][j] == 48) {
-                    sprite[i][j] = 0;
-                } else if((int) buffer[i][j] == 49) {
-                    sprite[i][j] = 1;
-                }
-            }
-        }
-
-        fclose(fp);  // ensure you close the file after reading
-
-        return sprite;
-
-    }
-
-    return 0;
-}
-
-void listdir(void)
-{
-    DIR *d;
-    struct dirent *p;
-
-    d = opendir("/sd/assets");
-    if (d != NULL) {
-        while ((p = readdir(d)) != NULL) {
-            printf(" - %s\n", p->d_name);
-        }
-    } else {
-        printf("Could not open directory!\n");
-    }
-    closedir(d);
-}
-
-stringvec list_ghosts()
-{
-// Connections to SD card holder on K64F (SPI interface)
-    //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS
-    std::string path = "/sd/ghosts";
-    printf("Listing directory: %s\n", path.c_str());
-    DIR *d;
-    struct dirent *p;
-
-    stringvec files;
-
-    d = opendir(path.c_str());
-    if (d != NULL) {
-        while ((p = readdir(d)) != NULL) {
-            files.push_back(p->d_name);
-        }
-    } else {
-        printf("Could not open directory!\n");
-    }
-    closedir(d);
-
-    stringvec correct_files;
-
-    for(int i = 0; i < files.size(); i++) {
-        if(hasEnding(files[i], ".ghost")) {
-            correct_files.push_back(files[i]);
-        }
-    }
-
-    return correct_files;
-}
-
-
-// Function taken from https://stackoverflow.com/a/874160/10436605
-bool hasEnding (std::string const &fullString, std::string const &ending)
-{
-    if (fullString.length() >= ending.length()) {
-        return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
-    } else {
-        return false;
-    }
-}
-
-void int_to_bin_digit(unsigned int in, int count, int* out)
-{
-    /* assert: count <= sizeof(int)*CHAR_BIT */
-    unsigned int mask = 1U << (count-1);
-    int i;
-    for (i = 0; i < count; i++) {
-        out[i] = (in & mask) ? 1 : 0;
-        in <<= 1;
-    }
-}
-
-
 // Button A event-triggered interrupt
 void buttonX_isr()
 {
     g_buttonX_flag = 1;   // set flag in ISR
-    if(!g_buttonTesting) {
+    if(!g_buttonTesting) {                  // Check if the button sensitivity is being tested
         wait_ms(g_button_sesnsitivity);
     } else {
         wait_ms(g_buttonSensitivityTest);