Control library for the Sparkfun Entertainment Trackballer breakout board.
Diff: trackballer.cpp
- Revision:
- 0:2743c73d648d
- Child:
- 2:3c680dd598b7
--- /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