A program to demonstrate using the joystick of the mbed application shield.
Fork of app-shield-joystick by
main.cpp@5:47721be170f9, 2017-09-19 (annotated)
- Committer:
- sarahmarshy
- Date:
- Tue Sep 19 16:59:28 2017 +0000
- Revision:
- 5:47721be170f9
- Parent:
- 2:507020c78d79
Update example program to move a ball around the screen
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 0:0325ecbd1916 | 1 | #include "mbed.h" |
sarahmarshy | 5:47721be170f9 | 2 | #include "C12832.h" |
sarahmarshy | 5:47721be170f9 | 3 | |
sarahmarshy | 5:47721be170f9 | 4 | // Max X value of LCD screen |
sarahmarshy | 5:47721be170f9 | 5 | #define MAX_X 124 |
sarahmarshy | 5:47721be170f9 | 6 | // Max Y value of LCD screen |
sarahmarshy | 5:47721be170f9 | 7 | #define MAX_Y 28 |
chris | 0:0325ecbd1916 | 8 | |
sarahmarshy | 5:47721be170f9 | 9 | struct ball_t { |
sarahmarshy | 5:47721be170f9 | 10 | int y; |
sarahmarshy | 5:47721be170f9 | 11 | int x; |
sarahmarshy | 5:47721be170f9 | 12 | }; |
sarahmarshy | 5:47721be170f9 | 13 | ball_t ball; // Keep track of ball's x and y |
sarahmarshy | 5:47721be170f9 | 14 | C12832 lcd(D11, D13, D12, D7, D10); |
chris | 0:0325ecbd1916 | 15 | |
sarahmarshy | 5:47721be170f9 | 16 | // Joystick Pins |
chris | 2:507020c78d79 | 17 | DigitalIn up(A2); |
chris | 2:507020c78d79 | 18 | DigitalIn down(A3); |
chris | 2:507020c78d79 | 19 | DigitalIn left(A4); |
sarahmarshy | 5:47721be170f9 | 20 | DigitalIn right(A5); |
chris | 2:507020c78d79 | 21 | DigitalIn fire(D4); |
chris | 0:0325ecbd1916 | 22 | |
sarahmarshy | 5:47721be170f9 | 23 | void draw_ball() { |
sarahmarshy | 5:47721be170f9 | 24 | // Clear LCD |
sarahmarshy | 5:47721be170f9 | 25 | lcd.cls(); |
sarahmarshy | 5:47721be170f9 | 26 | // Calculate ball's position based on joystick movement |
sarahmarshy | 5:47721be170f9 | 27 | if(left){ |
sarahmarshy | 5:47721be170f9 | 28 | ball.x = ball.x > 0 ? ball.x-1 : MAX_X; |
sarahmarshy | 5:47721be170f9 | 29 | } |
sarahmarshy | 5:47721be170f9 | 30 | if(right) { |
sarahmarshy | 5:47721be170f9 | 31 | ball.x = ball.x < MAX_X ? ball.x+1 : 0; |
sarahmarshy | 5:47721be170f9 | 32 | } |
sarahmarshy | 5:47721be170f9 | 33 | if(up) { |
sarahmarshy | 5:47721be170f9 | 34 | ball.y = ball.y > 0 ? ball.y-1 : MAX_Y; |
sarahmarshy | 5:47721be170f9 | 35 | } |
sarahmarshy | 5:47721be170f9 | 36 | if(down) { |
sarahmarshy | 5:47721be170f9 | 37 | ball.y = ball.y < MAX_Y ? ball.y+1 : 0; |
sarahmarshy | 5:47721be170f9 | 38 | } |
sarahmarshy | 5:47721be170f9 | 39 | if(fire){ |
sarahmarshy | 5:47721be170f9 | 40 | // Increment ball's x position by 10 |
sarahmarshy | 5:47721be170f9 | 41 | ball.x = ball.x < MAX_X-10 ? ball.x+10 : 10-(MAX_X-ball.x); |
sarahmarshy | 5:47721be170f9 | 42 | } |
sarahmarshy | 5:47721be170f9 | 43 | |
sarahmarshy | 5:47721be170f9 | 44 | lcd.fillcircle(ball.x, ball.y, 3, 1); |
sarahmarshy | 5:47721be170f9 | 45 | } |
chris | 0:0325ecbd1916 | 46 | |
chris | 0:0325ecbd1916 | 47 | int main() |
chris | 0:0325ecbd1916 | 48 | { |
sarahmarshy | 5:47721be170f9 | 49 | // Put the ball in the middle of the screen initially |
sarahmarshy | 5:47721be170f9 | 50 | ball.x = MAX_X/2; |
sarahmarshy | 5:47721be170f9 | 51 | ball.y = MAX_Y/2; |
sarahmarshy | 5:47721be170f9 | 52 | |
sarahmarshy | 5:47721be170f9 | 53 | EventQueue queue; |
sarahmarshy | 5:47721be170f9 | 54 | // Update the ball's position every 50 ms |
sarahmarshy | 5:47721be170f9 | 55 | queue.call_every(50, draw_ball); |
sarahmarshy | 5:47721be170f9 | 56 | queue.dispatch(); |
chris | 0:0325ecbd1916 | 57 | } |
chris | 0:0325ecbd1916 | 58 | |
chris | 0:0325ecbd1916 | 59 |