Use a Sparkfun Blackberry Trackball Breakout Board as a mouse.
Dependencies: mbed BBTrackball
acceleration.cpp@0:26bb60735515, 2011-12-11 (annotated)
- Committer:
- AdamGreen
- Date:
- Sun Dec 11 05:38:54 2011 +0000
- Revision:
- 0:26bb60735515
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AdamGreen | 0:26bb60735515 | 1 | /* Copyright 2011 Adam Green (http://mbed.org/users/AdamGreen/) |
AdamGreen | 0:26bb60735515 | 2 | |
AdamGreen | 0:26bb60735515 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); |
AdamGreen | 0:26bb60735515 | 4 | you may not use this file except in compliance with the License. |
AdamGreen | 0:26bb60735515 | 5 | You may obtain a copy of the License at |
AdamGreen | 0:26bb60735515 | 6 | |
AdamGreen | 0:26bb60735515 | 7 | http://www.apache.org/licenses/LICENSE-2.0 |
AdamGreen | 0:26bb60735515 | 8 | |
AdamGreen | 0:26bb60735515 | 9 | Unless required by applicable law or agreed to in writing, software |
AdamGreen | 0:26bb60735515 | 10 | distributed under the License is distributed on an "AS IS" BASIS, |
AdamGreen | 0:26bb60735515 | 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
AdamGreen | 0:26bb60735515 | 12 | See the License for the specific language governing permissions and |
AdamGreen | 0:26bb60735515 | 13 | limitations under the License. |
AdamGreen | 0:26bb60735515 | 14 | */ |
AdamGreen | 0:26bb60735515 | 15 | /* Implementation of classes to apply acceleration to input from |
AdamGreen | 0:26bb60735515 | 16 | Sparkfun's Blackberry Trackball Breakout board. |
AdamGreen | 0:26bb60735515 | 17 | */ |
AdamGreen | 0:26bb60735515 | 18 | #include <mbed.h> |
AdamGreen | 0:26bb60735515 | 19 | #include "acceleration.h" |
AdamGreen | 0:26bb60735515 | 20 | |
AdamGreen | 0:26bb60735515 | 21 | #define ARRAYSIZE(X) (sizeof(X)/sizeof(X[0])) |
AdamGreen | 0:26bb60735515 | 22 | |
AdamGreen | 0:26bb60735515 | 23 | |
AdamGreen | 0:26bb60735515 | 24 | template<size_t SampleCount, |
AdamGreen | 0:26bb60735515 | 25 | int DecelerateThreshold, |
AdamGreen | 0:26bb60735515 | 26 | int AccelerateThreshold, |
AdamGreen | 0:26bb60735515 | 27 | int MaxAcceleration> |
AdamGreen | 0:26bb60735515 | 28 | unsigned char CAcceleratedRoller<SampleCount, |
AdamGreen | 0:26bb60735515 | 29 | DecelerateThreshold, |
AdamGreen | 0:26bb60735515 | 30 | AccelerateThreshold, |
AdamGreen | 0:26bb60735515 | 31 | MaxAcceleration>::ClampToUChar(short Value) |
AdamGreen | 0:26bb60735515 | 32 | { |
AdamGreen | 0:26bb60735515 | 33 | if (Value < 0) |
AdamGreen | 0:26bb60735515 | 34 | { |
AdamGreen | 0:26bb60735515 | 35 | Value = 0; |
AdamGreen | 0:26bb60735515 | 36 | } |
AdamGreen | 0:26bb60735515 | 37 | else if (Value > 0xFF) |
AdamGreen | 0:26bb60735515 | 38 | { |
AdamGreen | 0:26bb60735515 | 39 | Value = 0xFF; |
AdamGreen | 0:26bb60735515 | 40 | } |
AdamGreen | 0:26bb60735515 | 41 | |
AdamGreen | 0:26bb60735515 | 42 | return Value; |
AdamGreen | 0:26bb60735515 | 43 | } |
AdamGreen | 0:26bb60735515 | 44 | |
AdamGreen | 0:26bb60735515 | 45 | |
AdamGreen | 0:26bb60735515 | 46 | template<size_t SampleCount, |
AdamGreen | 0:26bb60735515 | 47 | int DecelerateThreshold, |
AdamGreen | 0:26bb60735515 | 48 | int AccelerateThreshold, |
AdamGreen | 0:26bb60735515 | 49 | int MaxAcceleration> |
AdamGreen | 0:26bb60735515 | 50 | void CAcceleratedRoller<SampleCount, |
AdamGreen | 0:26bb60735515 | 51 | DecelerateThreshold, |
AdamGreen | 0:26bb60735515 | 52 | AccelerateThreshold, |
AdamGreen | 0:26bb60735515 | 53 | MaxAcceleration>::AccumulateSample(short CurrVelocity) |
AdamGreen | 0:26bb60735515 | 54 | { |
AdamGreen | 0:26bb60735515 | 55 | unsigned char ClampedVelocity = ClampToUChar(CurrVelocity); |
AdamGreen | 0:26bb60735515 | 56 | |
AdamGreen | 0:26bb60735515 | 57 | // Calculate a running sum over the last SampleCount velocity samples. |
AdamGreen | 0:26bb60735515 | 58 | m_VelocitySum -= m_Samples[m_SampleIndex]; |
AdamGreen | 0:26bb60735515 | 59 | m_VelocitySum += ClampedVelocity; |
AdamGreen | 0:26bb60735515 | 60 | m_Samples[m_SampleIndex] = ClampedVelocity; |
AdamGreen | 0:26bb60735515 | 61 | |
AdamGreen | 0:26bb60735515 | 62 | m_SampleIndex++; |
AdamGreen | 0:26bb60735515 | 63 | if (m_SampleIndex >= SampleCount) |
AdamGreen | 0:26bb60735515 | 64 | { |
AdamGreen | 0:26bb60735515 | 65 | m_SampleIndex = 0; |
AdamGreen | 0:26bb60735515 | 66 | } |
AdamGreen | 0:26bb60735515 | 67 | |
AdamGreen | 0:26bb60735515 | 68 | // As we see a sustained high velocity, increase acceleration and decrease |
AdamGreen | 0:26bb60735515 | 69 | // as the user slows down the velocity. |
AdamGreen | 0:26bb60735515 | 70 | if (m_VelocitySum > AccelerateThreshold && m_Acceleration < MaxAcceleration) |
AdamGreen | 0:26bb60735515 | 71 | { |
AdamGreen | 0:26bb60735515 | 72 | m_Acceleration++; |
AdamGreen | 0:26bb60735515 | 73 | } |
AdamGreen | 0:26bb60735515 | 74 | if (m_VelocitySum < DecelerateThreshold && m_Acceleration > 1) |
AdamGreen | 0:26bb60735515 | 75 | { |
AdamGreen | 0:26bb60735515 | 76 | m_Acceleration--; |
AdamGreen | 0:26bb60735515 | 77 | } |
AdamGreen | 0:26bb60735515 | 78 | } |
AdamGreen | 0:26bb60735515 | 79 | |
AdamGreen | 0:26bb60735515 | 80 | |
AdamGreen | 0:26bb60735515 | 81 | |
AdamGreen | 0:26bb60735515 | 82 | void CAcceleratedTrackball::UpdateLEDColourBasedOnMotion(int VelocityX, |
AdamGreen | 0:26bb60735515 | 83 | int VelocityY, |
AdamGreen | 0:26bb60735515 | 84 | int ButtonPressed) |
AdamGreen | 0:26bb60735515 | 85 | { |
AdamGreen | 0:26bb60735515 | 86 | static const CBBTrackball::SColour RedColour = {255, 0, 0, 0}; |
AdamGreen | 0:26bb60735515 | 87 | static const CBBTrackball::SColour GreenColour = { 0, 255, 0, 0}; |
AdamGreen | 0:26bb60735515 | 88 | static const CBBTrackball::SColour BlueColour = { 0, 0, 255, 0}; |
AdamGreen | 0:26bb60735515 | 89 | |
AdamGreen | 0:26bb60735515 | 90 | // If the trackball is being moved then make it green, otherwise leave it |
AdamGreen | 0:26bb60735515 | 91 | // red. |
AdamGreen | 0:26bb60735515 | 92 | if (VelocityX || VelocityY) |
AdamGreen | 0:26bb60735515 | 93 | { |
AdamGreen | 0:26bb60735515 | 94 | m_IterationsSinceLastMotion = 0; |
AdamGreen | 0:26bb60735515 | 95 | SetColour(&GreenColour); |
AdamGreen | 0:26bb60735515 | 96 | } |
AdamGreen | 0:26bb60735515 | 97 | else |
AdamGreen | 0:26bb60735515 | 98 | { |
AdamGreen | 0:26bb60735515 | 99 | if (m_IterationsSinceLastMotion > 250) |
AdamGreen | 0:26bb60735515 | 100 | { |
AdamGreen | 0:26bb60735515 | 101 | SetColour(&RedColour); |
AdamGreen | 0:26bb60735515 | 102 | } |
AdamGreen | 0:26bb60735515 | 103 | else |
AdamGreen | 0:26bb60735515 | 104 | { |
AdamGreen | 0:26bb60735515 | 105 | m_IterationsSinceLastMotion++; |
AdamGreen | 0:26bb60735515 | 106 | } |
AdamGreen | 0:26bb60735515 | 107 | } |
AdamGreen | 0:26bb60735515 | 108 | |
AdamGreen | 0:26bb60735515 | 109 | // Switch the button to blue when the button is pressed. |
AdamGreen | 0:26bb60735515 | 110 | if (ButtonPressed) |
AdamGreen | 0:26bb60735515 | 111 | { |
AdamGreen | 0:26bb60735515 | 112 | SetColour(&BlueColour); |
AdamGreen | 0:26bb60735515 | 113 | } |
AdamGreen | 0:26bb60735515 | 114 | } |
AdamGreen | 0:26bb60735515 | 115 | |
AdamGreen | 0:26bb60735515 | 116 | |
AdamGreen | 0:26bb60735515 | 117 | void CAcceleratedTrackball::GetState(int& DeltaX, int& DeltaY, int& ButtonPressed) |
AdamGreen | 0:26bb60735515 | 118 | { |
AdamGreen | 0:26bb60735515 | 119 | CBBTrackball::SState TrackballState; |
AdamGreen | 0:26bb60735515 | 120 | short VelocityX; |
AdamGreen | 0:26bb60735515 | 121 | short VelocityY; |
AdamGreen | 0:26bb60735515 | 122 | |
AdamGreen | 0:26bb60735515 | 123 | CBBTrackball::GetState(&TrackballState); |
AdamGreen | 0:26bb60735515 | 124 | |
AdamGreen | 0:26bb60735515 | 125 | m_UpAcceleration.AccumulateSample(TrackballState.Up); |
AdamGreen | 0:26bb60735515 | 126 | m_DownAcceleration.AccumulateSample(TrackballState.Down); |
AdamGreen | 0:26bb60735515 | 127 | m_LeftAcceleration.AccumulateSample(TrackballState.Left); |
AdamGreen | 0:26bb60735515 | 128 | m_RightAcceleration.AccumulateSample(TrackballState.Right); |
AdamGreen | 0:26bb60735515 | 129 | |
AdamGreen | 0:26bb60735515 | 130 | // NOTE: The breakout board is rotated 90 degrees on my breadboard. |
AdamGreen | 0:26bb60735515 | 131 | VelocityX = TrackballState.Up * m_UpAcceleration.Acceleration() - |
AdamGreen | 0:26bb60735515 | 132 | TrackballState.Down * m_DownAcceleration.Acceleration(); |
AdamGreen | 0:26bb60735515 | 133 | VelocityY = TrackballState.Right * m_RightAcceleration.Acceleration() - |
AdamGreen | 0:26bb60735515 | 134 | TrackballState.Left * m_LeftAcceleration.Acceleration(); |
AdamGreen | 0:26bb60735515 | 135 | |
AdamGreen | 0:26bb60735515 | 136 | UpdateLEDColourBasedOnMotion(VelocityX, VelocityY, TrackballState.ButtonPressed); |
AdamGreen | 0:26bb60735515 | 137 | |
AdamGreen | 0:26bb60735515 | 138 | DeltaX = VelocityX; |
AdamGreen | 0:26bb60735515 | 139 | DeltaY = VelocityY; |
AdamGreen | 0:26bb60735515 | 140 | ButtonPressed = TrackballState.ButtonPressed; |
AdamGreen | 0:26bb60735515 | 141 | } |