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

Dependents:   BBTrackball_Sample

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BBTrackball.cpp Source File

BBTrackball.cpp

00001 /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/)
00002 
00003    Licensed under the Apache License, Version 2.0 (the "License");
00004    you may not use this file except in compliance with the License.
00005    You may obtain a copy of the License at
00006 
00007        http://www.apache.org/licenses/LICENSE-2.0
00008 
00009    Unless required by applicable law or agreed to in writing, software
00010    distributed under the License is distributed on an "AS IS" BASIS,
00011    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012    See the License for the specific language governing permissions and
00013    limitations under the License.
00014 */
00015 /* Implementation of class to control Sparkfun's Blackberry Trackball:
00016      http://www.sparkfun.com/products/9320
00017 */
00018 #include <limits.h>
00019 #include "BBTrackball.h"
00020 
00021 
00022 
00023 // Public methods.
00024 void CBBTrackball::GetState(SState* pState)
00025 {
00026     pState->ButtonPressed = !m_ButtonState;
00027     pState->Up = UpCount();
00028     pState->Down = DownCount();
00029     pState->Left = LeftCount();
00030     pState->Right = RightCount();
00031 }
00032 
00033 
00034 void CBBTrackball::SetColour(const SColour* pColour)
00035 {
00036     m_BluePWM.write((float)pColour->Blue / 255.0f);
00037     m_RedPWM.write((float)pColour->Red / 255.0f);
00038     m_GreenPWM.write((float)pColour->Green / 255.0f);
00039     m_WhitePWM.write((float)pColour->White / 255.0f);
00040 }        
00041 
00042 
00043 
00044 // Protected methods.
00045 void CBBTrackball::UpISR(void)
00046 {
00047     m_UpCount++;
00048 }
00049 
00050 void CBBTrackball::DownISR(void)
00051 {
00052     m_DownCount++;
00053 }
00054 
00055 void CBBTrackball::LeftISR(void)
00056 {
00057     m_LeftCount++;
00058 }
00059 
00060 void CBBTrackball::RightISR(void)
00061 {
00062     m_RightCount++;
00063 }
00064 
00065 void CBBTrackball::ButtonSampleISR(void)
00066 {
00067     int ButtonSample = m_Button.read();
00068     
00069     if (ButtonSample != m_ButtonState)
00070     {
00071         // The button appears to be transition to a new state. Don't actually
00072         // switch to the new state until enough samples have been seen to
00073         // reduce button bounce.
00074         if (m_ButtonNewStateCount++ > 5)
00075         {
00076             m_ButtonState = ButtonSample;
00077             m_ButtonNewStateCount = 0;
00078         }
00079     }
00080     else
00081     {
00082         m_ButtonNewStateCount = 0;
00083     }
00084 }
00085 
00086 short CBBTrackball::ThresholdToShort(unsigned int Value)
00087 {
00088     if (Value > SHRT_MAX)
00089     {
00090         Value = SHRT_MAX;
00091     }
00092     
00093     return (short)Value;
00094 }
00095 
00096 short CBBTrackball::UpCount(void)
00097 {
00098     unsigned int Count;
00099     
00100     Count = m_UpCount;
00101     m_UpCount = 0;
00102 
00103     return ThresholdToShort(Count);
00104 }
00105 
00106 short CBBTrackball::DownCount(void)
00107 {
00108     unsigned int Count;
00109     
00110     Count = m_DownCount;
00111     m_DownCount = 0;
00112 
00113     return ThresholdToShort(Count);
00114 }
00115 
00116 short CBBTrackball::LeftCount(void)
00117 {
00118     unsigned int Count;
00119     
00120     Count = m_LeftCount;
00121     m_LeftCount = 0;
00122 
00123     return ThresholdToShort(Count);
00124 }
00125 
00126 short CBBTrackball::RightCount(void)
00127 {
00128     unsigned int Count;
00129     
00130     Count = m_RightCount;
00131     m_RightCount = 0;
00132 
00133     return ThresholdToShort(Count);
00134 }