Control library for the Sparkfun Entertainment Trackballer breakout board.
trackballer.cpp
- Committer:
- Nakor
- Date:
- 2011-02-19
- Revision:
- 0:2743c73d648d
- Child:
- 2:3c680dd598b7
File content as of revision 0:2743c73d648d:
#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(); } }