Control library for the Sparkfun Entertainment Trackballer breakout board.

Committer:
Nakor
Date:
Sun Feb 20 18:05:08 2011 +0000
Revision:
15:a66aea99500e
Parent:
14:d566efcd787b

        

Who changed what in which revision?

UserRevisionLine numberNew 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 6:c0ebd0f8f873 7 trackballer::trackballer(PinName button, PinName right, PinName down, PinName left, PinName up, PinName red, PinName green, PinName blue, PinName white, char limits)
Nakor 0:2743c73d648d 8 {
Nakor 0:2743c73d648d 9
Nakor 6:c0ebd0f8f873 10 _limits = limits;
Nakor 6:c0ebd0f8f873 11
Nakor 0:2743c73d648d 12 _buttonPin = new DigitalIn(button);
Nakor 0:2743c73d648d 13 _rightPin = new DigitalIn(right);
Nakor 0:2743c73d648d 14 _downPin = new DigitalIn(down);
Nakor 0:2743c73d648d 15 _leftPin = new DigitalIn(left);
Nakor 0:2743c73d648d 16 _upPin = new DigitalIn(up);
Nakor 0:2743c73d648d 17
Nakor 0:2743c73d648d 18 _redLED = new PwmOut(red);
Nakor 0:2743c73d648d 19 _greenLED = new PwmOut(green);
Nakor 0:2743c73d648d 20 _blueLED = new PwmOut(blue);
Nakor 0:2743c73d648d 21 _whiteLED = new PwmOut(white);
Nakor 0:2743c73d648d 22
Nakor 0:2743c73d648d 23
Nakor 0:2743c73d648d 24
Nakor 0:2743c73d648d 25 _buttonPushCounter = 0; // counter for the number of button presses
Nakor 0:2743c73d648d 26
Nakor 11:35347e6474f9 27 _buttonState = 1; // current state of the button
Nakor 11:35347e6474f9 28 _lastButtonState = 1; // previous state of the button
Nakor 0:2743c73d648d 29
Nakor 0:2743c73d648d 30 _upState = 0;
Nakor 0:2743c73d648d 31 _downState = 0;
Nakor 0:2743c73d648d 32 _leftState = 0;
Nakor 0:2743c73d648d 33 _rightState = 0;
Nakor 0:2743c73d648d 34 _lastUpState = 0;
Nakor 0:2743c73d648d 35 _lastDownState = 0;
Nakor 0:2743c73d648d 36 _lastLeftState = 0;
Nakor 0:2743c73d648d 37 _lastRightState = 0;
Nakor 0:2743c73d648d 38
Nakor 7:229fc175e05e 39 _xPosition = 0.5;
Nakor 7:229fc175e05e 40 _yPosition = 0.5;
Nakor 0:2743c73d648d 41
Nakor 0:2743c73d648d 42
Nakor 0:2743c73d648d 43 _buttonPin->mode(PullUp);
Nakor 0:2743c73d648d 44
Nakor 0:2743c73d648d 45 _redLED->write(0.0);
Nakor 0:2743c73d648d 46 _greenLED->write(0.0);
Nakor 0:2743c73d648d 47 _blueLED->write(0.0);
Nakor 0:2743c73d648d 48 _whiteLED->write(0.0);
Nakor 0:2743c73d648d 49
Nakor 0:2743c73d648d 50 _direction = 0x00;
Nakor 0:2743c73d648d 51
Nakor 0:2743c73d648d 52 _outputTimer.start();
Nakor 0:2743c73d648d 53
Nakor 0:2743c73d648d 54 }
Nakor 0:2743c73d648d 55
Nakor 0:2743c73d648d 56
Nakor 0:2743c73d648d 57
Nakor 0:2743c73d648d 58 /******************/
Nakor 0:2743c73d648d 59 /* Public */
Nakor 0:2743c73d648d 60 /******************/
Nakor 0:2743c73d648d 61
Nakor 0:2743c73d648d 62
Nakor 5:0abcaea1fb07 63 void trackballer::getState(float &xPosition, float &yPosition, char &button)
Nakor 0:2743c73d648d 64 {
Nakor 15:a66aea99500e 65 char heldState = 0x00;
Nakor 15:a66aea99500e 66 char isHeld = 0x00;
Nakor 15:a66aea99500e 67 char exitWhile = 0x00;
Nakor 0:2743c73d648d 68
Nakor 0:2743c73d648d 69 _buttonState = _buttonPin->read();
Nakor 0:2743c73d648d 70
Nakor 14:d566efcd787b 71 //int counter1 = 0;
Nakor 14:d566efcd787b 72
Nakor 0:2743c73d648d 73 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 74 if (_buttonState != _lastButtonState)
Nakor 0:2743c73d648d 75 {
Nakor 14:d566efcd787b 76
Nakor 0:2743c73d648d 77 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 78 if (_buttonState == 1)
Nakor 0:2743c73d648d 79 {
Nakor 15:a66aea99500e 80
Nakor 15:a66aea99500e 81
Nakor 14:d566efcd787b 82 /*
Nakor 14:d566efcd787b 83 while(_buttonPin->read() == _buttonState)
Nakor 14:d566efcd787b 84 {
Nakor 14:d566efcd787b 85 //printf("HELD\n");
Nakor 14:d566efcd787b 86 counter1++;
Nakor 14:d566efcd787b 87 if(counter1 > 20)
Nakor 14:d566efcd787b 88 {
Nakor 14:d566efcd787b 89 printf("HELD\n");
Nakor 14:d566efcd787b 90 return;
Nakor 14:d566efcd787b 91 }
Nakor 14:d566efcd787b 92 }
Nakor 14:d566efcd787b 93 */
Nakor 0:2743c73d648d 94 // if the current state is 1 then the button
Nakor 0:2743c73d648d 95 // went from off to on:
Nakor 0:2743c73d648d 96 _buttonPushCounter++;
Nakor 10:9b35f63411ad 97 button = 0x01;
Nakor 10:9b35f63411ad 98 //printf("Number of button pushes: %i\n", _buttonPushCounter);
Nakor 0:2743c73d648d 99 }
Nakor 15:a66aea99500e 100 else if(_buttonState == 0)
Nakor 15:a66aea99500e 101 {
Nakor 15:a66aea99500e 102 _holdTimer.start();
Nakor 15:a66aea99500e 103 while(_holdTimer.read() < 2.0 && !exitWhile)
Nakor 15:a66aea99500e 104 {
Nakor 15:a66aea99500e 105 heldState = !_buttonPin->read();
Nakor 15:a66aea99500e 106
Nakor 15:a66aea99500e 107 if(heldState == 0)
Nakor 15:a66aea99500e 108 {
Nakor 15:a66aea99500e 109 isHeld = 0x00;
Nakor 15:a66aea99500e 110 exitWhile = 0x01;
Nakor 15:a66aea99500e 111 }
Nakor 15:a66aea99500e 112 else
Nakor 15:a66aea99500e 113 {
Nakor 15:a66aea99500e 114 if(_holdTimer.read() > 1.0)
Nakor 15:a66aea99500e 115 {
Nakor 15:a66aea99500e 116 isHeld = 0x01;
Nakor 15:a66aea99500e 117 exitWhile = 0x01;
Nakor 15:a66aea99500e 118 }
Nakor 15:a66aea99500e 119 }
Nakor 15:a66aea99500e 120
Nakor 15:a66aea99500e 121
Nakor 15:a66aea99500e 122 }
Nakor 15:a66aea99500e 123
Nakor 15:a66aea99500e 124 _holdTimer.stop();
Nakor 15:a66aea99500e 125 _holdTimer.reset();
Nakor 15:a66aea99500e 126
Nakor 15:a66aea99500e 127 if(isHeld)
Nakor 15:a66aea99500e 128 {
Nakor 15:a66aea99500e 129 printf("HELD\n");
Nakor 15:a66aea99500e 130 button = 0x03;
Nakor 15:a66aea99500e 131 return;
Nakor 15:a66aea99500e 132 }
Nakor 15:a66aea99500e 133 }
Nakor 0:2743c73d648d 134 }
Nakor 10:9b35f63411ad 135 else
Nakor 10:9b35f63411ad 136 {
Nakor 10:9b35f63411ad 137 button = 0x00;
Nakor 10:9b35f63411ad 138 }
Nakor 0:2743c73d648d 139 _lastButtonState = _buttonState;
Nakor 10:9b35f63411ad 140 //button = !_buttonState;
Nakor 0:2743c73d648d 141
Nakor 0:2743c73d648d 142
Nakor 0:2743c73d648d 143 if (_buttonPushCounter % 2 == 0)
Nakor 0:2743c73d648d 144 {
Nakor 0:2743c73d648d 145 _redLED->write(1);
Nakor 8:2602108edf40 146 //button = 0x01;
Nakor 0:2743c73d648d 147 }
Nakor 0:2743c73d648d 148 else
Nakor 0:2743c73d648d 149 {
Nakor 0:2743c73d648d 150 _redLED->write(0);
Nakor 8:2602108edf40 151 //button = 0x00;
Nakor 0:2743c73d648d 152 }
Nakor 0:2743c73d648d 153
Nakor 0:2743c73d648d 154 _upState = _upPin->read();
Nakor 0:2743c73d648d 155
Nakor 0:2743c73d648d 156 if (_upState != _lastUpState)
Nakor 0:2743c73d648d 157 {
Nakor 0:2743c73d648d 158 if (_upState == 1)
Nakor 0:2743c73d648d 159 {
Nakor 0:2743c73d648d 160 // if the current state is 1 then the button
Nakor 0:2743c73d648d 161 // went from off to on:
Nakor 0:2743c73d648d 162 _yPosition-=TRACK_INC;
Nakor 6:c0ebd0f8f873 163 if(_limits)
Nakor 0:2743c73d648d 164 {
Nakor 6:c0ebd0f8f873 165 if (_yPosition < 0.0)
Nakor 6:c0ebd0f8f873 166 {
Nakor 6:c0ebd0f8f873 167 _yPosition = 0.0;
Nakor 6:c0ebd0f8f873 168 }
Nakor 0:2743c73d648d 169 }
Nakor 0:2743c73d648d 170 }
Nakor 0:2743c73d648d 171 }
Nakor 0:2743c73d648d 172 _lastUpState = _upState;
Nakor 0:2743c73d648d 173
Nakor 0:2743c73d648d 174
Nakor 0:2743c73d648d 175 _downState = _downPin->read();
Nakor 0:2743c73d648d 176
Nakor 0:2743c73d648d 177 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 178 if (_downState != _lastDownState)
Nakor 0:2743c73d648d 179 {
Nakor 0:2743c73d648d 180 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 181 if (_downState == 1)
Nakor 0:2743c73d648d 182 {
Nakor 0:2743c73d648d 183 // if the current state is 1 then the button
Nakor 0:2743c73d648d 184 // went from off to on:
Nakor 0:2743c73d648d 185 _yPosition+=TRACK_INC;
Nakor 6:c0ebd0f8f873 186 if(_limits)
Nakor 0:2743c73d648d 187 {
Nakor 6:c0ebd0f8f873 188 if (_yPosition > 1.0)
Nakor 6:c0ebd0f8f873 189 {
Nakor 6:c0ebd0f8f873 190 _yPosition = 1.0;
Nakor 6:c0ebd0f8f873 191 }
Nakor 0:2743c73d648d 192 }
Nakor 0:2743c73d648d 193 }
Nakor 0:2743c73d648d 194 }
Nakor 0:2743c73d648d 195 _lastDownState = _downState;
Nakor 0:2743c73d648d 196
Nakor 0:2743c73d648d 197
Nakor 0:2743c73d648d 198 _leftState = _leftPin->read();
Nakor 0:2743c73d648d 199
Nakor 0:2743c73d648d 200 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 201 if (_leftState != _lastLeftState)
Nakor 0:2743c73d648d 202 {
Nakor 0:2743c73d648d 203 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 204 if (_leftState == 1)
Nakor 0:2743c73d648d 205 {
Nakor 0:2743c73d648d 206 // if the current state is 1 then the button
Nakor 0:2743c73d648d 207 // went from off to on:
Nakor 0:2743c73d648d 208 _xPosition-=TRACK_INC;
Nakor 6:c0ebd0f8f873 209 if(_limits)
Nakor 0:2743c73d648d 210 {
Nakor 6:c0ebd0f8f873 211 if (_xPosition < 0.0)
Nakor 6:c0ebd0f8f873 212 {
Nakor 6:c0ebd0f8f873 213 _xPosition = 0.0;
Nakor 6:c0ebd0f8f873 214 }
Nakor 0:2743c73d648d 215 }
Nakor 0:2743c73d648d 216 }
Nakor 0:2743c73d648d 217 }
Nakor 0:2743c73d648d 218 _lastLeftState = _leftState;
Nakor 0:2743c73d648d 219
Nakor 0:2743c73d648d 220 _rightState = _rightPin->read();
Nakor 0:2743c73d648d 221
Nakor 0:2743c73d648d 222 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 223 if (_rightState != _lastRightState)
Nakor 0:2743c73d648d 224 {
Nakor 0:2743c73d648d 225 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 226 if (_rightState == 1)
Nakor 0:2743c73d648d 227 {
Nakor 0:2743c73d648d 228 // if the current state is 1 then the button
Nakor 0:2743c73d648d 229 // went from off to on:
Nakor 0:2743c73d648d 230 _xPosition+=TRACK_INC;
Nakor 6:c0ebd0f8f873 231 if(_limits)
Nakor 0:2743c73d648d 232 {
Nakor 6:c0ebd0f8f873 233 if (_xPosition > 1.0)
Nakor 6:c0ebd0f8f873 234 {
Nakor 6:c0ebd0f8f873 235 _xPosition = 1.0;
Nakor 6:c0ebd0f8f873 236 }
Nakor 0:2743c73d648d 237 }
Nakor 0:2743c73d648d 238 }
Nakor 0:2743c73d648d 239 }
Nakor 0:2743c73d648d 240 _lastRightState = _rightState;
Nakor 0:2743c73d648d 241
Nakor 0:2743c73d648d 242 _blueLED->write(_xPosition);
Nakor 0:2743c73d648d 243 _greenLED->write(_yPosition);
Nakor 0:2743c73d648d 244
Nakor 0:2743c73d648d 245 xPosition = _xPosition;
Nakor 0:2743c73d648d 246 yPosition = _yPosition;
Nakor 0:2743c73d648d 247
Nakor 0:2743c73d648d 248
Nakor 6:c0ebd0f8f873 249
Nakor 6:c0ebd0f8f873 250 #if DEBUG_POSITION
Nakor 4:3d1791d6608a 251 int ixPosition = _xPosition * 95;
Nakor 4:3d1791d6608a 252 int iyPosition = _yPosition * 63;
Nakor 4:3d1791d6608a 253
Nakor 6:c0ebd0f8f873 254
Nakor 6:c0ebd0f8f873 255
Nakor 0:2743c73d648d 256 if (_outputTimer.read_ms() > 1000)
Nakor 0:2743c73d648d 257 {
Nakor 4:3d1791d6608a 258 printf("Position: (x%i, y%i)\n", ixPosition, iyPosition);
Nakor 0:2743c73d648d 259
Nakor 0:2743c73d648d 260 _outputTimer.reset();
Nakor 0:2743c73d648d 261
Nakor 0:2743c73d648d 262 }
Nakor 6:c0ebd0f8f873 263 #endif
Nakor 0:2743c73d648d 264
Nakor 7:229fc175e05e 265 }
Nakor 7:229fc175e05e 266
Nakor 7:229fc175e05e 267 void trackballer::reset()
Nakor 7:229fc175e05e 268 {
Nakor 7:229fc175e05e 269 _xPosition = 0.5;
Nakor 7:229fc175e05e 270 _yPosition = 0.5;
Nakor 0:2743c73d648d 271 }