Control library for the Sparkfun Entertainment Trackballer breakout board.
Revision 0:2743c73d648d, committed 2011-02-19
- Comitter:
- Nakor
- Date:
- Sat Feb 19 19:14:01 2011 +0000
- Child:
- 1:0129b1984b5a
- Commit message:
- First commit.
Changed in this revision
trackballer.cpp | Show annotated file Show diff for this revision Revisions of this file |
trackballer.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trackballer.cpp Sat Feb 19 19:14:01 2011 +0000 @@ -0,0 +1,182 @@ +#include "mbed.h" +#include "trackballer.h" + + + + +trackballer::trackballer(PinName button, PinName right, PinName down, PinName left, PinName up, PinName red, PinName green, PinName blue, PinName white) +{ + + _buttonPin = new DigitalIn(button); + _rightPin = new DigitalIn(right); + _downPin = new DigitalIn(down); + _leftPin = new DigitalIn(left); + _upPin = new DigitalIn(up); + + _redLED = new PwmOut(red); + _greenLED = new PwmOut(green); + _blueLED = new PwmOut(blue); + _whiteLED = new PwmOut(white); + + + + _buttonPushCounter = 0; // counter for the number of button presses + + _buttonState = 0; // current state of the button + _lastButtonState = 0; // previous state of the button + + _upState = 0; + _downState = 0; + _leftState = 0; + _rightState = 0; + _lastUpState = 0; + _lastDownState = 0; + _lastLeftState = 0; + _lastRightState = 0; + + _xPosition = 0.0; + _yPosition = 0.0; + + + _buttonPin->mode(PullUp); + + _redLED->write(0.0); + _greenLED->write(0.0); + _blueLED->write(0.0); + _whiteLED->write(0.0); + + _direction = 0x00; + + _outputTimer.start(); + +} + + + +/******************/ +/* Public */ +/******************/ + + +void trackballer::getDirection(float &xPosition, float &yPosition) +{ + + _buttonState = _buttonPin->read(); + + // compare the _buttonState to its previous state + if (_buttonState != _lastButtonState) + { + // if the state has changed, increment the counter + if (_buttonState == 1) + { + // if the current state is 1 then the button + // went from off to on: + _buttonPushCounter++; + printf("Number of button pushes: %i\n", _buttonPushCounter); + } + } + _lastButtonState = _buttonState; + + + if (_buttonPushCounter % 2 == 0) + { + _redLED->write(1); + } + else + { + _redLED->write(0); + } + + _upState = _upPin->read(); + + if (_upState != _lastUpState) + { + if (_upState == 1) + { + // if the current state is 1 then the button + // went from off to on: + _yPosition-=TRACK_INC; + if (_yPosition < 0.0) + { + _yPosition = 0.0; + } + } + } + _lastUpState = _upState; + + + _downState = _downPin->read(); + + // compare the _buttonState to its previous state + if (_downState != _lastDownState) + { + // if the state has changed, increment the counter + if (_downState == 1) + { + // if the current state is 1 then the button + // went from off to on: + _yPosition+=TRACK_INC; + if (_yPosition > 1.0) + { + _yPosition = 1.0; + } + } + } + _lastDownState = _downState; + + + _leftState = _leftPin->read(); + + // compare the _buttonState to its previous state + if (_leftState != _lastLeftState) + { + // if the state has changed, increment the counter + if (_leftState == 1) + { + // if the current state is 1 then the button + // went from off to on: + _xPosition-=TRACK_INC; + if (_xPosition < 0.0) + { + _xPosition = 0.0; + } + } + } + _lastLeftState = _leftState; + + _rightState = _rightPin->read(); + + // compare the _buttonState to its previous state + if (_rightState != _lastRightState) + { + // if the state has changed, increment the counter + if (_rightState == 1) + { + // if the current state is 1 then the button + // went from off to on: + _xPosition+=TRACK_INC; + if (_xPosition > 1.0) + { + _xPosition = 1.0; + } + } + } + _lastRightState = _rightState; + + _blueLED->write(_xPosition); + _greenLED->write(_yPosition); + + xPosition = _xPosition; + yPosition = _yPosition; + + + + if (_outputTimer.read_ms() > 1000) + { + printf("Position: (x%f, y%f)\n", _xPosition, _yPosition); + + _outputTimer.reset(); + + } + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/trackballer.h Sat Feb 19 19:14:01 2011 +0000 @@ -0,0 +1,110 @@ +/* This library is for Sparkfun Entertainment's Trackballer breakout board. + * The board consists (mainly) of the trackball, 4 hall effect sensors, some magnets (obviously), + * a button, and 4 leds (white, red, green, blue). + * + * This library (trackballer) by Aaron Goselin. + * 2010 Aaron Goselin. + * + * You are free to use this as you like, but I would prefer credits stay in tact. + * + */ + +#ifndef _TRACKBALLER_ +#define _TRACKBALLER_ + +#include "mbed.h" +//#include "PinDetect_m.h" + +#define TRACK_INC 0.02 + + +/** The 7fold game is a simple number game. 7 slots (horizontally) are filled one + * at a time with numbers 1 through 7. If the number of digits vertically or horizontally + * add up to the value of a digit in that row (that are touching) then those digits are removed. + * + * This uses the serially controlled uOLEDs by 4D Systems. + * + * This is a private library written by Aaron Goselin. While this may change in the future, + * in the case of accidental posting to the internet in general, use of this library by an + * individual or organization other than Aaron Goselin is prohibited. + * + * Example: + * @code + * // Draw text on the screen. + * // Accessing from main() + * #include "mbed.h" + * #include "uOLED.h" + * #include "uOLED_watch.h" + * #include "games/7fold/7fold.h" + * + * uOLED SGC(p9, p10, p11); + * uOLED_watch watch(&SGC); + * 7fold 7fold(); + * + * int main() + * { + * // Draws 7fold main menu. + * monkey.draw7foldMainMenu(); + * } + * @endcode + */ +class trackballer { + +public: + + + + /** Create an instance of the 7fold class. + * + * Accepts a pointer to the 7fold class. + * + * Example: + * @code + * uOLED_menus menus(&SGC, &watch); + * @endcode + */ + trackballer(PinName button, PinName right, PinName down, PinName left, PinName up, PinName red, PinName green, PinName blue, PinName white); + + void getDirection(float &xPosition, float &yPosition); + + + + +protected: + Timer _outputTimer; + + DigitalIn * _buttonPin; + DigitalIn * _rightPin; + DigitalIn * _downPin; + DigitalIn * _leftPin; + DigitalIn * _upPin; + + PwmOut * _redLED; + PwmOut * _greenLED; + PwmOut * _blueLED; + PwmOut * _whiteLED; + + int _buttonPushCounter; // counter for the number of button presses + + char _buttonState; // current state of the button + char _lastButtonState; // previous state of the button + + char _upState; + char _downState; + char _leftState; + char _rightState; + char _lastUpState; + char _lastDownState; + char _lastLeftState; + char _lastRightState; + + float _xPosition; + float _yPosition; + + + char _direction; + + +}; + +#endif \ No newline at end of file