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

Dependencies:   Trackball mbed

Files at this revision

API Documentation at this revision

Comitter:
jkerickson
Date:
Tue Oct 20 21:11:52 2015 +0000
Child:
1:f0caa0dfa669
Commit message:
Basic functionality of trackball/LEDs

Changed in this revision

Trackball.h 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
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Trackball.h	Tue Oct 20 21:11:52 2015 +0000
@@ -0,0 +1,66 @@
+#include "mbed.h"
+
+#define dir_UP 1
+#define dir_DOWN 2
+#define dir_LEFT 3
+#define dir_RIGHT 4
+#define dir_BUTTON 5
+#define color_WHITE 1
+#define color_RED 2
+#define color_GREEN 3
+#define color_BLUE 4
+
+//Setup a new class for a Trackball Module
+class Trackball
+{
+public:
+    Trackball(PinName pin_btn, PinName pin_lft, PinName pin_rht, PinName pin_up, PinName pin_dwn, PinName pin_wht, PinName pin_grn, PinName pin_red, PinName pin_blu);
+    unsigned int read(int dir);
+    void write(unsigned int val, int color);
+private:
+//class sets up the pins
+    DigitalIn _pin_btn;
+    DigitalIn _pin_lft;
+    DigitalIn _pin_rht;
+    DigitalIn _pin_up;
+    DigitalIn _pin_dwn;
+    DigitalOut _pin_wht;
+    DigitalOut _pin_grn;
+    DigitalOut _pin_red;
+    DigitalOut _pin_blu;
+};
+
+Trackball::Trackball(PinName pin_btn, PinName pin_lft, PinName pin_rht, PinName pin_up, PinName pin_dwn, PinName pin_wht, PinName pin_grn, PinName pin_red, PinName pin_blu)
+    : _pin_btn(pin_btn), _pin_lft(pin_lft), _pin_rht(pin_rht), _pin_up(pin_up), _pin_dwn(pin_dwn), _pin_wht(pin_wht), _pin_grn(pin_grn), _pin_red(pin_red), _pin_blu(pin_blu)
+{
+       //initialize all LED pins to off.
+
+    _pin_wht = 0;
+    _pin_grn = 0;
+    _pin_red = 0;
+    _pin_blu = 0;
+}
+
+void Trackball::write(unsigned int val, int color)
+
+    {
+        if (color == color_WHITE){_pin_wht = val;}
+        else if(color == color_BLUE){_pin_blu = val;}
+        else if(color == color_GREEN){_pin_grn = val;}
+        else if(color == color_RED){_pin_red = val;}
+        else {printf("Invalid LED Color");}
+    }
+unsigned int Trackball::read(int dir)
+
+    {
+        unsigned int val;
+        if(dir == dir_UP){val = _pin_up;}
+        else if(dir == dir_DOWN){val = _pin_dwn;}
+        else if(dir == dir_RIGHT){val = _pin_rht;}
+        else if(dir == dir_LEFT){val = _pin_lft;}
+        else if(dir == dir_BUTTON){val = _pin_btn;}
+        else {val = 1;}
+        
+        return val;
+        
+    }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 20 21:11:52 2015 +0000
@@ -0,0 +1,79 @@
+#include "mbed.h"
+#include "Trackball.h"
+
+//set up the trackball
+
+Trackball trackball(p12, p13, p14, p15, p16, p17, p18, p19, p20);
+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
+    
+    trackball.write(1, color_WHITE);
+    wait(2);
+    trackball.write(0, color_WHITE);
+    trackball.write(1, color_BLUE);
+    wait(2);
+    trackball.write(0, color_BLUE);
+    trackball.write(1, color_GREEN);
+    wait(2);
+    trackball.write(0, color_GREEN);
+    trackball.write(1, color_RED);
+    wait(2);
+    trackball.write(0, color_RED);
+
+int x_pos = 0;
+int y_pos = 0;
+  
+    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.
+ 
+        if(trackball.read(dir_UP) == 0){
+            y_pos++;
+            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);
+            }
+        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++;
+            trackball.write(1, color_RED);
+            trackball.write(0, color_WHITE);
+            trackball.write(0, color_GREEN);
+            trackball.write(0, color_BLUE);
+            }
+        else if(trackball.read(dir_BUTTON) == 0){
+            pc.printf("Button pressed.");
+            }
+        pc.printf("X pos: %d, Y pos: %d\n", x_pos, y_pos);  
+        wait(.2);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Oct 20 21:11:52 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/34e6b704fe68
\ No newline at end of file