Control library for the Sparkfun Entertainment Trackballer breakout board.
trackballer.cpp@0:2743c73d648d, 2011-02-19 (annotated)
- Committer:
- Nakor
- Date:
- Sat Feb 19 19:14:01 2011 +0000
- Revision:
- 0:2743c73d648d
- Child:
- 2:3c680dd598b7
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nakor | 0:2743c73d648d | 1 | #include "mbed.h" |
Nakor | 0:2743c73d648d | 2 | #include "trackballer.h" |
Nakor | 0:2743c73d648d | 3 | |
Nakor | 0:2743c73d648d | 4 | |
Nakor | 0:2743c73d648d | 5 | |
Nakor | 0:2743c73d648d | 6 | |
Nakor | 0:2743c73d648d | 7 | trackballer::trackballer(PinName button, PinName right, PinName down, PinName left, PinName up, PinName red, PinName green, PinName blue, PinName white) |
Nakor | 0:2743c73d648d | 8 | { |
Nakor | 0:2743c73d648d | 9 | |
Nakor | 0:2743c73d648d | 10 | _buttonPin = new DigitalIn(button); |
Nakor | 0:2743c73d648d | 11 | _rightPin = new DigitalIn(right); |
Nakor | 0:2743c73d648d | 12 | _downPin = new DigitalIn(down); |
Nakor | 0:2743c73d648d | 13 | _leftPin = new DigitalIn(left); |
Nakor | 0:2743c73d648d | 14 | _upPin = new DigitalIn(up); |
Nakor | 0:2743c73d648d | 15 | |
Nakor | 0:2743c73d648d | 16 | _redLED = new PwmOut(red); |
Nakor | 0:2743c73d648d | 17 | _greenLED = new PwmOut(green); |
Nakor | 0:2743c73d648d | 18 | _blueLED = new PwmOut(blue); |
Nakor | 0:2743c73d648d | 19 | _whiteLED = new PwmOut(white); |
Nakor | 0:2743c73d648d | 20 | |
Nakor | 0:2743c73d648d | 21 | |
Nakor | 0:2743c73d648d | 22 | |
Nakor | 0:2743c73d648d | 23 | _buttonPushCounter = 0; // counter for the number of button presses |
Nakor | 0:2743c73d648d | 24 | |
Nakor | 0:2743c73d648d | 25 | _buttonState = 0; // current state of the button |
Nakor | 0:2743c73d648d | 26 | _lastButtonState = 0; // previous state of the button |
Nakor | 0:2743c73d648d | 27 | |
Nakor | 0:2743c73d648d | 28 | _upState = 0; |
Nakor | 0:2743c73d648d | 29 | _downState = 0; |
Nakor | 0:2743c73d648d | 30 | _leftState = 0; |
Nakor | 0:2743c73d648d | 31 | _rightState = 0; |
Nakor | 0:2743c73d648d | 32 | _lastUpState = 0; |
Nakor | 0:2743c73d648d | 33 | _lastDownState = 0; |
Nakor | 0:2743c73d648d | 34 | _lastLeftState = 0; |
Nakor | 0:2743c73d648d | 35 | _lastRightState = 0; |
Nakor | 0:2743c73d648d | 36 | |
Nakor | 0:2743c73d648d | 37 | _xPosition = 0.0; |
Nakor | 0:2743c73d648d | 38 | _yPosition = 0.0; |
Nakor | 0:2743c73d648d | 39 | |
Nakor | 0:2743c73d648d | 40 | |
Nakor | 0:2743c73d648d | 41 | _buttonPin->mode(PullUp); |
Nakor | 0:2743c73d648d | 42 | |
Nakor | 0:2743c73d648d | 43 | _redLED->write(0.0); |
Nakor | 0:2743c73d648d | 44 | _greenLED->write(0.0); |
Nakor | 0:2743c73d648d | 45 | _blueLED->write(0.0); |
Nakor | 0:2743c73d648d | 46 | _whiteLED->write(0.0); |
Nakor | 0:2743c73d648d | 47 | |
Nakor | 0:2743c73d648d | 48 | _direction = 0x00; |
Nakor | 0:2743c73d648d | 49 | |
Nakor | 0:2743c73d648d | 50 | _outputTimer.start(); |
Nakor | 0:2743c73d648d | 51 | |
Nakor | 0:2743c73d648d | 52 | } |
Nakor | 0:2743c73d648d | 53 | |
Nakor | 0:2743c73d648d | 54 | |
Nakor | 0:2743c73d648d | 55 | |
Nakor | 0:2743c73d648d | 56 | /******************/ |
Nakor | 0:2743c73d648d | 57 | /* Public */ |
Nakor | 0:2743c73d648d | 58 | /******************/ |
Nakor | 0:2743c73d648d | 59 | |
Nakor | 0:2743c73d648d | 60 | |
Nakor | 0:2743c73d648d | 61 | void trackballer::getDirection(float &xPosition, float &yPosition) |
Nakor | 0:2743c73d648d | 62 | { |
Nakor | 0:2743c73d648d | 63 | |
Nakor | 0:2743c73d648d | 64 | _buttonState = _buttonPin->read(); |
Nakor | 0:2743c73d648d | 65 | |
Nakor | 0:2743c73d648d | 66 | // compare the _buttonState to its previous state |
Nakor | 0:2743c73d648d | 67 | if (_buttonState != _lastButtonState) |
Nakor | 0:2743c73d648d | 68 | { |
Nakor | 0:2743c73d648d | 69 | // if the state has changed, increment the counter |
Nakor | 0:2743c73d648d | 70 | if (_buttonState == 1) |
Nakor | 0:2743c73d648d | 71 | { |
Nakor | 0:2743c73d648d | 72 | // if the current state is 1 then the button |
Nakor | 0:2743c73d648d | 73 | // went from off to on: |
Nakor | 0:2743c73d648d | 74 | _buttonPushCounter++; |
Nakor | 0:2743c73d648d | 75 | printf("Number of button pushes: %i\n", _buttonPushCounter); |
Nakor | 0:2743c73d648d | 76 | } |
Nakor | 0:2743c73d648d | 77 | } |
Nakor | 0:2743c73d648d | 78 | _lastButtonState = _buttonState; |
Nakor | 0:2743c73d648d | 79 | |
Nakor | 0:2743c73d648d | 80 | |
Nakor | 0:2743c73d648d | 81 | if (_buttonPushCounter % 2 == 0) |
Nakor | 0:2743c73d648d | 82 | { |
Nakor | 0:2743c73d648d | 83 | _redLED->write(1); |
Nakor | 0:2743c73d648d | 84 | } |
Nakor | 0:2743c73d648d | 85 | else |
Nakor | 0:2743c73d648d | 86 | { |
Nakor | 0:2743c73d648d | 87 | _redLED->write(0); |
Nakor | 0:2743c73d648d | 88 | } |
Nakor | 0:2743c73d648d | 89 | |
Nakor | 0:2743c73d648d | 90 | _upState = _upPin->read(); |
Nakor | 0:2743c73d648d | 91 | |
Nakor | 0:2743c73d648d | 92 | if (_upState != _lastUpState) |
Nakor | 0:2743c73d648d | 93 | { |
Nakor | 0:2743c73d648d | 94 | if (_upState == 1) |
Nakor | 0:2743c73d648d | 95 | { |
Nakor | 0:2743c73d648d | 96 | // if the current state is 1 then the button |
Nakor | 0:2743c73d648d | 97 | // went from off to on: |
Nakor | 0:2743c73d648d | 98 | _yPosition-=TRACK_INC; |
Nakor | 0:2743c73d648d | 99 | if (_yPosition < 0.0) |
Nakor | 0:2743c73d648d | 100 | { |
Nakor | 0:2743c73d648d | 101 | _yPosition = 0.0; |
Nakor | 0:2743c73d648d | 102 | } |
Nakor | 0:2743c73d648d | 103 | } |
Nakor | 0:2743c73d648d | 104 | } |
Nakor | 0:2743c73d648d | 105 | _lastUpState = _upState; |
Nakor | 0:2743c73d648d | 106 | |
Nakor | 0:2743c73d648d | 107 | |
Nakor | 0:2743c73d648d | 108 | _downState = _downPin->read(); |
Nakor | 0:2743c73d648d | 109 | |
Nakor | 0:2743c73d648d | 110 | // compare the _buttonState to its previous state |
Nakor | 0:2743c73d648d | 111 | if (_downState != _lastDownState) |
Nakor | 0:2743c73d648d | 112 | { |
Nakor | 0:2743c73d648d | 113 | // if the state has changed, increment the counter |
Nakor | 0:2743c73d648d | 114 | if (_downState == 1) |
Nakor | 0:2743c73d648d | 115 | { |
Nakor | 0:2743c73d648d | 116 | // if the current state is 1 then the button |
Nakor | 0:2743c73d648d | 117 | // went from off to on: |
Nakor | 0:2743c73d648d | 118 | _yPosition+=TRACK_INC; |
Nakor | 0:2743c73d648d | 119 | if (_yPosition > 1.0) |
Nakor | 0:2743c73d648d | 120 | { |
Nakor | 0:2743c73d648d | 121 | _yPosition = 1.0; |
Nakor | 0:2743c73d648d | 122 | } |
Nakor | 0:2743c73d648d | 123 | } |
Nakor | 0:2743c73d648d | 124 | } |
Nakor | 0:2743c73d648d | 125 | _lastDownState = _downState; |
Nakor | 0:2743c73d648d | 126 | |
Nakor | 0:2743c73d648d | 127 | |
Nakor | 0:2743c73d648d | 128 | _leftState = _leftPin->read(); |
Nakor | 0:2743c73d648d | 129 | |
Nakor | 0:2743c73d648d | 130 | // compare the _buttonState to its previous state |
Nakor | 0:2743c73d648d | 131 | if (_leftState != _lastLeftState) |
Nakor | 0:2743c73d648d | 132 | { |
Nakor | 0:2743c73d648d | 133 | // if the state has changed, increment the counter |
Nakor | 0:2743c73d648d | 134 | if (_leftState == 1) |
Nakor | 0:2743c73d648d | 135 | { |
Nakor | 0:2743c73d648d | 136 | // if the current state is 1 then the button |
Nakor | 0:2743c73d648d | 137 | // went from off to on: |
Nakor | 0:2743c73d648d | 138 | _xPosition-=TRACK_INC; |
Nakor | 0:2743c73d648d | 139 | if (_xPosition < 0.0) |
Nakor | 0:2743c73d648d | 140 | { |
Nakor | 0:2743c73d648d | 141 | _xPosition = 0.0; |
Nakor | 0:2743c73d648d | 142 | } |
Nakor | 0:2743c73d648d | 143 | } |
Nakor | 0:2743c73d648d | 144 | } |
Nakor | 0:2743c73d648d | 145 | _lastLeftState = _leftState; |
Nakor | 0:2743c73d648d | 146 | |
Nakor | 0:2743c73d648d | 147 | _rightState = _rightPin->read(); |
Nakor | 0:2743c73d648d | 148 | |
Nakor | 0:2743c73d648d | 149 | // compare the _buttonState to its previous state |
Nakor | 0:2743c73d648d | 150 | if (_rightState != _lastRightState) |
Nakor | 0:2743c73d648d | 151 | { |
Nakor | 0:2743c73d648d | 152 | // if the state has changed, increment the counter |
Nakor | 0:2743c73d648d | 153 | if (_rightState == 1) |
Nakor | 0:2743c73d648d | 154 | { |
Nakor | 0:2743c73d648d | 155 | // if the current state is 1 then the button |
Nakor | 0:2743c73d648d | 156 | // went from off to on: |
Nakor | 0:2743c73d648d | 157 | _xPosition+=TRACK_INC; |
Nakor | 0:2743c73d648d | 158 | if (_xPosition > 1.0) |
Nakor | 0:2743c73d648d | 159 | { |
Nakor | 0:2743c73d648d | 160 | _xPosition = 1.0; |
Nakor | 0:2743c73d648d | 161 | } |
Nakor | 0:2743c73d648d | 162 | } |
Nakor | 0:2743c73d648d | 163 | } |
Nakor | 0:2743c73d648d | 164 | _lastRightState = _rightState; |
Nakor | 0:2743c73d648d | 165 | |
Nakor | 0:2743c73d648d | 166 | _blueLED->write(_xPosition); |
Nakor | 0:2743c73d648d | 167 | _greenLED->write(_yPosition); |
Nakor | 0:2743c73d648d | 168 | |
Nakor | 0:2743c73d648d | 169 | xPosition = _xPosition; |
Nakor | 0:2743c73d648d | 170 | yPosition = _yPosition; |
Nakor | 0:2743c73d648d | 171 | |
Nakor | 0:2743c73d648d | 172 | |
Nakor | 0:2743c73d648d | 173 | |
Nakor | 0:2743c73d648d | 174 | if (_outputTimer.read_ms() > 1000) |
Nakor | 0:2743c73d648d | 175 | { |
Nakor | 0:2743c73d648d | 176 | printf("Position: (x%f, y%f)\n", _xPosition, _yPosition); |
Nakor | 0:2743c73d648d | 177 | |
Nakor | 0:2743c73d648d | 178 | _outputTimer.reset(); |
Nakor | 0:2743c73d648d | 179 | |
Nakor | 0:2743c73d648d | 180 | } |
Nakor | 0:2743c73d648d | 181 | |
Nakor | 0:2743c73d648d | 182 | } |