Use signed index values for ButtonEnum

This commit is contained in:
David Madison 2021-03-25 20:48:37 -04:00
parent 5753cc661a
commit f9a408204d
8 changed files with 38 additions and 42 deletions

View file

@ -139,8 +139,8 @@ enum StatusEnum {
};
inline int8_t getPS3ButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
if (index >= sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0])) return -1;
const int8_t index = legacyButtonValues(b);
if ((uint8_t) index >= (sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0]))) return -1;
return index;
}

View file

@ -33,8 +33,8 @@ enum DPADEnum {
//#define PRINTREPORT // Uncomment to print the report send by the PS4 Controller
int8_t PS4Parser::getButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
if (index >= sizeof(PS4_BUTTONS) / sizeof(PS4_BUTTONS[0])) return -1;
const int8_t index = legacyButtonValues(b);
if ((uint8_t) index >= (sizeof(PS4_BUTTONS) / sizeof(PS4_BUTTONS[0]))) return -1;
return index;
}

View file

@ -37,8 +37,8 @@ enum DPADEnum {
//#define PRINTREPORT // Uncomment to print the report send by the PS5 Controller
int8_t PS5Parser::getButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
if (index >= sizeof(PS5_BUTTONS) / sizeof(PS5_BUTTONS[0])) return -1;
const int8_t index = legacyButtonValues(b);
if ((uint8_t) index >= (sizeof(PS5_BUTTONS) / sizeof(PS5_BUTTONS[0]))) return -1;
return index;
}

View file

@ -50,7 +50,7 @@ uint8_t PSBuzz::OnInitSuccessful() {
};
int8_t PSBuzz::getButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
const int8_t index = legacyButtonValues(b);
if (index > 4) return -1; // 5 buttons, 0-4 inclusive
return index;
}

View file

@ -1095,14 +1095,14 @@ void WII::readWiiBalanceBoardCalibration() {
/************************************************************/
int8_t WII::getButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
if (index >= sizeof(WII_BUTTONS) / sizeof(WII_BUTTONS[0])) return -1;
const int8_t index = legacyButtonValues(b);
if ((uint8_t) index >= (sizeof(WII_BUTTONS) / sizeof(WII_BUTTONS[0]))) return -1;
return index;
}
int8_t WII::getButtonIndexPro(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
if (index >= sizeof(WII_PROCONTROLLER_BUTTONS) / sizeof(WII_PROCONTROLLER_BUTTONS[0])) return -1;
const int8_t index = legacyButtonValues(b);
if ((uint8_t) index >= (sizeof(WII_PROCONTROLLER_BUTTONS) / sizeof(WII_PROCONTROLLER_BUTTONS[0]))) return -1;
return index;
}

View file

@ -55,8 +55,8 @@ enum DPADEnum {
};
int8_t XBOXONESParser::getButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
if (index >= sizeof(XBOX_ONE_S_BUTTONS) / sizeof(XBOX_ONE_S_BUTTONS[0])) return -1;
const int8_t index = legacyButtonValues(b);
if ((uint8_t) index >= (sizeof(XBOX_ONE_S_BUTTONS) / sizeof(XBOX_ONE_S_BUTTONS[0]))) return -1;
return index;
}

View file

@ -183,87 +183,83 @@ enum ButtonEnum {
/**@}*/
};
inline uint8_t legacyButtonValues(ButtonEnum key) {
uint8_t out;
inline int8_t legacyButtonValues(ButtonEnum key) {
switch (key) {
case UP:
case RED:
out = 0; break;
return 0;
case RIGHT:
case YELLOW:
out = 1; break;
return 1;
case DOWN:
case GREEN:
out = 2; break;
return 2;
case LEFT:
case ORANGE:
out = 3; break;
return 3;
case SELECT:
case SHARE:
case BACK:
case VIEW:
case BLUE:
case CREATE:
out = 4; break;
return 4;
case PLUS:
case START:
case OPTIONS:
case MENU:
out = 5; break;
return 5;
case TWO:
case L3:
out = 6; break;
return 6;
case ONE:
case R3:
out = 7; break;
return 7;
case MINUS:
case L2:
case BLACK:
out = 8; break;
return 8;
case HOME:
case R2:
case WHITE:
out = 9; break;
return 9;
case Z:
case L1:
out = 10; break;
return 10;
case C:
case R1:
out = 11; break;
return 11;
case B:
case TRIANGLE:
out = 12; break;
return 12;
case A:
case CIRCLE:
out = 13; break;
return 13;
case CROSS:
case X:
out = 14; break;
return 14;
case SQUARE:
case Y:
out = 15; break;
return 15;
case L:
case PS:
case XBOX:
out = 16; break;
return 16;
case R:
case MOVE:
case TOUCHPAD:
out = 17; break;
case SYNC:
out = 17; break;
return 17;
case ZL:
case T:
case MICROPHONE:
out = 18; break;
return 18;
case ZR:
out = 19; break;
return 19;
default:
out = 0; break;
return -1;
}
return out;
return -1;
}
/** Joysticks on the PS3 and Xbox controllers. */

View file

@ -63,8 +63,8 @@ const uint16_t XBOX_BUTTONS[] PROGMEM = {
};
inline int8_t getXboxButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b);
if (index >= sizeof(XBOX_BUTTONS) / sizeof(XBOX_BUTTONS[0])) return -1;
const int8_t index = legacyButtonValues(b);
if ((uint8_t) index >= (sizeof(XBOX_BUTTONS) / sizeof(XBOX_BUTTONS[0]))) return -1;
return index;
}