![](/media/cache/profiles/99f0e91e4f90ecc1c3bdee598eadca30.50x50_q85.jpg)
Just an example program for Sparkfun's Trackballer breakout board.
main.cpp@0:dd150b03a225, 2011-02-17 (annotated)
- Committer:
- Nakor
- Date:
- Thu Feb 17 20:55:46 2011 +0000
- Revision:
- 0:dd150b03a225
First commit.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nakor | 0:dd150b03a225 | 1 | #include "mbed.h" |
Nakor | 0:dd150b03a225 | 2 | |
Nakor | 0:dd150b03a225 | 3 | #define TRACK_INC 0.02 |
Nakor | 0:dd150b03a225 | 4 | |
Nakor | 0:dd150b03a225 | 5 | DigitalIn buttonPin(p8); |
Nakor | 0:dd150b03a225 | 6 | DigitalIn rightPin(p15); |
Nakor | 0:dd150b03a225 | 7 | DigitalIn leftPin(p16); |
Nakor | 0:dd150b03a225 | 8 | DigitalIn downPin(p17); |
Nakor | 0:dd150b03a225 | 9 | DigitalIn upPin(p18); |
Nakor | 0:dd150b03a225 | 10 | |
Nakor | 0:dd150b03a225 | 11 | PwmOut blueLED(p23); |
Nakor | 0:dd150b03a225 | 12 | PwmOut redLED(p24); |
Nakor | 0:dd150b03a225 | 13 | PwmOut greenLED(p25); |
Nakor | 0:dd150b03a225 | 14 | PwmOut whiteLED(p26); |
Nakor | 0:dd150b03a225 | 15 | |
Nakor | 0:dd150b03a225 | 16 | Timer outputTimer; |
Nakor | 0:dd150b03a225 | 17 | Serial PC(USBTX, USBRX); |
Nakor | 0:dd150b03a225 | 18 | |
Nakor | 0:dd150b03a225 | 19 | |
Nakor | 0:dd150b03a225 | 20 | int buttonPushCounter = 0; // counter for the number of button presses |
Nakor | 0:dd150b03a225 | 21 | |
Nakor | 0:dd150b03a225 | 22 | char buttonState = 0; // current state of the button |
Nakor | 0:dd150b03a225 | 23 | char lastButtonState = 0; // previous state of the button |
Nakor | 0:dd150b03a225 | 24 | |
Nakor | 0:dd150b03a225 | 25 | char upState = 0; |
Nakor | 0:dd150b03a225 | 26 | char downState = 0; |
Nakor | 0:dd150b03a225 | 27 | char leftState = 0; |
Nakor | 0:dd150b03a225 | 28 | char rightState = 0; |
Nakor | 0:dd150b03a225 | 29 | char lastUpState = 0; |
Nakor | 0:dd150b03a225 | 30 | char lastDownState = 0; |
Nakor | 0:dd150b03a225 | 31 | char lastLeftState = 0; |
Nakor | 0:dd150b03a225 | 32 | char lastRightState = 0; |
Nakor | 0:dd150b03a225 | 33 | |
Nakor | 0:dd150b03a225 | 34 | float xPosition = 0.0; |
Nakor | 0:dd150b03a225 | 35 | float yPosition = 0.0; |
Nakor | 0:dd150b03a225 | 36 | |
Nakor | 0:dd150b03a225 | 37 | |
Nakor | 0:dd150b03a225 | 38 | |
Nakor | 0:dd150b03a225 | 39 | void setup() |
Nakor | 0:dd150b03a225 | 40 | { |
Nakor | 0:dd150b03a225 | 41 | PC.baud(230400); |
Nakor | 0:dd150b03a225 | 42 | |
Nakor | 0:dd150b03a225 | 43 | buttonPin.mode(PullUp); |
Nakor | 0:dd150b03a225 | 44 | |
Nakor | 0:dd150b03a225 | 45 | redLED = 0.0; |
Nakor | 0:dd150b03a225 | 46 | greenLED = 0.0; |
Nakor | 0:dd150b03a225 | 47 | blueLED = 0.0; |
Nakor | 0:dd150b03a225 | 48 | whiteLED = 0.0; |
Nakor | 0:dd150b03a225 | 49 | |
Nakor | 0:dd150b03a225 | 50 | outputTimer.start(); |
Nakor | 0:dd150b03a225 | 51 | } |
Nakor | 0:dd150b03a225 | 52 | |
Nakor | 0:dd150b03a225 | 53 | |
Nakor | 0:dd150b03a225 | 54 | int main() |
Nakor | 0:dd150b03a225 | 55 | { |
Nakor | 0:dd150b03a225 | 56 | setup(); |
Nakor | 0:dd150b03a225 | 57 | |
Nakor | 0:dd150b03a225 | 58 | while (1) |
Nakor | 0:dd150b03a225 | 59 | { |
Nakor | 0:dd150b03a225 | 60 | |
Nakor | 0:dd150b03a225 | 61 | buttonState = buttonPin.read(); |
Nakor | 0:dd150b03a225 | 62 | |
Nakor | 0:dd150b03a225 | 63 | // compare the buttonState to its previous state |
Nakor | 0:dd150b03a225 | 64 | if (buttonState != lastButtonState) |
Nakor | 0:dd150b03a225 | 65 | { |
Nakor | 0:dd150b03a225 | 66 | // if the state has changed, increment the counter |
Nakor | 0:dd150b03a225 | 67 | if (buttonState == 1) |
Nakor | 0:dd150b03a225 | 68 | { |
Nakor | 0:dd150b03a225 | 69 | // if the current state is 1 then the button |
Nakor | 0:dd150b03a225 | 70 | // went from off to on: |
Nakor | 0:dd150b03a225 | 71 | buttonPushCounter++; |
Nakor | 0:dd150b03a225 | 72 | printf("Number of button pushes: %i\n", buttonPushCounter); |
Nakor | 0:dd150b03a225 | 73 | } |
Nakor | 0:dd150b03a225 | 74 | } |
Nakor | 0:dd150b03a225 | 75 | lastButtonState = buttonState; |
Nakor | 0:dd150b03a225 | 76 | |
Nakor | 0:dd150b03a225 | 77 | |
Nakor | 0:dd150b03a225 | 78 | if (buttonPushCounter % 2 == 0) |
Nakor | 0:dd150b03a225 | 79 | { |
Nakor | 0:dd150b03a225 | 80 | redLED = 1; |
Nakor | 0:dd150b03a225 | 81 | } |
Nakor | 0:dd150b03a225 | 82 | else |
Nakor | 0:dd150b03a225 | 83 | { |
Nakor | 0:dd150b03a225 | 84 | redLED = 0; |
Nakor | 0:dd150b03a225 | 85 | } |
Nakor | 0:dd150b03a225 | 86 | |
Nakor | 0:dd150b03a225 | 87 | upState = upPin.read(); |
Nakor | 0:dd150b03a225 | 88 | |
Nakor | 0:dd150b03a225 | 89 | if (upState != lastUpState) |
Nakor | 0:dd150b03a225 | 90 | { |
Nakor | 0:dd150b03a225 | 91 | if (upState == 1) |
Nakor | 0:dd150b03a225 | 92 | { |
Nakor | 0:dd150b03a225 | 93 | // if the current state is 1 then the button |
Nakor | 0:dd150b03a225 | 94 | // went from off to on: |
Nakor | 0:dd150b03a225 | 95 | yPosition+=TRACK_INC; |
Nakor | 0:dd150b03a225 | 96 | if (yPosition > 1.0) |
Nakor | 0:dd150b03a225 | 97 | { |
Nakor | 0:dd150b03a225 | 98 | yPosition = 1.0; |
Nakor | 0:dd150b03a225 | 99 | } |
Nakor | 0:dd150b03a225 | 100 | } |
Nakor | 0:dd150b03a225 | 101 | } |
Nakor | 0:dd150b03a225 | 102 | lastUpState = upState; |
Nakor | 0:dd150b03a225 | 103 | |
Nakor | 0:dd150b03a225 | 104 | |
Nakor | 0:dd150b03a225 | 105 | downState = downPin.read(); |
Nakor | 0:dd150b03a225 | 106 | |
Nakor | 0:dd150b03a225 | 107 | // compare the buttonState to its previous state |
Nakor | 0:dd150b03a225 | 108 | if (downState != lastDownState) |
Nakor | 0:dd150b03a225 | 109 | { |
Nakor | 0:dd150b03a225 | 110 | // if the state has changed, increment the counter |
Nakor | 0:dd150b03a225 | 111 | if (downState == 1) |
Nakor | 0:dd150b03a225 | 112 | { |
Nakor | 0:dd150b03a225 | 113 | // if the current state is 1 then the button |
Nakor | 0:dd150b03a225 | 114 | // went from off to on: |
Nakor | 0:dd150b03a225 | 115 | yPosition-=TRACK_INC; |
Nakor | 0:dd150b03a225 | 116 | if (yPosition < 0.0) |
Nakor | 0:dd150b03a225 | 117 | { |
Nakor | 0:dd150b03a225 | 118 | yPosition = 0.0; |
Nakor | 0:dd150b03a225 | 119 | } |
Nakor | 0:dd150b03a225 | 120 | } |
Nakor | 0:dd150b03a225 | 121 | } |
Nakor | 0:dd150b03a225 | 122 | lastDownState = downState; |
Nakor | 0:dd150b03a225 | 123 | |
Nakor | 0:dd150b03a225 | 124 | |
Nakor | 0:dd150b03a225 | 125 | leftState = leftPin.read(); |
Nakor | 0:dd150b03a225 | 126 | |
Nakor | 0:dd150b03a225 | 127 | // compare the buttonState to its previous state |
Nakor | 0:dd150b03a225 | 128 | if (leftState != lastLeftState) |
Nakor | 0:dd150b03a225 | 129 | { |
Nakor | 0:dd150b03a225 | 130 | // if the state has changed, increment the counter |
Nakor | 0:dd150b03a225 | 131 | if (leftState == 1) |
Nakor | 0:dd150b03a225 | 132 | { |
Nakor | 0:dd150b03a225 | 133 | // if the current state is 1 then the button |
Nakor | 0:dd150b03a225 | 134 | // went from off to on: |
Nakor | 0:dd150b03a225 | 135 | xPosition-=TRACK_INC; |
Nakor | 0:dd150b03a225 | 136 | if (xPosition < 0.0) |
Nakor | 0:dd150b03a225 | 137 | { |
Nakor | 0:dd150b03a225 | 138 | xPosition = 0.0; |
Nakor | 0:dd150b03a225 | 139 | } |
Nakor | 0:dd150b03a225 | 140 | } |
Nakor | 0:dd150b03a225 | 141 | } |
Nakor | 0:dd150b03a225 | 142 | lastLeftState = leftState; |
Nakor | 0:dd150b03a225 | 143 | |
Nakor | 0:dd150b03a225 | 144 | rightState = rightPin.read(); |
Nakor | 0:dd150b03a225 | 145 | |
Nakor | 0:dd150b03a225 | 146 | // compare the buttonState to its previous state |
Nakor | 0:dd150b03a225 | 147 | if (rightState != lastRightState) |
Nakor | 0:dd150b03a225 | 148 | { |
Nakor | 0:dd150b03a225 | 149 | // if the state has changed, increment the counter |
Nakor | 0:dd150b03a225 | 150 | if (rightState == 1) |
Nakor | 0:dd150b03a225 | 151 | { |
Nakor | 0:dd150b03a225 | 152 | // if the current state is 1 then the button |
Nakor | 0:dd150b03a225 | 153 | // went from off to on: |
Nakor | 0:dd150b03a225 | 154 | xPosition+=TRACK_INC; |
Nakor | 0:dd150b03a225 | 155 | if (xPosition > 1.0) |
Nakor | 0:dd150b03a225 | 156 | { |
Nakor | 0:dd150b03a225 | 157 | xPosition = 1.0; |
Nakor | 0:dd150b03a225 | 158 | } |
Nakor | 0:dd150b03a225 | 159 | } |
Nakor | 0:dd150b03a225 | 160 | } |
Nakor | 0:dd150b03a225 | 161 | lastRightState = rightState; |
Nakor | 0:dd150b03a225 | 162 | |
Nakor | 0:dd150b03a225 | 163 | blueLED = xPosition; |
Nakor | 0:dd150b03a225 | 164 | greenLED = yPosition; |
Nakor | 0:dd150b03a225 | 165 | |
Nakor | 0:dd150b03a225 | 166 | |
Nakor | 0:dd150b03a225 | 167 | |
Nakor | 0:dd150b03a225 | 168 | if (outputTimer.read_ms() > 1000) |
Nakor | 0:dd150b03a225 | 169 | { |
Nakor | 0:dd150b03a225 | 170 | printf("Position: (x%f, y%f)\n", xPosition, yPosition); |
Nakor | 0:dd150b03a225 | 171 | |
Nakor | 0:dd150b03a225 | 172 | outputTimer.reset(); |
Nakor | 0:dd150b03a225 | 173 | |
Nakor | 0:dd150b03a225 | 174 | } |
Nakor | 0:dd150b03a225 | 175 | } |
Nakor | 0:dd150b03a225 | 176 | } |