[Arduino] Joystick HID raw data van .cpp naar main INO

Pagina: 1
Acties:

Acties:
  • 0 Henk 'm!

  • Seesar
  • Registratie: September 2002
  • Laatst online: 06-06 13:42

Seesar

Icon thnx to l0c4lh0st

Topicstarter
M.b.v van deze source file in dit draadje: https://www.3dconnexion.c...viewtopic.php?f=19&t=5642

kan ik de raw data uitlezen van een 3dconnexion 3d muis. Deze raw data wordt via een serial.print in de .cpp file naar de serial monitor weggeschreven.

Dat werkt allemaal vlekkeloos, maar ik wil graag de raw data van de .cpp gebruiken in de .ino zodat ik deze data kan filteren en naar een standaard lcd kan sturen.

Nu is mijn kennis beperkt (ik werk nagenoeg nooit buiten de .ino zoals .h of .cpp) en ik heb een hint/richting nodig hoe ik data die in geval van een event change gemeld wordt en naar de main body gestuurd wordt zodat dat ik deze data (6 stuks) als integer kan filteren en versturen naar de LCD.

De data X, Y, & Z kunnen namelijk 2 verschillende waardes zijn (afhankelijk van ID 1 of ID 2) en ik zou ze bijvoorbeeld willen toewijzen aan integers, A t/m F.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 if(evt->ID==1) {
    Serial.print(" ");
        Serial.print(evt->X);
    Serial.print("\t ");
    Serial.print(evt->Y);
    Serial.print("\t ");
    Serial.print(evt->Z);
  }
  else if(evt->ID==2) {
    Serial.print("\t\t\t\t\t\t\t ");
    Serial.print(evt->X);
    Serial.print("\t ");
    Serial.print(evt->Y);
    Serial.print("\t ");
    Serial.print(evt->Z);
    Serial.println(" ");


M.a.w. als ik via een standaard

code:
1
2
3
4
5
6
if(evt->ID==1) {
    
  X = evt-> X;
  Y = evt-> Y;
  Z = evt-> Z;
}


en dan X, Y, & Z aanroep in de ino wordt er geen schifting gemaakt of het ID 1 of ID 2 is.

Ter volledigheid hier de originele source files:

INO

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Code by circuitsathome.com
// Modified by M. Koch 2013-07-17
// Using a SpaceNavigator (3dconnexion) with a "usb-host-shield 2.0" and arduino
// Update:  05-08-2015: extract data & send to display

#include <avr/pgmspace.h>
#include <Usb.h>
#include <usbhub.h>
#include <avr/pgmspace.h>
#include <hid.h>
#include <hiduniversal.h>
#include "hidjoystickrptparser.h"

USB                                             Usb;
USBHub                                          Hub(&Usb);
HIDUniversal                                    Hid(&Usb);
JoystickEvents                                  JoyEvents;
JoystickReportParser                            Joy(&JoyEvents);

void setup()
{
  Serial.begin( 115200 );
  Serial.println("Start");

  if (Usb.Init() == -1)
      Serial.println("OSC did not start.");
      
  delay( 200 );

  if (!Hid.SetReportParser(0, &Joy))
      ErrorMessage<uint8_t>(PSTR("SetReportParser"), 1  ); 
}

void loop()
{
    Usb.Task();
}

HIDjoystickrptparser.cpp

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Code by circuitsathome.com
// Modified by M. Koch 2013-07-17

#include "hidjoystickrptparser.h"

JoystickReportParser::JoystickReportParser(JoystickEvents *evt) : 
    joyEvents(evt),
    oldHat(0xDE),
    oldButtons(0)
{
    for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
        oldPad[i]   = 0xD; 
}

void JoystickReportParser::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
{
    bool match = true;

    // Checking if there are changes in report since the method was last called
    for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++)
        if (buf[i] != oldPad[i])
        {
            match = false;
            break;
        }

    // Calling Game Pad event handler
    if (!match && joyEvents)
    {
        joyEvents->OnGamePadChanged((const GamePadEventData*)buf);

        for (uint8_t i=0; i<RPT_GEMEPAD_LEN; i++) oldPad[i] = buf[i];
    }
    
    uint8_t hat = (buf[5] & 0xF);

    // Calling Hat Switch event handler
    if (hat != oldHat && joyEvents)
    {
        //joyEvents->OnHatSwitch(hat);
        oldHat = hat;
    }

    uint16_t buttons = (0x0000 | buf[6]);
    buttons <<= 4;
    buttons |= (buf[5] >> 4);
    uint16_t changes = (buttons ^ oldButtons);

    // Calling Button Event Handler for every button changed
    if (changes)
    {
        for (uint8_t i=0; i<0x0C; i++)
        {
            uint16_t mask = (0x0001 << i);

            //if (((mask & changes) > 0) && joyEvents)
                //if ((buttons & mask) > 0)
                    //joyEvents->OnButtonDn(i+1);
                //else
                    //joyEvents->OnButtonUp(i+1);
        }
        oldButtons = buttons;
    }
}

void JoystickEvents::OnGamePadChanged(const GamePadEventData *evt)
{
  if(evt->ID==1) {
    Serial.print(" ");
        Serial.print(evt->X);
    Serial.print("\t ");
    Serial.print(evt->Y);
    Serial.print("\t ");
    Serial.print(evt->Z);
  }
  else if(evt->ID==2) {
    Serial.print("\t\t\t\t\t\t\t ");
    Serial.print(evt->X);
    Serial.print("\t ");
    Serial.print(evt->Y);
    Serial.print("\t ");
    Serial.print(evt->Z);
    Serial.println(" ");
    }
    else if(evt->ID==3 ) {
        if(evt->X==1){
          Serial.print("Taster links");
        }
        else if(evt->X==2){
          Serial.print("Taster rechts");
        }
        else { //if(evt->X==0){
          Serial.print("kein Taster");
        }        
        Serial.println(" ");
    }
    //Serial.println("  ");
}


en de hidjoystickrptparser.h

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Code by circuitsathome.com
// Modified by M. Koch 2013-07-17

#if !defined(__HIDJOYSTICKRPTPARSER_H__)
#define __HIDJOYSTICKRPTPARSER_H__

#include <inttypes.h>
#include <avr/pgmspace.h>

#include "Usb.h"

#if defined(ARDUINO) && ARDUINO >=100
#include "Arduino.h"
#else
#include <WProgram.h>
#endif


#include "hid.h"

struct GamePadEventData
{
    byte ID;
        short X, Y, Z;
};

class JoystickEvents
{
public:
    virtual void OnGamePadChanged(const GamePadEventData *evt);
    //virtual void OnHatSwitch(uint8_t hat);
    //virtual void OnButtonUp(uint8_t but_id);
    //virtual void OnButtonDn(uint8_t but_id);
};

#define RPT_GEMEPAD_LEN     5

class JoystickReportParser : public HIDReportParser
{
    JoystickEvents              *joyEvents;
    uint8_t             oldPad[RPT_GEMEPAD_LEN];
    uint8_t             oldHat;
    uint16_t            oldButtons;

public:
    JoystickReportParser(JoystickEvents *evt);
    virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf);
};

#endif // __HIDJOYSTICKRPTPARSER_H__

T60P Widescreen