James Cummins / Mbed 2 deprecated el17jnc

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Ball Class Reference

Ball Class Reference

Ball Class. More...

#include <Ball.h>

Public Member Functions

 Ball ()
 Create a Ball object.
 ~Ball ()
 Delete a ball member to free up memory.
void init (int radius)
 Initialise ball object.
void read_input (FXOS8700CQ &accelerometer)
 read the input from the accelerometer and convert it to an instantaneous velocity for the ball.
void update ()
 update the coordinates of the ball within the range of the screen
void draw (N5110 &lcd)
 render the ball onto the LCD screen
Vector2D get_velocity ()
 get the instantaneous velocity of the ball
Vector2D get_position ()
 get the instantaneous position of the ball
int get_radius ()
 get the radius of the ball
int get_ball_speed ()
 get the speed multiplying factor of the ball.
void set_ball_speed (int ball_speed)
 Set the sensitivity of the ball's motion to the accelerometer input.
void set_velocity (Vector2D vel)
 set the instantaneous velocities in the x and y directions
void set_position (Vector2D pos)
 set the instantaneous x and y positions of the ball
void set_radius (int radius)
 set the radius of the ball

Detailed Description

Ball Class.

Library for creating a ball and moving it round a pre-defined area. The library also implements drawing the ball on an LCD display

Author:
James Cummins
#include "mbed.h"
#include "ball.h"
#define RADIUS 3

Ball ball;
FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL);
N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);
Gamepad gamepad;

int main(){
    ball.init(RADIUS);  //first must initialise the ball with its radius
    
    //can check what radius we've initialised with...
    int radius = ball.get_radius();
    
    //...and change it if preferred
    radius = 5;
    ball.set_radius(radius);
    
    //Check the current sensitivity of the ball to input motion
    int sensitivity = ball.get_ball_speed();
    
    //and can set it to a different value to cause greater movement for the
    //same degree of tilt in the gamepad
    sensitivity = 3;
    ball.set_ball_speed(sensitivity);
    
    //read_input, update and draw combine to monitor an input and display the
    //subsequent output changes on an LCD display 
    ball.read_input(accelerometer);
    ball.update();
    
    //Can read the ball's position and velocity into Vector2D structs
    Vector2D coords = ball.get_position();
    Vector2D vel = ball.get_velocity();
    
    if(coords.x > 80){ coords.x = 80; }
    if(vel.y > 5){ vel.y = 5; }
    
    //Passing a Vector2D into set_position or set_velocity 
    //moves the ball to a new desired location or at a new speed
    ball.set_position(coords);
    ball.set_velocity(vel);
    
    ball.draw(lcd);
}

Definition at line 68 of file Ball.h.


Constructor & Destructor Documentation

Ball (  )

Create a Ball object.

Definition at line 3 of file Ball.cpp.

~Ball (  )

Delete a ball member to free up memory.

Definition at line 6 of file Ball.cpp.


Member Function Documentation

void draw ( N5110 lcd )

render the ball onto the LCD screen

Parameters:
lcd- N5110 object to interface with the LCD

Definition at line 18 of file Ball.cpp.

int get_ball_speed (  )

get the speed multiplying factor of the ball.

Essentially the sensitivity to the accelerometer input

Returns:
ball speed multiplier as an integer in the range 1-10

Definition at line 55 of file Ball.cpp.

Vector2D get_position (  )

get the instantaneous position of the ball

Returns:
x and y positions in a 2D vector. 0 < x < 83 and 0 < y < 47.

Definition at line 40 of file Ball.cpp.

int get_radius (  )

get the radius of the ball

Returns:
radius of the ball as an integer

Definition at line 50 of file Ball.cpp.

Vector2D get_velocity (  )

get the instantaneous velocity of the ball

Returns:
x and y velocities of the ball in a 2D vector

Definition at line 45 of file Ball.cpp.

void init ( int  radius )

Initialise ball object.

Sets the initial position of the ball to the centre of the LCD display. Sets the initial velocity of the ball to 0 and the sensitivity of the ball to 5/10.

Parameters:
radius- integer for radius of the ball

Definition at line 9 of file Ball.cpp.

void read_input ( FXOS8700CQ &  accelerometer )

read the input from the accelerometer and convert it to an instantaneous velocity for the ball.

Parameters:
accelerometer- FXOS8700CQ object to retrieve acceleromter readings

Definition at line 23 of file Ball.cpp.

void set_ball_speed ( int  ball_speed )

Set the sensitivity of the ball's motion to the accelerometer input.

Parameters:
ball_speed- integer in the range 1-10

Definition at line 60 of file Ball.cpp.

void set_position ( Vector2D  pos )

set the instantaneous x and y positions of the ball

Parameters:
pos- a 2D vector (using the gamepad class) of the desired x and y coordinates

Definition at line 69 of file Ball.cpp.

void set_radius ( int  radius )

set the radius of the ball

Parameters:
radius- integer value

Definition at line 74 of file Ball.cpp.

void set_velocity ( Vector2D  vel )

set the instantaneous velocities in the x and y directions

Parameters:
vel- a 2D vector (using the gamepad class) of the desired x and y velocities

Definition at line 64 of file Ball.cpp.

void update (  )

update the coordinates of the ball within the range of the screen

Definition at line 29 of file Ball.cpp.