A program to demonstrate using the joystick of the mbed application shield.

Dependencies:   C12832

Fork of app-shield-joystick by Chris Styles

Files at this revision

API Documentation at this revision

Comitter:
sarahmarshy
Date:
Tue Sep 19 16:59:28 2017 +0000
Parent:
4:0c234f96926c
Commit message:
Update example program to move a ball around the screen

Changed in this revision

C12832.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 0c234f96926c -r 47721be170f9 C12832.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/C12832.lib	Tue Sep 19 16:59:28 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/users/chris/code/C12832/#7de323fa46fe
diff -r 0c234f96926c -r 47721be170f9 main.cpp
--- a/main.cpp	Mon Sep 18 20:21:59 2017 +0000
+++ b/main.cpp	Tue Sep 19 16:59:28 2017 +0000
@@ -1,24 +1,59 @@
 #include "mbed.h"
+#include "C12832.h"
+
+// Max X value of LCD screen
+#define MAX_X 124
+// Max Y value of LCD screen
+#define MAX_Y 28
 
-DigitalOut red_led(D5);
-DigitalOut blue_led(D8);
-DigitalOut green_led(D9);
+struct ball_t {
+    int y;
+    int x;
+};
+ball_t ball; // Keep track of ball's x and y
+C12832 lcd(D11, D13, D12, D7, D10);
 
+// Joystick Pins
 DigitalIn up(A2);
 DigitalIn down(A3);
 DigitalIn left(A4);
-AnalogIn right(A5);
+DigitalIn right(A5);
 DigitalIn fire(D4);
 
+void draw_ball() {
+    // Clear LCD
+    lcd.cls();
+    // Calculate ball's position based on joystick movement
+    if(left){
+        ball.x = ball.x > 0 ? ball.x-1 : MAX_X;
+    }
+    if(right) {
+        ball.x = ball.x < MAX_X ? ball.x+1 : 0;
+    }
+    if(up) {
+        ball.y = ball.y > 0 ? ball.y-1 : MAX_Y;
+    }
+    if(down) {
+        ball.y = ball.y < MAX_Y ? ball.y+1 : 0;
+    }
+    if(fire){
+        // Increment ball's x position by 10
+        ball.x = ball.x < MAX_X-10 ? ball.x+10 : 10-(MAX_X-ball.x);
+    }
+    
+    lcd.fillcircle(ball.x, ball.y, 3, 1);
+}
 
 int main()
 {
-
-    while (1) {
-        red_led =  !up && ! fire;
-        blue_led = !down;
-        green_led= !left && !right;
-    }
+    // Put the ball in the middle of the screen initially
+    ball.x = MAX_X/2;
+    ball.y = MAX_Y/2;
+    
+    EventQueue queue;
+    // Update the ball's position every 50 ms
+    queue.call_every(50, draw_ball);
+    queue.dispatch();
 }