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) { inline int8_t getPS3ButtonIndex(ButtonEnum b) {
const uint8_t index = legacyButtonValues(b); const int8_t index = legacyButtonValues(b);
if (index >= sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0])) return -1; if ((uint8_t) index >= (sizeof(PS3_BUTTONS) / sizeof(PS3_BUTTONS[0]))) return -1;
return index; return index;
} }

View file

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

View file

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

View file

@ -50,7 +50,7 @@ uint8_t PSBuzz::OnInitSuccessful() {
}; };
int8_t PSBuzz::getButtonIndex(ButtonEnum b) { 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 if (index > 4) return -1; // 5 buttons, 0-4 inclusive
return index; return index;
} }

View file

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

View file

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

View file

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