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
--- a/SB.cpp	Sat Dec 01 18:23:50 2012 +0000
+++ b/SB.cpp	Sat Dec 01 21:38:49 2012 +0000
@@ -1,13 +1,11 @@
 #include "SpaceBall.h"
 
-#define Sleep(x) wait_ms(x*10)
+#define Sleep(x) wait_ms(x)
 
-extern Serial pc;
+//extern Serial pc;
 
 ///////////////////////////////////////////////////////////////////////////////
 
-
-
 /*
     Convert characters to nibbles
 */
@@ -42,16 +40,15 @@
 
 void SpaceBall::InitSB()
 {
-    m_packlen=1;
     m_resetoccured=0;
 
-   m_spaceball4000 = 0; /* re-determine which type it is     */
+    m_spaceball4000 = 0; /* re-determine which type it is     */
     m_leftymode4000 = 0; /* re-determine if its in lefty mode */
 
+    _fScaleT = 1.0 / 5000.0;
+    _fScaleR = 1.0 / 2000.0;
+
     if (!m_resetoccured) {
-
-       // pc.printf("Sending reset command to spaceball...\n");
-
         m_resetoccured=1;
         _serial.printf ( "@\r" ); /* force reset */
         }
@@ -132,7 +129,7 @@
         case 'D':                                       /* Ball data */
             if (_idx != 15) return;
             for (i = 0; i < 6; i++) {
-                m_axis[i] = (short)( (_data[2*i + 3] << 8) | _data[2*i + 2] );
+                _axis[i] = (short)( (_data[2*i + 3] << 8) | _data[2*i + 2] );
             }
             DoChangedAxis();
             break;
@@ -141,7 +138,7 @@
         //pc.printf("d%d",_idx);
             if (_idx != 25) return;
             for (i = 0; i < 6; i++) {
-                m_axis[i] = (short)( (
+                _axis[i] = (short)( (
                     DecodeSM(_data[4*i+4]) | (DecodeSM(_data[4*i+3])<<4) | (DecodeSM(_data[4*i+2])<<8) | (DecodeSM(_data[4*i+1])<<12) )
                     ^ 0x8000 );
             }
@@ -166,7 +163,7 @@
             if (m_spaceball4000)
                 break;
 
-            m_buttons =
+            _buttons =
                 ((_data[1] & 0x10) <<  8) | /* 2003 pick button is ??? */
                 ((_data[1] & 0x20) <<  9) | /* 3003 rezero button      */
                 ((_data[1] & 0x08) << 11) | /* 2003 rezero button      */
@@ -174,25 +171,14 @@
                 ((_data[2] & 0x30) <<  8) | /* 3003 Left/Right buttons */
                 ((_data[2] & 0x0F));        /* 1,2,3,4  (2003/4000)    */
 
-            //bChanged = true;
             DoChangedButtons();
-            /*
-                        input_report_key(dev, BTN_1, (data[2] & 0x01) || (data[2] & 0x20));
-                        input_report_key(dev, BTN_2, data[2] & 0x02);
-                        input_report_key(dev, BTN_3, data[2] & 0x04);
-                        input_report_key(dev, BTN_4, data[2] & 0x08);
-                        input_report_key(dev, BTN_5, data[1] & 0x01);
-                        input_report_key(dev, BTN_6, data[1] & 0x02);
-                        input_report_key(dev, BTN_7, data[1] & 0x04);
-                        input_report_key(dev, BTN_8, data[1] & 0x10);
-                        */
             break;
 
         case 'k':                                       /* Button data */
         //pc.printf("k%d",_idx);
-        pc.printf("<%s>\r\n",_data);
+        //pc.printf("<%s>\r\n",_data);
             if (_idx != 4) return;
-            m_buttons = DecodeSM(_data[1]) | (DecodeSM(_data[2])<<4) | (DecodeSM(_data[3])<<8);
+            _buttons = DecodeSM(_data[1]) | (DecodeSM(_data[2])<<4) | (DecodeSM(_data[3])<<8);
 
             DoChangedButtons();
             break;
@@ -210,7 +196,7 @@
 
             /* Spaceball 4000 series "expanded" button press event      */
             /* includes data for 12 buttons, and left/right orientation */
-            m_buttons =
+            _buttons =
                 (((~_data[1]) & 0x20) << 10) |  /* "left handed" mode  */
                 ((_data[1] & 0x1F) <<  7)    |  /* 8,9,10,11,12        */
                 ((_data[2] & 0x3F)      )    |  /* 1,2,3,4,5,6 (4000)  */
@@ -222,23 +208,7 @@
             else
                 m_leftymode4000 = 0; /* right handed mode */
 
-            //bChanged = true;
             DoChangedButtons();
-            /*
-            input_report_key(dev, BTN_1, data[2] & 0x01);
-            input_report_key(dev, BTN_2, data[2] & 0x02);
-            input_report_key(dev, BTN_3, data[2] & 0x04);
-            input_report_key(dev, BTN_4, data[2] & 0x08);
-            input_report_key(dev, BTN_5, data[2] & 0x10);
-            input_report_key(dev, BTN_6, data[2] & 0x20);
-            input_report_key(dev, BTN_7, data[2] & 0x80);
-            input_report_key(dev, BTN_8, data[1] & 0x01);
-            input_report_key(dev, BTN_9, data[1] & 0x02);
-            input_report_key(dev, BTN_A, data[1] & 0x04);
-            input_report_key(dev, BTN_B, data[1] & 0x08);
-            input_report_key(dev, BTN_C, data[1] & 0x10);
-            input_report_key(dev, BTN_MODE, data[1] & 0x20);
-            */
             break;
 
         case 'E':                                       /* Device error */
@@ -266,7 +236,6 @@
             /* the device, and assume that its completely schizophrenic */
             /* at this moment, we must reset it again at this point     */
             m_resetoccured=1;
-            //JKReset();
             //Init();
             break;
 
@@ -281,11 +250,9 @@
             break;
             
         default:
-            pc.printf("<%s>\r\n",_data);
+            //pc.printf("<%s>\r\n",_data);
             break;
-
     }
-
 }
 
 ///////////////////////////////////////////////////////////////////////////////