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

SpaceBall.cpp

Committer:
jocis
Date:
2012-12-01
Revision:
1:e6282b645d9b
Parent:
0:f67a8fffd94a
Child:
2:a7c0fcd157f7

File content as of revision 1:e6282b645d9b:

#include "SpaceBall.h"

#define Sleep(x) wait_ms(x*10)
extern Serial pc;

///////////////////////////////////////////////////////////////////////////////

SpaceBall::SpaceBall ( PinName tx, PinName rx, bool bSpaceOrb ) : _serial ( tx, rx )
{
    _bSpaceOrb = bSpaceOrb;
    _serial.baud ( 9600 );
    
    _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 */

    _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;
    _idx = 0;
    
    _serial.attach ( this, &SpaceBall::SerialISR );

    //Init();  
    
};

///////////////////////////////////////////////////////////////////////////////

void SpaceBall::Init()
{
    if ( _bSpaceOrb )
        InitSO ();
    else
        InitSB ();
}

///////////////////////////////////////////////////////////////////////////////

void SpaceBall::SerialISR(void)
{
    char c;
    
    while ( _serial.readable() )
    {
        c = _serial.getc();
        
        //printf("%c", c);
        
        Process ( c );
    }
}

///////////////////////////////////////////////////////////////////////////////

void SpaceBall::Process ( char c )
{
    if ( _bSpaceOrb )
        ProcessSO ( c );
    else
        ProcessSB ( c );
}

///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////