Class to interface with Sparkfun's Blackberry Trackball Breakout Board.
Dependents: BBTrackball_Sample
BBTrackball.h@0:ad0f8a08c470, 2011-12-08 (annotated)
- Committer:
- AdamGreen
- Date:
- Thu Dec 08 07:29:12 2011 +0000
- Revision:
- 0:ad0f8a08c470
- Child:
- 1:94c8e1e74dc1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AdamGreen | 0:ad0f8a08c470 | 1 | /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/) |
AdamGreen | 0:ad0f8a08c470 | 2 | |
AdamGreen | 0:ad0f8a08c470 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
AdamGreen | 0:ad0f8a08c470 | 4 | you may not use this file except in compliance with the License. |
AdamGreen | 0:ad0f8a08c470 | 5 | You may obtain a copy of the License at |
AdamGreen | 0:ad0f8a08c470 | 6 | |
AdamGreen | 0:ad0f8a08c470 | 7 | http://www.apache.org/licenses/LICENSE-2.0 |
AdamGreen | 0:ad0f8a08c470 | 8 | |
AdamGreen | 0:ad0f8a08c470 | 9 | Unless required by applicable law or agreed to in writing, software |
AdamGreen | 0:ad0f8a08c470 | 10 | distributed under the License is distributed on an "AS IS" BASIS, |
AdamGreen | 0:ad0f8a08c470 | 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AdamGreen | 0:ad0f8a08c470 | 12 | See the License for the specific language governing permissions and |
AdamGreen | 0:ad0f8a08c470 | 13 | limitations under the License. |
AdamGreen | 0:ad0f8a08c470 | 14 | */ |
AdamGreen | 0:ad0f8a08c470 | 15 | /* Header file for class to control Sparkfun's Blackberry Trackball: |
AdamGreen | 0:ad0f8a08c470 | 16 | http://www.sparkfun.com/products/9320 |
AdamGreen | 0:ad0f8a08c470 | 17 | */ |
AdamGreen | 0:ad0f8a08c470 | 18 | #ifndef _BBTRACKBALL_H_ |
AdamGreen | 0:ad0f8a08c470 | 19 | #define _BBTRACKBALL_H_ |
AdamGreen | 0:ad0f8a08c470 | 20 | #include <mbed.h> |
AdamGreen | 0:ad0f8a08c470 | 21 | |
AdamGreen | 0:ad0f8a08c470 | 22 | namespace AFP |
AdamGreen | 0:ad0f8a08c470 | 23 | { |
AdamGreen | 0:ad0f8a08c470 | 24 | |
AdamGreen | 0:ad0f8a08c470 | 25 | /** A class to interface with the Sparkfun Blackberry Trackball Breakout Board. |
AdamGreen | 0:ad0f8a08c470 | 26 | * http://www.sparkfun.com/products/9320 |
AdamGreen | 0:ad0f8a08c470 | 27 | * |
AdamGreen | 0:ad0f8a08c470 | 28 | * This breakout board uses a hall effect for each direction of motion (up, |
AdamGreen | 0:ad0f8a08c470 | 29 | * down, left, right) and generates pulses as the trackball is rotated in each |
AdamGreen | 0:ad0f8a08c470 | 30 | * of these directions. This class counts both the rising and falling edges |
AdamGreen | 0:ad0f8a08c470 | 31 | * of these pulses in an interrupt handler. The main program can query for |
AdamGreen | 0:ad0f8a08c470 | 32 | * the number of pulses that have been generated since the previous query was |
AdamGreen | 0:ad0f8a08c470 | 33 | * made. |
AdamGreen | 0:ad0f8a08c470 | 34 | * |
AdamGreen | 0:ad0f8a08c470 | 35 | * The breakout board also has a push button placed beneath the trackball so |
AdamGreen | 0:ad0f8a08c470 | 36 | * that user presses can be detected as well. This class will provide the |
AdamGreen | 0:ad0f8a08c470 | 37 | * state of this button as well after filtering it for the purpose of |
AdamGreen | 0:ad0f8a08c470 | 38 | * debouncing. |
AdamGreen | 0:ad0f8a08c470 | 39 | * |
AdamGreen | 0:ad0f8a08c470 | 40 | * The last feature of this breakout board that is supported by the class |
AdamGreen | 0:ad0f8a08c470 | 41 | * includes the 4 LEDs that have been placed beneath the trackball. This |
AdamGreen | 0:ad0f8a08c470 | 42 | * class allows the caller to specify an individual brightness value for each |
AdamGreen | 0:ad0f8a08c470 | 43 | * of the LEDs (red, blue, green, white). |
AdamGreen | 0:ad0f8a08c470 | 44 | * |
AdamGreen | 0:ad0f8a08c470 | 45 | * Example: |
AdamGreen | 0:ad0f8a08c470 | 46 | * @code |
AdamGreen | 0:ad0f8a08c470 | 47 | #include <mbed.h> |
AdamGreen | 0:ad0f8a08c470 | 48 | #include "BBTrackball.h" |
AdamGreen | 0:ad0f8a08c470 | 49 | |
AdamGreen | 0:ad0f8a08c470 | 50 | int main() |
AdamGreen | 0:ad0f8a08c470 | 51 | { |
AdamGreen | 0:ad0f8a08c470 | 52 | // UNDONE: Add something to this section. |
AdamGreen | 0:ad0f8a08c470 | 53 | return 0; |
AdamGreen | 0:ad0f8a08c470 | 54 | } |
AdamGreen | 0:ad0f8a08c470 | 55 | * @endcode |
AdamGreen | 0:ad0f8a08c470 | 56 | */ |
AdamGreen | 0:ad0f8a08c470 | 57 | class CBBTrackball |
AdamGreen | 0:ad0f8a08c470 | 58 | { |
AdamGreen | 0:ad0f8a08c470 | 59 | public: |
AdamGreen | 0:ad0f8a08c470 | 60 | /** A structure in which the current state of the trackball is returned |
AdamGreen | 0:ad0f8a08c470 | 61 | from the GetState() method. |
AdamGreen | 0:ad0f8a08c470 | 62 | |
AdamGreen | 0:ad0f8a08c470 | 63 | @see GetState() |
AdamGreen | 0:ad0f8a08c470 | 64 | */ |
AdamGreen | 0:ad0f8a08c470 | 65 | struct SState |
AdamGreen | 0:ad0f8a08c470 | 66 | { |
AdamGreen | 0:ad0f8a08c470 | 67 | int ButtonPressed; /**< 1 if the button is currently pressed and 0 otherwise. */ |
AdamGreen | 0:ad0f8a08c470 | 68 | short Up; /**< Number of upward pulses since last call. */ |
AdamGreen | 0:ad0f8a08c470 | 69 | short Down; /**< Number of downward pulses since last call. */ |
AdamGreen | 0:ad0f8a08c470 | 70 | short Left; /**< Number of leftward pulses since last call. */ |
AdamGreen | 0:ad0f8a08c470 | 71 | short Right; /**< Number of rightward pulses since last call. */ |
AdamGreen | 0:ad0f8a08c470 | 72 | }; |
AdamGreen | 0:ad0f8a08c470 | 73 | |
AdamGreen | 0:ad0f8a08c470 | 74 | /** |
AdamGreen | 0:ad0f8a08c470 | 75 | * Creates a CBBTrackball object. |
AdamGreen | 0:ad0f8a08c470 | 76 | * |
AdamGreen | 0:ad0f8a08c470 | 77 | * Create a CBBTrackball object, binds it to the specified input/output |
AdamGreen | 0:ad0f8a08c470 | 78 | * pins, and initializes the required interrupt handling. |
AdamGreen | 0:ad0f8a08c470 | 79 | * |
AdamGreen | 0:ad0f8a08c470 | 80 | * @param BluePin The mbed pin which is connected to the BLU pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 81 | * @param RedPin The mbed pin which is connected to the RED pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 82 | * @param GreenPin The mbed pin which is connected to the GRN pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 83 | * @param WhitePin The mbed pin which is connected to the WHT pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 84 | * @param UpPin The mbed pin which is connected to the UP pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 85 | * @param DownPin The mbed pin which is connected to the DWN pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 86 | * @param LeftPin The mbed pin which is connected to the LFT pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 87 | * @param RightPin The mbed pin which is connected to the RHT pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 88 | * @param ButtonPin The mbed pin which is connected to the BTN pin of the breakout board. |
AdamGreen | 0:ad0f8a08c470 | 89 | */ |
AdamGreen | 0:ad0f8a08c470 | 90 | CBBTrackball(PinName BluePin, |
AdamGreen | 0:ad0f8a08c470 | 91 | PinName RedPin, |
AdamGreen | 0:ad0f8a08c470 | 92 | PinName GreenPin, |
AdamGreen | 0:ad0f8a08c470 | 93 | PinName WhitePin, |
AdamGreen | 0:ad0f8a08c470 | 94 | PinName UpPin, |
AdamGreen | 0:ad0f8a08c470 | 95 | PinName DownPin, |
AdamGreen | 0:ad0f8a08c470 | 96 | PinName LeftPin, |
AdamGreen | 0:ad0f8a08c470 | 97 | PinName RightPin, |
AdamGreen | 0:ad0f8a08c470 | 98 | PinName ButtonPin) : m_UpInterrupt(UpPin), |
AdamGreen | 0:ad0f8a08c470 | 99 | m_DownInterrupt(DownPin), |
AdamGreen | 0:ad0f8a08c470 | 100 | m_LeftInterrupt(LeftPin), |
AdamGreen | 0:ad0f8a08c470 | 101 | m_RightInterrupt(RightPin), |
AdamGreen | 0:ad0f8a08c470 | 102 | m_UpCount(0), |
AdamGreen | 0:ad0f8a08c470 | 103 | m_DownCount(0), |
AdamGreen | 0:ad0f8a08c470 | 104 | m_LeftCount(0), |
AdamGreen | 0:ad0f8a08c470 | 105 | m_RightCount(0) |
AdamGreen | 0:ad0f8a08c470 | 106 | { |
AdamGreen | 0:ad0f8a08c470 | 107 | m_UpInterrupt.rise<CBBTrackball>(this, &CBBTrackball::UpISR); |
AdamGreen | 0:ad0f8a08c470 | 108 | m_DownInterrupt.rise<CBBTrackball>(this, &CBBTrackball::DownISR); |
AdamGreen | 0:ad0f8a08c470 | 109 | m_LeftInterrupt.rise<CBBTrackball>(this, &CBBTrackball::LeftISR); |
AdamGreen | 0:ad0f8a08c470 | 110 | m_RightInterrupt.rise<CBBTrackball>(this, &CBBTrackball::RightISR); |
AdamGreen | 0:ad0f8a08c470 | 111 | } |
AdamGreen | 0:ad0f8a08c470 | 112 | |
AdamGreen | 0:ad0f8a08c470 | 113 | |
AdamGreen | 0:ad0f8a08c470 | 114 | /** Gets current state. |
AdamGreen | 0:ad0f8a08c470 | 115 | * |
AdamGreen | 0:ad0f8a08c470 | 116 | * @param pState points to the structure to be filled in with the current state. |
AdamGreen | 0:ad0f8a08c470 | 117 | */ |
AdamGreen | 0:ad0f8a08c470 | 118 | void GetState(SState* pState); |
AdamGreen | 0:ad0f8a08c470 | 119 | |
AdamGreen | 0:ad0f8a08c470 | 120 | |
AdamGreen | 0:ad0f8a08c470 | 121 | protected: |
AdamGreen | 0:ad0f8a08c470 | 122 | void UpISR(void); |
AdamGreen | 0:ad0f8a08c470 | 123 | void DownISR(void); |
AdamGreen | 0:ad0f8a08c470 | 124 | void LeftISR(void); |
AdamGreen | 0:ad0f8a08c470 | 125 | void RightISR(void); |
AdamGreen | 0:ad0f8a08c470 | 126 | |
AdamGreen | 0:ad0f8a08c470 | 127 | InterruptIn m_UpInterrupt; |
AdamGreen | 0:ad0f8a08c470 | 128 | InterruptIn m_DownInterrupt; |
AdamGreen | 0:ad0f8a08c470 | 129 | InterruptIn m_LeftInterrupt; |
AdamGreen | 0:ad0f8a08c470 | 130 | InterruptIn m_RightInterrupt; |
AdamGreen | 0:ad0f8a08c470 | 131 | |
AdamGreen | 0:ad0f8a08c470 | 132 | int m_UpCount; |
AdamGreen | 0:ad0f8a08c470 | 133 | int m_DownCount; |
AdamGreen | 0:ad0f8a08c470 | 134 | int m_LeftCount; |
AdamGreen | 0:ad0f8a08c470 | 135 | int m_RightCount; |
AdamGreen | 0:ad0f8a08c470 | 136 | }; |
AdamGreen | 0:ad0f8a08c470 | 137 | |
AdamGreen | 0:ad0f8a08c470 | 138 | } // namespace AFP |
AdamGreen | 0:ad0f8a08c470 | 139 | using namespace AFP; |
AdamGreen | 0:ad0f8a08c470 | 140 | |
AdamGreen | 0:ad0f8a08c470 | 141 | #endif /* _BBTRACKBALL_H__BBTRACKBALL_H_ */ |