George Sykes ELEC2645 project

Dependencies:   mbed

https://os.mbed.com/media/uploads/el18gs/pixil-frame-0.png

GHOST HUNTER

In a world of ghostly horrors there is much money to be made in underground ghost fighting rings. You've managed to get hold of a Ghostbuster, a special piece of equipment that allows you to catch, train and fight ghosts.

Instructions

Below you will find the instructions for the game. Please note that due to COVID-19 a large part of the game (fighting ghosts) could not be added as it would have required access to a second gamepad which i could not acquire.

Welcome screen

When first started you will be presented with a welcome screen

  • Pot 1 to adjust the contrast on the screen
  • Press A to continue.

Main menu

You have three options, catch ghosts (add ghosts to your inventory), inventory (sell ghosts) or settings(adjust the games settings).

  • Press X and B to move the selection up and down respectively
  • Press A to enter the selected submenu

Catch Ghost

Will now be presented with two challenges. In the first you need to find a ghost, in the second you catch it. Theses stages will start automatically.

Find ghost

Rotate the gamepad on its roll and pitch axis until all the LED's turn on. The ones on the left indicate roll and the right pitch.

  • Rotate the gamepad on it roll and pitch to light up the LED's

Catch ghost

Return the gamepad to a comfortable position and use the joystick to move the crosshairs onto the ghost sprite. When ready press the A button to catch the ghost. You will be told what kind of ghost you have captured and it will be added to your inventory.

  • Press A to catch the ghost
  • Move the joystick to move the crosshairs

Inventory

The inventory allows you to view your ghosts and sell them.

  • Use Pot 1 to scroll through the ghosts
  • Pot 2 to scroll up and down the details of the individual ghosts
  • Press X to prepare to sell a ghost and press again to confirm, if you don't press again the sale screen will disappear after 5 seconds
  • Press Start to return to the main menu

Settings

This menu allows you to adjust some of the settings of the game.

  • Press X to go up one option
  • Press B to go down one option
  • Press A to enter the selected submenu
  • Press Start to return to the main menu

Contrast

Set the contrast of the LCD screen, the contrast will adjust on this screen so you can see the effect (contrast is bounded between 0.4 and 0.6).

  • Pot 1 to increase or decrease the contrast
  • Press A to set the contrast

Button Delay

Set the minimum time between button presses; if this is too low the game will detect two button presses when there was only one, too high and the buttons will seem unresponsive. So as to ensure these issues do not occur while changing the setting button X temporarily operates on the new delay but none of the others will until A is pressed.

  • Pot 1 to increase or decrease the delay
  • Press X to test the new delay, this will toggle the small circle to be filled in or unfilled
  • Press A to save the setting

main.cpp

Committer:
el18gs
Date:
2020-05-11
Revision:
4:2e8d7c6d2953
Parent:
3:9d811414d35e
Child:
8:4220d116f17c

File content as of revision 4:2e8d7c6d2953:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering
University of Leeds
2019/20
Name: George Sykes
Username: el18gs
Student ID Number: 201235346
Date: 08/02/2020
*/

// includes
#include "mbed.h"
#include "Gamepad.h"
#include "N5110.h"
#include "SDFileSystem.h"
#include "FX0S8700CQ.h"
#include "Ghost.h"
#include "Inventory.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;
FX0S8700CQ accel;
SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS

// Connections to SD card holder on K64F (SPI interface)
//SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS

// Declare all the inputs
InterruptIn buttonX(PTC5);
InterruptIn buttonA(PTC7);
InterruptIn buttonB(PTC9);
InterruptIn buttonY(PTC0);
InterruptIn buttonStart(PTC8);

// Declare output
// RED, YELLOW, GREEN
BusOut left_LED(PTC3,PTC2,PTA2);
BusOut right_LED(PTC11,PTC10,PTA1);

// Function prototypes
void welcome();
int game_menu();
void catch_ghosts();
void display_inventory(Inventory inventory);
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);


// Settings variables
volatile int g_button_sesnsitivity = 250;
volatile bool g_buttonTesting = false;
volatile int g_buttonSensitivityTest = 250;


// ISR functions
void buttonX_isr();
void buttonA_isr();
void buttonB_isr();
void buttonY_isr();
void buttonStart_isr();

// ISR flags
volatile int g_buttonX_flag = 0;
volatile int g_buttonA_flag = 0;
volatile int g_buttonB_flag = 0;
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
    */

    Inventory inventory;

    lcd.init(); // Initialise the screen
    lcd.backLightOn();

    welcome();
    int choice = -1;
    while(1) {
        choice = game_menu();
        switch (choice) {
            case 0:
                catch_ghosts();
                inventory.regen();
                break;
            case 1:
                display_inventory(inventory);
                break;
            case 2:
                settings();
                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()
{

    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();
    }
}

void catch_ghosts()
{
    
    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/");
    caught_ghost.save();

    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;
}

void display_inventory(Inventory inventory)
{
    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();
            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);
                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)
{
    //SDFileSystem sd(PTE3, PTE1, PTE2, PTE4, "sd"); // MOSI, MISO, SCK, CS

    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) {
        wait_ms(g_button_sesnsitivity);
    } else {
        wait_ms(g_buttonSensitivityTest);
    }
}

// Button A event-triggered interrupt
void buttonA_isr()
{
    g_buttonA_flag = 1;   // set flag in ISR
    wait_ms(g_button_sesnsitivity);
}

// Button A event-triggered interrupt
void buttonB_isr()
{
    g_buttonB_flag = 1;   // set flag in ISR
    wait_ms(g_button_sesnsitivity);
}

// Button A event-triggered interrupt
void buttonY_isr()
{
    g_buttonY_flag = 1;   // set flag in ISR
    wait_ms(g_button_sesnsitivity);
}

// Button A event-triggered interrupt
void buttonStart_isr()
{
    g_buttonStart_flag = 1;   // set flag in ISR
    wait_ms(g_button_sesnsitivity);
}