├── README.md ├── USBHIDSpaceNavigator.ino ├── hidspacenavigatorrptparser.cpp └── hidspacenavigatorrptparser.h /README.md: -------------------------------------------------------------------------------- 1 | # 3D-Mouse-Arduino 2 | Examples of using a 3D Mouse with an Arduino 3 | -------------------------------------------------------------------------------- /USBHIDSpaceNavigator.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | // Satisfy IDE, which only needs to see the include statment in the ino. 6 | #ifdef dobogusinclude 7 | #include 8 | #include 9 | #endif 10 | 11 | #include "hidspacenavigatorrptparser.h" 12 | 13 | USB Usb; 14 | USBHub Hub(&Usb); 15 | HIDUniversal Hid(&Usb); 16 | SpaceNavigatorEvent SpaceNavigatorEvent; 17 | SpaceNavigatorReportParser SpaceNavigator(&SpaceNavigatorEvent); 18 | 19 | void setup() { 20 | Serial.begin(115200); // set the serial monitor at 115200 too 21 | #if !defined(__MIPSEL__) 22 | while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection 23 | #endif 24 | Serial.println("Start"); 25 | 26 | if (Usb.Init() == -1) 27 | Serial.println("OSC did not start."); 28 | 29 | delay(200); 30 | 31 | if (!Hid.SetReportParser(0, &SpaceNavigator)) 32 | ErrorMessage (PSTR("SetReportParser"), 1); 33 | } 34 | 35 | void loop() { 36 | Usb.Task(); 37 | } 38 | -------------------------------------------------------------------------------- /hidspacenavigatorrptparser.cpp: -------------------------------------------------------------------------------- 1 | #include "hidspacenavigatorrptparser.h" 2 | 3 | SpaceNavigatorReportParser::SpaceNavigatorReportParser(SpaceNavigatorEvent *evt) : 4 | spaceNavigatorEvent(evt) 5 | { 6 | } 7 | 8 | void SpaceNavigatorReportParser::Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) { 9 | 10 | // Calling SpaceNavigator event handler 11 | spaceNavigatorEvent->OnSpaceNavigatorChanged((const SpaceNavigatorEventData*)buf); 12 | } 13 | 14 | void SpaceNavigatorEvent::OnSpaceNavigatorChanged(const SpaceNavigatorEventData *evt) { 15 | // Serial.print("report ID: "); 16 | // PrintHex (evt->rptid, 0x80); 17 | // Serial.print("\txl: "); 18 | // PrintHex (evt->xl, 0x80); 19 | // Serial.print("\txh: "); 20 | // PrintHex (evt->xh, 0x80); 21 | // Serial.print("\tyl: "); 22 | // PrintHex (evt->yl, 0x80); 23 | // Serial.print("\tyh: "); 24 | // PrintHex (evt->yh, 0x80); 25 | // Serial.print("\tzl: "); 26 | // PrintHex (evt->zl, 0x80); 27 | // Serial.print("\tzh: "); 28 | // PrintHex (evt->zh, 0x80); 29 | 30 | // Translation vector 31 | if (evt->rptid == 1) { 32 | Tx = evt->xl+(evt->xh<<8); 33 | Ty = evt->yl+(evt->yh<<8); 34 | Tz = evt->zl+(evt->zh<<8); 35 | } 36 | 37 | // Rotation vector 38 | else if (evt->rptid == 2) { 39 | Rx = evt->xl+(evt->xh<<8); 40 | Ry = evt->yl+(evt->yh<<8); 41 | Rz = evt->zl+(evt->zh<<8); 42 | } 43 | 44 | // Buttons 45 | else if (evt->rptid == 3) { 46 | } 47 | 48 | Serial.print("\tT:( "); 49 | Serial.print(Tx); 50 | Serial.print(" , "); 51 | Serial.print(Ty); 52 | Serial.print(" , "); 53 | Serial.print(Tz); 54 | Serial.print(" )"); 55 | 56 | Serial.print("\tR:( "); 57 | Serial.print(Rx); 58 | Serial.print(" , "); 59 | Serial.print(Ry); 60 | Serial.print(" , "); 61 | Serial.print(Rz); 62 | Serial.print(" )"); 63 | 64 | Serial.println(""); 65 | } 66 | 67 | void SpaceNavigatorEvent::OnButton(uint8_t but_id) { 68 | Serial.print("Button: "); 69 | Serial.println(but_id, DEC); 70 | } 71 | 72 | -------------------------------------------------------------------------------- /hidspacenavigatorrptparser.h: -------------------------------------------------------------------------------- 1 | #if !defined(__HIDSPACENAVIGATORRPTPARSER_H__) 2 | #define __HIDSPACENAVIGATORRPTPARSER_H__ 3 | 4 | #include 5 | 6 | struct SpaceNavigatorEventData { 7 | uint8_t rptid, xl, xh, yl, yh, zl, zh; 8 | }; 9 | 10 | class SpaceNavigatorEvent { 11 | public: 12 | SpaceNavigatorEvent() { Tx=Ty=Tz=Rx=Ry=Rz=0; }; 13 | virtual void OnSpaceNavigatorChanged(const SpaceNavigatorEventData *evt); 14 | virtual void OnButton(uint8_t but_id); 15 | 16 | int16_t Tx, Ty, Tz, Rx, Ry, Rz; 17 | }; 18 | 19 | class SpaceNavigatorReportParser : public HIDReportParser { 20 | SpaceNavigatorEvent *spaceNavigatorEvent; 21 | 22 | public: 23 | SpaceNavigatorReportParser(SpaceNavigatorEvent *evt); 24 | 25 | virtual void Parse(USBHID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf); 26 | }; 27 | 28 | #endif // __HIDSPACENAVIGATORRPTPARSER_H__ 29 | --------------------------------------------------------------------------------