From 807adf1efd26d72fa7df67d3dcae200f298f7669 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Fri, 22 May 2020 22:40:14 -0500 Subject: [PATCH 01/14] Adds xbox chatpad to reciever, not tested --- XBOXChatpadEnums.h | 79 ++++++++++++++++++++++++ XBOXRECV.cpp | 150 +++++++++++++++++++++++++++++++++++++++++++++ XBOXRECV.h | 30 +++++++++ 3 files changed, 259 insertions(+) create mode 100644 XBOXChatpadEnums.h diff --git a/XBOXChatpadEnums.h b/XBOXChatpadEnums.h new file mode 100644 index 000000000..fdb31c040 --- /dev/null +++ b/XBOXChatpadEnums.h @@ -0,0 +1,79 @@ +/* Copyright (C) 2020 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"). + + Based on the project from here: https://github.com/Kytech/xbox360wirelesschatpad + */ + +#ifndef _xbox_chatpad_enums_h_ +#define _xbox_chatpad_enums_h_ + +enum ChatpadModiferEnum { + MODIFER_GREENBUTTON = 0, + MODIFER_ORANGEBUTTON = 1, + MODIFER_CAPSLOCK = 2, + MODIFER_SHIFT = 3, + MODIFER_MESSENGER = 4 +}; + +/** Buttons on the chatpad + * This only accounts for the main keys and does not + * use the modifiers (orange/green) + */ +enum ChatpadButtonEnum { + XBOX_CHATPAD_D7 = 0, + XBOX_CHATPAD_D6 = 1, + XBOX_CHATPAD_D5 = 2, + XBOX_CHATPAD_D4 = 3, + XBOX_CHATPAD_D3 = 4, + XBOX_CHATPAD_D2 = 5, + XBOX_CHATPAD_D1 = 6, + + XBOX_CHATPAD_U = 7, + XBOX_CHATPAD_Y = 8, + XBOX_CHATPAD_T = 9, + XBOX_CHATPAD_R = 10, + XBOX_CHATPAD_E = 11, + XBOX_CHATPAD_W = 12, + XBOX_CHATPAD_Q = 13, + + XBOX_CHATPAD_J = 14, + XBOX_CHATPAD_H = 15, + XBOX_CHATPAD_G = 16, + XBOX_CHATPAD_F = 17, + XBOX_CHATPAD_D = 18, + XBOX_CHATPAD_S = 19, + XBOX_CHATPAD_A = 20, + + XBOX_CHATPAD_M = 21, + XBOX_CHATPAD_N = 22, + XBOX_CHATPAD_B = 23, + XBOX_CHATPAD_V = 24, + XBOX_CHATPAD_C = 25, + XBOX_CHATPAD_X = 26, + XBOX_CHATPAD_Z = 27, + + XBOX_CHATPAD_RIGHT = 28, + XBOX_CHATPAD_SPACE = 29, + XBOX_CHATPAD_LEFT = 30, + + XBOX_CHATPAD_COMMA = 31, + XBOX_CHATPAD_ENTER = 32, + XBOX_CHATPAD_P = 33, + XBOX_CHATPAD_D0 = 34, + XBOX_CHATPAD_D9 = 35, + XBOX_CHATPAD_D8 = 36, + + XBOX_CHATPAD_BACK = 37, + XBOX_CHATPAD_L = 38, + XBOX_CHATPAD_O = 39, + XBOX_CHATPAD_I = 40, + XBOX_CHATPAD_K = 41 +}; + +#endif diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 031713d43..8c51185d7 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -298,6 +298,8 @@ uint8_t XBOXRECV::Poll() { checkStatus(); } + sendChatpadInitIfNeeded(); + uint8_t inputPipe; uint16_t bufferSize; for(uint8_t i = 0; i < 4; i++) { @@ -364,6 +366,11 @@ void XBOXRECV::readReport(uint8_t controller) { controllerStatus[controller] = ((uint16_t)readBuf[3] << 8) | readBuf[4]; return; } + if(readBuf[1] == 0x02) { + // Chatpad data + processChatpadData(controller, readBuf); + return; + } if(readBuf[1] != 0x01) // Check if it's the correct report - the receiver also sends different status reports return; @@ -407,6 +414,100 @@ void XBOXRECV::printReport(uint8_t controller __attribute__((unused)), uint8_t n #endif } + +void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { + // This function is called anytime received data is identified as chatpad data + // It will parse the data, and depending on the value, send a keyboard command, + // adjust a modifier for later use, flag initialization, or note the LED status. + if (dataPacket[24] == 0xF0) { + if (dataPacket[25] == 0x03) { + // This data represents handshake request, flag keep-alive to send + // chatpad initialization data. + chatpadInitNeeded[controller] = true; + } + else if (dataPacket[25] == 0x04) { + // This data represents the LED status. Not used because unsure of workings + //chatpadLED["Green"] = (dataPacket[26] & 0x08) > 0; + //chatpadLED["Orange"] = (dataPacket[26] & 0x10) > 0; + //chatpadLED["Messenger"] = (dataPacket[26] & 0x01) > 0; + //chatpadLED["Capslock"] = (dataPacket[26] & 0x20) > 0; + //Backlight = (dataPacket[26] & 0x80) > 0; + } + else { + // TODO: How to deal with invalid data? + return; + } + } + else if (dataPacket[24] == 0x00) { + // This data represents a key-press event + // Check if anything has changed since the last dataPacket + bool dataChanged = false; + if (!firstChatpadRun) { + if (chatpadDataPacketLast[controller][0] != dataPacket[25]) { + dataChanged = true; + } + else if (chatpadDataPacketLast[controller][1] != dataPacket[26]) { + dataChanged = true; + } + else if (chatpadDataPacketLast[controller][2] != dataPacket[27]) { + dataChanged = true; + } + } + else { + firstChatpadRun = false; + dataChanged = true; + } + + // Store bits 25-27 of the data packet for later comparison + chatpadDataPacketLast[controller][0] = dataPacket[25]; + chatpadDataPacketLast[controller][1] = dataPacket[26]; + chatpadDataPacketLast[controller][2] = dataPacket[27]; + + if (dataChanged) + { + // Record the Modifier Statuses + chatpadMod[controller][(int)MODIFER_GREENBUTTON] = (dataPacket[25] & 0x02) > 0; + chatpadMod[controller][(int)MODIFER_ORANGEBUTTON] = (dataPacket[25] & 0x04) > 0; + chatpadMod[controller][(int)MODIFER_SHIFT] = (dataPacket[25] & 0x01) > 0; + chatpadMod[controller][(int)MODIFER_MESSENGER] = (dataPacket[25] & 0x08) > 0; + + // Toggle Capslock Modifier based on Orange and Shift Modifiers + if (chatpadMod[controller][(int)MODIFER_ORANGEBUTTON] && chatpadMod[controller][(int)MODIFER_SHIFT]) + chatpadMod[controller][(int)MODIFER_CAPSLOCK] = !chatpadMod[controller][(int)MODIFER_CAPSLOCK]; + + + // Set the Upper-Case flag and Shift Key status based on the + // XOR of Shift and Capslock Modifiers. + flagUpperCase = chatpadMod[controller][(int)MODIFER_SHIFT] ^ chatpadMod[controller][(int)MODIFER_CAPSLOCK]; + + // Process the two different possible keys that could be held down + ProcessChatpadKeypress(controller, dataPacket[26]); + ProcessChatpadKeypress(controller, dataPacket[27]); + + if(chatpadState[controller] != chatpadOldState[controller]) { + chatpadStateChanged[controller] = true; + chatpadClickState[controller] = (chatpadState[controller]) & ((~chatpadOldState[controller])); // Update click state variable + chatpadOldState[controller] = chatpadState[controller]; + } + } + } + else { + // TODO: Handle invalid data + } +} + +void XBOXRECV::ProcessChatpadKeypress(uint8_t controller, uint8_t value) { + value = value - 17; + value = (value & 0x70 >> 1) | (value & 0x07); + + if (value > 50) { + // Invalid button + return; + } + + chatpadState[controller] |= (1 << value); +} + uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) { if(b == L2) // These are analog buttons return (uint8_t)(ButtonState[controller] >> 8); @@ -445,6 +546,23 @@ bool XBOXRECV::buttonChanged(uint8_t controller) { return state; } +bool XBOXRECV::chatpadChanged(uint8_t controller) { + bool state = chatpadStateChanged[controller]; + chatpadStateChanged[controller] = false; + return state; +} + +uint8_t XBOXRECV::getChatpadPress(ChatpadButtonEnum b, uint8_t controller) { + return (bool)(chatpadState[controller] & ((uint64_t)(1 << b))); +} + +bool XBOXRECV::getChatpadClick(ChatpadButtonEnum b, uint8_t controller) { + uint64_t mask = ((uint64_t)(1 << b)); + bool click = (chatpadClickState[controller] & mask); + chatpadClickState[controller] &= ~mask; // clear "click" event + return click; +} + /* ControllerStatus Breakdown ControllerStatus[controller] & 0x0001 // 0 @@ -524,6 +642,22 @@ void XBOXRECV::setLedBlink(LEDEnum led, uint8_t controller) { setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]), controller); } + +void XBOXRECV::setChatpadLed(uint8_t ledNumber, bool value, uint8_t controller) { + uint8_t ledOnByte[4] = {0x08, 0x09, 0x0A, 0x0B}; + uint8_t ledOffByte[4] = {0x00, 0x01, 0x02, 0x03}; + writeBuf[0] = 0x00; + writeBuf[1] = 0x00; + writeBuf[2] = 0x0C; + + if (value) { + writeBuf[3] = 0x08 + ledNumber; + } else { + writeBuf[3] = ledNumber; + } + XboxCommand(controller, writeBuf, 4); +} + void XBOXRECV::setLedMode(LEDModeEnum ledMode, uint8_t controller) { // This function is used to do some speciel LED stuff the controller supports setLedRaw((uint8_t)ledMode, controller); } @@ -554,6 +688,22 @@ void XBOXRECV::checkStatus() { } } +void XBOXRECV::sendChatpadInitIfNeeded() { + // ChatpadInit + writeBuf[0] = 0x00; + writeBuf[1] = 0x00; + writeBuf[2] = 0x0C; + writeBuf[3] = 0x1B; + for(uint8_t i = 0; i < 4; i++) { + if(chatpadInitNeeded[i]) { + if (Xbox360Connected[i]) { + XboxCommand(i, writeBuf, 4); + } + chatpadInitNeeded[i] = false; + } + } +} + void XBOXRECV::setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller) { writeBuf[0] = 0x00; writeBuf[1] = 0x01; diff --git a/XBOXRECV.h b/XBOXRECV.h index 4f9214653..31dde9bb5 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -22,6 +22,7 @@ #include "Usb.h" #include "xboxEnums.h" +#include "XBOXChatpadEnums.h" /* Data Xbox 360 taken from descriptors */ #define EP_MAXPKTSIZE 32 // max size for data via USB @@ -223,6 +224,17 @@ class XBOXRECV : public USBDeviceConfig { void attachOnInit(void (*funcOnInit)(void)) { pFuncOnInit = funcOnInit; }; + + uint8_t getChatpadPress(ChatpadButtonEnum b, uint8_t controller = 0); + bool getChatpadClick(ChatpadButtonEnum b, uint8_t controller = 0); + bool chatpadChanged(uint8_t controller); + + /** + * Set the chatpad LEDs on or off + * @param ledNumber Capslock = 0, Green = 1, Orange = 2, Messanger = 3 + * @param controller The controller to read from. Default to 0. + */ + void setChatpadLed(uint8_t ledNumber, bool value, uint8_t controller = 0); /**@}*/ /** True if a wireless receiver is connected. */ @@ -272,5 +284,23 @@ class XBOXRECV : public USBDeviceConfig { /* Private commands */ void XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes); void checkStatus(); + + /* Chatpad Commands */ + void sendChatpadInitIfNeeded(); + void processChatpadData(uint8_t controller, uint8_t* epBuffer); + void ProcessChatpadKeypress(uint8_t controller, uint8_t value); + + /* Chatpad State */ + bool firstChatpadRun = true; + bool chatpadInitNeeded[4] = { true }; + bool chatpadMod[4][5] = { false }; + uint8_t chatpadDataPacketLast[4][3]; + bool flagUpperCase = false; + + /* Variables to store chatpad buttons */ + uint64_t chatpadState[4]; + uint64_t chatpadOldState[4]; + uint64_t chatpadClickState[4]; + bool chatpadStateChanged[4]; // True if a button has changed }; #endif From 90b35468aabd903d1ce4cec083142cfd5ebb5d14 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 13:12:11 -0500 Subject: [PATCH 02/14] Adds default argument for controller call --- XBOXRECV.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XBOXRECV.h b/XBOXRECV.h index 31dde9bb5..8e94c68af 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -227,7 +227,7 @@ class XBOXRECV : public USBDeviceConfig { uint8_t getChatpadPress(ChatpadButtonEnum b, uint8_t controller = 0); bool getChatpadClick(ChatpadButtonEnum b, uint8_t controller = 0); - bool chatpadChanged(uint8_t controller); + bool chatpadChanged(uint8_t controller = 0); /** * Set the chatpad LEDs on or off From 119e832a05e41fd73da81d77d85a3e8300dac8ba Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 15:54:15 -0500 Subject: [PATCH 03/14] Fixes for gamepad presses, number decoding, and keep alives --- XBOXChatpadEnums.h | 47 ++++++++++++++++------------- XBOXRECV.cpp | 73 +++++++++++++++++++++++++++++----------------- XBOXRECV.h | 12 ++++---- 3 files changed, 80 insertions(+), 52 deletions(-) diff --git a/XBOXChatpadEnums.h b/XBOXChatpadEnums.h index fdb31c040..212fbc7f0 100644 --- a/XBOXChatpadEnums.h +++ b/XBOXChatpadEnums.h @@ -14,11 +14,17 @@ #define _xbox_chatpad_enums_h_ enum ChatpadModiferEnum { - MODIFER_GREENBUTTON = 0, - MODIFER_ORANGEBUTTON = 1, - MODIFER_CAPSLOCK = 2, - MODIFER_SHIFT = 3, - MODIFER_MESSENGER = 4 + MODIFER_SHIFT = 0, + MODIFER_GREENBUTTON = 1, + MODIFER_ORANGEBUTTON = 2, + MODIFER_MESSENGER = 3 +}; + +enum ChatpadLEDEnum { + CHATPADLED_CAPSLOCK = 0, + CHATPADLED_GREEN = 1, + CHATPADLED_ORANGE = 2, + CHATPADLED_MESSENGER = 3, }; /** Buttons on the chatpad @@ -50,7 +56,7 @@ enum ChatpadButtonEnum { XBOX_CHATPAD_S = 19, XBOX_CHATPAD_A = 20, - XBOX_CHATPAD_M = 21, + XBOX_CHATPAD_M = 31, XBOX_CHATPAD_N = 22, XBOX_CHATPAD_B = 23, XBOX_CHATPAD_V = 24, @@ -58,22 +64,23 @@ enum ChatpadButtonEnum { XBOX_CHATPAD_X = 26, XBOX_CHATPAD_Z = 27, - XBOX_CHATPAD_RIGHT = 28, - XBOX_CHATPAD_SPACE = 29, - XBOX_CHATPAD_LEFT = 30, + XBOX_CHATPAD_RIGHT = 30, + XBOX_CHATPAD_SPACE = 33, + XBOX_CHATPAD_LEFT = 34, - XBOX_CHATPAD_COMMA = 31, - XBOX_CHATPAD_ENTER = 32, - XBOX_CHATPAD_P = 33, - XBOX_CHATPAD_D0 = 34, - XBOX_CHATPAD_D9 = 35, - XBOX_CHATPAD_D8 = 36, + XBOX_CHATPAD_COMMA = 39, + XBOX_CHATPAD_PERIOD = 32, + XBOX_CHATPAD_ENTER = 40, + XBOX_CHATPAD_P = 41, + XBOX_CHATPAD_D0 = 42, + XBOX_CHATPAD_D9 = 43, + XBOX_CHATPAD_D8 = 44, - XBOX_CHATPAD_BACK = 37, - XBOX_CHATPAD_L = 38, - XBOX_CHATPAD_O = 39, - XBOX_CHATPAD_I = 40, - XBOX_CHATPAD_K = 41 + XBOX_CHATPAD_BACK = 46, + XBOX_CHATPAD_L = 47, + XBOX_CHATPAD_O = 50, + XBOX_CHATPAD_I = 51, + XBOX_CHATPAD_K = 52 }; #endif diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 8c51185d7..aaf63e32b 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -466,28 +466,21 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { if (dataChanged) { // Record the Modifier Statuses - chatpadMod[controller][(int)MODIFER_GREENBUTTON] = (dataPacket[25] & 0x02) > 0; - chatpadMod[controller][(int)MODIFER_ORANGEBUTTON] = (dataPacket[25] & 0x04) > 0; - chatpadMod[controller][(int)MODIFER_SHIFT] = (dataPacket[25] & 0x01) > 0; - chatpadMod[controller][(int)MODIFER_MESSENGER] = (dataPacket[25] & 0x08) > 0; - - // Toggle Capslock Modifier based on Orange and Shift Modifiers - if (chatpadMod[controller][(int)MODIFER_ORANGEBUTTON] && chatpadMod[controller][(int)MODIFER_SHIFT]) - chatpadMod[controller][(int)MODIFER_CAPSLOCK] = !chatpadMod[controller][(int)MODIFER_CAPSLOCK]; - - - // Set the Upper-Case flag and Shift Key status based on the - // XOR of Shift and Capslock Modifiers. - flagUpperCase = chatpadMod[controller][(int)MODIFER_SHIFT] ^ chatpadMod[controller][(int)MODIFER_CAPSLOCK]; + chatpadModState[controller] = dataPacket[25]; // Process the two different possible keys that could be held down ProcessChatpadKeypress(controller, dataPacket[26]); ProcessChatpadKeypress(controller, dataPacket[27]); - if(chatpadState[controller] != chatpadOldState[controller]) { + if(chatpadClickState[controller] != chatpadClickStateOld[controller]) { chatpadStateChanged[controller] = true; - chatpadClickState[controller] = (chatpadState[controller]) & ((~chatpadOldState[controller])); // Update click state variable - chatpadOldState[controller] = chatpadState[controller]; + chatpadClickStateOld[controller] = chatpadClickState[controller]; + } + + if(chatpadModState[controller] != chatpadModStateOld[controller]) { + // Update click state variable + chatpadModClickState[controller] = (chatpadModState[controller]) & ((~chatpadModStateOld[controller])); + chatpadModStateOld[controller] = chatpadModState[controller]; } } } @@ -497,15 +490,14 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { } void XBOXRECV::ProcessChatpadKeypress(uint8_t controller, uint8_t value) { - value = value - 17; - value = (value & 0x70 >> 1) | (value & 0x07); + value = (((value & 0xF0) - 0x10) >> 1) | ((value & 0x0F) - 1); if (value > 50) { // Invalid button return; } - chatpadState[controller] |= (1 << value); + chatpadClickState[controller] |= (1 << value); } uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) { @@ -546,16 +538,23 @@ bool XBOXRECV::buttonChanged(uint8_t controller) { return state; } +bool XBOXRECV::getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller) { + return (bool)(chatpadModState[controller] & (1 << b)); +} + +bool XBOXRECV::getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller) { + uint8_t mask = (1 << b); + bool click = (chatpadClickState[controller] & mask); + chatpadClickState[controller] &= ~mask; // clear "click" event + return click; +} + bool XBOXRECV::chatpadChanged(uint8_t controller) { bool state = chatpadStateChanged[controller]; chatpadStateChanged[controller] = false; return state; } -uint8_t XBOXRECV::getChatpadPress(ChatpadButtonEnum b, uint8_t controller) { - return (bool)(chatpadState[controller] & ((uint64_t)(1 << b))); -} - bool XBOXRECV::getChatpadClick(ChatpadButtonEnum b, uint8_t controller) { uint64_t mask = ((uint64_t)(1 << b)); bool click = (chatpadClickState[controller] & mask); @@ -643,7 +642,7 @@ void XBOXRECV::setLedBlink(LEDEnum led, uint8_t controller) { } -void XBOXRECV::setChatpadLed(uint8_t ledNumber, bool value, uint8_t controller) { +void XBOXRECV::setChatpadLed(ChatpadLEDEnum ledNumber, bool value, uint8_t controller) { uint8_t ledOnByte[4] = {0x08, 0x09, 0x0A, 0x0B}; uint8_t ledOffByte[4] = {0x00, 0x01, 0x02, 0x03}; writeBuf[0] = 0x00; @@ -686,6 +685,25 @@ void XBOXRECV::checkStatus() { if(Xbox360Connected[i]) XboxCommand(i, writeBuf, 4); } + + // Keep Alive 1 + writeBuf[0] = 0x00; + writeBuf[1] = 0x00; + writeBuf[2] = 0x0C; + writeBuf[3] = 0x1F; + for(uint8_t i = 0; i < 4; i++) { + if(Xbox360Connected[i]) + XboxCommand(i, writeBuf, 4); + } + // Keep Alive 2 + writeBuf[0] = 0x00; + writeBuf[1] = 0x00; + writeBuf[2] = 0x0C; + writeBuf[3] = 0x1F; + for(uint8_t i = 0; i < 4; i++) { + if(Xbox360Connected[i]) + XboxCommand(i, writeBuf, 4); + } } void XBOXRECV::sendChatpadInitIfNeeded() { @@ -696,9 +714,10 @@ void XBOXRECV::sendChatpadInitIfNeeded() { writeBuf[3] = 0x1B; for(uint8_t i = 0; i < 4; i++) { if(chatpadInitNeeded[i]) { - if (Xbox360Connected[i]) { - XboxCommand(i, writeBuf, 4); - } + #ifdef EXTRADEBUG + Notify(PSTR("\r\nSending Chatpad Init to controller"), 0x80); + #endif + XboxCommand(i, writeBuf, 4); chatpadInitNeeded[i] = false; } } diff --git a/XBOXRECV.h b/XBOXRECV.h index 8e94c68af..1a0453d82 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -225,7 +225,8 @@ class XBOXRECV : public USBDeviceConfig { pFuncOnInit = funcOnInit; }; - uint8_t getChatpadPress(ChatpadButtonEnum b, uint8_t controller = 0); + bool getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller = 0); + bool getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller = 0); bool getChatpadClick(ChatpadButtonEnum b, uint8_t controller = 0); bool chatpadChanged(uint8_t controller = 0); @@ -234,7 +235,7 @@ class XBOXRECV : public USBDeviceConfig { * @param ledNumber Capslock = 0, Green = 1, Orange = 2, Messanger = 3 * @param controller The controller to read from. Default to 0. */ - void setChatpadLed(uint8_t ledNumber, bool value, uint8_t controller = 0); + void setChatpadLed(ChatpadLEDEnum led, bool value, uint8_t controller = 0); /**@}*/ /** True if a wireless receiver is connected. */ @@ -293,14 +294,15 @@ class XBOXRECV : public USBDeviceConfig { /* Chatpad State */ bool firstChatpadRun = true; bool chatpadInitNeeded[4] = { true }; - bool chatpadMod[4][5] = { false }; + uint8_t chatpadModState[4] = {0}; + uint8_t chatpadModStateOld[4] = {0}; + uint8_t chatpadModClickState[4] = {0}; uint8_t chatpadDataPacketLast[4][3]; bool flagUpperCase = false; /* Variables to store chatpad buttons */ - uint64_t chatpadState[4]; - uint64_t chatpadOldState[4]; uint64_t chatpadClickState[4]; + uint64_t chatpadClickStateOld[4]; bool chatpadStateChanged[4]; // True if a button has changed }; #endif From 8023b9acc54e016e831f0a1381a82faf48a33e14 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 16:11:23 -0500 Subject: [PATCH 04/14] Fixes enums again --- XBOXChatpadEnums.h | 73 +++++++++++++++++++++++----------------------- 1 file changed, 37 insertions(+), 36 deletions(-) diff --git a/XBOXChatpadEnums.h b/XBOXChatpadEnums.h index 212fbc7f0..ddc80c81d 100644 --- a/XBOXChatpadEnums.h +++ b/XBOXChatpadEnums.h @@ -40,47 +40,48 @@ enum ChatpadButtonEnum { XBOX_CHATPAD_D2 = 5, XBOX_CHATPAD_D1 = 6, - XBOX_CHATPAD_U = 7, - XBOX_CHATPAD_Y = 8, - XBOX_CHATPAD_T = 9, - XBOX_CHATPAD_R = 10, - XBOX_CHATPAD_E = 11, - XBOX_CHATPAD_W = 12, - XBOX_CHATPAD_Q = 13, + XBOX_CHATPAD_U = 8, + XBOX_CHATPAD_Y = 9, + XBOX_CHATPAD_T = 10, + XBOX_CHATPAD_R = 11, + XBOX_CHATPAD_E = 12, + XBOX_CHATPAD_W = 13, + XBOX_CHATPAD_Q = 14, - XBOX_CHATPAD_J = 14, - XBOX_CHATPAD_H = 15, - XBOX_CHATPAD_G = 16, - XBOX_CHATPAD_F = 17, - XBOX_CHATPAD_D = 18, - XBOX_CHATPAD_S = 19, - XBOX_CHATPAD_A = 20, + XBOX_CHATPAD_J = 16, + XBOX_CHATPAD_H = 17, + XBOX_CHATPAD_G = 18, + XBOX_CHATPAD_F = 19, + XBOX_CHATPAD_D = 20, + XBOX_CHATPAD_S = 21, + XBOX_CHATPAD_A = 22, - XBOX_CHATPAD_M = 31, - XBOX_CHATPAD_N = 22, - XBOX_CHATPAD_B = 23, - XBOX_CHATPAD_V = 24, - XBOX_CHATPAD_C = 25, - XBOX_CHATPAD_X = 26, - XBOX_CHATPAD_Z = 27, + XBOX_CHATPAD_M = 33, + XBOX_CHATPAD_N = 24, + XBOX_CHATPAD_B = 25, + XBOX_CHATPAD_V = 26, + XBOX_CHATPAD_C = 27, + XBOX_CHATPAD_X = 28, + XBOX_CHATPAD_Z = 29, - XBOX_CHATPAD_RIGHT = 30, - XBOX_CHATPAD_SPACE = 33, - XBOX_CHATPAD_LEFT = 34, + XBOX_CHATPAD_RIGHT = 32, + XBOX_CHATPAD_SPACE = 35, + XBOX_CHATPAD_LEFT = 36, - XBOX_CHATPAD_COMMA = 39, - XBOX_CHATPAD_PERIOD = 32, - XBOX_CHATPAD_ENTER = 40, - XBOX_CHATPAD_P = 41, - XBOX_CHATPAD_D0 = 42, - XBOX_CHATPAD_D9 = 43, - XBOX_CHATPAD_D8 = 44, + XBOX_CHATPAD_COMMA = 41, + XBOX_CHATPAD_PERIOD = 34, + XBOX_CHATPAD_ENTER = 42, + XBOX_CHATPAD_P = 43, + XBOX_CHATPAD_D0 = 44, + XBOX_CHATPAD_D9 = 45, + XBOX_CHATPAD_D8 = 46, - XBOX_CHATPAD_BACK = 46, - XBOX_CHATPAD_L = 47, - XBOX_CHATPAD_O = 50, - XBOX_CHATPAD_I = 51, - XBOX_CHATPAD_K = 52 + XBOX_CHATPAD_BACK = 48, + XBOX_CHATPAD_L = 49, + XBOX_CHATPAD_O = 52, + XBOX_CHATPAD_I = 53, + XBOX_CHATPAD_K = 54 }; #endif + From e3c143981e453708c7b7a0c11c88753cd78beedb Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 16:48:22 -0500 Subject: [PATCH 05/14] Changes modifier to directly read state --- XBOXChatpadEnums.h | 1 - XBOXRECV.cpp | 15 +-------------- XBOXRECV.h | 5 +---- 3 files changed, 2 insertions(+), 19 deletions(-) diff --git a/XBOXChatpadEnums.h b/XBOXChatpadEnums.h index ddc80c81d..2806d8f5c 100644 --- a/XBOXChatpadEnums.h +++ b/XBOXChatpadEnums.h @@ -84,4 +84,3 @@ enum ChatpadButtonEnum { }; #endif - diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index aaf63e32b..d5c69965e 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -476,12 +476,6 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { chatpadStateChanged[controller] = true; chatpadClickStateOld[controller] = chatpadClickState[controller]; } - - if(chatpadModState[controller] != chatpadModStateOld[controller]) { - // Update click state variable - chatpadModClickState[controller] = (chatpadModState[controller]) & ((~chatpadModStateOld[controller])); - chatpadModStateOld[controller] = chatpadModState[controller]; - } } } else { @@ -538,17 +532,10 @@ bool XBOXRECV::buttonChanged(uint8_t controller) { return state; } -bool XBOXRECV::getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller) { +bool XBOXRECV::getChatpadModifier(ChatpadModiferEnum b, uint8_t controller) { return (bool)(chatpadModState[controller] & (1 << b)); } -bool XBOXRECV::getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller) { - uint8_t mask = (1 << b); - bool click = (chatpadClickState[controller] & mask); - chatpadClickState[controller] &= ~mask; // clear "click" event - return click; -} - bool XBOXRECV::chatpadChanged(uint8_t controller) { bool state = chatpadStateChanged[controller]; chatpadStateChanged[controller] = false; diff --git a/XBOXRECV.h b/XBOXRECV.h index 1a0453d82..ae0c076cf 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -225,8 +225,7 @@ class XBOXRECV : public USBDeviceConfig { pFuncOnInit = funcOnInit; }; - bool getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller = 0); - bool getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller = 0); + bool getChatpadModifier(ChatpadModiferEnum b, uint8_t controller = 0); bool getChatpadClick(ChatpadButtonEnum b, uint8_t controller = 0); bool chatpadChanged(uint8_t controller = 0); @@ -295,8 +294,6 @@ class XBOXRECV : public USBDeviceConfig { bool firstChatpadRun = true; bool chatpadInitNeeded[4] = { true }; uint8_t chatpadModState[4] = {0}; - uint8_t chatpadModStateOld[4] = {0}; - uint8_t chatpadModClickState[4] = {0}; uint8_t chatpadDataPacketLast[4][3]; bool flagUpperCase = false; From c045b6ceb3127a3ef9217bd4946bd3c2a837616d Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 16:57:10 -0500 Subject: [PATCH 06/14] Makes the LEDs based on modifer states --- XBOXRECV.cpp | 7 +++++++ XBOXRECV.h | 1 + 2 files changed, 8 insertions(+) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index d5c69965e..a5f4c9761 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -476,6 +476,13 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { chatpadStateChanged[controller] = true; chatpadClickStateOld[controller] = chatpadClickState[controller]; } + + if (chatpadModState[controller] != chatpadModStateLast[controller]) { + for (int i = 0; i < 4; i ++) { + setChatpadLed(i, (bool)(1 << i)); + } + chatpadModStateLast[controller] = chatpadModState[controller]; + } } } else { diff --git a/XBOXRECV.h b/XBOXRECV.h index ae0c076cf..f2aa58cc6 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -294,6 +294,7 @@ class XBOXRECV : public USBDeviceConfig { bool firstChatpadRun = true; bool chatpadInitNeeded[4] = { true }; uint8_t chatpadModState[4] = {0}; + uint8_t chatpadModStateLast[4] = {0}; uint8_t chatpadDataPacketLast[4][3]; bool flagUpperCase = false; From 6897e6e7af733674586d4a4ee77fdc0c27764a44 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 17:02:09 -0500 Subject: [PATCH 07/14] Fixes bits when setting LEDs --- XBOXRECV.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index a5f4c9761..e21f75a0c 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -479,7 +479,7 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { if (chatpadModState[controller] != chatpadModStateLast[controller]) { for (int i = 0; i < 4; i ++) { - setChatpadLed(i, (bool)(1 << i)); + setChatpadLed((ChatpadLEDEnum)i, (bool)(1 << i) & chatpadModState[controller]); } chatpadModStateLast[controller] = chatpadModState[controller]; } From c34ca95b15063c2febbaf358ab542ba40e5c9ba8 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 17:09:43 -0500 Subject: [PATCH 08/14] Fixes bit decoding --- XBOXRECV.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index e21f75a0c..9469e3314 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -479,7 +479,7 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { if (chatpadModState[controller] != chatpadModStateLast[controller]) { for (int i = 0; i < 4; i ++) { - setChatpadLed((ChatpadLEDEnum)i, (bool)(1 << i) & chatpadModState[controller]); + setChatpadLed((ChatpadLEDEnum)i, (bool)((1 << i) & chatpadModState[controller])); } chatpadModStateLast[controller] = chatpadModState[controller]; } From 3fa1d24a997aae7989e8a3e5e707f3df598eca90 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 17:30:35 -0500 Subject: [PATCH 09/14] Revert click logic --- XBOXRECV.cpp | 18 ++++++++++++------ XBOXRECV.h | 6 ++++-- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 9469e3314..aaf63e32b 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -477,11 +477,10 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { chatpadClickStateOld[controller] = chatpadClickState[controller]; } - if (chatpadModState[controller] != chatpadModStateLast[controller]) { - for (int i = 0; i < 4; i ++) { - setChatpadLed((ChatpadLEDEnum)i, (bool)((1 << i) & chatpadModState[controller])); - } - chatpadModStateLast[controller] = chatpadModState[controller]; + if(chatpadModState[controller] != chatpadModStateOld[controller]) { + // Update click state variable + chatpadModClickState[controller] = (chatpadModState[controller]) & ((~chatpadModStateOld[controller])); + chatpadModStateOld[controller] = chatpadModState[controller]; } } } @@ -539,10 +538,17 @@ bool XBOXRECV::buttonChanged(uint8_t controller) { return state; } -bool XBOXRECV::getChatpadModifier(ChatpadModiferEnum b, uint8_t controller) { +bool XBOXRECV::getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller) { return (bool)(chatpadModState[controller] & (1 << b)); } +bool XBOXRECV::getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller) { + uint8_t mask = (1 << b); + bool click = (chatpadClickState[controller] & mask); + chatpadClickState[controller] &= ~mask; // clear "click" event + return click; +} + bool XBOXRECV::chatpadChanged(uint8_t controller) { bool state = chatpadStateChanged[controller]; chatpadStateChanged[controller] = false; diff --git a/XBOXRECV.h b/XBOXRECV.h index f2aa58cc6..1a0453d82 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -225,7 +225,8 @@ class XBOXRECV : public USBDeviceConfig { pFuncOnInit = funcOnInit; }; - bool getChatpadModifier(ChatpadModiferEnum b, uint8_t controller = 0); + bool getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller = 0); + bool getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller = 0); bool getChatpadClick(ChatpadButtonEnum b, uint8_t controller = 0); bool chatpadChanged(uint8_t controller = 0); @@ -294,7 +295,8 @@ class XBOXRECV : public USBDeviceConfig { bool firstChatpadRun = true; bool chatpadInitNeeded[4] = { true }; uint8_t chatpadModState[4] = {0}; - uint8_t chatpadModStateLast[4] = {0}; + uint8_t chatpadModStateOld[4] = {0}; + uint8_t chatpadModClickState[4] = {0}; uint8_t chatpadDataPacketLast[4][3]; bool flagUpperCase = false; From e9d6e55d13df38b5ec37338e6913b5610ac3cc22 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Sat, 23 May 2020 17:38:30 -0500 Subject: [PATCH 10/14] Fixes wrong variable --- XBOXRECV.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index aaf63e32b..bf5c82aa7 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -544,8 +544,8 @@ bool XBOXRECV::getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller) bool XBOXRECV::getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller) { uint8_t mask = (1 << b); - bool click = (chatpadClickState[controller] & mask); - chatpadClickState[controller] &= ~mask; // clear "click" event + bool click = (chatpadModClickState[controller] & mask); + chatpadModClickState[controller] &= ~mask; // clear "click" event return click; } From 476aadc3afba8adbb59efa5d48b800e8e232e384 Mon Sep 17 00:00:00 2001 From: Will Toth Date: Mon, 15 Jun 2020 19:56:19 -0500 Subject: [PATCH 11/14] Clean up handling of modifier and cast uint64_t for button flags --- XBOXRECV.cpp | 26 +++++++++++--------------- XBOXRECV.h | 7 ++++--- 2 files changed, 15 insertions(+), 18 deletions(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index bf5c82aa7..92b71cfd5 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -466,7 +466,7 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { if (dataChanged) { // Record the Modifier Statuses - chatpadModState[controller] = dataPacket[25]; + chatpadModRaw[controller] = dataPacket[25]; // Process the two different possible keys that could be held down ProcessChatpadKeypress(controller, dataPacket[26]); @@ -477,10 +477,12 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { chatpadClickStateOld[controller] = chatpadClickState[controller]; } - if(chatpadModState[controller] != chatpadModStateOld[controller]) { + if(chatpadModRaw[controller] != chatpadModRawOld[controller]) { // Update click state variable - chatpadModClickState[controller] = (chatpadModState[controller]) & ((~chatpadModStateOld[controller])); - chatpadModStateOld[controller] = chatpadModState[controller]; + chatpadModState[controller] = (chatpadModRaw[controller]) & ((~chatpadModRawOld[controller])); + chatpadModRawOld[controller] = chatpadModRaw[controller]; + + setChatpadLed( (ChatpadLEDEnum)i, (1 << i) & chatpadModState[controller]); } } } @@ -492,12 +494,12 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { void XBOXRECV::ProcessChatpadKeypress(uint8_t controller, uint8_t value) { value = (((value & 0xF0) - 0x10) >> 1) | ((value & 0x0F) - 1); - if (value > 50) { + if (value > __XBOX_CHATPAD_ENUM_MAX) { // Invalid button return; } - chatpadClickState[controller] |= (1 << value); + chatpadClickState[controller] |= (((uint64_t)1) << ((uint64_t)value)); } uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) { @@ -538,17 +540,10 @@ bool XBOXRECV::buttonChanged(uint8_t controller) { return state; } -bool XBOXRECV::getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller) { +bool XBOXRECV::getChatpadModifier(ChatpadModiferEnum b, uint8_t controller) { return (bool)(chatpadModState[controller] & (1 << b)); } -bool XBOXRECV::getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller) { - uint8_t mask = (1 << b); - bool click = (chatpadModClickState[controller] & mask); - chatpadModClickState[controller] &= ~mask; // clear "click" event - return click; -} - bool XBOXRECV::chatpadChanged(uint8_t controller) { bool state = chatpadStateChanged[controller]; chatpadStateChanged[controller] = false; @@ -556,7 +551,7 @@ bool XBOXRECV::chatpadChanged(uint8_t controller) { } bool XBOXRECV::getChatpadClick(ChatpadButtonEnum b, uint8_t controller) { - uint64_t mask = ((uint64_t)(1 << b)); + uint64_t mask = ((uint64_t)1) << ((uint64_t)b); bool click = (chatpadClickState[controller] & mask); chatpadClickState[controller] &= ~mask; // clear "click" event return click; @@ -751,3 +746,4 @@ void XBOXRECV::onInit(uint8_t controller) { setLedOn(led, controller); } } + diff --git a/XBOXRECV.h b/XBOXRECV.h index 1a0453d82..9dbd98b81 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -225,8 +225,7 @@ class XBOXRECV : public USBDeviceConfig { pFuncOnInit = funcOnInit; }; - bool getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller = 0); - bool getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller = 0); + bool getChatpadModifier(ChatpadModiferEnum b, uint8_t controller = 0); bool getChatpadClick(ChatpadButtonEnum b, uint8_t controller = 0); bool chatpadChanged(uint8_t controller = 0); @@ -294,8 +293,9 @@ class XBOXRECV : public USBDeviceConfig { /* Chatpad State */ bool firstChatpadRun = true; bool chatpadInitNeeded[4] = { true }; + uint8_t chatpadModRaw[4] = {0}; uint8_t chatpadModState[4] = {0}; - uint8_t chatpadModStateOld[4] = {0}; + uint8_t chatpadModRawOld[4] = {0}; uint8_t chatpadModClickState[4] = {0}; uint8_t chatpadDataPacketLast[4][3]; bool flagUpperCase = false; @@ -306,3 +306,4 @@ class XBOXRECV : public USBDeviceConfig { bool chatpadStateChanged[4]; // True if a button has changed }; #endif + From d6e6d93825f5e16307e441e85189bf464ec96d5c Mon Sep 17 00:00:00 2001 From: Will Toth Date: Mon, 15 Jun 2020 20:01:56 -0500 Subject: [PATCH 12/14] Fix for setChatpapLED on modifer press --- XBOXRECV.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 92b71cfd5..4c3ec3ac5 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -482,7 +482,9 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { chatpadModState[controller] = (chatpadModRaw[controller]) & ((~chatpadModRawOld[controller])); chatpadModRawOld[controller] = chatpadModRaw[controller]; - setChatpadLed( (ChatpadLEDEnum)i, (1 << i) & chatpadModState[controller]); + for (int i = 0; i < __NUM_CHATPAD_LED; i++) { + setChatpadLed( (ChatpadLEDEnum)i, (1 << i) & chatpadModState[controller]); + } } } } From 6112ad1a5c9a1f733a5f2760784ac0c474a45cdb Mon Sep 17 00:00:00 2001 From: Will Toth Date: Wed, 17 Jun 2020 16:14:03 -0500 Subject: [PATCH 13/14] Missed file push adding enum numbers --- XBOXChatpadEnums.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/XBOXChatpadEnums.h b/XBOXChatpadEnums.h index 2806d8f5c..8e91f02d0 100644 --- a/XBOXChatpadEnums.h +++ b/XBOXChatpadEnums.h @@ -25,6 +25,8 @@ enum ChatpadLEDEnum { CHATPADLED_GREEN = 1, CHATPADLED_ORANGE = 2, CHATPADLED_MESSENGER = 3, + + __NUM_CHATPAD_LED }; /** Buttons on the chatpad @@ -80,7 +82,9 @@ enum ChatpadButtonEnum { XBOX_CHATPAD_L = 49, XBOX_CHATPAD_O = 52, XBOX_CHATPAD_I = 53, - XBOX_CHATPAD_K = 54 + XBOX_CHATPAD_K = 54, + + __XBOX_CHATPAD_ENUM_MAX }; #endif From dcda522e3cbfc2f4b4fc767ac76f1e59784dad76 Mon Sep 17 00:00:00 2001 From: apsteven Date: Thu, 9 Jul 2020 22:45:53 +0100 Subject: [PATCH 14/14] Update XBOXRECV.cpp Modify to get the modifier click function working --- XBOXRECV.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index 4c3ec3ac5..dada2d141 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -466,7 +466,7 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { if (dataChanged) { // Record the Modifier Statuses - chatpadModRaw[controller] = dataPacket[25]; + chatpadModState[controller] = dataPacket[25]; // Process the two different possible keys that could be held down ProcessChatpadKeypress(controller, dataPacket[26]); @@ -477,14 +477,10 @@ void XBOXRECV::processChatpadData(uint8_t controller, uint8_t* dataPacket) { chatpadClickStateOld[controller] = chatpadClickState[controller]; } - if(chatpadModRaw[controller] != chatpadModRawOld[controller]) { + if(chatpadModState[controller] != chatpadModStateOld[controller]) { // Update click state variable - chatpadModState[controller] = (chatpadModRaw[controller]) & ((~chatpadModRawOld[controller])); - chatpadModRawOld[controller] = chatpadModRaw[controller]; - - for (int i = 0; i < __NUM_CHATPAD_LED; i++) { - setChatpadLed( (ChatpadLEDEnum)i, (1 << i) & chatpadModState[controller]); - } + chatpadModClickState[controller] = (chatpadModState[controller]) & ((~chatpadModStateOld[controller])); + chatpadModStateOld[controller] = chatpadModState[controller]; } } } @@ -501,7 +497,7 @@ void XBOXRECV::ProcessChatpadKeypress(uint8_t controller, uint8_t value) { return; } - chatpadClickState[controller] |= (((uint64_t)1) << ((uint64_t)value)); + chatpadClickState[controller] |= (((uint64_t)1) << ((uint64_t)value)); } uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) { @@ -542,10 +538,17 @@ bool XBOXRECV::buttonChanged(uint8_t controller) { return state; } -bool XBOXRECV::getChatpadModifier(ChatpadModiferEnum b, uint8_t controller) { +bool XBOXRECV::getChatpadModifierPress(ChatpadModiferEnum b, uint8_t controller) { return (bool)(chatpadModState[controller] & (1 << b)); } +bool XBOXRECV::getChatpadModifierClick(ChatpadModiferEnum b, uint8_t controller) { + uint8_t mask = (1 << b); + bool click = (chatpadModClickState[controller] & mask); + chatpadModClickState[controller] &= ~mask; // clear "click" event + return click; +} + bool XBOXRECV::chatpadChanged(uint8_t controller) { bool state = chatpadStateChanged[controller]; chatpadStateChanged[controller] = false; @@ -748,4 +751,3 @@ void XBOXRECV::onInit(uint8_t controller) { setLedOn(led, controller); } } -