Li Ruofan 201199450
Dependencies: mbed Gamepad Joystick
Revision 8:b4a2954dd74f, committed 2020-05-16
- Comitter:
- DannyLee
- Date:
- Sat May 16 17:26:10 2020 +0000
- Parent:
- 7:e3844250b77d
- Commit message:
- Debugging
Changed in this revision
diff -r e3844250b77d -r b4a2954dd74f Gamepad.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Gamepad.lib Sat May 16 17:26:10 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/eencae/code/Gamepad/#c0959710291b
diff -r e3844250b77d -r b4a2954dd74f Gamepad/Gamepad.cpp --- a/Gamepad/Gamepad.cpp Fri May 15 22:23:22 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,341 +0,0 @@ -#include "Gamepad.h" - -#include "mbed.h" - -//////////// constructor/destructor //////////// -Gamepad::Gamepad() - : - _led1(new PwmOut(PTA1)), - _led2(new PwmOut(PTA2)), - _led3(new PwmOut(PTC2)), - _led4(new PwmOut(PTC3)), - _led5(new PwmOut(PTC4)), - _led6(new PwmOut(PTD3)), - - _button_A(new InterruptIn(PTB9)), - _button_B(new InterruptIn(PTD0)), - _button_X(new InterruptIn(PTC17)), - _button_Y(new InterruptIn(PTC12)), - _button_L(new InterruptIn(PTB18)), - _button_R(new InterruptIn(PTB3)), - _button_back(new InterruptIn(PTB19)), - _button_start(new InterruptIn(PTC5)), - _button_joystick(new InterruptIn(PTC16)), - - _vert(new AnalogIn(PTB10)), - _horiz(new AnalogIn(PTB11)), - - _buzzer(new PwmOut(PTC10)), - _pot(new AnalogIn(PTB2)), - - _timeout(new Timeout()), - - _event_state(0), - - _x0(0), - _y0(0) -{} - -Gamepad::~Gamepad() -{ - delete _led1,_led2,_led3,_led4,_led5,_led6; - delete _button_A,_button_B,_button_joystick,_vert,_horiz; - delete _button_X,_button_Y,_button_back,_button_start; - delete _button_L,_button_R, _buzzer, _pot, _timeout; -} - -///////////////// public methods ///////////////// - -void Gamepad::init() -{ - leds_off(); - init_buttons(); - - // read centred values of joystick - _x0 = _horiz->read(); - _y0 = _vert->read(); - - // clear all flags - _event_state = 0; -} - -void Gamepad::leds_off() -{ - leds(0.0); -} - -void Gamepad::leds_on() -{ - leds(1.0); -} - -void Gamepad::leds(float val) const -{ - if (val < 0.0f) { - val = 0.0f; - } - if (val > 1.0f) { - val = 1.0f; - } - - // leds are active-low, so subtract from 1.0 - // 0.0 corresponds to fully-off, 1.0 to fully-on - val = 1.0f - val; - - _led1->write(val); - _led2->write(val); - _led3->write(val); - _led4->write(val); - _led5->write(val); - _led6->write(val); -} - -void Gamepad::led(int n,float val) const -{ - // ensure they are within vlaid range - if (val < 0.0f) { - val = 0.0f; - } - if (val > 1.0f) { - val = 1.0f; - } - - switch (n) { - - // check for valid LED number and set value - - case 1: - _led1->write(1.0f-val); // active-low so subtract from 1 - break; - case 2: - _led2->write(1.0f-val); // active-low so subtract from 1 - break; - case 3: - _led3->write(1.0f-val); // active-low so subtract from 1 - break; - case 4: - _led4->write(1.0f-val); // active-low so subtract from 1 - break; - case 5: - _led5->write(1.0f-val); // active-low so subtract from 1 - break; - case 6: - _led6->write(1.0f-val); // active-low so subtract from 1 - break; - - } -} - -float Gamepad::read_pot() const -{ - return _pot->read(); -} - -void Gamepad::tone(float frequency, float duration) -{ - _buzzer->period(1.0f/frequency); - _buzzer->write(0.5); // 50% duty cycle - square wave - _timeout->attach(callback(this, &Gamepad::tone_off), duration ); -} - -bool Gamepad::check_event(GamepadEvent const id) -{ - // Check whether event flag is set - if (_event_state[id]) { - _event_state.reset(id); // clear flag - return true; - } else { - return false; - } -} - -// this method gets the magnitude of the joystick movement -float Gamepad::get_mag() -{ - Polar p = get_polar(); - return p.mag; -} - -// this method gets the angle of joystick movement (0 to 360, 0 North) -float Gamepad::get_angle() -{ - Polar p = get_polar(); - return p.angle; -} - -Direction Gamepad::get_direction() -{ - float angle = get_angle(); // 0 to 360, -1 for centred - - Direction d; - // partition 360 into segments and check which segment the angle is in - if (angle < 0.0f) { - d = CENTRE; // check for -1.0 angle - } else if (angle < 22.5f) { // then keep going in 45 degree increments - d = N; - } else if (angle < 67.5f) { - d = NE; - } else if (angle < 112.5f) { - d = E; - } else if (angle < 157.5f) { - d = SE; - } else if (angle < 202.5f) { - d = S; - } else if (angle < 247.5f) { - d = SW; - } else if (angle < 292.5f) { - d = W; - } else if (angle < 337.5f) { - d = NW; - } else { - d = N; - } - - return d; -} - -///////////////////// private methods //////////////////////// - -void Gamepad::tone_off() -{ - // called after timeout - _buzzer->write(0.0); -} - -void Gamepad::init_buttons() -{ - // turn on pull-downs as other side of button is connected to 3V3 - // button is 0 when not pressed and 1 when pressed - _button_A->mode(PullDown); - _button_B->mode(PullDown); - _button_X->mode(PullDown); - _button_Y->mode(PullDown); - _button_back->mode(PullDown); - _button_start->mode(PullDown); - _button_L->mode(PullDown); - _button_R->mode(PullDown); - _button_joystick->mode(PullDown); - // therefore setup rising edge interrupts - _button_A->rise(callback(this,&Gamepad::a_isr)); - _button_B->rise(callback(this,&Gamepad::b_isr)); - _button_X->rise(callback(this,&Gamepad::x_isr)); - _button_Y->rise(callback(this,&Gamepad::y_isr)); - _button_L->rise(callback(this,&Gamepad::l_isr)); - _button_R->rise(callback(this,&Gamepad::r_isr)); - _button_start->rise(callback(this,&Gamepad::start_isr)); - _button_back->rise(callback(this,&Gamepad::back_isr)); - _button_joystick->rise(callback(this,&Gamepad::joy_isr)); -} - -// button interrupts ISRs -// Each of these simply sets the appropriate event bit in the _event_state -// variable -void Gamepad::a_isr() -{ - _event_state.set(A_PRESSED); -} -void Gamepad::b_isr() -{ - _event_state.set(B_PRESSED); -} -void Gamepad::x_isr() -{ - _event_state.set(X_PRESSED); -} -void Gamepad::y_isr() -{ - _event_state.set(Y_PRESSED); -} -void Gamepad::l_isr() -{ - _event_state.set(L_PRESSED); -} -void Gamepad::r_isr() -{ - _event_state.set(R_PRESSED); -} -void Gamepad::back_isr() -{ - _event_state.set(BACK_PRESSED); -} -void Gamepad::start_isr() -{ - _event_state.set(START_PRESSED); -} -void Gamepad::joy_isr() -{ - _event_state.set(JOY_PRESSED); -} - -// get raw joystick coordinate in range -1 to 1 -// Direction (x,y) -// North (0,1) -// East (1,0) -// South (0,-1) -// West (-1,0) -Vector2D Gamepad::get_coord() -{ - // read() returns value in range 0.0 to 1.0 so is scaled and centre value - // substracted to get values in the range -1.0 to 1.0 - float x = 2.0f*( _horiz->read() - _x0 ); - float y = 2.0f*( _vert->read() - _y0 ); - - // Note: the x value here is inverted to ensure the positive x is to the - // right. This is simply due to how the potentiometer on the joystick - // I was using was connected up. It could have been corrected in hardware - // by swapping the power supply pins. Instead it is done in software so may - // need to be changed depending on your wiring setup - - Vector2D coord = {-x,y}; - return coord; -} - -// This maps the raw x,y coord onto a circular grid. -// See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html -Vector2D Gamepad::get_mapped_coord() -{ - Vector2D coord = get_coord(); - - // do the transformation - float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); - float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); - - Vector2D mapped_coord = {x,y}; - return mapped_coord; -} - -// this function converts the mapped coordinates into polar form -Polar Gamepad::get_polar() -{ - // get the mapped coordinate - Vector2D coord = get_mapped_coord(); - - // at this point, 0 degrees (i.e. x-axis) will be defined to the East. - // We want 0 degrees to correspond to North and increase clockwise to 359 - // like a compass heading, so we need to swap the axis and invert y - float x = coord.y; - float y = coord.x; - - float mag = sqrt(x*x+y*y); // pythagoras - float angle = RAD2DEG*atan2(y,x); - // angle will be in range -180 to 180, so add 360 to negative angles to - // move to 0 to 360 range - if (angle < 0.0f) { - angle+=360.0f; - } - - // the noise on the ADC causes the values of x and y to fluctuate slightly - // around the centred values. This causes the random angle values to get - // calculated when the joystick is centred and untouched. This is also when - // the magnitude is very small, so we can check for a small magnitude and then - // set the angle to -1. This will inform us when the angle is invalid and the - // joystick is centred - - if (mag < TOL) { - mag = 0.0f; - angle = -1.0f; - } - - Polar p = {mag,angle}; - return p; -} \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f Gamepad/Gamepad.h --- a/Gamepad/Gamepad.h Fri May 15 22:23:22 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,205 +0,0 @@ -#ifndef GAMEPAD_H -#define GAMEPAD_H - -#include <bitset> - -// Forward declaration of the classes that we use from the mbed library -// This avoids the need for us to include the huge mbed.h header inside our -// own library API -namespace mbed -{ -class AnalogIn; -class InterruptIn; -class PwmOut; -class Timeout; -} - -#define TOL 0.1f -#define RAD2DEG 57.2957795131f - -/** Enum for direction */ -enum Direction { - CENTRE, /**< joystick centred */ - N, /**< pushed North (0)*/ - NE, /**< pushed North-East (45) */ - E, /**< pushed East (90) */ - SE, /**< pushed South-East (135) */ - S, /**< pushed South (180) */ - SW, /**< pushed South-West (225) */ - W, /**< pushed West (270) */ - NW /**< pushed North-West (315) */ -}; - -/** Vector 2D struct */ -struct Vector2D { - float x; /**< float for x value */ - float y; /**< float for y value */ -}; - -/** Polar coordinate struct */ -struct Polar { - float mag; /**< float for magnitude */ - float angle; /**< float for angle (in degrees) */ -}; - -/** Gamepad Class - * @brief Library for interfacing with ELEC2645 Gamepad PCB, University of Leeds - * @author Dr Craig A. Evans - * @author Dr Alex Valavanis - */ -class Gamepad -{ -public: -/** Gamepad events - * @brief List of events that can be registered on the gamepad - */ -enum GamepadEvent { - A_PRESSED, ///< Button A has been pressed - B_PRESSED, ///< Button B has been pressed - X_PRESSED, ///< Button X has been pressed - Y_PRESSED, ///< Button Y has been pressed - L_PRESSED, ///< Button L has been pressed - R_PRESSED, ///< Button R has been pressed - BACK_PRESSED, ///< Button "Back" has been pressed - START_PRESSED, ///< Button "Start" has been pressed - JOY_PRESSED, ///< Joystick button has been pressed - N_EVENTS ///< A dummy flag that marks the end of the list -}; - -private: - mbed::PwmOut *_led1; - mbed::PwmOut *_led2; - mbed::PwmOut *_led3; - mbed::PwmOut *_led4; - mbed::PwmOut *_led5; - mbed::PwmOut *_led6; - - mbed::InterruptIn *_button_A; - mbed::InterruptIn *_button_B; - mbed::InterruptIn *_button_X; - mbed::InterruptIn *_button_Y; - mbed::InterruptIn *_button_L; - mbed::InterruptIn *_button_R; - mbed::InterruptIn *_button_back; - mbed::InterruptIn *_button_start; - mbed::InterruptIn *_button_joystick; - - mbed::AnalogIn *_vert; - mbed::AnalogIn *_horiz; - - mbed::PwmOut *_buzzer; - mbed::AnalogIn *_pot; - - mbed::Timeout *_timeout; - - std::bitset<N_EVENTS> _event_state; ///< A binary list of buttons that has been pressed - - // centred x,y values - float _x0; - float _y0; - -public: - /** Constructor */ - Gamepad(); - - /** Destructor */ - ~Gamepad(); - - /** Initialise all peripherals and configure interrupts */ - void init(); - - /** Turn all LEDs on */ - void leds_on(); - - /** Turn all LEDs off */ - void leds_off(); - - /** Set all LEDs to duty-cycle - *@param value in range 0.0 to 1.0 - */ - void leds(float val) const; - - /** Set LED to duty-cycle - *@param led number (0 to 5) - *@param value in range 0.0 to 1.0 - */ - void led(int n,float val) const; - - /** Read potentiometer - *@returns potentiometer value in range 0.0 to 1.0 - */ - float read_pot() const; - - /** Play tone on piezo - * @param frequency in Hz - * @param duration of tone in seconds - */ - void tone(float frequency, float duration); - - /** - * @brief Check whether an event flag has been set and clear it - * @param id[in] The ID of the event to test - * @return true if the event occurred - */ - bool check_event(GamepadEvent const id); - - /** - * @brief Get the raw binary event state - * @return The event state as a binary code - * @details The check_event() function is likely to be more useful than - * this, for testing whether a given event has occurred. It can be - * useful for debugging via the terminal, however, for example: - * @code - * std::cout << gamepad.get_raw_event_state() << std::endl; - * @endcode - */ - inline std::bitset<N_EVENTS> get_raw_event_state() const { - return _event_state; - } - - /** Get magnitude of joystick movement - * @returns value in range 0.0 to 1.0 - */ - float get_mag(); - - /** Get angle of joystick movement - * @returns value in range 0.0 to 359.9. 0.0 corresponds to N, 180.0 to S. -1.0 is central - */ - float get_angle(); - - /** Gets joystick direction - * @returns an enum: CENTRE, N, NE, E, SE, S, SW, W, NW, - */ - Direction get_direction(); // N,NE,E,SE etc. - - /** Gets raw cartesian co-ordinates of joystick - * @returns a struct with x,y members, each in the range 0.0 to 1.0 - */ - Vector2D get_coord(); // cartesian co-ordinates x,y - - /** Gets cartesian coordinates mapped to circular grid - * @returns a struct with x,y members, each in the range 0.0 to 1.0 - */ - Vector2D get_mapped_coord(); // x,y mapped to circle - - /** Gets polar coordinates of the joystick - * @returns a struct contains mag and angle - */ - Polar get_polar(); // mag and angle in struct form - -private: - void init_buttons(); - void tone_off(); - - void a_isr(); - void b_isr(); - void x_isr(); - void y_isr(); - void l_isr(); - void r_isr(); - void back_isr(); - void start_isr(); - void joy_isr(); -}; - -#endif \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f Joystick.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Joystick.lib Sat May 16 17:26:10 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/eencae/code/Joystick/#649b59b2fb26
diff -r e3844250b77d -r b4a2954dd74f Joystick/Joystick.cpp --- a/Joystick/Joystick.cpp Fri May 15 22:23:22 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,502 +0,0 @@ -#include "Joystick.h" - -Joystick::Joystick(PinName vertPin,PinName horizPin,PinName clickPin) -{ - vert = new AnalogIn(vertPin); - horiz = new AnalogIn(horizPin); - click = new InterruptIn(clickPin); -} - -void Joystick::init() -{ - // read centred values of joystick - _x0 = horiz->read(); - _y0 = vert->read(); - - // this assumes that the joystick is centred when the init function is called - // if perfectly centred, the pots should read 0.5, but this may - // not be the case and x0 and y0 will be used to calibrate readings - - // turn on pull-down for button -> this assumes the other side of the button - // is connected to +3V3 so we read 1 when pressed and 0 when not pressed - click->mode(PullDown); - // we therefore need to fire the interrupt on a rising edge - click->rise(callback(this,&Joystick::click_isr)); - // need to use a callback since mbed-os5 - basically tells it to look in this class for the ISR - _click_flag = 0; - -} - -Direction Joystick::get_direction() -{ - float angle = get_angle(); // 0 to 360, -1 for centred - - Direction d; - // partition 360 into segments and check which segment the angle is in - if (angle < 0.0f) { - d = CENTRE; // check for -1.0 angle - } else if (angle < 22.5f) { // then keep going in 45 degree increments - d = N; - } else if (angle < 67.5f) { - d = NE; - } else if (angle < 112.5f) { - d = E; - } else if (angle < 157.5f) { - d = SE; - } else if (angle < 202.5f) { - d = S; - } else if (angle < 247.5f) { - d = SW; - } else if (angle < 292.5f) { - d = W; - } else if (angle < 337.5f) { - d = NW; - } else { - d = N; - } - - return d; -} - -// this method gets the magnitude of the joystick movement -float Joystick::get_mag() -{ - Polar p = get_polar(); - return p.mag; -} - -// this method gets the angle of joystick movement (0 to 360, 0 North) -float Joystick::get_angle() -{ - Polar p = get_polar(); - return p.angle; -} - -// get raw joystick coordinate in range -1 to 1 -// Direction (x,y) -// North (0,1) -// East (1,0) -// South (0,-1) -// West (-1,0) -Vector2D Joystick::get_coord() -{ - // read() returns value in range 0.0 to 1.0 so is scaled and centre value - // substracted to get values in the range -1.0 to 1.0 - float x = 2.0f*( horiz->read() - _x0 ); - float y = 2.0f*( vert->read() - _y0 ); - - // Note: the x value here is inverted to ensure the positive x is to the - // right. This is simply due to how the potentiometer on the joystick - // I was using was connected up. It could have been corrected in hardware - // by swapping the power supply pins. Instead it is done in software so may - // need to be changed depending on your wiring setup - - Vector2D coord = {-x,y}; - return coord; -} - -// This maps the raw x,y coord onto a circular grid. -// See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html -Vector2D Joystick::get_mapped_coord() -{ - Vector2D coord = get_coord(); - - // do the transformation - float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); - float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); - - Vector2D mapped_coord = {x,y}; - return mapped_coord; -} - -// this function converts the mapped coordinates into polar form -Polar Joystick::get_polar() -{ - // get the mapped coordinate - Vector2D coord = get_mapped_coord(); - - // at this point, 0 degrees (i.e. x-axis) will be defined to the East. - // We want 0 degrees to correspond to North and increase clockwise to 359 - // like a compass heading, so we need to swap the axis and invert y - float x = coord.y; - float y = coord.x; - - float mag = sqrt(x*x+y*y); // pythagoras - float angle = RAD2DEG*atan2(y,x); - // angle will be in range -180 to 180, so add 360 to negative angles to - // move to 0 to 360 range - if (angle < 0.0f) { - angle+=360.0f; - } - - // the noise on the ADC causes the values of x and y to fluctuate slightly - // around the centred values. This causes the random angle values to get - // calculated when the joystick is centred and untouched. This is also when - // the magnitude is very small, so we can check for a small magnitude and then - // set the angle to -1. This will inform us when the angle is invalid and the - // joystick is centred - - if (mag < TOL) { - mag = 0.0f; - angle = -1.0f; - } - - Polar p = {mag,angle}; - return p; -} - -bool Joystick::button_pressed() -{ - // ISR must have been triggered - if (_click_flag) { - _click_flag = 0; // clear flag - return true; - } else { - return false; - } -} - -void Joystick::click_isr() -{ - _click_flag = 1; -}#include "Gamepad.h" - -#include "mbed.h" - -//////////// constructor/destructor //////////// -Gamepad::Gamepad() - : - _led1(new PwmOut(PTA1)), - _led2(new PwmOut(PTA2)), - _led3(new PwmOut(PTC2)), - _led4(new PwmOut(PTC3)), - _led5(new PwmOut(PTC4)), - _led6(new PwmOut(PTD3)), - - _button_A(new InterruptIn(PTB9)), - _button_B(new InterruptIn(PTD0)), - _button_X(new InterruptIn(PTC17)), - _button_Y(new InterruptIn(PTC12)), - _button_L(new InterruptIn(PTB18)), - _button_R(new InterruptIn(PTB3)), - _button_back(new InterruptIn(PTB19)), - _button_start(new InterruptIn(PTC5)), - _button_joystick(new InterruptIn(PTC16)), - - _vert(new AnalogIn(PTB10)), - _horiz(new AnalogIn(PTB11)), - - _buzzer(new PwmOut(PTC10)), - _pot(new AnalogIn(PTB2)), - - _timeout(new Timeout()), - - _event_state(0), - - _x0(0), - _y0(0) -{} - -Gamepad::~Gamepad() -{ - delete _led1,_led2,_led3,_led4,_led5,_led6; - delete _button_A,_button_B,_button_joystick,_vert,_horiz; - delete _button_X,_button_Y,_button_back,_button_start; - delete _button_L,_button_R, _buzzer, _pot, _timeout; -} - -///////////////// public methods ///////////////// - -void Gamepad::init() -{ - leds_off(); - init_buttons(); - - // read centred values of joystick - _x0 = _horiz->read(); - _y0 = _vert->read(); - - // clear all flags - _event_state = 0; -} - -void Gamepad::leds_off() -{ - leds(0.0); -} - -void Gamepad::leds_on() -{ - leds(1.0); -} - -void Gamepad::leds(float val) const -{ - if (val < 0.0f) { - val = 0.0f; - } - if (val > 1.0f) { - val = 1.0f; - } - - // leds are active-low, so subtract from 1.0 - // 0.0 corresponds to fully-off, 1.0 to fully-on - val = 1.0f - val; - - _led1->write(val); - _led2->write(val); - _led3->write(val); - _led4->write(val); - _led5->write(val); - _led6->write(val); -} - -void Gamepad::led(int n,float val) const -{ - // ensure they are within vlaid range - if (val < 0.0f) { - val = 0.0f; - } - if (val > 1.0f) { - val = 1.0f; - } - - switch (n) { - - // check for valid LED number and set value - - case 1: - _led1->write(1.0f-val); // active-low so subtract from 1 - break; - case 2: - _led2->write(1.0f-val); // active-low so subtract from 1 - break; - case 3: - _led3->write(1.0f-val); // active-low so subtract from 1 - break; - case 4: - _led4->write(1.0f-val); // active-low so subtract from 1 - break; - case 5: - _led5->write(1.0f-val); // active-low so subtract from 1 - break; - case 6: - _led6->write(1.0f-val); // active-low so subtract from 1 - break; - - } -} - -float Gamepad::read_pot() const -{ - return _pot->read(); -} - -void Gamepad::tone(float frequency, float duration) -{ - _buzzer->period(1.0f/frequency); - _buzzer->write(0.5); // 50% duty cycle - square wave - _timeout->attach(callback(this, &Gamepad::tone_off), duration ); -} - -bool Gamepad::check_event(GamepadEvent const id) -{ - // Check whether event flag is set - if (_event_state[id]) { - _event_state.reset(id); // clear flag - return true; - } else { - return false; - } -} - -// this method gets the magnitude of the joystick movement -float Gamepad::get_mag() -{ - Polar p = get_polar(); - return p.mag; -} - -// this method gets the angle of joystick movement (0 to 360, 0 North) -float Gamepad::get_angle() -{ - Polar p = get_polar(); - return p.angle; -} - -Direction Gamepad::get_direction() -{ - float angle = get_angle(); // 0 to 360, -1 for centred - - Direction d; - // partition 360 into segments and check which segment the angle is in - if (angle < 0.0f) { - d = CENTRE; // check for -1.0 angle - } else if (angle < 22.5f) { // then keep going in 45 degree increments - d = N; - } else if (angle < 67.5f) { - d = NE; - } else if (angle < 112.5f) { - d = E; - } else if (angle < 157.5f) { - d = SE; - } else if (angle < 202.5f) { - d = S; - } else if (angle < 247.5f) { - d = SW; - } else if (angle < 292.5f) { - d = W; - } else if (angle < 337.5f) { - d = NW; - } else { - d = N; - } - - return d; -} - -///////////////////// private methods //////////////////////// - -void Gamepad::tone_off() -{ - // called after timeout - _buzzer->write(0.0); -} - -void Gamepad::init_buttons() -{ - // turn on pull-downs as other side of button is connected to 3V3 - // button is 0 when not pressed and 1 when pressed - _button_A->mode(PullDown); - _button_B->mode(PullDown); - _button_X->mode(PullDown); - _button_Y->mode(PullDown); - _button_back->mode(PullDown); - _button_start->mode(PullDown); - _button_L->mode(PullDown); - _button_R->mode(PullDown); - _button_joystick->mode(PullDown); - // therefore setup rising edge interrupts - _button_A->rise(callback(this,&Gamepad::a_isr)); - _button_B->rise(callback(this,&Gamepad::b_isr)); - _button_X->rise(callback(this,&Gamepad::x_isr)); - _button_Y->rise(callback(this,&Gamepad::y_isr)); - _button_L->rise(callback(this,&Gamepad::l_isr)); - _button_R->rise(callback(this,&Gamepad::r_isr)); - _button_start->rise(callback(this,&Gamepad::start_isr)); - _button_back->rise(callback(this,&Gamepad::back_isr)); - _button_joystick->rise(callback(this,&Gamepad::joy_isr)); -} - -// button interrupts ISRs -// Each of these simply sets the appropriate event bit in the _event_state -// variable -void Gamepad::a_isr() -{ - _event_state.set(A_PRESSED); -} -void Gamepad::b_isr() -{ - _event_state.set(B_PRESSED); -} -void Gamepad::x_isr() -{ - _event_state.set(X_PRESSED); -} -void Gamepad::y_isr() -{ - _event_state.set(Y_PRESSED); -} -void Gamepad::l_isr() -{ - _event_state.set(L_PRESSED); -} -void Gamepad::r_isr() -{ - _event_state.set(R_PRESSED); -} -void Gamepad::back_isr() -{ - _event_state.set(BACK_PRESSED); -} -void Gamepad::start_isr() -{ - _event_state.set(START_PRESSED); -} -void Gamepad::joy_isr() -{ - _event_state.set(JOY_PRESSED); -} - -// get raw joystick coordinate in range -1 to 1 -// Direction (x,y) -// North (0,1) -// East (1,0) -// South (0,-1) -// West (-1,0) -Vector2D Gamepad::get_coord() -{ - // read() returns value in range 0.0 to 1.0 so is scaled and centre value - // substracted to get values in the range -1.0 to 1.0 - float x = 2.0f*( _horiz->read() - _x0 ); - float y = 2.0f*( _vert->read() - _y0 ); - - // Note: the x value here is inverted to ensure the positive x is to the - // right. This is simply due to how the potentiometer on the joystick - // I was using was connected up. It could have been corrected in hardware - // by swapping the power supply pins. Instead it is done in software so may - // need to be changed depending on your wiring setup - - Vector2D coord = {-x,y}; - return coord; -} - -// This maps the raw x,y coord onto a circular grid. -// See: http://mathproofs.blogspot.co.uk/2005/07/mapping-square-to-circle.html -Vector2D Gamepad::get_mapped_coord() -{ - Vector2D coord = get_coord(); - - // do the transformation - float x = coord.x*sqrt(1.0f-pow(coord.y,2.0f)/2.0f); - float y = coord.y*sqrt(1.0f-pow(coord.x,2.0f)/2.0f); - - Vector2D mapped_coord = {x,y}; - return mapped_coord; -} - -// this function converts the mapped coordinates into polar form -Polar Gamepad::get_polar() -{ - // get the mapped coordinate - Vector2D coord = get_mapped_coord(); - - // at this point, 0 degrees (i.e. x-axis) will be defined to the East. - // We want 0 degrees to correspond to North and increase clockwise to 359 - // like a compass heading, so we need to swap the axis and invert y - float x = coord.y; - float y = coord.x; - - float mag = sqrt(x*x+y*y); // pythagoras - float angle = RAD2DEG*atan2(y,x); - // angle will be in range -180 to 180, so add 360 to negative angles to - // move to 0 to 360 range - if (angle < 0.0f) { - angle+=360.0f; - } - - // the noise on the ADC causes the values of x and y to fluctuate slightly - // around the centred values. This causes the random angle values to get - // calculated when the joystick is centred and untouched. This is also when - // the magnitude is very small, so we can check for a small magnitude and then - // set the angle to -1. This will inform us when the angle is invalid and the - // joystick is centred - - if (mag < TOL) { - mag = 0.0f; - angle = -1.0f; - } - - Polar p = {mag,angle}; - return p; -} \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f Joystick/Joystick.h --- a/Joystick/Joystick.h Fri May 15 22:23:22 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -#ifndef JOYSTICK_H -#define JOYSTICK_H - -#include "mbed.h" - -// this value can be tuned to alter tolerance of joystick movement -#define TOL 0.1f -#define RAD2DEG 57.2957795131f - -enum Direction { - CENTRE, // 0 - N, // 1 - NE, // 2 - E, // 3 - SE, // 4 - S, // 5 - SW, // 6 - W, // 7 - NW // 8 -}; - -struct Vector2D { - float x; - float y; -}; - -struct Polar { - float mag; - float angle; -}; - -/** Joystick Class -@author Dr Craig A. Evans, University of Leeds -@brief Library for interfacing with analogue joystick - -Example: - -@code - -#include "mbed.h" -#include "Joystick.h" - -// y x button -Joystick joystick(PTB10,PTB11,PTC16); - -int main() { - - joystick.init(); - - while(1) { - - Vector2D coord = joystick.get_coord(); - printf("Coord = %f,%f\n",coord.x,coord.y); - - Vector2D mapped_coord = joystick.get_mapped_coord(); - printf("Mapped coord = %f,%f\n",mapped_coord.x,mapped_coord.y); - - float mag = joystick.get_mag(); - float angle = joystick.get_angle(); - printf("Mag = %f Angle = %f\n",mag,angle); - - Direction d = joystick.get_direction(); - printf("Direction = %i\n",d); - - if (joystick.button_pressed() ) { - printf("Button Pressed\n"); - } - - wait(0.5); - } - - -} - -* @endcode -*/ -class Joystick -{ -public: - - // y-pot x-pot button - Joystick(PinName vertPin,PinName horizPin,PinName clickPin); - - void init(); // needs to be called at start with joystick centred - float get_mag(); // polar - float get_angle(); // polar - Vector2D get_coord(); // cartesian co-ordinates x,y - Vector2D get_mapped_coord(); // x,y mapped to circle - Direction get_direction(); // N,NE,E,SE etc. - Polar get_polar(); // mag and angle in struct form - bool button_pressed(); // read button flag set in ISR when button pressed - -private: - - AnalogIn *vert; - AnalogIn *horiz; - InterruptIn *click; - - int _click_flag; // flag set in ISR - void click_isr(); // ISR on button press - - // centred x,y values - float _x0; - float _y0; - -}; - -#endif \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f N5110/Bitmap.cpp --- a/N5110/Bitmap.cpp Fri May 15 22:23:22 2020 +0000 +++ b/N5110/Bitmap.cpp Sat May 16 17:26:10 2020 +0000 @@ -90,7 +90,7 @@ // Find the required value of the pixel at the given location within // the bitmap data and then write it to the LCD screen int pixel = get_pixel(bitmap_row, bitmap_col); - lcd.setPixel(screen_col, screen_row, pixel); + lcd.setPixel(screen_col, screen_row,pixel); } } } \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f N5110/N5110.cpp --- a/N5110/N5110.cpp Fri May 15 22:23:22 2020 +0000 +++ b/N5110/N5110.cpp Sat May 16 17:26:10 2020 +0000 @@ -198,6 +198,16 @@ buffer[x][y/8] |= (1 << y%8); } } +void N5110::setPixel(unsigned int const x, + unsigned int const y, + bool const state) +{ + if (x<WIDTH && y<HEIGHT) { // check within range + // calculate bank and shift 1 to required position in the data byte + if(state) buffer[x][y/8] |= (1 << y%8); + else buffer[x][y/8] &= ~(1 << y%8); + } +} void N5110::clearPixel(unsigned int const x, unsigned int const y)
diff -r e3844250b77d -r b4a2954dd74f N5110/N5110.h --- a/N5110/N5110.h Fri May 15 22:23:22 2020 +0000 +++ b/N5110/N5110.h Sat May 16 17:26:10 2020 +0000 @@ -239,7 +239,9 @@ * Sets the display up in horizontal addressing mode and with normal video mode. */ void init(); - + void setPixel(unsigned int const x, + unsigned int const y, + bool const state); /** Turn off * * Powers down the display and turns of the backlight.
diff -r e3844250b77d -r b4a2954dd74f UFO/UFO.cpp --- a/UFO/UFO.cpp Fri May 15 22:23:22 2020 +0000 +++ b/UFO/UFO.cpp Sat May 16 17:26:10 2020 +0000 @@ -9,11 +9,11 @@ } -void UFO::init(int width,int height, int speed){ +void UFO::init(int sizeX,int sizeY){ - _width = width; - _height = height; // define the size of UFO - _x = rand() % (84- _width); + _sizeX = sizeX; + _sizeY = sizeY; + _x = rand() % (84- _sizeX); _y = 0; _speed=2; } @@ -33,14 +33,14 @@ void UFO::draw(N5110 &lcd) { //different ufo shapes for each level - int UFO[5][12] = { + int UFO[60] = { 1,1,1,1,1,1,1,1,1,1,1,1 , 1,1,1,0,0,0,0,0,0,1,1,1 , 1,1,1,0,0,0,0,0,0,1,1,1 , 1,1,1,0,0,0,0,0,0,1,1,1 , 1,1,1,1,1,1,1,1,1,1,1,1 }; - Bitmap sprite(enemy, _width, _height); + Bitmap sprite(UFO, _sizeX, _sizeY); sprite.render(lcd, _x, _y); } int UFO::getBlood(){
diff -r e3844250b77d -r b4a2954dd74f UFO/UFO.h --- a/UFO/UFO.h Fri May 15 22:23:22 2020 +0000 +++ b/UFO/UFO.h Sat May 16 17:26:10 2020 +0000 @@ -24,7 +24,7 @@ @param the columns of UFO image (int) @param the rows of UFO image (int) */ - void init(int x,int y,int _width,int _height); + void init(int _sizeX,int _sizeY); /* Get the position of UFO in lcd @return the position of UFO in lcd @@ -39,7 +39,7 @@ * @param lcd (N5110) * @param the value difficulty mode (int) */ - void draw(N5110 &lcd,int mode); + void draw(N5110 &lcd); /** Set the UFO speed * @param the value of UFO speed @@ -64,8 +64,8 @@ private: int _x; int _y; - int _width; - int _height; + int _sizeX; + int _sizeY; int _speed; int _blood;
diff -r e3844250b77d -r b4a2954dd74f bgm/bgm.cpp --- a/bgm/bgm.cpp Fri May 15 22:23:22 2020 +0000 +++ b/bgm/bgm.cpp Sat May 16 17:26:10 2020 +0000 @@ -1,27 +1,27 @@ #include "bgm.h" // construct & destruct/ -bgm::bgm() +Bgm::Bgm() : _timeout(new Timeout()), _buzzer(new PwmOut(PTC10)) {} -bgm::~bgm() +Bgm::~Bgm() { } -void Sound::tone(float frequency, float duration) +void Bgm::tone(float frequency, float duration) { _buzzer->period(1.0f/frequency); _buzzer->write(0.5); // 50% duty cycle - square wave - _timeout->attach(callback(this, &Sound::tone_off), duration ); + _timeout->attach(callback(this, &Bgm::tone_off), duration ); } // The number "1,2,3,4,5,6,7" means each number note in the notation // Excerpted from famous Chinese and English songs -void bgm::welcome(){ +void Bgm::welcome(){ tone(784.0f,0.4f); wait(0.4f); tone(784.0f,0.1f); @@ -61,7 +61,7 @@ } -void bgm::battlefield(){ +void Bgm::battlefield(){ tone(659.0f,0.3f); wait(0.3f); tone(659.0f,0.2f); @@ -107,7 +107,7 @@ } -void bgm::died(){ +void Bgm::died(){ tone(784.0f,0.2f); wait(0.2f); tone(880.0f,0.3f); @@ -135,7 +135,7 @@ } -void bgm::tone_off() +void Bgm::tone_off() { // called after timeout _buzzer->write(0.0);
diff -r e3844250b77d -r b4a2954dd74f homepage/homepage.cpp --- a/homepage/homepage.cpp Fri May 15 22:23:22 2020 +0000 +++ b/homepage/homepage.cpp Sat May 16 17:26:10 2020 +0000 @@ -1,10 +1,10 @@ #include "homepage.h" -Homepage::homepage(){ +Homepage::Homepage(){ } -Homepage::~homepage() +Homepage::~Homepage() {} void Homepage::welcome(N5110 &lcd,BusOut &output,Bgm &bgm) @@ -12,8 +12,6 @@ //show "Spaceship" lcd.printString(" Spaceship ",0,1); lcd.refresh(); - drawEverything(lcd); - lcd.refresh(); output=0b000000; //welcome bgm @@ -33,23 +31,22 @@ output=0b111111; lcd.clear(); } -void Homepage::homepage(N5110 &lcd,InterruptIn &buttonA, InterruptIn &start,int *score,int n) +void Homepage::homepage(N5110 &lcd,InterruptIn &buttonB, InterruptIn &start,int *score,int n) { //show Homepage and check the pressed button lcd.refresh(); while(1){ lcd.printString(" Press Start to start ",0,0); - lcd.printString(" A Rules",0,2); + lcd.printString(" Press B to Rules",0,2); lcd.refresh(); - if(buttonA){ + if(buttonB){ wait(0.5f); lcd.clear(); - //show the game rule - rules(lcd,buttonA); + rules(lcd,buttonB); }else if(start){ - lcd.clear(); //start the game + lcd.clear(); break; } } @@ -62,30 +59,8 @@ sprintf(buffer,"%d",score); lcd.printString(buffer,0,0); } -void Homepage::drawEverything(N5110 &lcd){ - - int Spaceship[15][10] = { - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,1,1,0,0,0,0, - 0,0,0,0,1,1,0,0,0,0, - 0,0,1,1,0,0,1,1,0,0, - 0,1,1,1,1,1,1,1,1,0, - 0,1,1,1,1,1,1,1,1,0, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,0,0,1,1,1,1, - 0,1,1,1,0,0,1,1,1,0, - 0,0,1,1,0,0,1,1,0,0, - 0,0,0,1,1,1,1,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - //draw spaceship - Bitmap sprite2(_Spaceship, 16, 10); - sprite2.render(lcd, 28, 28); -} -void Homepage::rules(N5110 &lcd,InterruptIn &buttonA) { + +void Homepage::rules(N5110 &lcd,InterruptIn &buttonB) { //show game rules while(1) { lcd.clear(); @@ -102,7 +77,7 @@ } } } -int homepage::again(N5110 &lcd,int score,InterruptIn &buttonX,InterruptIn &buttonY){ +int Homepage::again(N5110 &lcd,int score,InterruptIn &buttonX,InterruptIn &buttonY){ lcd.clear(); while(1){ @@ -123,7 +98,6 @@ return 0; } }else{ - // player doesn't have enough scores , so game over directly lcd.printString("Fail",0,1); lcd.printString("Press Y return",0,2); lcd.refresh();
diff -r e3844250b77d -r b4a2954dd74f homepage/homepage.h --- a/homepage/homepage.h Fri May 15 22:23:22 2020 +0000 +++ b/homepage/homepage.h Sat May 16 17:26:10 2020 +0000 @@ -37,7 +37,7 @@ * @param the button B (InterruptIn) * @param the start button (InterruptIn) */ - void homepage(N5110 &lcd,InterruptIn &buttonA,InterruptIn &buttonB, InterruptIn &start, int *score,int n); + void homepage(N5110 &lcd,InterruptIn &buttonB, InterruptIn &start, int *score,int n); /** The rangking lists * @param lcd (N5110) @@ -51,14 +51,13 @@ * @param lcd (N5110) * @param the value of score (int) */ - void drawEverything(N5110 &lcd); - + /** Display rules of the game * @param lcd (N5110) * @param the button B (InterruptIn) */ - void rules(N5110 &lcd,InterruptIn &buttonA,InterruptIn &buttonB); + void rules(N5110 &lcd,InterruptIn &buttonB); /**Play again the game when score > 10
diff -r e3844250b77d -r b4a2954dd74f myShip/spaceship.cpp --- a/myShip/spaceship.cpp Fri May 15 22:23:22 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* SPACE RACE Game using Arduino and Nokia 5110 LCD - * Coded by: Ruofan Li - * Date: 28-4-2020 - * Input -> Joystick (x0,y0) -*/ - -#include <spaceship.h> // Library for spaceship.cpp - -//*Construct and Destruct -Spaceship::Spaceship() -{ - -} - -Spaceship::~Spaceship() -{ - -} - -void Spaceship::init(int x,int y,int width,int height) -{ - _x = x; //x value is fixed - _y = y; //y is also fixed - _width = width; - _height = height; - _speed = 4; -} - -void Spaceship::draw(N5110 &lcd) -{ - // draw Spaceship in screen of N5110. -int _Spaceship[] = { - 0,0,0,0,0,0,0,0,0,0, - 0,0,0,0,1,1,0,0,0,0, - 0,0,0,0,1,1,0,0,0,0, - 0,0,1,1,0,0,1,1,0,0, - 0,1,1,1,1,1,1,1,1,0, - 0,1,1,1,1,1,1,1,1,0, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,1,1,1,1,1,1, - 1,1,1,1,0,0,1,1,1,1, - 0,1,1,1,0,0,1,1,1,0, - 0,0,1,1,0,0,1,1,0,0, - 0,0,0,1,1,1,1,0,0,0, - 0,0,0,0,0,0,0,0,0,0, - - Bitmap sprite(_Plane, _sizeX, _sizeY); - sprite.render(lcd, _x, _y); -}; - -void Spaceship::update(int d) -{ - - if(d == 3){ - //turn right - _x+=_speed; - - if(_x>75){ - - _x = 75; - } - }else if(d == 7){ - //turn left - _x-=_speed; - - if(_x<0) { - _x = 0; - } - } -} - -// get the position of the spaceship -Vector2D Spaceship::get_Pos() -{ - Vector2D p = {_x,_y}; - return p; -} \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f myShip/spaceship.h --- a/myShip/spaceship.h Fri May 15 22:23:22 2020 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -#ifndef spaceship_H -#define spaceship_H - -#include "mbed.h" -#include "N5110.h" -#include "Joystick.h" -#include "Bitmap.h" -/* spaceship Class -@Library for Spaceship object in the spaceship project -@coded by Li Ruofan -@May 2020 -*/ -class spaceship{ - -public: - /* Construct function of our ship*/ - Spaceship(); - - /* Destruct function of our ship*/ - ~Spaceship(); - - - /* Initiate the position and the size of the ship - @param the value of horizontal position x (int) - @param the value of vertical position y (int) - @param the columns of spaceship image (int) - @param the rows of spaceship image (int) - */ - void init(int x,int y,int width,int height); - - /* draw the image of the ship - @param lcd (N5110) - */ - void draw(N5110 &lcd); - - void update(); - /// access and mutate - - /* get position and speed of spaceship in the lcd - @return the current postion of spaceship - */ - void set_speed(Vector2D v); - Vector2D get_speed(); - Vector2D get_Pos(); - -private: - Vector2D _velocity; - int _x; - int _y; - int _width; - int _height; - int _speed; - -}; -#endif \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f shot/shot.h --- a/shot/shot.h Fri May 15 22:23:22 2020 +0000 +++ b/shot/shot.h Sat May 16 17:26:10 2020 +0000 @@ -1,43 +1,43 @@ -#ifndef BULLET_H -#define BULLET_H +#ifndef Shot_H +#define Shot_H #include "mbed.h" #include "N5110.h" #include "Joystick.h" #include "Bitmap.h" -/** Bullet Class -@brief Library for one of the objects bullet of star war, University of Leeds -@author Huang Xinjie -@date May 2019 +/** Shot Class +@brief Library for one of the objects Shot of spaceship, University of Leeds +@author Li Ruofan +@date May 2020 */ -class Bullet{ +class Shot{ public: /** Constructor */ - Bullet(); + Shot(); /** Destructor */ - ~Bullet(); + ~Shot(); - /** Initialize the position and the size of the bullet + /** Initialize the position and the size of the Shot * @param the value of horizontal position x (int) * @param the value of vertical position x (int) - * @param the number of columns of bullet image (int) - * @param the number of rows of bullet image (int) + * @param the number of columns of Shot image (int) + * @param the number of rows of Shot image (int) */ void init(int x,int y,int sizeX,int sizeY); - /** draw the image of the bullet + /** draw the image of the Shot * @param lcd (N5110) */ - void draw(N5110 &lcd); //draw the bullet + void draw(N5110 &lcd); //draw the Shot - /** Update the position of the bullet */ + /** Update the position of the Shot */ void update(); - /** get the position of bullet in the lcd - * @return the current postion of bullet + /** get the position of Shot in the lcd + * @return the current postion of Shot */ Vector2D getPos(); private:
diff -r e3844250b77d -r b4a2954dd74f spaceship/spaceship.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spaceship/spaceship.cpp Sat May 16 17:26:10 2020 +0000 @@ -0,0 +1,80 @@ +/* SPACE RACE Game using Arduino and Nokia 5110 LCD + * Coded by: Ruofan Li + * Date: 28-4-2020 + * Input -> Joystick (x0,y0) +*/ + +#include <spaceship.h> // Library for spaceship.cpp + +//*Construct and Destruct +Spaceship::Spaceship() +{ + +} + +Spaceship::~Spaceship() +{ + +} + +void Spaceship::init(int x,int y,int width,int height) +{ + _x = x; //x value is fixed + _y = y; //y is also fixed + _width = width; + _height = height; + _speed = 4; +} + +void Spaceship::draw(N5110 &lcd) +{ + // draw Spaceship in screen of N5110. +int spaceship[] = { + 0,0,0,0,0,0,0,0,0,0, + 0,0,0,0,1,1,0,0,0,0, + 0,0,0,0,1,1,0,0,0,0, + 0,0,1,1,0,0,1,1,0,0, + 0,1,1,1,1,1,1,1,1,0, + 0,1,1,1,1,1,1,1,1,0, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,1,1,1,1,1,1, + 1,1,1,1,0,0,1,1,1,1, + 0,1,1,1,0,0,1,1,1,0, + 0,0,1,1,0,0,1,1,0,0, + 0,0,0,1,1,1,1,0,0,0, + 0,0,0,0,0,0,0,0,0,0, + + Bitmap sprite(spaceship, _sizeX, _sizeY); + sprite.render(lcd, _x, _y); +}; + +void Spaceship::update(int d) +{ + + if(d == 3){ + //turn right + _x+=_speed; + + if(_x>75){ + + _x = 75; + } + }else if(d == 7){ + //turn left + _x-=_speed; + + if(_x<0) { + _x = 0; + } + } +} + +// get the position of the spaceship +Vector2D Spaceship::get_Pos() +{ + Vector2D p = {_x,_y}; + return p; +} \ No newline at end of file
diff -r e3844250b77d -r b4a2954dd74f spaceship/spaceship.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/spaceship/spaceship.h Sat May 16 17:26:10 2020 +0000 @@ -0,0 +1,55 @@ +#ifndef spaceship_H +#define spaceship_H + +#include "mbed.h" +#include "N5110.h" +#include "Joystick.h" +#include "Bitmap.h" +/* spaceship Class +@Library for Spaceship object in the spaceship project +@coded by Li Ruofan +@May 2020 +*/ +class Spaceship{ + +public: + /* Construct function of our ship*/ + Spaceship(); + + /* Destruct function of our ship*/ + ~Spaceship(); + + + /* Initiate the position and the size of the ship + @param the value of horizontal position x (int) + @param the value of vertical position y (int) + @param the columns of spaceship image (int) + @param the rows of spaceship image (int) + */ + void init(int x,int y,int width,int height); + + /* draw the image of the ship + @param lcd (N5110) + */ + void draw(N5110 &lcd); + + void update(); + /// access and mutate + + /* get position and speed of spaceship in the lcd + @return the current postion of spaceship + */ + void set_speed(Vector2D v); + Vector2D get_speed(); + Vector2D get_Pos(); + +private: + Vector2D _velocity; + int _x; + int _y; + int _width; + int _height; + int _speed; + +}; +#endif \ No newline at end of file