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

Committer:
jocis
Date:
Sat Dec 01 18:23:50 2012 +0000
Revision:
1:e6282b645d9b
Parent:
0:f67a8fffd94a
Child:
2:a7c0fcd157f7
Implemented SpaceMouse and SpaceOrb

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jocis 0:f67a8fffd94a 1 #include "SpaceBall.h"
jocis 0:f67a8fffd94a 2
jocis 0:f67a8fffd94a 3 #define Sleep(x) wait_ms(x*10)
jocis 1:e6282b645d9b 4 extern Serial pc;
jocis 0:f67a8fffd94a 5
jocis 0:f67a8fffd94a 6 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 7
jocis 0:f67a8fffd94a 8 SpaceBall::SpaceBall ( PinName tx, PinName rx, bool bSpaceOrb ) : _serial ( tx, rx )
jocis 0:f67a8fffd94a 9 {
jocis 0:f67a8fffd94a 10 _bSpaceOrb = bSpaceOrb;
jocis 0:f67a8fffd94a 11 _serial.baud ( 9600 );
jocis 0:f67a8fffd94a 12
jocis 0:f67a8fffd94a 13 _fScale = 1.0f;
jocis 0:f67a8fffd94a 14
jocis 0:f67a8fffd94a 15 m_axis[0] = 0; /* last translational data received */
jocis 0:f67a8fffd94a 16 m_axis[1] = 0;
jocis 0:f67a8fffd94a 17 m_axis[2] = 0;
jocis 0:f67a8fffd94a 18 m_axis[3] = 0; /* last rotational data received */
jocis 0:f67a8fffd94a 19 m_axis[4] = 0;
jocis 0:f67a8fffd94a 20 m_axis[5] = 0;
jocis 0:f67a8fffd94a 21 m_buttons = 0; /* current button status */
jocis 0:f67a8fffd94a 22
jocis 1:e6282b645d9b 23 _data[0] = 0;
jocis 0:f67a8fffd94a 24 m_resetstring[0]= 0;
jocis 0:f67a8fffd94a 25 m_bufpos = 0; /* current char position in packet buffer */
jocis 0:f67a8fffd94a 26 m_packtype = 0; /* what kind of packet is it */
jocis 0:f67a8fffd94a 27 m_packlen = 0; /* how many bytes do we ultimately expect? */
jocis 0:f67a8fffd94a 28 m_escapedchar = 0; /* if set, we're processing an escape sequence */
jocis 0:f67a8fffd94a 29 m_erroroccured = 0; /* if set, we've received an error packet or packets */
jocis 0:f67a8fffd94a 30 m_resetoccured = 0; /* if set, ball was reset, so have to reinitialize it */
jocis 0:f67a8fffd94a 31 m_spaceball4000 = 0; /* if set, its a Spaceball 4000 */
jocis 0:f67a8fffd94a 32 m_leftymode4000 = 0; /* if set, Spaceball 4000 in "lefty" orientation */
jocis 0:f67a8fffd94a 33 m_timer = 0; /* time since last packet was received */
jocis 1:e6282b645d9b 34
jocis 1:e6282b645d9b 35
jocis 1:e6282b645d9b 36 _escape = false;
jocis 1:e6282b645d9b 37 _idx = 0;
jocis 1:e6282b645d9b 38
jocis 0:f67a8fffd94a 39 _serial.attach ( this, &SpaceBall::SerialISR );
jocis 0:f67a8fffd94a 40
jocis 1:e6282b645d9b 41 //Init();
jocis 0:f67a8fffd94a 42
jocis 0:f67a8fffd94a 43 };
jocis 0:f67a8fffd94a 44
jocis 0:f67a8fffd94a 45 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 46
jocis 0:f67a8fffd94a 47 void SpaceBall::Init()
jocis 0:f67a8fffd94a 48 {
jocis 1:e6282b645d9b 49 if ( _bSpaceOrb )
jocis 1:e6282b645d9b 50 InitSO ();
jocis 1:e6282b645d9b 51 else
jocis 1:e6282b645d9b 52 InitSB ();
jocis 0:f67a8fffd94a 53 }
jocis 0:f67a8fffd94a 54
jocis 0:f67a8fffd94a 55 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 56
jocis 0:f67a8fffd94a 57 void SpaceBall::SerialISR(void)
jocis 0:f67a8fffd94a 58 {
jocis 0:f67a8fffd94a 59 char c;
jocis 0:f67a8fffd94a 60
jocis 0:f67a8fffd94a 61 while ( _serial.readable() )
jocis 0:f67a8fffd94a 62 {
jocis 0:f67a8fffd94a 63 c = _serial.getc();
jocis 0:f67a8fffd94a 64
jocis 0:f67a8fffd94a 65 //printf("%c", c);
jocis 0:f67a8fffd94a 66
jocis 0:f67a8fffd94a 67 Process ( c );
jocis 0:f67a8fffd94a 68 }
jocis 0:f67a8fffd94a 69 }
jocis 0:f67a8fffd94a 70
jocis 0:f67a8fffd94a 71 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 72
jocis 0:f67a8fffd94a 73 void SpaceBall::Process ( char c )
jocis 0:f67a8fffd94a 74 {
jocis 0:f67a8fffd94a 75 if ( _bSpaceOrb )
jocis 0:f67a8fffd94a 76 ProcessSO ( c );
jocis 0:f67a8fffd94a 77 else
jocis 0:f67a8fffd94a 78 ProcessSB ( c );
jocis 0:f67a8fffd94a 79 }
jocis 0:f67a8fffd94a 80
jocis 0:f67a8fffd94a 81 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 82 ///////////////////////////////////////////////////////////////////////////////
jocis 0:f67a8fffd94a 83 ///////////////////////////////////////////////////////////////////////////////