Control library for the Sparkfun Entertainment Trackballer breakout board.

Committer:
Nakor
Date:
Sun Feb 20 16:17:07 2011 +0000
Revision:
9:4f8eafec5a7b
Parent:
8:2602108edf40
Child:
10:9b35f63411ad

        

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 0:2743c73d648d 27 _buttonState = 0; // current state of the button
Nakor 0:2743c73d648d 28 _lastButtonState = 0; // 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 0:2743c73d648d 65
Nakor 0:2743c73d648d 66 _buttonState = _buttonPin->read();
Nakor 0:2743c73d648d 67
Nakor 0:2743c73d648d 68 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 69 if (_buttonState != _lastButtonState)
Nakor 0:2743c73d648d 70 {
Nakor 0:2743c73d648d 71 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 72 if (_buttonState == 1)
Nakor 0:2743c73d648d 73 {
Nakor 0:2743c73d648d 74 // if the current state is 1 then the button
Nakor 0:2743c73d648d 75 // went from off to on:
Nakor 0:2743c73d648d 76 _buttonPushCounter++;
Nakor 0:2743c73d648d 77 printf("Number of button pushes: %i\n", _buttonPushCounter);
Nakor 0:2743c73d648d 78 }
Nakor 0:2743c73d648d 79 }
Nakor 0:2743c73d648d 80 _lastButtonState = _buttonState;
Nakor 9:4f8eafec5a7b 81 button = !_buttonState;
Nakor 0:2743c73d648d 82
Nakor 0:2743c73d648d 83
Nakor 0:2743c73d648d 84 if (_buttonPushCounter % 2 == 0)
Nakor 0:2743c73d648d 85 {
Nakor 0:2743c73d648d 86 _redLED->write(1);
Nakor 8:2602108edf40 87 //button = 0x01;
Nakor 0:2743c73d648d 88 }
Nakor 0:2743c73d648d 89 else
Nakor 0:2743c73d648d 90 {
Nakor 0:2743c73d648d 91 _redLED->write(0);
Nakor 8:2602108edf40 92 //button = 0x00;
Nakor 0:2743c73d648d 93 }
Nakor 0:2743c73d648d 94
Nakor 0:2743c73d648d 95 _upState = _upPin->read();
Nakor 0:2743c73d648d 96
Nakor 0:2743c73d648d 97 if (_upState != _lastUpState)
Nakor 0:2743c73d648d 98 {
Nakor 0:2743c73d648d 99 if (_upState == 1)
Nakor 0:2743c73d648d 100 {
Nakor 0:2743c73d648d 101 // if the current state is 1 then the button
Nakor 0:2743c73d648d 102 // went from off to on:
Nakor 0:2743c73d648d 103 _yPosition-=TRACK_INC;
Nakor 6:c0ebd0f8f873 104 if(_limits)
Nakor 0:2743c73d648d 105 {
Nakor 6:c0ebd0f8f873 106 if (_yPosition < 0.0)
Nakor 6:c0ebd0f8f873 107 {
Nakor 6:c0ebd0f8f873 108 _yPosition = 0.0;
Nakor 6:c0ebd0f8f873 109 }
Nakor 0:2743c73d648d 110 }
Nakor 0:2743c73d648d 111 }
Nakor 0:2743c73d648d 112 }
Nakor 0:2743c73d648d 113 _lastUpState = _upState;
Nakor 0:2743c73d648d 114
Nakor 0:2743c73d648d 115
Nakor 0:2743c73d648d 116 _downState = _downPin->read();
Nakor 0:2743c73d648d 117
Nakor 0:2743c73d648d 118 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 119 if (_downState != _lastDownState)
Nakor 0:2743c73d648d 120 {
Nakor 0:2743c73d648d 121 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 122 if (_downState == 1)
Nakor 0:2743c73d648d 123 {
Nakor 0:2743c73d648d 124 // if the current state is 1 then the button
Nakor 0:2743c73d648d 125 // went from off to on:
Nakor 0:2743c73d648d 126 _yPosition+=TRACK_INC;
Nakor 6:c0ebd0f8f873 127 if(_limits)
Nakor 0:2743c73d648d 128 {
Nakor 6:c0ebd0f8f873 129 if (_yPosition > 1.0)
Nakor 6:c0ebd0f8f873 130 {
Nakor 6:c0ebd0f8f873 131 _yPosition = 1.0;
Nakor 6:c0ebd0f8f873 132 }
Nakor 0:2743c73d648d 133 }
Nakor 0:2743c73d648d 134 }
Nakor 0:2743c73d648d 135 }
Nakor 0:2743c73d648d 136 _lastDownState = _downState;
Nakor 0:2743c73d648d 137
Nakor 0:2743c73d648d 138
Nakor 0:2743c73d648d 139 _leftState = _leftPin->read();
Nakor 0:2743c73d648d 140
Nakor 0:2743c73d648d 141 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 142 if (_leftState != _lastLeftState)
Nakor 0:2743c73d648d 143 {
Nakor 0:2743c73d648d 144 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 145 if (_leftState == 1)
Nakor 0:2743c73d648d 146 {
Nakor 0:2743c73d648d 147 // if the current state is 1 then the button
Nakor 0:2743c73d648d 148 // went from off to on:
Nakor 0:2743c73d648d 149 _xPosition-=TRACK_INC;
Nakor 6:c0ebd0f8f873 150 if(_limits)
Nakor 0:2743c73d648d 151 {
Nakor 6:c0ebd0f8f873 152 if (_xPosition < 0.0)
Nakor 6:c0ebd0f8f873 153 {
Nakor 6:c0ebd0f8f873 154 _xPosition = 0.0;
Nakor 6:c0ebd0f8f873 155 }
Nakor 0:2743c73d648d 156 }
Nakor 0:2743c73d648d 157 }
Nakor 0:2743c73d648d 158 }
Nakor 0:2743c73d648d 159 _lastLeftState = _leftState;
Nakor 0:2743c73d648d 160
Nakor 0:2743c73d648d 161 _rightState = _rightPin->read();
Nakor 0:2743c73d648d 162
Nakor 0:2743c73d648d 163 // compare the _buttonState to its previous state
Nakor 0:2743c73d648d 164 if (_rightState != _lastRightState)
Nakor 0:2743c73d648d 165 {
Nakor 0:2743c73d648d 166 // if the state has changed, increment the counter
Nakor 0:2743c73d648d 167 if (_rightState == 1)
Nakor 0:2743c73d648d 168 {
Nakor 0:2743c73d648d 169 // if the current state is 1 then the button
Nakor 0:2743c73d648d 170 // went from off to on:
Nakor 0:2743c73d648d 171 _xPosition+=TRACK_INC;
Nakor 6:c0ebd0f8f873 172 if(_limits)
Nakor 0:2743c73d648d 173 {
Nakor 6:c0ebd0f8f873 174 if (_xPosition > 1.0)
Nakor 6:c0ebd0f8f873 175 {
Nakor 6:c0ebd0f8f873 176 _xPosition = 1.0;
Nakor 6:c0ebd0f8f873 177 }
Nakor 0:2743c73d648d 178 }
Nakor 0:2743c73d648d 179 }
Nakor 0:2743c73d648d 180 }
Nakor 0:2743c73d648d 181 _lastRightState = _rightState;
Nakor 0:2743c73d648d 182
Nakor 0:2743c73d648d 183 _blueLED->write(_xPosition);
Nakor 0:2743c73d648d 184 _greenLED->write(_yPosition);
Nakor 0:2743c73d648d 185
Nakor 0:2743c73d648d 186 xPosition = _xPosition;
Nakor 0:2743c73d648d 187 yPosition = _yPosition;
Nakor 0:2743c73d648d 188
Nakor 0:2743c73d648d 189
Nakor 6:c0ebd0f8f873 190
Nakor 6:c0ebd0f8f873 191 #if DEBUG_POSITION
Nakor 4:3d1791d6608a 192 int ixPosition = _xPosition * 95;
Nakor 4:3d1791d6608a 193 int iyPosition = _yPosition * 63;
Nakor 4:3d1791d6608a 194
Nakor 6:c0ebd0f8f873 195
Nakor 6:c0ebd0f8f873 196
Nakor 0:2743c73d648d 197 if (_outputTimer.read_ms() > 1000)
Nakor 0:2743c73d648d 198 {
Nakor 4:3d1791d6608a 199 printf("Position: (x%i, y%i)\n", ixPosition, iyPosition);
Nakor 0:2743c73d648d 200
Nakor 0:2743c73d648d 201 _outputTimer.reset();
Nakor 0:2743c73d648d 202
Nakor 0:2743c73d648d 203 }
Nakor 6:c0ebd0f8f873 204 #endif
Nakor 0:2743c73d648d 205
Nakor 7:229fc175e05e 206 }
Nakor 7:229fc175e05e 207
Nakor 7:229fc175e05e 208 void trackballer::reset()
Nakor 7:229fc175e05e 209 {
Nakor 7:229fc175e05e 210 _xPosition = 0.5;
Nakor 7:229fc175e05e 211 _yPosition = 0.5;
Nakor 0:2743c73d648d 212 }