forked from felis/USB_Host_Shield_2.0
-
Notifications
You must be signed in to change notification settings - Fork 6
/
XBOXONESParser.h
129 lines (106 loc) · 4.32 KB
/
XBOXONESParser.h
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* Copyright (C) 2020 Kristian Sloth Lauszus. All rights reserved.
This software may be distributed and modified under the terms of the GNU
General Public License version 2 (GPL2) as published by the Free Software
Foundation and appearing in the file GPL2.TXT included in the packaging of
this file. Please note that GPL2 Section 2[b] requires that all works based
on this software must also be made publicly available under the terms of
the GPL2 ("Copyleft").
Contact information
-------------------
Kristian Sloth Lauszus
Web : https://lauszus.com
e-mail : [email protected]
*/
#ifndef _xboxonesparser_h_
#define _xboxonesparser_h_
#include "Usb.h"
#include "controllerEnums.h"
union XboxOneSButtons {
struct {
uint8_t dpad : 4;
uint8_t reserved : 4;
uint8_t a : 1;
uint8_t b : 1;
uint8_t x : 1;
uint8_t y : 1;
uint8_t l1 : 1;
uint8_t r1 : 1;
uint8_t view : 1;
uint8_t menu : 1;
uint8_t l3 : 1;
uint8_t r3 : 1;
uint8_t reserved2 : 6;
} __attribute__((packed));
uint32_t val : 24;
} __attribute__((packed));
struct XboxOneSData {
/* Button and joystick values */
uint16_t hatValue[4];
uint16_t trigger[2];
XboxOneSButtons btn;
} __attribute__((packed));
/** This class parses all the data sent by the Xbox One S controller */
class XBOXONESParser {
public:
/** Constructor for the XBOXONESParser class. */
XBOXONESParser() {
Reset();
};
/** @name Xbox One S Controller functions */
/**
* getButtonPress(ButtonEnum b) will return true as long as the button is held down.
*
* While getButtonClick(ButtonEnum b) will only return it once.
*
* So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
* @param b ::ButtonEnum to read.
* @return getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press.
*/
uint16_t getButtonPress(ButtonEnum b);
bool getButtonClick(ButtonEnum b);
/**@}*/
/**
* Used to read the analog joystick.
* @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
* @return Return the analog value as a 16-bit signed integer.
*/
int16_t getAnalogHat(AnalogHatEnum a);
/** Used to set the rumble off. */
void setRumbleOff();
/**
* Used to turn on rumble continuously.
* @param leftTrigger Left trigger force.
* @param rightTrigger Right trigger force.
* @param leftMotor Left motor force.
* @param rightMotor Right motor force.
*/
void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor);
protected:
/**
* Used to parse data sent from the Xbox One S controller.
* @param len Length of the data.
* @param buf Pointer to the data buffer.
*/
void Parse(uint8_t len, uint8_t *buf);
/** Used to reset the different buffers to their default values */
void Reset();
/**
* Send the output to the Xbox One S controller. This is implemented in XBOXONESBT.h.
* @param output Pointer to data buffer.
* @param nbytes Bytes to send.
*/
virtual void sendOutputReport(uint8_t *data, uint8_t nbytes) = 0;
private:
static int8_t getButtonIndexXboxOneS(ButtonEnum b);
bool checkDpad(ButtonEnum b); // Used to check Xbox One S DPAD buttons
XboxOneSData xboxOneSData;
XboxOneSButtons oldButtonState, buttonClickState;
uint8_t oldDpad;
// The Xbox button is sent in a separate report
uint8_t xboxButtonState, xboxOldButtonState, xboxbuttonClickState;
uint16_t triggerOld[2];
bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
bool R2Clicked;
};
#endif