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 3Ball 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 motionint 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 structsVector2D 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);
}
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.
This site uses cookies to store information on your computer.
By continuing to use our site, you consent to our cookies.
If you are not happy with the use of these cookies, please review our
Cookie Policy
to learn how they can be disabled.
By disabling cookies, some features of the site will not work.
Access Warning
You do not have the correct permissions to perform this operation.