My ELEC2645 project. Nikollao Sulollari. 200804685

Dependencies:   N5110 SoftPWM mbed

Fork of Treasure_Hunt by ELEC2645 (2015/16)

main.cpp

Committer:
Nikollao
Date:
2016-04-18
Revision:
8:606a488fa720
Parent:
7:f31a4b4beb91
Child:
9:ce2d9c42edea

File content as of revision 8:606a488fa720:

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

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

int main()
{
    lcd.init();
    init_K64F();
    init_serial();
    button.fall(&button_isr); ///assign rise with ISR
    init_game(); ///initialize game
    calibrateJoystick(); ///calibrate joystick

    ticker.attach(&timer_isr,0.1); ///attach ticker with ISR every 0.1 sec
    reset = level; ///set reset = level to check later if level has increased
    buzzer = 0.5;
    buzzer.period(1/4000);
    //buzzer.write(0.5);
    
    while (1) {

        if (g_timer_flag) {

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

            if (heroY < -45) { ///if hero has reached the top of the screen
                heroY = 0; ///hero goes back to the bottom of the screen
                level++; ///level increases by 1
            }

            if (heroY >= 0) {
                heroY = 0;
            }
            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;

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

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 timeout_isr()
{
    if (button) {
        pc.printf("button is pressed! \n");
    }
}