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

Dependents:   BBTrackball_Sample

Committer:
AdamGreen
Date:
Fri Dec 09 19:07:54 2011 +0000
Revision:
1:94c8e1e74dc1
Parent:
0:ad0f8a08c470
Child:
2:7715a78a7175

        

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 1:94c8e1e74dc1 48 #include "BBTrackball.h"
AdamGreen 1:94c8e1e74dc1 49
AdamGreen 1:94c8e1e74dc1 50 int main()
AdamGreen 1:94c8e1e74dc1 51 {
AdamGreen 1:94c8e1e74dc1 52 // UNDONE: Add something to this section.
AdamGreen 1:94c8e1e74dc1 53 return 0;
AdamGreen 1:94c8e1e74dc1 54 }
AdamGreen 1:94c8e1e74dc1 55 * @endcode
AdamGreen 1:94c8e1e74dc1 56 */
AdamGreen 1:94c8e1e74dc1 57 class CBBTrackball
AdamGreen 1:94c8e1e74dc1 58 {
AdamGreen 1:94c8e1e74dc1 59 public:
AdamGreen 1:94c8e1e74dc1 60 /** A structure in which the current state of the trackball is returned
AdamGreen 1:94c8e1e74dc1 61 from the GetState() method.
AdamGreen 1:94c8e1e74dc1 62
AdamGreen 1:94c8e1e74dc1 63 @see GetState()
AdamGreen 1:94c8e1e74dc1 64 */
AdamGreen 1:94c8e1e74dc1 65 struct SState
AdamGreen 1:94c8e1e74dc1 66 {
AdamGreen 1:94c8e1e74dc1 67 int ButtonPressed; /**< 1 if the button is currently pressed and 0 otherwise. */
AdamGreen 1:94c8e1e74dc1 68 short Up; /**< Number of upward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 69 short Down; /**< Number of downward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 70 short Left; /**< Number of leftward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 71 short Right; /**< Number of rightward pulses since last call. */
AdamGreen 1:94c8e1e74dc1 72 };
AdamGreen 1:94c8e1e74dc1 73
AdamGreen 1:94c8e1e74dc1 74 /** A structure used to represent the colour of the illumination for the
AdamGreen 1:94c8e1e74dc1 75 trackball.
AdamGreen 1:94c8e1e74dc1 76
AdamGreen 1:94c8e1e74dc1 77 @see SetColour()
AdamGreen 1:94c8e1e74dc1 78 */
AdamGreen 1:94c8e1e74dc1 79 struct SColour
AdamGreen 1:94c8e1e74dc1 80 {
AdamGreen 1:94c8e1e74dc1 81 unsigned char Red; /**< Red colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 82 unsigned char Green; /**< Green colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 83 unsigned char Blue; /**< Blue colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 84 unsigned char White; /**< Additional white colour component (0 - 255). */
AdamGreen 1:94c8e1e74dc1 85 };
AdamGreen 1:94c8e1e74dc1 86
AdamGreen 1:94c8e1e74dc1 87 /**
AdamGreen 1:94c8e1e74dc1 88 * Create a CBBTrackball object, binds it to the specified input/output
AdamGreen 1:94c8e1e74dc1 89 * pins, and initializes the required interrupt handling.
AdamGreen 1:94c8e1e74dc1 90 *
AdamGreen 1:94c8e1e74dc1 91 * @param BluePin The mbed pin which is connected to the BLU pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 92 * @param RedPin The mbed pin which is connected to the RED pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 93 * @param GreenPin The mbed pin which is connected to the GRN pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 94 * @param WhitePin The mbed pin which is connected to the WHT pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 95 * @param UpPin The mbed pin which is connected to the UP pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 96 * @param DownPin The mbed pin which is connected to the DWN pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 97 * @param LeftPin The mbed pin which is connected to the LFT pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 98 * @param RightPin The mbed pin which is connected to the RHT pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 99 * @param ButtonPin The mbed pin which is connected to the BTN pin of the breakout board.
AdamGreen 1:94c8e1e74dc1 100 */
AdamGreen 1:94c8e1e74dc1 101 CBBTrackball(PinName BluePin,
AdamGreen 1:94c8e1e74dc1 102 PinName RedPin,
AdamGreen 1:94c8e1e74dc1 103 PinName GreenPin,
AdamGreen 1:94c8e1e74dc1 104 PinName WhitePin,
AdamGreen 1:94c8e1e74dc1 105 PinName UpPin,
AdamGreen 1:94c8e1e74dc1 106 PinName DownPin,
AdamGreen 1:94c8e1e74dc1 107 PinName LeftPin,
AdamGreen 1:94c8e1e74dc1 108 PinName RightPin,
AdamGreen 1:94c8e1e74dc1 109 PinName ButtonPin) : m_BluePWM(BluePin),
AdamGreen 1:94c8e1e74dc1 110 m_RedPWM(RedPin),
AdamGreen 1:94c8e1e74dc1 111 m_GreenPWM(GreenPin),
AdamGreen 1:94c8e1e74dc1 112 m_WhitePWM(WhitePin),
AdamGreen 1:94c8e1e74dc1 113 m_UpInterrupt(UpPin),
AdamGreen 1:94c8e1e74dc1 114 m_DownInterrupt(DownPin),
AdamGreen 1:94c8e1e74dc1 115 m_LeftInterrupt(LeftPin),
AdamGreen 1:94c8e1e74dc1 116 m_RightInterrupt(RightPin),
AdamGreen 1:94c8e1e74dc1 117 m_Button(ButtonPin),
AdamGreen 1:94c8e1e74dc1 118 m_UpCount(0),
AdamGreen 1:94c8e1e74dc1 119 m_DownCount(0),
AdamGreen 1:94c8e1e74dc1 120 m_LeftCount(0),
AdamGreen 1:94c8e1e74dc1 121 m_RightCount(0),
AdamGreen 1:94c8e1e74dc1 122 m_ButtonState(1),
AdamGreen 1:94c8e1e74dc1 123 m_ButtonNewStateCount(0)
AdamGreen 1:94c8e1e74dc1 124 {
AdamGreen 1:94c8e1e74dc1 125 static const SColour AllLEDsOff = { 0, 0, 0, 0 };
AdamGreen 1:94c8e1e74dc1 126
AdamGreen 1:94c8e1e74dc1 127 m_UpInterrupt.rise<CBBTrackball>(this, &CBBTrackball::UpISR);
AdamGreen 1:94c8e1e74dc1 128 m_DownInterrupt.rise<CBBTrackball>(this, &CBBTrackball::DownISR);
AdamGreen 1:94c8e1e74dc1 129 m_LeftInterrupt.rise<CBBTrackball>(this, &CBBTrackball::LeftISR);
AdamGreen 1:94c8e1e74dc1 130 m_RightInterrupt.rise<CBBTrackball>(this, &CBBTrackball::RightISR);
AdamGreen 1:94c8e1e74dc1 131 m_ButtonSampleTicker.attach_us<CBBTrackball>(this, &CBBTrackball::ButtonSampleISR, 100);
AdamGreen 1:94c8e1e74dc1 132 SetColour(&AllLEDsOff);
AdamGreen 1:94c8e1e74dc1 133 }
AdamGreen 1:94c8e1e74dc1 134
AdamGreen 1:94c8e1e74dc1 135
AdamGreen 1:94c8e1e74dc1 136 /** Gets current state of the trackball.
AdamGreen 1:94c8e1e74dc1 137 *
AdamGreen 1:94c8e1e74dc1 138 * Returns state indicating how much the trackball has moved in each
AdamGreen 1:94c8e1e74dc1 139 * direction since the last call to GetState() and the current state of the
AdamGreen 1:94c8e1e74dc1 140 * push button.
AdamGreen 1:94c8e1e74dc1 141 *
AdamGreen 1:94c8e1e74dc1 142 * @param pState points to the state structure to be filled in with the
AdamGreen 1:94c8e1e74dc1 143 * current state.
AdamGreen 1:94c8e1e74dc1 144 */
AdamGreen 1:94c8e1e74dc1 145 void GetState(SState* pState);
AdamGreen 1:94c8e1e74dc1 146
AdamGreen 1:94c8e1e74dc1 147 /** Sets the colour of the trackball illumination.
AdamGreen 1:94c8e1e74dc1 148 *
AdamGreen 1:94c8e1e74dc1 149 * @param pColour points to the colour structure used to determine the
AdamGreen 1:94c8e1e74dc1 150 * illumination contribution from each of the 4 LEDs found beneath
AdamGreen 1:94c8e1e74dc1 151 * the trackball.
AdamGreen 1:94c8e1e74dc1 152 */
AdamGreen 1:94c8e1e74dc1 153 void SetColour(const SColour* pColour);
AdamGreen 1:94c8e1e74dc1 154
AdamGreen 1:94c8e1e74dc1 155
AdamGreen 1:94c8e1e74dc1 156 protected:
AdamGreen 1:94c8e1e74dc1 157 void UpISR(void);
AdamGreen 1:94c8e1e74dc1 158 void DownISR(void);
AdamGreen 1:94c8e1e74dc1 159 void LeftISR(void);
AdamGreen 1:94c8e1e74dc1 160 void RightISR(void);
AdamGreen 1:94c8e1e74dc1 161 void ButtonSampleISR(void);
AdamGreen 1:94c8e1e74dc1 162 short ThresholdToShort(unsigned int Value);
AdamGreen 1:94c8e1e74dc1 163 short UpCount(void);
AdamGreen 1:94c8e1e74dc1 164 short DownCount(void);
AdamGreen 1:94c8e1e74dc1 165 short LeftCount(void);
AdamGreen 1:94c8e1e74dc1 166 short RightCount(void);
AdamGreen 1:94c8e1e74dc1 167
AdamGreen 1:94c8e1e74dc1 168 PwmOut m_BluePWM;
AdamGreen 1:94c8e1e74dc1 169 PwmOut m_RedPWM;
AdamGreen 1:94c8e1e74dc1 170 PwmOut m_GreenPWM;
AdamGreen 1:94c8e1e74dc1 171 PwmOut m_WhitePWM;
AdamGreen 1:94c8e1e74dc1 172
AdamGreen 1:94c8e1e74dc1 173 InterruptIn m_UpInterrupt;
AdamGreen 1:94c8e1e74dc1 174 InterruptIn m_DownInterrupt;
AdamGreen 1:94c8e1e74dc1 175 InterruptIn m_LeftInterrupt;
AdamGreen 1:94c8e1e74dc1 176 InterruptIn m_RightInterrupt;
AdamGreen 1:94c8e1e74dc1 177
AdamGreen 1:94c8e1e74dc1 178 DigitalIn m_Button;
AdamGreen 1:94c8e1e74dc1 179 Ticker m_ButtonSampleTicker;
AdamGreen 1:94c8e1e74dc1 180
AdamGreen 1:94c8e1e74dc1 181 unsigned int m_UpCount;
AdamGreen 1:94c8e1e74dc1 182 unsigned int m_DownCount;
AdamGreen 1:94c8e1e74dc1 183 unsigned int m_LeftCount;
AdamGreen 1:94c8e1e74dc1 184 unsigned int m_RightCount;
AdamGreen 1:94c8e1e74dc1 185
AdamGreen 1:94c8e1e74dc1 186 int m_ButtonState;
AdamGreen 1:94c8e1e74dc1 187 unsigned int m_ButtonNewStateCount;
AdamGreen 1:94c8e1e74dc1 188 };
AdamGreen 1:94c8e1e74dc1 189
AdamGreen 1:94c8e1e74dc1 190 } // namespace AFP
AdamGreen 1:94c8e1e74dc1 191 using namespace AFP;
AdamGreen 1:94c8e1e74dc1 192
AdamGreen 1:94c8e1e74dc1 193 #endif /* _BBTRACKBALL_H__BBTRACKBALL_H_ */