Better getButtonClick routine

This commit is contained in:
Kristian Lauszus 2012-10-02 01:43:00 +02:00
parent bf646fdd0e
commit 75a84c5e80
3 changed files with 17 additions and 32 deletions

View file

@ -359,9 +359,10 @@ void PS3BT::ACLData(uint8_t* ACLData) {
//Notify(PSTR("\r\nButtonState"); //Notify(PSTR("\r\nButtonState");
//PrintHex<uint32_t>(ButtonState); //PrintHex<uint32_t>(ButtonState);
if(ButtonState != OldButtonState) if(ButtonState != OldButtonState) {
ButtonClickState = ButtonState; ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
OldButtonState = ButtonState; OldButtonState = ButtonState;
}
#ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers #ifdef PRINTREPORT // Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers
for(uint8_t i = 10; i < 58;i++) { for(uint8_t i = 10; i < 58;i++) {

View file

@ -308,9 +308,10 @@ void PS3USB::readReport() {
//Notify(PSTR("\r\nButtonState"); //Notify(PSTR("\r\nButtonState");
//PrintHex<uint32_t>(ButtonState); //PrintHex<uint32_t>(ButtonState);
if(ButtonState != OldButtonState) if(ButtonState != OldButtonState) {
ButtonClickState = ButtonState; // Update click state variable ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
OldButtonState = ButtonState; OldButtonState = ButtonState;
}
} }
void PS3USB::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers void PS3USB::printReport() { //Uncomment "#define PRINTREPORT" to print the report send by the PS3 Controllers

33
Wii.cpp
View file

@ -165,11 +165,11 @@ void WII::ACLData(uint8_t* l2capinbuf) {
if(l2capinbuf[20] & 0x02) // Only update the wiimote buttons, since the extension bytes are from the Motion Plus if(l2capinbuf[20] & 0x02) // Only update the wiimote buttons, since the extension bytes are from the Motion Plus
ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)(ButtonState & 0xFFFF0000))); ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)(ButtonState & 0xFFFF0000)));
else if (nunchuckConnected) // Update if it's a report from the Nunchuck else if (nunchuckConnected) // Update if it's a report from the Nunchuck
ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)(l2capinbuf[20] & 0x0C) << 14)); ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)((~l2capinbuf[20]) & 0x0C) << 14));
//else if(classicControllerConnected) // Update if it's a report from the Classic Controller //else if(classicControllerConnected) // Update if it's a report from the Classic Controller
} }
else if(nunchuckConnected) // The Nunchuck is directly connected else if(nunchuckConnected) // The Nunchuck is directly connected
ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)(l2capinbuf[20] & 0x03) << 16)); ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8) | ((uint32_t)((~l2capinbuf[20]) & 0x03) << 16));
//else if(classicControllerConnected) // The Classic Controller is directly connected //else if(classicControllerConnected) // The Classic Controller is directly connected
else if(!unknownExtensionConnected) else if(!unknownExtensionConnected)
ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8)); ButtonState = (uint32_t)((l2capinbuf[10] & 0x1F) | ((uint16_t)(l2capinbuf[11] & 0x9F) << 8));
@ -178,9 +178,10 @@ void WII::ACLData(uint8_t* l2capinbuf) {
PrintHex<uint32_t>(ButtonState); PrintHex<uint32_t>(ButtonState);
Notify(PSTR("\r\n")); Notify(PSTR("\r\n"));
#endif #endif
if(ButtonState != OldButtonState) if(ButtonState != OldButtonState) {
ButtonClickState = ButtonState; // Update click state variable ButtonClickState = ButtonState & ~OldButtonState; // Update click state variable
OldButtonState = ButtonState; OldButtonState = ButtonState;
}
} }
if(l2capinbuf[9] == 0x31 || l2capinbuf[9] == 0x35) { // Read the accelerometer if(l2capinbuf[9] == 0x31 || l2capinbuf[9] == 0x35) { // Read the accelerometer
accX = ((l2capinbuf[12] << 2) | (l2capinbuf[10] & 0x60 >> 5))-500; accX = ((l2capinbuf[12] << 2) | (l2capinbuf[10] & 0x60 >> 5))-500;
@ -249,11 +250,6 @@ void WII::ACLData(uint8_t* l2capinbuf) {
activateNunchuck = false; activateNunchuck = false;
motionPlusConnected = true; motionPlusConnected = true;
nunchuckConnected = true; nunchuckConnected = true;
ButtonState |= (Z | C);
ButtonState |= ((Z | C)<<2);
ButtonClickState = ButtonState;
OldButtonState = ButtonState;
} }
#ifdef DEBUG #ifdef DEBUG
else { else {
@ -570,13 +566,8 @@ void WII::Run() {
break; break;
case L2CAP_LED_STATE: case L2CAP_LED_STATE:
if(nunchuck_connected_flag) { if(nunchuck_connected_flag)
nunchuckConnected = true; nunchuckConnected = true;
ButtonState |= (Z | C); // Since the Nunchuck button are cleared when pressed we set the buttonstates like so
ButtonState |= ((Z | C)<<2); // And like this when it's connected since the bytes are shifter two to the left
ButtonClickState = ButtonState;
OldButtonState = ButtonState;
}
setLedOn(LED1); setLedOn(LED1);
l2cap_state = L2CAP_DONE; l2cap_state = L2CAP_DONE;
break; break;
@ -606,10 +597,6 @@ void WII::Run() {
#endif #endif
activateNunchuck = true; activateNunchuck = true;
nunchuckConnected = true; nunchuckConnected = true;
ButtonState |= (Z | C); // Since the Nunchuck button are cleared when pressed we set the buttonstates like so
ButtonState |= ((Z | C)<<2); // And like this when it's connected since the bytes are shifter two to the left
ButtonClickState = ButtonState;
OldButtonState = ButtonState;
} }
if(!motionPlusConnected) if(!motionPlusConnected)
stateCounter = 449; stateCounter = 449;
@ -792,11 +779,7 @@ bool WII::getButtonPress(Button b) { // Return true when a button is pressed
} }
bool WII::getButtonClick(Button b) { // Only return true when a button is clicked bool WII::getButtonClick(Button b) { // Only return true when a button is clicked
bool click = (ButtonClickState & (uint32_t)b); bool click = (ButtonClickState & (uint32_t)b);
if(b == Z || b == C) { ButtonClickState &= ~((uint32_t)b); // clear "click" event
click = !click; // The nunchuck buttons are cleared when pressed
ButtonClickState |= (uint32_t)b; // clear "click" event
} else
ButtonClickState &= ~((uint32_t)b); // clear "click" event
return click; return click;
} }
uint8_t WII::getAnalogHat(AnalogHat a) { uint8_t WII::getAnalogHat(AnalogHat a) {