USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
PS4BT.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2014 Kristian Lauszus, TKJ Electronics. 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 Lauszus, TKJ Electronics
14  Web : http://www.tkjelectronics.com
15  e-mail : kristianl@tkjelectronics.com
16  */
17 
18 #include "PS4BT.h"
19 
20 // To enable serial debugging see "settings.h"
21 //#define PRINTREPORT // Uncomment to print the report send by the PS4 Controller
22 
24 const uint8_t PS4_BUTTONS[] PROGMEM = {
25  DPAD_UP, // UP
26  DPAD_RIGHT, // RIGHT
27  DPAD_DOWN, // DOWN
28  DPAD_LEFT, // LEFT
29 
30  0x0C, // SHARE
31  0x0D, // OPTIONS
32  0x0E, // L3
33  0x0F, // R3
34 
35  0x0A, // L2
36  0x0B, // R2
37  0x08, // L1
38  0x09, // R1
39 
40  0x07, // TRIANGLE
41  0x06, // CIRCLE
42  0x05, // CROSS
43  0x04, // SQUARE
44 
45  0x10, // PS
46  0x11, // KEYPAD
47 };
48 
50 const uint8_t PS4_ANALOG_BUTTONS[] PROGMEM = {
51  0, 0, 0, 0, 0, 0, 0, 0, // Skip UP_ANALOG, RIGHT_ANALOG, DOWN_ANALOG, LEFT_ANALOG, SELECT, L3, R3 and START
52  0, // L2_ANALOG
53  1, // R2_ANALOG
54 };
55 
56 bool PS4BT::checkDpad(PS4Buttons ps4Buttons, DPADEnum b) {
57  return ps4Buttons.dpad == b;
58 }
59 
61  uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
62  if (b < 4) // Dpad
63  return checkDpad(ps4Data.btn, (DPADEnum)button);
64  else {
65  uint8_t index = button < 8 ? 0 : button < 16 ? 1 : 2;
66  uint8_t mask = (1 << (button - 8 * index));
67  return ps4Data.btn.val[index] & mask;
68  }
69 }
70 
72  uint8_t button = pgm_read_byte(&PS4_BUTTONS[(uint8_t)b]);
73  if (b < 4) { // Dpad
74  if (checkDpad(buttonClickState, (DPADEnum)button)) {
75  buttonClickState.dpad = DPAD_OFF;
76  return true;
77  }
78  return false;
79  } else {
80  uint8_t index = button < 8 ? 0 : button < 16 ? 1 : 2;
81  uint8_t mask = (1 << (button - 8 * index));
82 
83  bool click = buttonClickState.val[index] & mask;
84  buttonClickState.val[index] &= ~mask; // Clear "click" event
85  return click;
86  }
87 }
88 
90  return (uint8_t)(ps4Data.trigger[pgm_read_byte(&PS4_ANALOG_BUTTONS[(uint8_t)a])]);
91 }
92 
94  return ps4Data.hatValue[(uint8_t)a];
95 }
96 
97 void PS4BT::Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf) {
98  if (len == sizeof(PS4Data) && buf) {
99  memcpy(&ps4Data, buf, len);
100 
101  for (uint8_t i = 0; i < sizeof(ps4Data.btn); i++) {
102  if (ps4Data.btn.val[i] != oldButtonState.val[i]) {
103  buttonClickState.val[i] = ps4Data.btn.val[i] & ~oldButtonState.val[i]; // Update click state variable
104  oldButtonState.val[i] = ps4Data.btn.val[i];
105  if (i == 0)
106  buttonClickState.dpad = ps4Data.btn.dpad; // The DPAD buttons does not set the different bits, but set a value corresponding to the buttons pressed
107  }
108  }
109 #ifdef PRINTREPORT
110  for (uint8_t i = 0; i < len; i++) {
111  D_PrintHex<uint8_t > (buf[i], 0x80);
112  Notify(PSTR(" "), 0x80);
113  }
114  Notify(PSTR("\r\n"), 0x80);
115 #endif
116  }
117 }
Definition: PS4BT.h:33
uint8_t hatValue[4]
Definition: PS4BT.h:61
Definition: PS4BT.h:60
const uint8_t PS4_ANALOG_BUTTONS[]
Definition: PS4BT.cpp:50
virtual void Parse(HID *hid, bool is_rpt_id, uint8_t len, uint8_t *buf)
Definition: PS4BT.cpp:97
AnalogHatEnum
#define Notify(...)
Definition: message.h:44
const uint8_t PS4_BUTTONS[]
Definition: PS4BT.cpp:24
bool getButtonPress(ButtonEnum b)
Definition: PS4BT.cpp:60
uint8_t getAnalogHat(AnalogHatEnum a)
Definition: PS4BT.cpp:93
uint8_t val[3]
Definition: PS4BT.h:57
uint8_t trigger[2]
Definition: PS4BT.h:63
ButtonEnum
Definition: hid.h:143
uint8_t dpad
Definition: PS4BT.h:38
Definition: PS4BT.h:25
uint8_t getAnalogButton(ButtonEnum a)
Definition: PS4BT.cpp:89
PS4Buttons btn
Definition: PS4BT.h:62
bool getButtonClick(ButtonEnum b)
Definition: PS4BT.cpp:71
DPADEnum
Definition: PS4BT.h:24