OpenMoCo / SpaceBall

Dependents:   SpaceBall_Example

Committer:
jocis
Date:
Tue Sep 02 09:34:06 2014 +0000
Revision:
3:7bacae57a30b
Parent:
2:a7c0fcd157f7
Child:
4:f953792e45cb
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jocis 3:7bacae57a30b 1 /* mbed SpaceBall, SpaceMouse and SpaceOrb Library
jocis 3:7bacae57a30b 2 * Copyright (c) 2012 Jochen Krapf
jocis 3:7bacae57a30b 3 *
jocis 3:7bacae57a30b 4 * Permission is hereby granted, free of charge, to any person obtaining a copy
jocis 3:7bacae57a30b 5 * of this software and associated documentation files (the "Software"), to deal
jocis 3:7bacae57a30b 6 * in the Software without restriction, including without limitation the rights
jocis 3:7bacae57a30b 7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jocis 3:7bacae57a30b 8 * copies of the Software, and to permit persons to whom the Software is
jocis 3:7bacae57a30b 9 * furnished to do so, subject to the following conditions:
jocis 3:7bacae57a30b 10 *
jocis 3:7bacae57a30b 11 * The above copyright notice and this permission notice shall be included in
jocis 3:7bacae57a30b 12 * all copies or substantial portions of the Software.
jocis 3:7bacae57a30b 13 *
jocis 3:7bacae57a30b 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jocis 3:7bacae57a30b 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jocis 3:7bacae57a30b 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jocis 3:7bacae57a30b 17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jocis 3:7bacae57a30b 18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jocis 3:7bacae57a30b 19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jocis 3:7bacae57a30b 20 * THE SOFTWARE.
jocis 3:7bacae57a30b 21 */
jocis 3:7bacae57a30b 22
jocis 0:f67a8fffd94a 23 #include "mbed.h"
jocis 0:f67a8fffd94a 24
jocis 0:f67a8fffd94a 25 /* Spaceball Button bit-masks */
jocis 0:f67a8fffd94a 26 #define SBALL_BUTTON_1 0x0001 /* bit 0 */
jocis 0:f67a8fffd94a 27 #define SBALL_BUTTON_2 0x0002 /* bit 1 */
jocis 0:f67a8fffd94a 28 #define SBALL_BUTTON_3 0x0004 /* bit 2 */
jocis 0:f67a8fffd94a 29 #define SBALL_BUTTON_4 0x0008 /* bit 3 */
jocis 0:f67a8fffd94a 30 #define SBALL_BUTTON_5 0x0010 /* bit 4 */
jocis 0:f67a8fffd94a 31 #define SBALL_BUTTON_6 0x0020 /* bit 5 */
jocis 0:f67a8fffd94a 32 #define SBALL_BUTTON_7 0x0040 /* bit 6 */
jocis 0:f67a8fffd94a 33 #define SBALL_BUTTON_8 0x0080 /* bit 7 */
jocis 0:f67a8fffd94a 34 #define SBALL_BUTTON_9 0x0100 /* bit 8 */
jocis 0:f67a8fffd94a 35 #define SBALL_BUTTON_10 0x0200 /* bit 9 */
jocis 0:f67a8fffd94a 36 #define SBALL_BUTTON_11 0x0400 /* bit 10 */
jocis 0:f67a8fffd94a 37 #define SBALL_BUTTON_12 0x0800 /* bit 11 */
jocis 0:f67a8fffd94a 38
jocis 0:f67a8fffd94a 39 /* The Spaceball 3003 and 3003 FLX only have "left" and "right" buttons */
jocis 0:f67a8fffd94a 40 #define SBALL_BUTTON_RIGHT 0x1000 /* bit 12 */
jocis 0:f67a8fffd94a 41 #define SBALL_BUTTON_LEFT 0x2000 /* bit 13 */
jocis 0:f67a8fffd94a 42
jocis 0:f67a8fffd94a 43 /* The Spaceball 2003A and 2003B have a dedicated pick button on the ball */
jocis 0:f67a8fffd94a 44 /* The Spaceball 2003 FLX uses "button 9" as the pick button. */
jocis 0:f67a8fffd94a 45 /* All of them return this as "button 9" in their encoded button data */
jocis 0:f67a8fffd94a 46 #define SBALL_BUTTON_PICK SBALL_BUTTON_RIGHT /* bit 12 */
jocis 0:f67a8fffd94a 47
jocis 0:f67a8fffd94a 48 /* On Spaceball 2003A and 2003B, the Rezero is "button 8" on the device */
jocis 0:f67a8fffd94a 49 /* On the newer devices, there are dedicated rezero buttons */
jocis 0:f67a8fffd94a 50 #define SBALL_BUTTON_REZERO 0x4000 /* bit 14 */
jocis 0:f67a8fffd94a 51
jocis 0:f67a8fffd94a 52 /* The Spaceball 4000 FLX has a configurable palm rest which can be in */
jocis 0:f67a8fffd94a 53 /* either "left" or "right" handed mode. When it is configured in "left" */
jocis 0:f67a8fffd94a 54 /* handed mode, the "lefty" bit is set, and coordinate systems need to be */
jocis 0:f67a8fffd94a 55 /* inverted on one axis. */
jocis 0:f67a8fffd94a 56 #define SBALL_MODE_LEFTY 0x8000 /* bit 15 */
jocis 0:f67a8fffd94a 57
jocis 0:f67a8fffd94a 58
jocis 1:e6282b645d9b 59 #define SPACEBALL_MAX_LENGTH 128
jocis 1:e6282b645d9b 60
jocis 0:f67a8fffd94a 61 enum eSpaceBallAxis {
jocis 0:f67a8fffd94a 62 TX=0, TY, TZ,
jocis 0:f67a8fffd94a 63 Right=0, Forward, Up,
jocis 0:f67a8fffd94a 64 RX=3, RY, RZ,
jocis 0:f67a8fffd94a 65 Pitch=3, Roll, Yaw };
jocis 0:f67a8fffd94a 66
jocis 2:a7c0fcd157f7 67 enum eSpaceBallMapping {
jocis 2:a7c0fcd157f7 68 RAW=0, CNC, CNCvert };
jocis 2:a7c0fcd157f7 69
jocis 3:7bacae57a30b 70 /** SpaceBall class, based on serial connection
jocis 3:7bacae57a30b 71 *
jocis 3:7bacae57a30b 72 * Example:
jocis 3:7bacae57a30b 73 * @code
jocis 3:7bacae57a30b 74 * @endcode
jocis 3:7bacae57a30b 75 */
jocis 0:f67a8fffd94a 76 class SpaceBall {
jocis 0:f67a8fffd94a 77 public:
jocis 0:f67a8fffd94a 78
jocis 3:7bacae57a30b 79 /** Create a input object connected to the specified serial pin
jocis 3:7bacae57a30b 80 *
jocis 3:7bacae57a30b 81 * @param pin PwmOut pin to connect to
jocis 3:7bacae57a30b 82 */
jocis 0:f67a8fffd94a 83 SpaceBall ( PinName tx, PinName rx, bool bSpaceOrb=false );
jocis 0:f67a8fffd94a 84 ~SpaceBall() {};
jocis 0:f67a8fffd94a 85
jocis 3:7bacae57a30b 86 /** ...
jocis 3:7bacae57a30b 87 *
jocis 3:7bacae57a30b 88 * @param xxx ...
jocis 3:7bacae57a30b 89 */
jocis 0:f67a8fffd94a 90 void Init();
jocis 0:f67a8fffd94a 91
jocis 2:a7c0fcd157f7 92 void SetMapping ( int mapping )
jocis 2:a7c0fcd157f7 93 { _mapping = mapping; }
jocis 2:a7c0fcd157f7 94
jocis 0:f67a8fffd94a 95 void GetTranslation ( float* fValue[3] ) const
jocis 0:f67a8fffd94a 96 {
jocis 0:f67a8fffd94a 97 *fValue[0] = GetAxis ( TX );
jocis 0:f67a8fffd94a 98 *fValue[1] = GetAxis ( TY );
jocis 0:f67a8fffd94a 99 *fValue[2] = GetAxis ( TZ );
jocis 0:f67a8fffd94a 100 }
jocis 2:a7c0fcd157f7 101
jocis 0:f67a8fffd94a 102 void GetRotation ( float* fValue[3] ) const
jocis 0:f67a8fffd94a 103 {
jocis 0:f67a8fffd94a 104 *fValue[0] = GetAxis ( RX );
jocis 0:f67a8fffd94a 105 *fValue[1] = GetAxis ( RY );
jocis 0:f67a8fffd94a 106 *fValue[2] = GetAxis ( RZ );
jocis 0:f67a8fffd94a 107 }
jocis 2:a7c0fcd157f7 108
jocis 2:a7c0fcd157f7 109 float GetAxis ( int nAxis ) const;
jocis 2:a7c0fcd157f7 110
jocis 2:a7c0fcd157f7 111 int GetAxisRaw ( int nAxis ) const;
jocis 2:a7c0fcd157f7 112
jocis 0:f67a8fffd94a 113 float operator[] (eSpaceBallAxis nAxis) const
jocis 0:f67a8fffd94a 114 { return GetAxis ( nAxis ); }
jocis 0:f67a8fffd94a 115 float operator[] (int nAxis) const
jocis 0:f67a8fffd94a 116 { return GetAxis ( nAxis ); }
jocis 0:f67a8fffd94a 117
jocis 0:f67a8fffd94a 118 int GetButtons() const
jocis 2:a7c0fcd157f7 119 { return _buttons; }
jocis 2:a7c0fcd157f7 120
jocis 0:f67a8fffd94a 121 operator int (void) const
jocis 0:f67a8fffd94a 122 { return GetButtons(); }
jocis 0:f67a8fffd94a 123
jocis 0:f67a8fffd94a 124 void SetScale ( float fScale=1.0f )
jocis 0:f67a8fffd94a 125 { _fScale = fScale; }
jocis 0:f67a8fffd94a 126 float GetScale ( void )
jocis 0:f67a8fffd94a 127 { return _fScale; }
jocis 0:f67a8fffd94a 128
jocis 0:f67a8fffd94a 129
jocis 0:f67a8fffd94a 130 protected:
jocis 0:f67a8fffd94a 131 Serial _serial;
jocis 0:f67a8fffd94a 132 bool _bSpaceOrb;
jocis 0:f67a8fffd94a 133 float _fScale;
jocis 0:f67a8fffd94a 134
jocis 2:a7c0fcd157f7 135 int _axis[6]; /* last translational data received */
jocis 2:a7c0fcd157f7 136 int _buttons; /* current button status */
jocis 0:f67a8fffd94a 137
jocis 0:f67a8fffd94a 138 void SerialISR(void);
jocis 0:f67a8fffd94a 139 void Process ( char c );
jocis 0:f67a8fffd94a 140
jocis 0:f67a8fffd94a 141 virtual void DoChangedAxis (void) {};
jocis 0:f67a8fffd94a 142 virtual void DoChangedButtons (void) {};
jocis 0:f67a8fffd94a 143
jocis 0:f67a8fffd94a 144 private:
jocis 0:f67a8fffd94a 145 int m_erroroccured; /* if set, we've received an error packet or packets */
jocis 0:f67a8fffd94a 146 int m_resetoccured; /* if set, ball was reset, so have to reinitialize it */
jocis 0:f67a8fffd94a 147 int m_spaceball4000; /* if set, its a Spaceball 4000 */
jocis 0:f67a8fffd94a 148 int m_leftymode4000; /* if set, Spaceball 4000 in "lefty" orientation */
jocis 0:f67a8fffd94a 149
jocis 1:e6282b645d9b 150 bool _escape;
jocis 1:e6282b645d9b 151 int _idx;
jocis 1:e6282b645d9b 152 char _data[SPACEBALL_MAX_LENGTH];
jocis 2:a7c0fcd157f7 153
jocis 2:a7c0fcd157f7 154 float _fScaleR;
jocis 2:a7c0fcd157f7 155 float _fScaleT;
jocis 2:a7c0fcd157f7 156 int _mapping;
jocis 1:e6282b645d9b 157
jocis 1:e6282b645d9b 158 void InitSB();
jocis 1:e6282b645d9b 159 void InitSO();
jocis 0:f67a8fffd94a 160
jocis 0:f67a8fffd94a 161 void ProcessSB ( char c );
jocis 0:f67a8fffd94a 162 void ProcessSO ( char c );
jocis 1:e6282b645d9b 163
jocis 1:e6282b645d9b 164 void ProcessPacketSB ( void );
jocis 1:e6282b645d9b 165 void ProcessPacketSO ( void );
jocis 1:e6282b645d9b 166
jocis 0:f67a8fffd94a 167 };