Demo code for LED/trackball functionality of the Blackberry Trackerball Breakout board

Dependencies:   Trackball mbed

Revision:
1:f0caa0dfa669
Parent:
0:8e91b1443453
--- a/main.cpp	Tue Oct 20 21:11:52 2015 +0000
+++ b/main.cpp	Wed Oct 21 19:03:48 2015 +0000
@@ -4,18 +4,14 @@
 //set up the trackball
 
 Trackball trackball(p12, p13, p14, p15, p16, p17, p18, p19, p20);
+
+//for prints to serial port
 Serial pc(USBTX, USBRX);
 
-DigitalOut led1(LED1);
-DigitalOut led2(LED2);
-DigitalOut led3(LED3);
-DigitalOut led4(LED4);
 
 int main() {
     
-//initial LED testing
-//turn on each color for 2 seconds
-    
+    //turn on each LED individually for 2 seconds each
     trackball.write(1, color_WHITE);
     wait(2);
     trackball.write(0, color_WHITE);
@@ -29,51 +25,76 @@
     wait(2);
     trackball.write(0, color_RED);
 
-int x_pos = 0;
-int y_pos = 0;
-  
+    //positioning data
+    int x = 0;
+    int y = 0;
+    //keeping track of changes in movement
+    unsigned int old_up = 1;
+    unsigned int new_up = 1;
+    unsigned int old_down = 1;
+    unsigned int new_down = 1;
+    unsigned int old_right = 1;
+    unsigned int new_right = 1;
+    unsigned int old_left = 1;
+    unsigned int new_left = 1;
+    
+    
     while(1) {
-        //read the pins
-        //if LFT,RHT,UP,DWN,or BTN are 0
-        //write out to the colored LEDs
-        //keep track of x and y positions
-        //wait .2
-        
-        //make sure to turn off all other color LEDS when turning on a new one.
-        //keeping the writes inside the if statements prevents flashing of the LED if it is already on and needs to stay on.
+
+
+//comparing old val to new val makes sure to only register changes to the value as movement
+    new_up = trackball.read(dir_UP);
+    new_down = trackball.read(dir_DOWN);
+    new_left = trackball.read(dir_LEFT);
+    new_right = trackball.read(dir_RIGHT);
  
-        if(trackball.read(dir_UP) == 0){
-            y_pos++;
+        if((new_up == 0) && (old_up == 1)){
+            trackball.write(1, color_BLUE);
+            trackball.write(0, color_WHITE);
+            trackball.write(0, color_GREEN);
+            trackball.write(0, color_RED);
+            y++;
+            }
+        if((new_down == 0) && (old_down == 1)){
+            trackball.write(1, color_GREEN);
+            trackball.write(0, color_WHITE);
+            trackball.write(0, color_BLUE);
+            trackball.write(0, color_RED);
+            y--;
+            }
+        if((new_left == 0) && (old_left == 1)){
+            
             trackball.write(1, color_WHITE);
             trackball.write(0, color_GREEN);
             trackball.write(0, color_BLUE);
             trackball.write(0, color_RED);
-            }
-        else if(trackball.read(dir_DOWN) == 0){
-            y_pos--;
-            trackball.write(1, color_GREEN);
-            trackball.write(0, color_WHITE);
-            trackball.write(0, color_BLUE);
-            trackball.write(0, color_RED);
+            x--;
             }
-        else if(trackball.read(dir_LEFT) == 0){
-            x_pos--;
-            trackball.write(1, color_BLUE);
-            trackball.write(0, color_WHITE);
-            trackball.write(0, color_GREEN);
-            trackball.write(0, color_RED);
-            }
-        else if(trackball.read(dir_RIGHT) == 0){
-            x_pos++;
+        if((new_right == 0) && (old_right == 1)){
             trackball.write(1, color_RED);
             trackball.write(0, color_WHITE);
             trackball.write(0, color_GREEN);
             trackball.write(0, color_BLUE);
+            x++;  
             }
-        else if(trackball.read(dir_BUTTON) == 0){
-            pc.printf("Button pressed.");
+        if(trackball.read(dir_BUTTON) == 0){
+            trackball.write(1, color_RED);
+            trackball.write(1, color_BLUE);
+            trackball.write(1, color_GREEN);
+            trackball.write(1, color_WHITE);
             }
-        pc.printf("X pos: %d, Y pos: %d\n", x_pos, y_pos);  
-        wait(.2);
+            
+            pc.printf("X: %d, Y: %d\n", x, y);
+            
+            //keep track of these now old vals to compare to the future to see changes
+            old_up = new_up;
+            old_down = new_down;
+            old_left = new_left;
+            old_right = new_right;
+            
+            
+            wait(.2);
+    
+            
     }
 }