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.
Revision 19:c6ebd1394bda, committed 2018-05-08
- Comitter:
- ahmedhedait
- Date:
- Tue May 08 11:32:13 2018 +0000
- Parent:
- 18:d18b9185fa4f
- Child:
- 20:041affa5e242
- Commit message:
- I have implemented the drawing of the maze into its own class.
Changed in this revision
--- a/Joystick/Joystick.cpp Tue May 08 10:52:21 2018 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,162 +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;
-}
--- a/Joystick/Joystick.h Tue May 08 10:52:21 2018 +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
--- a/Joystick/mbed.bld Tue May 08 10:52:21 2018 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -https://os.mbed.com/users/mbed_official/code/mbed/builds/994bdf8177cb \ No newline at end of file
--- a/Maze/Maze.cpp Tue May 08 10:52:21 2018 +0000
+++ b/Maze/Maze.cpp Tue May 08 11:32:13 2018 +0000
@@ -9,4 +9,36 @@
Maze::~Maze()
{
+}
+
+void Maze::draw(N5110 &lcd)
+{
+ //I HAVE DRAWN THE RECTNAGLE OF THE MAZE IN TO START
+ lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);
+
+ // I HAVE DRAWN THE WALLS OF THE MAZE AS RECTANGLES AND JOINED THEM UP IN WHICH TO CREATE THE WALLS OF THE MAZE.
+ // a b c d
+ lcd.drawRect(10,0,1,39,FILL_BLACK);
+ lcd.drawRect(18,32,1,15,FILL_BLACK);
+ lcd.drawRect(18,25,18,1,FILL_BLACK);
+ lcd.drawRect(36,25,1,25,FILL_BLACK);
+ lcd.drawRect(45,18,1,30,FILL_BLACK);
+ lcd.drawRect(18,18,27,1,FILL_BLACK);
+ lcd.drawRect(18,10,27,1,FILL_BLACK);
+ lcd.drawRect(45,0,1,11,FILL_BLACK);
+ lcd.drawRect(55,6,1,45,FILL_BLACK);
+ lcd.drawRect(64,0,1,20,FILL_BLACK);
+ lcd.drawRect(64,27,1,13,FILL_BLACK);
+ lcd.drawRect(72,10,1,30,FILL_BLACK);
+ lcd.drawRect(64,40,20,1,FILL_BLACK);
+
+
+ // I REMOVED SOME PIXELS FROM THE RIGHT SIDE OF THE MAZE TO CREATE THE OPENING IN WHICH THE BALL SHOULD GO THROUGH.
+ lcd.setPixel(83,24,false);
+ lcd.setPixel(83,25,false);
+ lcd.setPixel(83,26,false);
+ lcd.setPixel(83,27,false);
+ lcd.setPixel(83,28,false);
+ lcd.setPixel(83,29,false);
+ lcd.setPixel(83,30,false);
}
\ No newline at end of file
--- a/Maze/Maze.h Tue May 08 10:52:21 2018 +0000
+++ b/Maze/Maze.h Tue May 08 11:32:13 2018 +0000
@@ -12,6 +12,8 @@
Maze();
~Maze();
+ void draw(N5110 &lcd);
+
private:
};
--- a/MazeEngine/MazeEngine.cpp Tue May 08 10:52:21 2018 +0000
+++ b/MazeEngine/MazeEngine.cpp Tue May 08 11:32:13 2018 +0000
@@ -8,4 +8,21 @@
MazeEngine::~MazeEngine()
{
+}
+
+void MazeEngine::init()
+{
+
+}
+
+void MazeEngine::read_input(Gamepad &pad)
+{
+ _d = pad.get_direction();
+}
+
+void MazeEngine::draw(N5110 &lcd)
+{
+ // draw the elements in the LCD buffer
+ // maze
+ _maze.draw(lcd);
}
\ No newline at end of file
--- a/MazeEngine/MazeEngine.h Tue May 08 10:52:21 2018 +0000
+++ b/MazeEngine/MazeEngine.h Tue May 08 11:32:13 2018 +0000
@@ -14,7 +14,14 @@
MazeEngine();
~MazeEngine();
+ void init();
+ void read_input(Gamepad &pad);
+ void draw(N5110 &lcd);
+
private:
+ Maze _maze;
+ Direction _d;
+
};
#endif
\ No newline at end of file
--- a/main.cpp Tue May 08 10:52:21 2018 +0000
+++ b/main.cpp Tue May 08 11:32:13 2018 +0000
@@ -22,6 +22,10 @@
+void render();
+
+
+
int main( )
{
// INTIALISING THE LCD AND GAMEPAD.
@@ -31,6 +35,8 @@
lcd.normalMode();
lcd.setBrightness(0.5);
+ maze.init();
+
// INTIALISING THE STARTING POSITION OF THE BALL IN SCREEN AND THE LETTERS.
float circy = 5;
@@ -40,226 +46,212 @@
int c;
int d;
-
- while(1) {
-
- lcd.clear();
-
-
- Direction dir = pad.get_direction();
- // printf("direction %i\n", dir);
-
-
- // GIVE THE BALL A CERTAIN VELOCITY.
- int speed = 1;
-
- if (dir == N) {
- circy -= speed;
- } else if (dir == S) {
- circy += speed;
- }
-
- if (dir == W) {
- circx -= speed;
- } else if (dir == E) {
- circx += speed;
- }
-
-
-
- //I HAVE DRAWN THE RECTNAGLE OF THE MAZE IN TO START
- lcd.drawRect(0,0,84,48,FILL_TRANSPARENT);
+ // game loop - read input, update the game state and render the display
+ while (1) {
+ maze.read_input(pad);
+ //maze.update(pad);
+ render();
+ wait(.035);
+ }
- // I HAVE DRAWN THE WALLS OF THE MAZE AS RECTANGLES AND JOINED THEM UP IN WHICH TO CREATE THE WALLS OF THE MAZE.
- // a b c d
- lcd.drawRect(10,0,1,39,FILL_BLACK);
- lcd.drawRect(18,32,1,15,FILL_BLACK);
- lcd.drawRect(18,25,18,1,FILL_BLACK);
- lcd.drawRect(36,25,1,25,FILL_BLACK);
- lcd.drawRect(45,18,1,30,FILL_BLACK);
- lcd.drawRect(18,18,27,1,FILL_BLACK);
- lcd.drawRect(18,10,27,1,FILL_BLACK);
- lcd.drawRect(45,0,1,11,FILL_BLACK);
- lcd.drawRect(55,6,1,45,FILL_BLACK);
- lcd.drawRect(64,0,1,20,FILL_BLACK);
- lcd.drawRect(64,27,1,13,FILL_BLACK);
- lcd.drawRect(72,10,1,30,FILL_BLACK);
- lcd.drawRect(64,40,20,1,FILL_BLACK);
+ /*
+ while(1) {
-
- // I REMOVED SOME PIXELS FROM THE RIGHT SIDE OF THE MAZE TO CREATE THE OPENING IN WHICH THE BALL SHOULD GO THROUGH.
- lcd.setPixel(83,24,false);
- lcd.setPixel(83,25,false);
- lcd.setPixel(83,26,false);
- lcd.setPixel(83,27,false);
- lcd.setPixel(83,28,false);
- lcd.setPixel(83,29,false);
- lcd.setPixel(83,30,false);
-
-
- // VERY SIMPLE CODE IN WHCIH I DREW THE BALL OF THE MAZE.
- lcd.drawCircle(circx,circy,2,FILL_BLACK);
+ lcd.clear();
- // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT OFF THE DIMENSIONS OF THE LCD SCREEN.
- if (circy < 3) {
- circy = 3;
- }
-
- if (circy > HEIGHT - 4) {
- circy = HEIGHT - 4;
- }
+ Direction dir = pad.get_direction();
+ // printf("direction %i\n", dir);
- if (circx < 3) {
- circx = 3;
- }
-
- // FOR LOOP TO STOP BALL FROM GOING THROUGH WALLS
- for (int i = 0; i <= 12; i++) {
- //printf(" Stage %d\n", i);
-
- if(i == 0) { // SET VARIABLES FOR EACH WALL
- a = 10;
- b = 0;
- c = 1;
- d = 39;
- } else if (i == 1) {
- a = 18;
- b = 32;
- c = 1;
- d = 15;
- }
+ // GIVE THE BALL A CERTAIN VELOCITY.
+ int speed = 1;
- else if (i == 2) {
- a = 36;
- b = 25;
- c = 1;
- d = 25;
- }
-
- else if(i == 3) {
- a = 45;
- b = 0;
- c = 1;
- d = 11;
- }
-
- else if (i == 4) {
- a = 45;
- b = 18;
- c = 1;
- d = 30;
+ if (dir == N) {
+ circy -= speed;
+ } else if (dir == S) {
+ circy += speed;
}
- else if (i == 5) {
- a = 55;
- b = 6;
- c = 1;
- d = 45;
- }
-
- else if (i == 6) {
- a = 64;
- b = 0;
- c = 1;
- d = 20;
- }
-
- else if (i == 7) {
- a = 64;
- b = 27;
- c = 1;
- d = 13;
- }
-
- else if (i == 8) {
- a = 72;
- b = 10;
- c = 1;
- d = 30;
- }
-
- else if (i == 9) {
- a = 18;
- b = 25;
- c = 18;
- d = 1;
- } else if (i == 10) {
- a = 18;
- b = 18;
- c = 27;
- d = 1;
- }
-
- else if (i == 11) {
- a = 18;
- b = 10;
- c = 27;
- d = 1;
- }
-
- else if (i == 12) {
- a = 64;
- b = 40;
- c = 20;
- d = 1;
+ if (dir == W) {
+ circx -= speed;
+ } else if (dir == E) {
+ circx += speed;
}
- if (
- (circy >= b - 2) && //top
- (circy <= 1 + b + d) && //bottom
- (circx >= a - 2) && //left
- (circx <= a + c + 1) //right
- ) {
- //left
- if (circx <= a - 2) {
- if(circx >= a - 3) {
- circx = a - 3;
- }
+ // VERY SIMPLE CODE IN WHCIH I DREW THE BALL OF THE MAZE.
+ lcd.drawCircle(circx,circy,2,FILL_BLACK);
+
+
+ // THIS CODE IS NEEDED TO MAKE SURE THAT THE BALL DOES NOT OFF THE DIMENSIONS OF THE LCD SCREEN.
+ if (circy < 3) {
+ circy = 3;
+ }
+
+ if (circy > HEIGHT - 4) {
+ circy = HEIGHT - 4;
+ }
+
+ if (circx < 3) {
+ circx = 3;
+ }
+
+ // FOR LOOP TO STOP BALL FROM GOING THROUGH WALLS
+ for (int i = 0; i <= 12; i++) {
+ //printf(" Stage %d\n", i);
+
+ if(i == 0) { // SET VARIABLES FOR EACH WALL
+ a = 10;
+ b = 0;
+ c = 1;
+ d = 39;
+
+ } else if (i == 1) {
+ a = 18;
+ b = 32;
+ c = 1;
+ d = 15;
+ }
+
+ else if (i == 2) {
+ a = 36;
+ b = 25;
+ c = 1;
+ d = 25;
}
- //right
- if(circx >= a + 2) {
- if(circx <= a + 3) {
- circx = a + 3;
- }
+ else if(i == 3) {
+ a = 45;
+ b = 0;
+ c = 1;
+ d = 11;
+ }
+
+ else if (i == 4) {
+ a = 45;
+ b = 18;
+ c = 1;
+ d = 30;
}
- //top
- if(circy <= b - 2) {
- if(circy >= b - 3)
- circy = b - 3;
+ else if (i == 5) {
+ a = 55;
+ b = 6;
+ c = 1;
+ d = 45;
+ }
+
+ else if (i == 6) {
+ a = 64;
+ b = 0;
+ c = 1;
+ d = 20;
+ }
+
+ else if (i == 7) {
+ a = 64;
+ b = 27;
+ c = 1;
+ d = 13;
+ }
+
+ else if (i == 8) {
+ a = 72;
+ b = 10;
+ c = 1;
+ d = 30;
}
- //bottom
- if(circy >= b + d) {
- if(circy <= 2 + b + d)
- (circy = 2 + b + d);
+ else if (i == 9) {
+ a = 18;
+ b = 25;
+ c = 18;
+ d = 1;
+ } else if (i == 10) {
+ a = 18;
+ b = 18;
+ c = 27;
+ d = 1;
+ }
+
+ else if (i == 11) {
+ a = 18;
+ b = 10;
+ c = 27;
+ d = 1;
+ }
+
+ else if (i == 12) {
+ a = 64;
+ b = 40;
+ c = 20;
+ d = 1;
+ }
+
+
+ if (
+ (circy >= b - 2) && //top
+ (circy <= 1 + b + d) && //bottom
+ (circx >= a - 2) && //left
+ (circx <= a + c + 1) //right
+ ) {
+ //left
+ if (circx <= a - 2) {
+ if(circx >= a - 3) {
+ circx = a - 3;
+ }
+ }
+
+ //right
+ if(circx >= a + 2) {
+ if(circx <= a + 3) {
+ circx = a + 3;
+ }
+ }
+
+ //top
+ if(circy <= b - 2) {
+ if(circy >= b - 3)
+ circy = b - 3;
+ }
+
+ //bottom
+ if(circy >= b + d) {
+ if(circy <= 2 + b + d)
+ (circy = 2 + b + d);
+ }
}
}
- }
- /* WHEN THE BALL REACHES THE Y-AXIS NEEDED WHICH IS 27, THEN THE JOYSTICK FREELY MOVE THE BALL RIGHT THROUGH THE OPENING OF THE SMAZE WALL,
- HOWEVER, IF THE BALL IS NOT EQUAL TO THE Y-AXIS NEEDED, THEN THE BALL MUST BE RESTRICTED TO MOVING SO THAT IT DOES NOT PASS THE WALLS. */
- if (circy == 27) {
- if (circx > WIDTH) {
- circx = WIDTH;
+ // WHEN THE BALL REACHES THE Y-AXIS NEEDED WHICH IS 27, THEN THE JOYSTICK FREELY MOVE THE BALL RIGHT THROUGH THE OPENING OF THE SMAZE WALL,
+ // HOWEVER, IF THE BALL IS NOT EQUAL TO THE Y-AXIS NEEDED, THEN THE BALL MUST BE RESTRICTED TO MOVING SO THAT IT DOES NOT PASS THE WALLS.
+ if (circy == 27) {
+ if (circx > WIDTH) {
+ circx = WIDTH;
+ }
+ } else if (circx > 80) {
+ circx = 80;
}
- } else if (circx > 80) {
- circx = 80;
- }
- /* HERE IS A SIMPLE CODE THAT WHEN THE BALL PASS THROUGH THE OPENING THEN THE SCREEN SHOULD BE CLEARED IN WHICH BRAVO IS PRINTED TO
- TELL THE USER THE GAME IS FINISHED. */
- if (circx > 83 & circy == 27) {
- lcd.clear();
- lcd.printString(" Bravo! ",12,2);
- }
+ // HERE IS A SIMPLE CODE THAT WHEN THE BALL PASS THROUGH THE OPENING THEN THE SCREEN SHOULD BE CLEARED IN WHICH BRAVO IS PRINTED TO
+ // TELL THE USER THE GAME IS FINISHED.
+ if (circx > 83 & circy == 27) {
+ lcd.clear();
+ lcd.printString(" Bravo! ",12,2);
+ }
- wait(.035);
- lcd.refresh();
- }
+ wait(.035);
+ lcd.refresh();
+ }*/
}
+
+// this function draws each frame on the LCD
+void render()
+{
+ // clear screen, re-draw and refresh
+ lcd.clear();
+ maze.draw(lcd);
+ lcd.refresh();
+}
\ No newline at end of file