Class to interface with Sparkfun's Blackberry Trackball Breakout Board.

Dependents:   BBTrackball_Sample

Committer:
AdamGreen
Date:
Fri Dec 09 19:25:47 2011 +0000
Revision:
2:7715a78a7175
Parent:
1:94c8e1e74dc1

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
AdamGreen 1:94c8e1e74dc1 1 /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/)
AdamGreen 1:94c8e1e74dc1 2
AdamGreen 1:94c8e1e74dc1 3 Licensed under the Apache License, Version 2.0 (the "License");
AdamGreen 1:94c8e1e74dc1 4 you may not use this file except in compliance with the License.
AdamGreen 1:94c8e1e74dc1 5 You may obtain a copy of the License at
AdamGreen 1:94c8e1e74dc1 6
AdamGreen 1:94c8e1e74dc1 7 http://www.apache.org/licenses/LICENSE-2.0
AdamGreen 1:94c8e1e74dc1 8
AdamGreen 1:94c8e1e74dc1 9 Unless required by applicable law or agreed to in writing, software
AdamGreen 1:94c8e1e74dc1 10 distributed under the License is distributed on an "AS IS" BASIS,
AdamGreen 1:94c8e1e74dc1 11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
AdamGreen 1:94c8e1e74dc1 12 See the License for the specific language governing permissions and
AdamGreen 1:94c8e1e74dc1 13 limitations under the License.
AdamGreen 1:94c8e1e74dc1 14 */
AdamGreen 1:94c8e1e74dc1 15 /* Header file for class to control Sparkfun's Blackberry Trackball:
AdamGreen 1:94c8e1e74dc1 16 http://www.sparkfun.com/products/9320
AdamGreen 1:94c8e1e74dc1 17 */
AdamGreen 1:94c8e1e74dc1 18 #ifndef _BBTRACKBALL_H_
AdamGreen 1:94c8e1e74dc1 19 #define _BBTRACKBALL_H_
AdamGreen 1:94c8e1e74dc1 20 #include <mbed.h>
AdamGreen 1:94c8e1e74dc1 21
AdamGreen 1:94c8e1e74dc1 22 namespace AFP
AdamGreen 1:94c8e1e74dc1 23 {
AdamGreen 1:94c8e1e74dc1 24
AdamGreen 1:94c8e1e74dc1 25 /** A class to interface with the Sparkfun Blackberry Trackball Breakout Board.
AdamGreen 1:94c8e1e74dc1 26 * http://www.sparkfun.com/products/9320
AdamGreen 1:94c8e1e74dc1 27 *
AdamGreen 1:94c8e1e74dc1 28 * This breakout board uses a hall effect for each direction of motion (up,
AdamGreen 1:94c8e1e74dc1 29 * down, left, right) and generates pulses as the trackball is rotated in each
AdamGreen 1:94c8e1e74dc1 30 * of these directions. This class counts both the rising and falling edges
AdamGreen 1:94c8e1e74dc1 31 * of these pulses in an interrupt handler. The main program can query for
AdamGreen 1:94c8e1e74dc1 32 * the number of pulses that have been generated since the previous query was
AdamGreen 1:94c8e1e74dc1 33 * made.
AdamGreen 1:94c8e1e74dc1 34 *
AdamGreen 1:94c8e1e74dc1 35 * The breakout board also has a push button placed beneath the trackball so
AdamGreen 1:94c8e1e74dc1 36 * that user presses can be detected as well. This class will provide the
AdamGreen 1:94c8e1e74dc1 37 * state of this button as well after filtering it for the purpose of
AdamGreen 1:94c8e1e74dc1 38 * debouncing.
AdamGreen 1:94c8e1e74dc1 39 *
AdamGreen 1:94c8e1e74dc1 40 * The last feature of this breakout board that is supported by the class
AdamGreen 1:94c8e1e74dc1 41 * includes the 4 LEDs that have been placed beneath the trackball. This
AdamGreen 1:94c8e1e74dc1 42 * class allows the caller to specify an individual brightness value for each
AdamGreen 1:94c8e1e74dc1 43 * of the LEDs (red, blue, green, white).
AdamGreen 1:94c8e1e74dc1 44 *
AdamGreen 1:94c8e1e74dc1 45 * Example:
AdamGreen 1:94c8e1e74dc1 46 * @code
AdamGreen 1:94c8e1e74dc1 47 #include <mbed.h>
AdamGreen 2:7715a78a7175 48 #include <USBMouse.h>
AdamGreen 1:94c8e1e74dc1 49 #include "BBTrackball.h"
AdamGreen 1:94c8e1e74dc1 50
AdamGreen 1:94c8e1e74dc1 51 int main()
AdamGreen 1:94c8e1e74dc1 52 {
AdamGreen 2:7715a78a7175 53 static const CBBTrackball::SColour GreenColour = { 0, 255, 0, 0 };
AdamGreen 2:7715a78a7175 54 static CBBTrackball Trackball(p20, // BLU
AdamGreen 2:7715a78a7175 55 p25, // RED
AdamGreen 2:7715a78a7175 56 p26, // GRN
AdamGreen 2:7715a78a7175 57 p10, // WHT
AdamGreen 2:7715a78a7175 58 p5, // UP
AdamGreen 2:7715a78a7175 59 p6, // DWN
AdamGreen 2:7715a78a7175 60 p7, // LFT
AdamGreen 2:7715a78a7175 61 p8, // RHT
AdamGreen 2:7715a78a7175 62 p9); // BTN
AdamGreen 2:7715a78a7175 63 static USBMouse Mouse;
AdamGreen 2:7715a78a7175 64
AdamGreen 2:7715a78a7175 65 // Turn the green LED on.
AdamGreen 2:7715a78a7175 66 Trackball.SetColour(&GreenColour);
AdamGreen 2:7715a78a7175 67
AdamGreen 2:7715a78a7175 68 for(;;)
AdamGreen 2:7715a78a7175 69 {
AdamGreen 2:7715a78a7175 70 CBBTrackball::SState TrackballState;
AdamGreen 2:7715a78a7175 71 int DeltaX;
AdamGreen 2:7715a78a7175 72 int DeltaY;
AdamGreen 2:7715a78a7175 73
AdamGreen 2:7715a78a7175 74 Trackball.GetState(&TrackballState);
AdamGreen 2:7715a78a7175 75
AdamGreen 2:7715a78a7175 76 // NOTE: The breakout board is rotated 90 degrees on my breadboard.
AdamGreen 2:7715a78a7175 77 DeltaX = TrackballState.Up - TrackballState.Down;
AdamGreen 2:7715a78a7175 78 DeltaY = TrackballState.Right - TrackballState.Left;
AdamGreen 2:7715a78a7175 79
AdamGreen 2:7715a78a7175 80 Mouse.update(DeltaX,
AdamGreen 2:7715a78a7175 81 DeltaY,
AdamGreen 2:7715a78a7175 82 TrackballState.ButtonPressed ? MOUSE_LEFT : 0,
AdamGreen 2:7715a78a7175 83 0);
AdamGreen 2:7715a78a7175 84 }
AdamGreen 1:94c8e1e74dc1 85 }
AdamGreen 1:94c8e1e74dc1 86 * @endcode
AdamGreen 1:94c8e1e74dc1 87 */
AdamGreen 1:94c8e1e74dc1 88 class CBBTrackball
AdamGreen 1:94c8e1e74dc1 89 {
AdamGreen 1:94c8e1e74dc1 90 public:
AdamGreen 1:94c8e1e74dc1 91 /** A structure in which the current state of the trackball is returned
AdamGreen 1:94c8e1e74dc1 92 from the GetState() method.
AdamGreen 1:94c8e1e74dc1 93
AdamGreen 1:94c8e1e74dc1 94 @see GetState()
AdamGreen 1:94c8e1e74dc1 95 */
AdamGreen 1:94c8e1e74dc1 96 struct SState
AdamGreen 1:94c8e1e74dc1 97 {
AdamGreen 1:94c8e1e74dc1 98 int ButtonPressed; /**< 1 if the button is currently pressed and 0 otherwise. */
AdamGreen 1:94c8e1e74dc1 99 short Up; /**< Number of upward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 100 short Down; /**< Number of downward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 101 short Left; /**< Number of leftward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 102 short Right; /**< Number of rightward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 103 };
AdamGreen 1:94c8e1e74dc1 104
AdamGreen 1:94c8e1e74dc1 105 /** A structure used to represent the colour of the illumination for the
AdamGreen 1:94c8e1e74dc1 106 trackball.
AdamGreen 1:94c8e1e74dc1 107
AdamGreen 1:94c8e1e74dc1 108 @see SetColour()
AdamGreen 1:94c8e1e74dc1 109 */
AdamGreen 1:94c8e1e74dc1 110 struct SColour
AdamGreen 1:94c8e1e74dc1 111 {
AdamGreen 1:94c8e1e74dc1 112 unsigned char Red; /**< Red colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 113 unsigned char Green; /**< Green colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 114 unsigned char Blue; /**< Blue colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 115 unsigned char White; /**< Additional white colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 116 };
AdamGreen 1:94c8e1e74dc1 117
AdamGreen 1:94c8e1e74dc1 118 /**
AdamGreen 1:94c8e1e74dc1 119 * Create a CBBTrackball object, binds it to the specified input/output
AdamGreen 1:94c8e1e74dc1 120 * pins, and initializes the required interrupt handling.
AdamGreen 1:94c8e1e74dc1 121 *
AdamGreen 1:94c8e1e74dc1 122 * @param BluePin The mbed pin which is connected to the BLU pin of the breakout board.
AdamGreen 2:7715a78a7175 123 * Must be a pin on which the mbed supports PwmOut.
AdamGreen 1:94c8e1e74dc1 124 * @param RedPin The mbed pin which is connected to the RED pin of the breakout board.
AdamGreen 2:7715a78a7175 125 * Must be a pin on which the mbed supports PwmOut.
AdamGreen 1:94c8e1e74dc1 126 * @param GreenPin The mbed pin which is connected to the GRN pin of the breakout board.
AdamGreen 2:7715a78a7175 127 * Must be a pin on which the mbed supports PwmOut.
AdamGreen 1:94c8e1e74dc1 128 * @param WhitePin The mbed pin which is connected to the WHT pin of the breakout board.
AdamGreen 2:7715a78a7175 129 * Must be a pin on which the mbed supports PwmOut.
AdamGreen 1:94c8e1e74dc1 130 * @param UpPin The mbed pin which is connected to the UP pin of the breakout board.
AdamGreen 2:7715a78a7175 131 * Must be a pin on which the mbed supports InterruptIn.
AdamGreen 1:94c8e1e74dc1 132 * @param DownPin The mbed pin which is connected to the DWN pin of the breakout board.
AdamGreen 2:7715a78a7175 133 * Must be a pin on which the mbed supports InterruptIn.
AdamGreen 1:94c8e1e74dc1 134 * @param LeftPin The mbed pin which is connected to the LFT pin of the breakout board.
AdamGreen 2:7715a78a7175 135 * Must be a pin on which the mbed supports InterruptIn.
AdamGreen 1:94c8e1e74dc1 136 * @param RightPin The mbed pin which is connected to the RHT pin of the breakout board.
AdamGreen 2:7715a78a7175 137 * Must be a pin on which the mbed supports InterruptIn.
AdamGreen 1:94c8e1e74dc1 138 * @param ButtonPin The mbed pin which is connected to the BTN pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 139 */
AdamGreen 1:94c8e1e74dc1 140 CBBTrackball(PinName BluePin,
AdamGreen 1:94c8e1e74dc1 141 PinName RedPin,
AdamGreen 1:94c8e1e74dc1 142 PinName GreenPin,
AdamGreen 1:94c8e1e74dc1 143 PinName WhitePin,
AdamGreen 1:94c8e1e74dc1 144 PinName UpPin,
AdamGreen 1:94c8e1e74dc1 145 PinName DownPin,
AdamGreen 1:94c8e1e74dc1 146 PinName LeftPin,
AdamGreen 1:94c8e1e74dc1 147 PinName RightPin,
AdamGreen 1:94c8e1e74dc1 148 PinName ButtonPin) : m_BluePWM(BluePin),
AdamGreen 1:94c8e1e74dc1 149 m_RedPWM(RedPin),
AdamGreen 1:94c8e1e74dc1 150 m_GreenPWM(GreenPin),
AdamGreen 1:94c8e1e74dc1 151 m_WhitePWM(WhitePin),
AdamGreen 1:94c8e1e74dc1 152 m_UpInterrupt(UpPin),
AdamGreen 1:94c8e1e74dc1 153 m_DownInterrupt(DownPin),
AdamGreen 1:94c8e1e74dc1 154 m_LeftInterrupt(LeftPin),
AdamGreen 1:94c8e1e74dc1 155 m_RightInterrupt(RightPin),
AdamGreen 1:94c8e1e74dc1 156 m_Button(ButtonPin),
AdamGreen 1:94c8e1e74dc1 157 m_UpCount(0),
AdamGreen 1:94c8e1e74dc1 158 m_DownCount(0),
AdamGreen 1:94c8e1e74dc1 159 m_LeftCount(0),
AdamGreen 1:94c8e1e74dc1 160 m_RightCount(0),
AdamGreen 1:94c8e1e74dc1 161 m_ButtonState(1),
AdamGreen 1:94c8e1e74dc1 162 m_ButtonNewStateCount(0)
AdamGreen 1:94c8e1e74dc1 163 {
AdamGreen 1:94c8e1e74dc1 164 static const SColour AllLEDsOff = { 0, 0, 0, 0 };
AdamGreen 1:94c8e1e74dc1 165
AdamGreen 1:94c8e1e74dc1 166 m_UpInterrupt.rise<CBBTrackball>(this, &CBBTrackball::UpISR);
AdamGreen 1:94c8e1e74dc1 167 m_DownInterrupt.rise<CBBTrackball>(this, &CBBTrackball::DownISR);
AdamGreen 1:94c8e1e74dc1 168 m_LeftInterrupt.rise<CBBTrackball>(this, &CBBTrackball::LeftISR);
AdamGreen 1:94c8e1e74dc1 169 m_RightInterrupt.rise<CBBTrackball>(this, &CBBTrackball::RightISR);
AdamGreen 1:94c8e1e74dc1 170 m_ButtonSampleTicker.attach_us<CBBTrackball>(this, &CBBTrackball::ButtonSampleISR, 100);
AdamGreen 1:94c8e1e74dc1 171 SetColour(&AllLEDsOff);
AdamGreen 1:94c8e1e74dc1 172 }
AdamGreen 1:94c8e1e74dc1 173
AdamGreen 1:94c8e1e74dc1 174
AdamGreen 1:94c8e1e74dc1 175 /** Gets current state of the trackball.
AdamGreen 1:94c8e1e74dc1 176 *
AdamGreen 1:94c8e1e74dc1 177 * Returns state indicating how much the trackball has moved in each
AdamGreen 1:94c8e1e74dc1 178 * direction since the last call to GetState() and the current state of the
AdamGreen 1:94c8e1e74dc1 179 * push button.
AdamGreen 1:94c8e1e74dc1 180 *
AdamGreen 1:94c8e1e74dc1 181 * @param pState points to the state structure to be filled in with the
AdamGreen 1:94c8e1e74dc1 182 * current state.
AdamGreen 1:94c8e1e74dc1 183 */
AdamGreen 1:94c8e1e74dc1 184 void GetState(SState* pState);
AdamGreen 1:94c8e1e74dc1 185
AdamGreen 1:94c8e1e74dc1 186 /** Sets the colour of the trackball illumination.
AdamGreen 1:94c8e1e74dc1 187 *
AdamGreen 1:94c8e1e74dc1 188 * @param pColour points to the colour structure used to determine the
AdamGreen 1:94c8e1e74dc1 189 * illumination contribution from each of the 4 LEDs found beneath
AdamGreen 1:94c8e1e74dc1 190 * the trackball.
AdamGreen 1:94c8e1e74dc1 191 */
AdamGreen 1:94c8e1e74dc1 192 void SetColour(const SColour* pColour);
AdamGreen 1:94c8e1e74dc1 193
AdamGreen 1:94c8e1e74dc1 194
AdamGreen 1:94c8e1e74dc1 195 protected:
AdamGreen 1:94c8e1e74dc1 196 void UpISR(void);
AdamGreen 1:94c8e1e74dc1 197 void DownISR(void);
AdamGreen 1:94c8e1e74dc1 198 void LeftISR(void);
AdamGreen 1:94c8e1e74dc1 199 void RightISR(void);
AdamGreen 1:94c8e1e74dc1 200 void ButtonSampleISR(void);
AdamGreen 1:94c8e1e74dc1 201 short ThresholdToShort(unsigned int Value);
AdamGreen 1:94c8e1e74dc1 202 short UpCount(void);
AdamGreen 1:94c8e1e74dc1 203 short DownCount(void);
AdamGreen 1:94c8e1e74dc1 204 short LeftCount(void);
AdamGreen 1:94c8e1e74dc1 205 short RightCount(void);
AdamGreen 1:94c8e1e74dc1 206
AdamGreen 1:94c8e1e74dc1 207 PwmOut m_BluePWM;
AdamGreen 1:94c8e1e74dc1 208 PwmOut m_RedPWM;
AdamGreen 1:94c8e1e74dc1 209 PwmOut m_GreenPWM;
AdamGreen 1:94c8e1e74dc1 210 PwmOut m_WhitePWM;
AdamGreen 1:94c8e1e74dc1 211
AdamGreen 1:94c8e1e74dc1 212 InterruptIn m_UpInterrupt;
AdamGreen 1:94c8e1e74dc1 213 InterruptIn m_DownInterrupt;
AdamGreen 1:94c8e1e74dc1 214 InterruptIn m_LeftInterrupt;
AdamGreen 1:94c8e1e74dc1 215 InterruptIn m_RightInterrupt;
AdamGreen 1:94c8e1e74dc1 216
AdamGreen 1:94c8e1e74dc1 217 DigitalIn m_Button;
AdamGreen 1:94c8e1e74dc1 218 Ticker m_ButtonSampleTicker;
AdamGreen 1:94c8e1e74dc1 219
AdamGreen 1:94c8e1e74dc1 220 unsigned int m_UpCount;
AdamGreen 1:94c8e1e74dc1 221 unsigned int m_DownCount;
AdamGreen 1:94c8e1e74dc1 222 unsigned int m_LeftCount;
AdamGreen 1:94c8e1e74dc1 223 unsigned int m_RightCount;
AdamGreen 1:94c8e1e74dc1 224
AdamGreen 1:94c8e1e74dc1 225 int m_ButtonState;
AdamGreen 1:94c8e1e74dc1 226 unsigned int m_ButtonNewStateCount;
AdamGreen 1:94c8e1e74dc1 227 };
AdamGreen 1:94c8e1e74dc1 228
AdamGreen 1:94c8e1e74dc1 229 } // namespace AFP
AdamGreen 1:94c8e1e74dc1 230 using namespace AFP;
AdamGreen 1:94c8e1e74dc1 231
AdamGreen 1:94c8e1e74dc1 232 #endif /* _BBTRACKBALL_H__BBTRACKBALL_H_ */