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:
Wed Sep 03 07:36:43 2014 +0000
Revision:
4:f953792e45cb
Parent:
2:a7c0fcd157f7
Added documentation

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jocis 1:e6282b645d9b 1 #include "SpaceBall.h"
jocis 1:e6282b645d9b 2
jocis 2:a7c0fcd157f7 3 //extern Serial pc;
jocis 1:e6282b645d9b 4
jocis 1:e6282b645d9b 5 ///////////////////////////////////////////////////////////////////////////////
jocis 1:e6282b645d9b 6
jocis 1:e6282b645d9b 7 static unsigned char spaceorb_xor[] = "SpaceWare";
jocis 2:a7c0fcd157f7 8 /*
jocis 1:e6282b645d9b 9 static unsigned char spaceorb_errors[][32] = {
jocis 1:e6282b645d9b 10 "EEPROM storing 0 failed", "Receive queue overflow", "Transmit queue timeout",
jocis 1:e6282b645d9b 11 "Bad packet", "Power brown-out", "EEPROM checksum error", "Hardware fault" };
jocis 2:a7c0fcd157f7 12 */
jocis 1:e6282b645d9b 13
jocis 1:e6282b645d9b 14 void SpaceBall::InitSO()
jocis 1:e6282b645d9b 15 {
jocis 2:a7c0fcd157f7 16 _fScaleT = 1.0 / 512.0;
jocis 2:a7c0fcd157f7 17 _fScaleR = 1.0 / 512.0;
jocis 1:e6282b645d9b 18 }
jocis 1:e6282b645d9b 19
jocis 1:e6282b645d9b 20 ///////////////////////////////////////////////////////////////////////////////
jocis 1:e6282b645d9b 21
jocis 1:e6282b645d9b 22 void SpaceBall::ProcessSO ( char data )
jocis 1:e6282b645d9b 23 {
jocis 1:e6282b645d9b 24 if (~data & 0x80) {
jocis 1:e6282b645d9b 25 _data[_idx] = 0;
jocis 1:e6282b645d9b 26 if (_idx)
jocis 1:e6282b645d9b 27 ProcessPacketSO();
jocis 1:e6282b645d9b 28 _idx = 0;
jocis 1:e6282b645d9b 29 }
jocis 1:e6282b645d9b 30 if (_idx < SPACEBALL_MAX_LENGTH)
jocis 1:e6282b645d9b 31 _data[_idx++] = data & 0x7f;
jocis 1:e6282b645d9b 32 }
jocis 1:e6282b645d9b 33
jocis 1:e6282b645d9b 34 ///////////////////////////////////////////////////////////////////////////////
jocis 1:e6282b645d9b 35
jocis 1:e6282b645d9b 36 void SpaceBall::ProcessPacketSO ( void )
jocis 1:e6282b645d9b 37 {
jocis 1:e6282b645d9b 38 int i;
jocis 1:e6282b645d9b 39 char c = 0;
jocis 1:e6282b645d9b 40
jocis 1:e6282b645d9b 41 if (_idx < 2) return;
jocis 1:e6282b645d9b 42 for (i = 0; i < _idx; i++) c ^= _data[i];
jocis 1:e6282b645d9b 43 if (c) return;
jocis 1:e6282b645d9b 44
jocis 1:e6282b645d9b 45 switch (_data[0]) {
jocis 1:e6282b645d9b 46
jocis 1:e6282b645d9b 47 case 'R': /* Reset packet */
jocis 1:e6282b645d9b 48 break;
jocis 1:e6282b645d9b 49
jocis 1:e6282b645d9b 50 case 'D': /* Ball + button data */
jocis 1:e6282b645d9b 51 {
jocis 1:e6282b645d9b 52 short axes[6];
jocis 1:e6282b645d9b 53
jocis 1:e6282b645d9b 54 if (_idx != 12) return;
jocis 1:e6282b645d9b 55 for (i = 0; i < 9; i++) _data[i+2] ^= spaceorb_xor[i];
jocis 1:e6282b645d9b 56 axes[0] = ( _data[2] << 3) | (_data[ 3] >> 4);
jocis 1:e6282b645d9b 57 axes[1] = ((_data[3] & 0x0f) << 6) | (_data[ 4] >> 1);
jocis 1:e6282b645d9b 58 axes[2] = ((_data[4] & 0x01) << 9) | (_data[ 5] << 2) | (_data[4] >> 5);
jocis 1:e6282b645d9b 59 axes[3] = ((_data[6] & 0x1f) << 5) | (_data[ 7] >> 2);
jocis 1:e6282b645d9b 60 axes[4] = ((_data[7] & 0x03) << 8) | (_data[ 8] << 1) | (_data[7] >> 6);
jocis 1:e6282b645d9b 61 axes[5] = ((_data[9] & 0x3f) << 4) | (_data[10] >> 3);
jocis 1:e6282b645d9b 62 for (i = 0; i < 6; i++)
jocis 2:a7c0fcd157f7 63 _axis[i] = axes[i] - ((axes[i] & 0x200) ? 1024 : 0);
jocis 2:a7c0fcd157f7 64 _buttons = _data[1];
jocis 1:e6282b645d9b 65 DoChangedAxis();
jocis 1:e6282b645d9b 66 DoChangedButtons();
jocis 1:e6282b645d9b 67 }
jocis 1:e6282b645d9b 68 break;
jocis 1:e6282b645d9b 69
jocis 1:e6282b645d9b 70 case 'K': /* Button data */
jocis 1:e6282b645d9b 71 if (_idx != 5) return;
jocis 2:a7c0fcd157f7 72 _buttons = _data[2];
jocis 1:e6282b645d9b 73 DoChangedButtons();
jocis 1:e6282b645d9b 74 break;
jocis 1:e6282b645d9b 75
jocis 1:e6282b645d9b 76 case 'E': /* Error packet */
jocis 1:e6282b645d9b 77 if (_idx != 4) return;
jocis 2:a7c0fcd157f7 78 //printf(KERN_ERR "joy-spaceorb: Device error. [ ");
jocis 2:a7c0fcd157f7 79 //for (i = 0; i < 7; i++) if (_data[1] & (1 << i)) printf("%s ", spaceorb_errors[i]);
jocis 2:a7c0fcd157f7 80 //printf("]\n");
jocis 1:e6282b645d9b 81 break;
jocis 1:e6282b645d9b 82
jocis 2:a7c0fcd157f7 83 default:
jocis 1:e6282b645d9b 84 /* eat and ignore these packets */
jocis 2:a7c0fcd157f7 85 //pc.printf("<%s>\r\n",_data);
jocis 1:e6282b645d9b 86 break;
jocis 1:e6282b645d9b 87 }
jocis 1:e6282b645d9b 88 }
jocis 1:e6282b645d9b 89
jocis 1:e6282b645d9b 90 ///////////////////////////////////////////////////////////////////////////////
jocis 1:e6282b645d9b 91 ///////////////////////////////////////////////////////////////////////////////
jocis 1:e6282b645d9b 92 ///////////////////////////////////////////////////////////////////////////////