diff --git a/XBOXRECV.cpp b/XBOXRECV.cpp index a0f14d23..34c99fea 100644 --- a/XBOXRECV.cpp +++ b/XBOXRECV.cpp @@ -352,6 +352,7 @@ void XBOXRECV::readReport(uint8_t controller) { //PrintHex(ButtonState[controller]); if(ButtonState[controller] != OldButtonState[controller]) { + buttonStateChanged[controller] = true; ButtonClickState[controller] = (ButtonState[controller] >> 16) & ((~OldButtonState[controller]) >> 16); // Update click state variable, but don't include the two trigger buttons L2 and R2 if(((uint8_t)OldButtonState[controller]) == 0 && ((uint8_t)ButtonState[controller]) != 0) // The L2 and R2 buttons are special as they are analog buttons R2Clicked[controller] = true; @@ -404,6 +405,11 @@ bool XBOXRECV::getButtonClick(uint8_t controller, Button b) { int16_t XBOXRECV::getAnalogHat(uint8_t controller, AnalogHat a) { return hatValue[controller][a]; } +bool XBOXRECV::buttonChanged(uint8_t controller) { + bool state = buttonStateChanged[controller]; + buttonStateChanged[controller] = false; + return state; +} /* ControllerStatus Breakdown ControllerStatus[controller] & 0x0001 // 0 @@ -424,16 +430,10 @@ ControllerStatus Breakdown ControllerStatus[controller] & 0x8000 // 0 */ uint8_t XBOXRECV::getBatteryLevel(uint8_t controller) { - if(((controllerStatus[controller] & 0x00C0) >> 6) == 0x3) - return 100; - else if(((controllerStatus[controller] & 0x00C0) >> 6) == 0x2) - return 67; - else if(((controllerStatus[controller] & 0x00C0) >> 6) == 0x1) - return 33; - else if(((controllerStatus[controller] & 0x00C0) >> 6) == 0x0) - return 0; - else - return -1; + uint8_t batteryLevel = ((controllerStatus[controller] & 0x00C0) >> 6) * 33; + if(batteryLevel == 99) + batteryLevel = 100; + return batteryLevel; } void XBOXRECV::XboxCommand(uint8_t controller, uint8_t* data, uint16_t nbytes) { diff --git a/XBOXRECV.h b/XBOXRECV.h index e25d635d..39caa475 100644 --- a/XBOXRECV.h +++ b/XBOXRECV.h @@ -131,7 +131,8 @@ public: void setLedOn(uint8_t controller, LED l); void setLedBlink(uint8_t controller, LED l); void setLedMode(uint8_t controller, LEDMode lm); - uint8_t getBatteryLevel(uint8_t controller); // Returns the battery level in percentage in 25% steps + uint8_t getBatteryLevel(uint8_t controller); // Returns the battery level in percentage in 33% steps + bool buttonChanged(uint8_t controller); bool XboxReceiverConnected; // True if a wireless receiver is connected uint8_t Xbox360Connected[4]; // Variable used to indicate if the XBOX 360 controller is successfully connected @@ -151,6 +152,7 @@ private: uint16_t ButtonClickState[4]; int16_t hatValue[4][4]; uint16_t controllerStatus[4]; + bool buttonStateChanged[4]; // True if a button has changed bool L2Clicked[4]; // These buttons are analog, so we use we use these bools to check if they where clicked or not bool R2Clicked[4]; diff --git a/keywords.txt b/keywords.txt index 9d20b056..486536d1 100644 --- a/keywords.txt +++ b/keywords.txt @@ -165,6 +165,7 @@ setLedRaw KEYWORD2 setLedBlink KEYWORD2 setLedMode KEYWORD2 getBatteryLevel KEYWORD2 +buttonChanged KEYWORD2 XboxReceiverConnected KEYWORD2 Xbox360Connected KEYWORD2