Blackberry Trackerball Breakout
A trackball breakout
Hello World
Import programBlackberryTrackerballBreakout_HelloWorld
Demo code for LED/trackball functionality of the Blackberry Trackerball Breakout board
Library
Import libraryTrackball
Library for functionality of Blackberry Trackerball Breakout
Pinout
Datasheet
https://cdn.sparkfun.com/datasheets/Components/Switches/AN4884.pdfNotes
Trackball
The BlackBerry Trackballer Breakout can function as four pushbuttons in one, tracking movement in up, down, left, and right directions as well as providing a clickable button similar to a regular pushbutton. It is useful for situations where intuitive movement is needed, such as gaming. In this Wiki, breakout board setup, schematics, header file for the Trackball class, and demo 'Hello World' type code is provided.
Setup & Pinout
Here is the datasheet.
Official SparkFun Schematic
The Trackball breakout uses 9 digital pins (5 input, 4 output) for its functionality. It also requires the 5V output from the mbed (Vu) and ground.
Breakout | Mbed |
---|---|
VCC | Vu (5v) |
GND | GND |
BTN | p12 |
LFT | p13 |
RHT | p14 |
UP | p15 |
DWN | p16 |
WHT | p17 |
GRN | p18 |
RED | p19 |
BLU | p20 |
Information
Any Digital In/Out pins may be used for all pins except VCC/GND!
Diagram showing pin connections to mbed
Information
The trackball is held in place using an upper mounting PCB, which is held in place with four screws and four white standoffs. If the white standoffs are left in place, the pins cannot be placed into the breadboard. Jumper wires may be used to connect the pins to the breadboard, or the white standoffs may be removed and another method of holding the mount in place can be used. Nuts in place of the white standoffs provide the same functionality, while the header pins are still able to be plugged into the breadboard.
Sample Code
Trackball Class
This class sets up the various DigitalIn and DigitalOut pins used with the device.
It also provides a 'read' function for reading the values of the DigitalIn pins (movement directions), and a 'write' function for writing to the DigitalOut pins (the LEDs).
Trackball.h
#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;} return val; }
Hello World Demo
This program lights up each color LED individually for 2 seconds, and then begins to read input from the pins to see what directions the ball is moving. Depending on the direction the ball is being moved, a different color LED is lit up to demonstrate the movement. When the ball is pressed as like a button, the all 4 LEDs turn on at the same time. The program also keeps track of the x and y positioning of the ball and prints it to the USB serial port. The positioning information could be useful for creating a simple game on an LCD screen where the player moves around in 4 directions.
main.cpp
#include "mbed.h" #include "Trackball.h" //set up the trackball Trackball trackball(p12, p13, p14, p15, p16, p17, p18, p19, p20); //for prints to serial port Serial pc(USBTX, USBRX); int main() { //turn on each LED individually for 2 seconds each 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); //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) { //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((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); x--; } 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++; } 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: %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); }
Blue, Red, Green, and White LEDs lighting up the trackerball
Below is a video of the Hello World Demo code being demonstrated.
Download Links
The whole project (including Trackball library) may be downloaded using the link below:
Import programBlackberryTrackerballBreakout_HelloWorld
Demo code for LED/trackball functionality of the Blackberry Trackerball Breakout board
Just the library code may be downloaded using the link below
Import libraryTrackball
Library for functionality of Blackberry Trackerball Breakout
Project Ideas
- Gaming
- Use trackball to move player in 4 directions around screen
- Scrolling
- Any project that involves scrolling through a list could use a trackball to scroll more quickly and easily than using up/down pushbuttons.
You need to log in to post a discussion