ELEC2645 (2018/19) / Mbed 2 deprecated el17lw

Dependencies:   mbed

main.cpp

Committer:
lewisgw
Date:
2019-03-11
Revision:
1:ded7ddd826ce
Parent:
0:d74302f8f847
Child:
2:16ae940b0aee

File content as of revision 1:ded7ddd826ce:

/*
ELEC2645 Embedded Systems Project
School of Electronic & Electrical Engineering University of Leeds
Name: Lewis Wooltorton
Username: el17lw
Student ID Number: 201122085 Date: March 2019
*/

#include "N5110.h"
#include "Gamepad.h"
#include <cmath>

N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad gamepad;  


// Sprites for Skateboarder moving right, moving left, standing left and standing right. These will be included in a Skateboarder class next.
int skate_right[17][12] =   {
    
    { 0,0,0,0,1,1,1,0,0,0,0,0 },
    { 0,0,0,0,1,1,1,1,0,0,0,0 },
    { 0,0,1,1,1,1,1,1,0,0,0,0 },
    { 0,0,0,0,1,0,1,1,0,0,0,0 },
    { 0,0,0,0,1,1,1,0,0,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,1,1,1,1,1,1,1,0,0,0 },
    { 0,1,1,0,1,1,0,0,1,1,0,0 },
    { 0,1,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,0,1,0,1,0,0,0,0,0 },
    { 0,0,0,0,1,0,0,1,0,0,0,0 },
    { 0,1,0,0,1,0,0,0,1,0,1,0 },
    { 0,0,1,1,1,1,1,1,1,1,0,0 },
    { 0,0,0,1,0,0,0,0,1,0,0,0 },
    
    
};

int skate_left[17][12] =   {
    
    { 0,0,0,0,1,1,1,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,1,1,0,0,0 },
    { 0,0,0,1,1,0,1,0,0,0,0,0 },
    { 0,0,0,0,1,1,1,0,0,0,0,0 },
    { 0,0,0,0,0,1,1,0,0,0,0,0 },
    { 0,0,0,0,0,1,1,1,0,0,0,0 },
    { 0,1,1,1,1,1,1,1,0,0,0,0 },
    { 0,0,0,1,1,1,1,1,1,0,0,0 },
    { 0,0,0,0,1,1,0,0,1,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,1,0,0 },
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,1,0,1,0,0,0,0,0,0 },
    { 0,0,1,0,0,1,0,0,0,0,0,0 },
    { 1,1,0,0,0,1,0,0,0,1,0,0 },
    { 0,1,1,1,1,1,1,1,1,0,0,0 },
    { 0,0,0,1,0,0,0,0,1,0,0,0 },
    
    
};

int skate_standing_left[17][12] =   {
    
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,1,1,0,0,0 },
    { 0,0,0,1,0,0,1,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0,0,0 },
    { 0,0,1,1,1,1,0,1,0,0,0,0 },
    { 1,1,0,1,1,1,0,1,0,0,0,0 },
    { 0,0,0,1,1,1,0,0,0,0,0,0 },
    { 0,0,1,1,1,1,0,0,0,0,0,0 },
    { 1,0,1,0,0,1,0,0,0,0,0,0 },
    { 1,1,0,0,0,1,0,0,0,0,0,0 },
    { 0,1,0,0,0,1,0,0,0,0,0,0 },
    { 1,0,1,0,0,1,0,0,0,0,0,0 },
    { 0,0,0,1,0,1,0,0,0,0,0,0 },
    { 0,0,1,0,1,1,1,0,0,0,0,0 },
    
    
};

int skate_standing_right[17][12] =   {
    
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0,0,0 },
    { 0,1,1,1,1,1,1,0,0,0,0,0 },
    { 0,0,0,1,0,0,1,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0,0,0 },
    { 0,0,0,0,1,1,0,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,0,0,0,0,0 },
    { 0,0,1,1,1,1,0,1,0,0,0,0 },
    { 0,1,0,1,1,1,0,0,1,0,0,0 },
    { 0,1,0,1,1,1,0,0,0,0,0,0 },
    { 0,0,0,1,1,1,1,1,0,0,0,0 },
    { 0,0,0,1,0,0,0,1,0,1,0,0 },
    { 0,0,0,1,0,0,0,1,1,0,0,0 },
    { 0,0,0,1,0,0,0,1,0,1,0,0 },
    { 0,0,0,1,0,0,1,0,0,0,0,0 },
    { 0,0,0,1,0,1,0,0,0,0,0,0 },
    { 0,0,1,1,1,0,1,0,0,0,0,0 },
    
    
};
// intialise game function. Process input and refresh LCD function will be implemented next along with the classes.
void init_game();

// main function. Note: code has not been properly formatted yet - the purpose is to get simple game mechanics to work.
int main(){
    
    init_game();
    
    //intialise variables for the game loop. These will be implemented more concisely in functions and classes next.
    int moving_counter = 0;
    int direction_state = 0; // integer to keep track of where the skater is facing. 0 for last moving direction was right, 1 if last moving direction was left.
    int x; // x position of the skater 
    int y; // y position of the skater
    int jump_counter = 0; // counter for jumping
       
    while(1){
        lcd.clear();
        lcd.drawLine(5,40,80,40,FILL_BLACK); // line for staker to move along
        Vector2D coord = gamepad.get_mapped_coord(); // get mapped coordinates of joystick
        
        // first we manipulate the y postion of the skater.
        //check if innterupt button A has occured. This means a jump (translation in y position) must take place. 
        if( gamepad.check_event(Gamepad::A_PRESSED) ){
            jump_counter = 40;
                }
        // if the jump counter is non zero (e.g. if an interupt has occured) deincriment it every main loop iteration until it is 0 again.
        // This means that initally y postion is increased by a lot, and every loop iteration the y postion falls until it is back to its default state. This is a jump of the skater.         
        if(jump_counter !=0){
            jump_counter--;        
            }
        y = 23 - 0.5*jump_counter; // Using 0.5*jump_counter to scale the fall time. 23 is the y postion when the skater is on the line.
                
        // now we manipulate the x postion of the skater
        
        //if the joystick is pushed in the horizontal right or left direction, increment or deincriment the moving counter respectivly, and add a scaled version of this onto the x postion. 
        //set the direction_state to 0 or 1 to indicate that the last moving postion was right or left respectivly.
        //print the correct sprite depending on the postion of the joystick ( ie left, right or centered). 
        //first two conditions are for joystick moving right or left, the second two are for joystick centered with pervious postion being left or right.
        if(coord.x > float(0.1)){
            moving_counter++;
            x = 40 + 0.4*moving_counter;
            direction_state = 0;
            lcd.drawSprite(x,y,17,12,(int *)skate_right); 
            } else if(coord.x < float(-0.1)) {
            moving_counter--;
            x = 40 + 0.4*moving_counter;
            direction_state = 1;
            lcd.drawSprite(x,y,17,12,(int *)skate_left);
            } else if(direction_state==1) {
            x = 40 + 0.4*moving_counter;
            lcd.drawSprite(x,y,17,12,(int *)skate_standing_left); 
            } else {
            x = 40 + 0.4*moving_counter;
            lcd.drawSprite(x,y,17,12,(int *)skate_standing_right); 
            }    
        lcd.refresh();
        
        wait(0.01); 
    
    }     
 }  
 
 void init_game() {
    
    gamepad.init();// initalise gamepad
    lcd.init(); // initialise LCD
    lcd.setContrast(0.5); // set contrast of LCD
    lcd.normalMode();      // use normal colour mode
    lcd.setBrightness(0.5); //set brightness of LCD
    }