My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

main.cpp

Committer:
Nikollao
Date:
2016-05-02
Revision:
16:a6ca6858af24
Parent:
15:c7af2ea5f164
Child:
17:2d424db3975f

File content as of revision 16:a6ca6858af24:

/**
@file main.cpp
@brief Game implementation

*/
#include "main.h"
#include "stdlib.h"

#include "P_defines.h"
#include "hero.h"
#include "joystick.h"
#include "game.h"
#include "background.h"

int main()
{
    lcd.init();
    init_K64F();
    init_serial();
    init_game(); ///initialize game
    
    calibrateJoystick(); ///calibrate joystick
    button.rise(&button_isr); ///assign rise with ISR
    button1.rise(&button1_isr);
    
    game_ticker.attach(&game_timer_isr,0.2);
    menu();
    game_ticker.detach();
    
    ticker.attach(&timer_isr,game_speed); ///attach ticker with ISR every 0.1 sec
    reset = level; ///set reset = level to check later if level has increased
    
    while (1) {

        if (g_timer_flag) {

            g_timer_flag = 0; ///reset flag
            lcd.clear();
            guidance();
            drawHero();
            moveHero();
            //hero();
            enemies();
            obstacles();

            if (reset < level) { ///if level has increased

                reset = level; ///update reset
                rectX = rand() % 84; ///set the position of rect enemy
                rectY = 0;
                circleX = 0; ///set position of circle enemy
                circleY = rand() % 47;
            }
            pc.printf("x = %f y = %f button = %d \n",joystick.x,joystick.y,joystick.button);
            pc.printf("heroY = %d , heroX = %d , n = %d \n",heroY, heroX, n);
            checkOverlap();
            updateJoystick();
            //callibrateJoystick();
            lcd.refresh();
        }
        sleep();
    }
}

void init_serial()
{
    // set to highest baud - ensure terminal software matches
    pc.baud(115200);
}

void init_K64F()
{
    // on-board LEDs are active-low, so set pin high to turn them off.
    r_led = 1;
    g_led = 1;
    b_led = 1;
    
    blue_led = 1;
    left_led = 1;
    centre_led = 1;
    right_led = 1;

    // since the on-board switches have external pull-ups, we should disable the internal pull-down
    // resistors that are enabled by default using InterruptIn
    sw2.mode(PullNone);
    sw3.mode(PullNone);
    button.mode(PullDown);
    button1.mode(PullDown);
}

void timer_isr ()
{
    g_timer_flag = 1;
}

void game_timer_isr()
{
    g_game_timer_flag = 1;
}

void sw2_isr()
{
    g_sw2_flag = 1;
}

void sw3_isr()
{
    g_sw3_flag = 1;
}

void button_isr()
{
    g_button_flag =1;
}

void button1_isr() {
    
    g_button1_flag =1;
}

void timeout_isr()
{
    if (button) {
        pc.printf("timeout \n");
    }
}