USB Host Shield 2.0
XBOXONESParser.h
Go to the documentation of this file.
1 /* Copyright (C) 2020 Kristian Sloth Lauszus. All rights reserved.
2 
3  This software may be distributed and modified under the terms of the GNU
4  General Public License version 2 (GPL2) as published by the Free Software
5  Foundation and appearing in the file GPL2.TXT included in the packaging of
6  this file. Please note that GPL2 Section 2[b] requires that all works based
7  on this software must also be made publicly available under the terms of
8  the GPL2 ("Copyleft").
9 
10  Contact information
11  -------------------
12 
13  Kristian Sloth Lauszus
14  Web : https://lauszus.com
15  e-mail : lauszus@gmail.com
16  */
17 
18 #ifndef _xboxonesparser_h_
19 #define _xboxonesparser_h_
20 
21 #include "Usb.h"
22 #include "controllerEnums.h"
23 
25  struct {
26  uint8_t dpad : 4;
27  uint8_t reserved : 4;
28 
29  uint8_t a : 1;
30  uint8_t b : 1;
31  uint8_t x : 1;
32  uint8_t y : 1;
33 
34  uint8_t l1 : 1;
35  uint8_t r1 : 1;
36  uint8_t view : 1;
37  uint8_t menu : 1;
38 
39  uint8_t l3 : 1;
40  uint8_t r3 : 1;
41  uint8_t reserved2 : 6;
42  } __attribute__((packed));
43  uint32_t val : 24;
44 } __attribute__((packed));
45 
46 struct XboxOneSData {
47  /* Button and joystick values */
48  uint16_t hatValue[4];
49  uint16_t trigger[2];
51 } __attribute__((packed));
52 
55 public:
58  Reset();
59  };
60 
72  uint16_t getButtonPress(ButtonEnum b);
73  bool getButtonClick(ButtonEnum b);
81  int16_t getAnalogHat(AnalogHatEnum a);
82 
84  void setRumbleOff();
85 
93  void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor);
94 
95 protected:
101  void Parse(uint8_t len, uint8_t *buf);
102 
104  void Reset();
105 
111  virtual void sendOutputReport(uint8_t *data, uint8_t nbytes) = 0;
112 
113 private:
114  static int8_t getButtonIndexXboxOneS(ButtonEnum b);
115 
116  bool checkDpad(ButtonEnum b); // Used to check Xbox One S DPAD buttons
117 
118  XboxOneSData xboxOneSData;
119  XboxOneSButtons oldButtonState, buttonClickState;
120  uint8_t oldDpad;
121 
122  // The Xbox button is sent in a separate report
123  uint8_t xboxButtonState, xboxOldButtonState, xboxbuttonClickState;
124 
125  uint16_t triggerOld[2];
126  bool L2Clicked; // These buttons are analog, so we use we use these bools to check if they where clicked or not
127  bool R2Clicked;
128 };
129 #endif
void setRumbleOn(uint8_t leftTrigger, uint8_t rightTrigger, uint8_t leftMotor, uint8_t rightMotor)
virtual void sendOutputReport(uint8_t *data, uint8_t nbytes)=0
int16_t getAnalogHat(AnalogHatEnum a)
void Parse(uint8_t len, uint8_t *buf)
uint16_t getButtonPress(ButtonEnum b)
bool getButtonClick(ButtonEnum b)
AnalogHatEnum
ButtonEnum
uint16_t hatValue[4]
uint16_t trigger[2]
XboxOneSButtons btn