Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Ball.h
00001 #ifndef BALL_H 00002 #define BALL_H 00003 00004 #include "mbed.h" 00005 #include "N5110.h" 00006 #include "Gamepad.h" 00007 #include "FXOS8700CQ.h" 00008 00009 /**Ball Class 00010 @brief Library for creating a ball and moving it round a pre-defined area. 00011 @brief The library also implements drawing the ball on an LCD display 00012 00013 @author James Cummins 00014 00015 @code 00016 00017 #include "mbed.h" 00018 #include "ball.h" 00019 #define RADIUS 3 00020 00021 Ball ball; 00022 FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL); 00023 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); 00024 Gamepad gamepad; 00025 00026 int main(){ 00027 ball.init(RADIUS); //first must initialise the ball with its radius 00028 00029 //can check what radius we've initialised with... 00030 int radius = ball.get_radius(); 00031 00032 //...and change it if preferred 00033 radius = 5; 00034 ball.set_radius(radius); 00035 00036 //Check the current sensitivity of the ball to input motion 00037 int sensitivity = ball.get_ball_speed(); 00038 00039 //and can set it to a different value to cause greater movement for the 00040 //same degree of tilt in the gamepad 00041 sensitivity = 3; 00042 ball.set_ball_speed(sensitivity); 00043 00044 //read_input, update and draw combine to monitor an input and display the 00045 //subsequent output changes on an LCD display 00046 ball.read_input(accelerometer); 00047 ball.update(); 00048 00049 //Can read the ball's position and velocity into Vector2D structs 00050 Vector2D coords = ball.get_position(); 00051 Vector2D vel = ball.get_velocity(); 00052 00053 if(coords.x > 80){ coords.x = 80; } 00054 if(vel.y > 5){ vel.y = 5; } 00055 00056 //Passing a Vector2D into set_position or set_velocity 00057 //moves the ball to a new desired location or at a new speed 00058 ball.set_position(coords); 00059 ball.set_velocity(vel); 00060 00061 ball.draw(lcd); 00062 } 00063 00064 @endcode 00065 */ 00066 00067 00068 class Ball { 00069 00070 public: 00071 /** 00072 * @brief Create a Ball object 00073 */ 00074 Ball(); 00075 /** 00076 * @brief Delete a ball member to free up memory 00077 */ 00078 ~Ball(); 00079 00080 /** Initialise ball object 00081 * 00082 * Sets the initial position of the ball to the centre of the LCD display. 00083 * Sets the initial velocity of the ball to 0 and the sensitivity of the 00084 * ball to 5/10. 00085 * @param radius - integer for radius of the ball 00086 */ 00087 void init(int radius); 00088 /** 00089 * @brief read the input from the accelerometer and convert it to an instantaneous velocity 00090 * for the ball. 00091 * @param accelerometer - FXOS8700CQ object to retrieve acceleromter readings 00092 */ 00093 void read_input(FXOS8700CQ &accelerometer); 00094 /** 00095 * @brief update the coordinates of the ball within the range of the screen 00096 */ 00097 void update(); 00098 /** 00099 * @brief render the ball onto the LCD screen 00100 * @param lcd - N5110 object to interface with the LCD 00101 */ 00102 void draw(N5110 &lcd); 00103 /** 00104 * @brief get the instantaneous velocity of the ball 00105 * @returns x and y velocities of the ball in a 2D vector 00106 */ 00107 Vector2D get_velocity(); 00108 /** 00109 * @brief get the instantaneous position of the ball 00110 * @returns x and y positions in a 2D vector. 0 < x < 83 and 0 < y < 47. 00111 */ 00112 Vector2D get_position(); 00113 /** 00114 * @brief get the radius of the ball 00115 * @returns radius of the ball as an integer 00116 */ 00117 int get_radius(); 00118 /** 00119 * @brief get the speed multiplying factor of the ball. Essentially the sensitivity 00120 * to the accelerometer input 00121 * @returns ball speed multiplier as an integer in the range 1-10 00122 */ 00123 int get_ball_speed(); 00124 /** 00125 * @brief Set the sensitivity of the ball's motion to the accelerometer input 00126 * @param ball_speed - integer in the range 1-10 00127 */ 00128 void set_ball_speed(int ball_speed); 00129 /** 00130 * @brief set the instantaneous velocities in the x and y directions 00131 * @param vel - a 2D vector (using the gamepad class) of the desired x and y velocities 00132 */ 00133 void set_velocity(Vector2D vel); 00134 /** 00135 * @brief set the instantaneous x and y positions of the ball 00136 * @param pos - a 2D vector (using the gamepad class) of the desired x and y coordinates 00137 */ 00138 void set_position(Vector2D pos); 00139 /** 00140 * @brief set the radius of the ball 00141 * @param radius - integer value 00142 */ 00143 void set_radius(int radius); 00144 00145 private: 00146 int _radius; 00147 int _x; 00148 int _y; 00149 Vector2D _velocity; 00150 int _ball_speed; 00151 }; 00152 #endif
Generated on Thu Jul 14 2022 00:39:27 by
1.7.2