Library to handle SpaceBall, SpaceMouse and SpaceOrb on serial port. Gets access to 3D rotation and translation vector as well as button status. (USB is not supported)

Dependents:   SpaceBall_Example

Library to handle SpaceBall, SpaceMouse and SpaceOrb on serial port. Gets access to 3D rotation and translation vector as well as button status. (USB is not supported)

All handling and decoding is done in the RX interrupt and the vector values can be read out asynchronously with different coordinate mappings.

Example:

#include "mbed.h"
#include "SpaceBall.h"
 
PwmOut led[] = {(LED1), (LED2), (LED3), (LED4) };
SpaceBall SBall(p9, p10);   // tx, rx, bSOrb
 
int main() {
    SBall.Init();
    
    while(1) {
 
        led[0] = abs( SBall[TX] ) + abs( SBall[TY] ) + abs( SBall[TZ] );
        led[1] = abs( SBall[RX] );
        led[2] = abs( SBall[RY] );
        led[3] = abs( SBall[RZ] );
        
        wait_us(500);
    }
}

In this exaple the 4 LEDs are powered dependent on force at the Spaceball. LED1 shows the sum of all translation forces. LED2 to LED4 shows the rotation forces.

For more information about SpaceBall devices see manufactorers page http://www.3dconnexion.com

For connecting a SpaceBall (or SpaceMouse or SpaceOrb) to mbed see page wiki/Serial-Connection

Example: SpaceBall 4000

/media/uploads/jocis/spaceball1.jpg

Revision:
2:a7c0fcd157f7
Parent:
1:e6282b645d9b
Child:
3:7bacae57a30b
--- a/SpaceBall.cpp	Sat Dec 01 18:23:50 2012 +0000
+++ b/SpaceBall.cpp	Sat Dec 01 21:38:49 2012 +0000
@@ -12,25 +12,19 @@
     
     _fScale = 1.0f;
 
-    m_axis[0]       = 0;      /* last translational data received */
-    m_axis[1]       = 0;
-    m_axis[2]       = 0;
-    m_axis[3]       = 0;        /* last rotational data received */
-    m_axis[4]       = 0;
-    m_axis[5]       = 0;
-    m_buttons       = 0;       /* current button status */
+    _axis[0]       = 0;      /* last translational data received */
+    _axis[1]       = 0;
+    _axis[2]       = 0;
+    _axis[3]       = 0;        /* last rotational data received */
+    _axis[4]       = 0;
+    _axis[5]       = 0;
+    _buttons       = 0;       /* current button status */
 
     _data[0]        = 0;
-    m_resetstring[0]= 0; 
-    m_bufpos        = 0;        /* current char position in packet buffer */
-    m_packtype      = 0;      /* what kind of packet is it */
-    m_packlen       = 0;       /* how many bytes do we ultimately expect? */
-    m_escapedchar   = 0;   /* if set, we're processing an escape sequence */
     m_erroroccured  = 0;  /* if set, we've received an error packet or packets */
     m_resetoccured  = 0;  /* if set, ball was reset, so have to reinitialize it */
     m_spaceball4000 = 0; /* if set, its a Spaceball 4000 */
     m_leftymode4000 = 0; /* if set, Spaceball 4000 in "lefty" orientation */
-    m_timer         = 0;         /* time since last packet was received */
 
 
     _escape = false;
@@ -62,8 +56,6 @@
     {
         c = _serial.getc();
         
-        //printf("%c", c);
-        
         Process ( c );
     }
 }
@@ -79,5 +71,57 @@
 }
 
 ///////////////////////////////////////////////////////////////////////////////
+
+float SpaceBall::GetAxis ( int nAxis ) const
+{
+    float fValue = 0;
+    
+    if ( _mapping == CNC )
+    {
+        if      ( nAxis == TX ) fValue = _axis[0];
+        else if ( nAxis == TY ) fValue = _axis[2];
+        else if ( nAxis == TZ ) fValue = _axis[1];
+        else if ( nAxis == RX ) fValue = -_axis[0];
+        else if ( nAxis == RY ) fValue = -_axis[2];
+        else if ( nAxis == RZ ) fValue = -_axis[1];
+    }
+    else if ( _mapping == CNCvert )
+    {
+        if      ( nAxis == TX ) fValue = _axis[0];
+        else if ( nAxis == TY ) fValue = _axis[1];
+        else if ( nAxis == TZ ) fValue = -_axis[2];
+        else if ( nAxis == RX ) fValue = -_axis[0];
+        else if ( nAxis == RY ) fValue = -_axis[1];
+        else if ( nAxis == RZ ) fValue = -_axis[2];
+    }
+    else
+    {
+        if      ( nAxis == TX ) fValue = _axis[0];
+        else if ( nAxis == TY ) fValue = _axis[1];
+        else if ( nAxis == TZ ) fValue = _axis[2];
+        else if ( nAxis == RX ) fValue = _axis[0];
+        else if ( nAxis == RY ) fValue = _axis[1];
+        else if ( nAxis == RZ ) fValue = _axis[2];
+    }
+    
+    if ( nAxis <= TZ )
+        fValue *= _fScaleT * _fScale;
+    else
+        fValue *= _fScaleR * _fScale;
+        
+    return fValue;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+int SpaceBall::GetAxisRaw ( int nAxis ) const
+    {
+        if ( nAxis<0 || nAxis>5 )
+            return 0;
+        
+        return _axis[nAxis];
+    }
+    
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
+///////////////////////////////////////////////////////////////////////////////