Can now turn LED off by calling setLedOn(OFF) for all controllers

Also renamed all enums, so they does not conflict with user code that easily
This commit is contained in:
Kristian Lauszus 2014-01-04 13:43:49 +01:00
parent ec38c78799
commit 257c96f5c1
18 changed files with 294 additions and 270 deletions

View file

@ -49,26 +49,26 @@ pBtd(p) // pointer to USB class instance - mandatory
Reset(); Reset();
} }
bool PS3BT::getButtonPress(Button b) { bool PS3BT::getButtonPress(ButtonEnum b) {
return (ButtonState & pgm_read_dword(&BUTTONS[(uint8_t)b])); return (ButtonState & pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]));
} }
bool PS3BT::getButtonClick(Button b) { bool PS3BT::getButtonClick(ButtonEnum b) {
uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]); uint32_t button = pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // Clear "click" event ButtonClickState &= ~button; // Clear "click" event
return click; return click;
} }
uint8_t PS3BT::getAnalogButton(Button a) { uint8_t PS3BT::getAnalogButton(ButtonEnum a) {
return (uint8_t)(l2capinbuf[pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a])]); return (uint8_t)(l2capinbuf[pgm_read_byte(&PS3_ANALOG_BUTTONS[(uint8_t)a])]);
} }
uint8_t PS3BT::getAnalogHat(AnalogHat a) { uint8_t PS3BT::getAnalogHat(AnalogHatEnum a) {
return (uint8_t)(l2capinbuf[(uint8_t)a + 15]); return (uint8_t)(l2capinbuf[(uint8_t)a + 15]);
} }
int16_t PS3BT::getSensor(Sensor a) { int16_t PS3BT::getSensor(SensorEnum a) {
if(PS3Connected) { if(PS3Connected) {
if(a == aX || a == aY || a == aZ || a == gZ) if(a == aX || a == aY || a == aZ || a == gZ)
return ((l2capinbuf[(uint16_t)a] << 8) | l2capinbuf[(uint16_t)a + 1]); return ((l2capinbuf[(uint16_t)a] << 8) | l2capinbuf[(uint16_t)a + 1]);
@ -85,7 +85,7 @@ int16_t PS3BT::getSensor(Sensor a) {
return 0; return 0;
} }
double PS3BT::getAngle(Angle a) { double PS3BT::getAngle(AngleEnum a) {
double accXval, accYval, accZval; double accXval, accYval, accZval;
if(PS3Connected) { if(PS3Connected) {
@ -112,7 +112,7 @@ double PS3BT::getAngle(Angle a) {
return (atan2(accXval, accZval) + PI) * RAD_TO_DEG; return (atan2(accXval, accZval) + PI) * RAD_TO_DEG;
} }
double PS3BT::get9DOFValues(Sensor a) { // Thanks to Manfred Piendl double PS3BT::get9DOFValues(SensorEnum a) { // Thanks to Manfred Piendl
if(!PS3MoveConnected) if(!PS3MoveConnected)
return 0; return 0;
int16_t value = getSensor(a); int16_t value = getSensor(a);
@ -156,7 +156,7 @@ String PS3BT::getTemperature() {
return "Error"; return "Error";
} }
bool PS3BT::getStatus(Status c) { bool PS3BT::getStatus(StatusEnum c) {
return (l2capinbuf[(uint16_t)c >> 8] == ((uint8_t)c & 0xff)); return (l2capinbuf[(uint16_t)c >> 8] == ((uint8_t)c & 0xff));
} }
@ -544,7 +544,7 @@ void PS3BT::setRumbleOff() {
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
} }
void PS3BT::setRumbleOn(Rumble mode) { void PS3BT::setRumbleOn(RumbleEnum mode) {
uint8_t power[2] = {0xff, 0x00}; // Defaults to RumbleLow uint8_t power[2] = {0xff, 0x00}; // Defaults to RumbleLow
if(mode == RumbleHigh) { if(mode == RumbleHigh) {
power[0] = 0x00; power[0] = 0x00;
@ -566,18 +566,22 @@ void PS3BT::setLedRaw(uint8_t value) {
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
} }
void PS3BT::setLedOff(LED a) { void PS3BT::setLedOff(LEDEnum a) {
HIDBuffer[11] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1)); HIDBuffer[11] &= ~((uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1));
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
} }
void PS3BT::setLedOn(LED a) { void PS3BT::setLedOn(LEDEnum a) {
HIDBuffer[11] |= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); if(a == OFF)
setLedRaw(0);
else {
HIDBuffer[11] |= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
}
} }
void PS3BT::setLedToggle(LED a) { void PS3BT::setLedToggle(LEDEnum a) {
HIDBuffer[11] ^= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); HIDBuffer[11] ^= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
HID_Command(HIDBuffer, HID_BUFFERSIZE); HID_Command(HIDBuffer, HID_BUFFERSIZE);
} }
@ -602,7 +606,7 @@ void PS3BT::HIDMove_Command(uint8_t* data, uint8_t nbytes) {
timerHID = millis(); timerHID = millis();
} }
void PS3BT::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { //Use this to set the Color using RGB values void PS3BT::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { // Use this to set the Color using RGB values
// Set the Bulb's values into the write buffer // Set the Bulb's values into the write buffer
HIDMoveBuffer[3] = r; HIDMoveBuffer[3] = r;
HIDMoveBuffer[4] = g; HIDMoveBuffer[4] = g;
@ -611,7 +615,7 @@ void PS3BT::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { //Use this to set the
HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE); HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE);
} }
void PS3BT::moveSetBulb(Colors color) { //Use this to set the Color using the predefined colors in enum void PS3BT::moveSetBulb(ColorsEnum color) { // Use this to set the Color using the predefined colors in enum
moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color)); moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color));
} }

72
PS3BT.h
View file

@ -56,32 +56,34 @@ public:
/** @name PS3 Controller functions */ /** @name PS3 Controller functions */
/** /**
* getButtonPress(Button b) will return true as long as the button is held down. * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
* *
* While getButtonClick(Button b) will only return it once. * While getButtonClick(ButtonEnum b) will only return it once.
* *
* So you instance if you need to increase a variable once you would use getButtonClick(Button b), * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(Button b). * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
* @param b ::ButtonEnum to read.
* @return getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press.
*/ */
bool getButtonPress(Button b); bool getButtonPress(ButtonEnum b);
bool getButtonClick(Button b); bool getButtonClick(ButtonEnum b);
/**@}*/ /**@}*/
/** @name PS3 Controller functions */ /** @name PS3 Controller functions */
/** /**
* Used to get the analog value from button presses. * Used to get the analog value from button presses.
* @param a The ::Button to read. * @param a The ::ButtonEnum to read.
* The supported buttons are: * The supported buttons are:
* ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2, * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
* ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T. * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
* @return Analog value in the range of 0-255. * @return Analog value in the range of 0-255.
*/ */
uint8_t getAnalogButton(Button a); uint8_t getAnalogButton(ButtonEnum a);
/** /**
* Used to read the analog joystick. * Used to read the analog joystick.
* @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY. * @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
* @return Return the analog value in the range of 0-255. * @return Return the analog value in the range of 0-255.
*/ */
uint8_t getAnalogHat(AnalogHat a); uint8_t getAnalogHat(AnalogHatEnum a);
/** /**
* Used to read the sensors inside the Dualshock 3 and Move controller. * Used to read the sensors inside the Dualshock 3 and Move controller.
* @param a * @param a
@ -90,47 +92,47 @@ public:
* and a temperature sensor inside. * and a temperature sensor inside.
* @return Return the raw sensor value. * @return Return the raw sensor value.
*/ */
int16_t getSensor(Sensor a); int16_t getSensor(SensorEnum a);
/** /**
* Use this to get ::Pitch and ::Roll calculated using the accelerometer. * Use this to get ::Pitch and ::Roll calculated using the accelerometer.
* @param a Either ::Pitch or ::Roll. * @param a Either ::Pitch or ::Roll.
* @return Return the angle in the range of 0-360. * @return Return the angle in the range of 0-360.
*/ */
double getAngle(Angle a); double getAngle(AngleEnum a);
/** /**
* Read the sensors inside the Move controller. * Read the sensors inside the Move controller.
* @param a ::aXmove, ::aYmove, ::aZmove, ::gXmove, ::gYmove, ::gZmove, ::mXmove, ::mYmove, and ::mXmove. * @param a ::aXmove, ::aYmove, ::aZmove, ::gXmove, ::gYmove, ::gZmove, ::mXmove, ::mYmove, and ::mXmove.
* @return The value in SI units. * @return The value in SI units.
*/ */
double get9DOFValues(Sensor a); double get9DOFValues(SensorEnum a);
/** /**
* Get the ::Status from the controller. * Get the status from the controller.
* @param c The ::Status you want to read. * @param c The ::StatusEnum you want to read.
* @return True if correct and false if not. * @return True if correct and false if not.
*/ */
bool getStatus(Status c); bool getStatus(StatusEnum c);
/** /**
* Read all the available ::Status from the controller. * Read all the available ::StatusEnum from the controller.
* @return One large string with all the information. * @return One large string with all the information.
*/ */
String getStatusString(); String getStatusString();
/** /**
* Read the temperature from the Move controller. * Read the temperature from the Move controller.
* @return The temperature in degrees celsius. * @return The temperature in degrees Celsius.
*/ */
String getTemperature(); String getTemperature();
/** Used to set all LEDs and ::Rumble off. */ /** Used to set all LEDs and rumble off. */
void setAllOff(); void setAllOff();
/** Turn off ::Rumble. */ /** Turn off rumble. */
void setRumbleOff(); void setRumbleOff();
/** /**
* Turn on ::Rumble. * Turn on rumble.
* @param mode Either ::RumbleHigh or ::RumbleLow. * @param mode Either ::RumbleHigh or ::RumbleLow.
*/ */
void setRumbleOn(Rumble mode); void setRumbleOn(RumbleEnum mode);
/** /**
* Turn on ::Rumble using custom duration and power. * Turn on rumble using custom duration and power.
* @param rightDuration The duration of the right/low rumble effect. * @param rightDuration The duration of the right/low rumble effect.
* @param rightPower The intensity of the right/low rumble effect. * @param rightPower The intensity of the right/low rumble effect.
* @param leftDuration The duration of the left/high rumble effect. * @param leftDuration The duration of the left/high rumble effect.
@ -139,8 +141,8 @@ public:
void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower); void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
/** /**
* Set LED value without using the ::LED enum. * Set LED value without using ::LEDEnum.
* @param value See: ::LED enum. * @param value See: ::LEDEnum.
*/ */
void setLedRaw(uint8_t value); void setLedRaw(uint8_t value);
@ -149,20 +151,20 @@ public:
setLedRaw(0); setLedRaw(0);
}; };
/** /**
* Turn the specific ::LED off. * Turn the specific LED off.
* @param a The ::LED to turn off. * @param a The ::LEDEnum to turn off.
*/ */
void setLedOff(LED a); void setLedOff(LEDEnum a);
/** /**
* Turn the specific ::LED on. * Turn the specific LED on.
* @param a The ::LED to turn on. * @param a The ::LEDEnum to turn on.
*/ */
void setLedOn(LED a); void setLedOn(LEDEnum a);
/** /**
* Toggle the specific ::LED. * Toggle the specific LED.
* @param a The ::LED to toggle. * @param a The ::LEDEnum to toggle.
*/ */
void setLedToggle(LED a); void setLedToggle(LEDEnum a);
/** /**
* Use this to set the Color using RGB values. * Use this to set the Color using RGB values.
@ -170,10 +172,10 @@ public:
*/ */
void moveSetBulb(uint8_t r, uint8_t g, uint8_t b); void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
/** /**
* Use this to set the color using the predefined colors in ::Colors. * Use this to set the color using the predefined colors in ::ColorsEnum.
* @param color The desired color. * @param color The desired color.
*/ */
void moveSetBulb(Colors color); void moveSetBulb(ColorsEnum color);
/** /**
* Set the rumble value inside the Move controller. * Set the rumble value inside the Move controller.
* @param rumble The desired value in the range from 64-255. * @param rumble The desired value in the range from 64-255.

View file

@ -20,8 +20,11 @@
#include "controllerEnums.h" #include "controllerEnums.h"
/** Size of the output report buffer for the Dualshock and Navigation controllers */
#define PS3_REPORT_BUFFER_SIZE 48
/** Report buffer for all PS3 commands */ /** Report buffer for all PS3 commands */
const uint8_t PS3_REPORT_BUFFER[] PROGMEM = { const uint8_t PS3_REPORT_BUFFER[PS3_REPORT_BUFFER_SIZE] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xff, 0x27, 0x10, 0x00, 0x32, 0xff, 0x27, 0x10, 0x00, 0x32,
@ -33,14 +36,12 @@ const uint8_t PS3_REPORT_BUFFER[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
}; };
/** Size of the output report buffer for the Dualshock and Navigation controllers */
#define PS3_REPORT_BUFFER_SIZE 48
/** Size of the output report buffer for the Move Controller */ /** Size of the output report buffer for the Move Controller */
#define MOVE_REPORT_BUFFER_SIZE 7 #define MOVE_REPORT_BUFFER_SIZE 7
/** Used to set the LEDs on the controllers */ /** Used to set the LEDs on the controllers */
const uint8_t LEDS[] PROGMEM = { const uint8_t PS3_LEDS[] PROGMEM = {
0x00, // OFF
0x01, // LED1 0x01, // LED1
0x02, // LED2 0x02, // LED2
0x04, // LED3 0x04, // LED3
@ -51,15 +52,15 @@ const uint8_t LEDS[] PROGMEM = {
0x0C, // LED7 0x0C, // LED7
0x0D, // LED8 0x0D, // LED8
0x0E, // LED9 0x0E, // LED9
0x0F // LED10 0x0F, // LED10
}; };
/** /**
* Buttons on the controllers * Buttons on the controllers
* *
* <B>Note:</B> that the location is shiftet 9 when it's connected via USB. * <B>Note:</B> that the location is shifted 9 when it's connected via USB.
*/ */
const uint32_t BUTTONS[] PROGMEM = { const uint32_t PS3_BUTTONS[] PROGMEM = {
0x10, // UP 0x10, // UP
0x20, // RIGHT 0x20, // RIGHT
0x40, // DOWN 0x40, // DOWN
@ -82,15 +83,15 @@ const uint32_t BUTTONS[] PROGMEM = {
0x010000, // PS 0x010000, // PS
0x080000, // MOVE - covers 12 bits - we only need to read the top 8 0x080000, // MOVE - covers 12 bits - we only need to read the top 8
0x100000 // T - covers 12 bits - we only need to read the top 8 0x100000, // T - covers 12 bits - we only need to read the top 8
}; };
/** /**
* Analog buttons on the controllers * Analog buttons on the controllers
* *
* <B>Note:</B> that the location is shiftet 9 when it's connected via USB. * <B>Note:</B> that the location is shifted 9 when it's connected via USB.
*/ */
const uint8_t ANALOGBUTTONS[] PROGMEM = { const uint8_t PS3_ANALOG_BUTTONS[] PROGMEM = {
23, // UP_ANALOG 23, // UP_ANALOG
24, // RIGHT_ANALOG 24, // RIGHT_ANALOG
25, // DOWN_ANALOG 25, // DOWN_ANALOG
@ -108,11 +109,11 @@ const uint8_t ANALOGBUTTONS[] PROGMEM = {
0, 0, // Skip PS and MOVE 0, 0, // Skip PS and MOVE
// Playstation Move Controller // Playstation Move Controller
15 // T_ANALOG - Both at byte 14 (last reading) and byte 15 (current reading) 15, // T_ANALOG - Both at byte 14 (last reading) and byte 15 (current reading)
}; };
/** Used to set the colors of the move controller. */ /** Used to set the colors of the move controller. */
enum Colors { enum ColorsEnum {
/** r = 255, g = 0, b = 0 */ /** r = 255, g = 0, b = 0 */
Red = 0xFF0000, Red = 0xFF0000,
/** r = 0, g = 255, b = 0 */ /** r = 0, g = 255, b = 0 */
@ -136,9 +137,9 @@ enum Colors {
/** /**
* Sensors inside the Sixaxis Dualshock 3 and Move controller. * Sensors inside the Sixaxis Dualshock 3 and Move controller.
* *
* <B>Note:</B> that the location is shiftet 9 when it's connected via USB. * <B>Note:</B> that the location is shifted 9 when it's connected via USB.
*/ */
enum Sensor { enum SensorEnum {
/** Accelerometer x-axis */ /** Accelerometer x-axis */
aX = 50, aX = 50,
/** Accelerometer y-axis */ /** Accelerometer y-axis */
@ -174,13 +175,13 @@ enum Sensor {
}; };
/** Used to get the angle calculated using the accelerometer. */ /** Used to get the angle calculated using the accelerometer. */
enum Angle { enum AngleEnum {
Pitch = 0x01, Pitch = 0x01,
Roll = 0x02, Roll = 0x02,
}; };
enum Status { enum StatusEnum {
// Note that the location is shiftet 9 when it's connected via USB // Note that the location is shifted 9 when it's connected via USB
// Byte location | bit location // Byte location | bit location
Plugged = (38 << 8) | 0x02, Plugged = (38 << 8) | 0x02,
Unplugged = (38 << 8) | 0x03, Unplugged = (38 << 8) | 0x03,
@ -201,13 +202,13 @@ enum Status {
MoveHigh = (21 << 8) | 0x04, MoveHigh = (21 << 8) | 0x04,
MoveFull = (21 << 8) | 0x05, MoveFull = (21 << 8) | 0x05,
CableRumble = (40 << 8) | 0x10, //Opperating by USB and rumble is turned on CableRumble = (40 << 8) | 0x10, // Operating by USB and rumble is turned on
Cable = (40 << 8) | 0x12, //Opperating by USB and rumble is turned off Cable = (40 << 8) | 0x12, // Operating by USB and rumble is turned off
BluetoothRumble = (40 << 8) | 0x14, //Opperating by bluetooth and rumble is turned on BluetoothRumble = (40 << 8) | 0x14, // Operating by Bluetooth and rumble is turned on
Bluetooth = (40 << 8) | 0x16, //Opperating by bluetooth and rumble is turned off Bluetooth = (40 << 8) | 0x16, // Operating by Bluetooth and rumble is turned off
}; };
enum Rumble { enum RumbleEnum {
RumbleHigh = 0x10, RumbleHigh = 0x10,
RumbleLow = 0x20, RumbleLow = 0x20,
}; };

View file

@ -312,30 +312,30 @@ void PS3USB::printReport() { // Uncomment "#define PRINTREPORT" to print the rep
#endif #endif
} }
bool PS3USB::getButtonPress(Button b) { bool PS3USB::getButtonPress(ButtonEnum b) {
return (ButtonState & pgm_read_dword(&BUTTONS[(uint8_t)b])); return (ButtonState & pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]));
} }
bool PS3USB::getButtonClick(Button b) { bool PS3USB::getButtonClick(ButtonEnum b) {
uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]); uint32_t button = pgm_read_dword(&PS3_BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // Clear "click" event ButtonClickState &= ~button; // Clear "click" event
return click; return click;
} }
uint8_t PS3USB::getAnalogButton(Button a) { uint8_t PS3USB::getAnalogButton(ButtonEnum a) {
return (uint8_t)(readBuf[(pgm_read_byte(&ANALOGBUTTONS[(uint8_t)a])) - 9]); return (uint8_t)(readBuf[(pgm_read_byte(&PS3_ANALOG_BUTTONS[(uint8_t)a])) - 9]);
} }
uint8_t PS3USB::getAnalogHat(AnalogHat a) { uint8_t PS3USB::getAnalogHat(AnalogHatEnum a) {
return (uint8_t)(readBuf[((uint8_t)a + 6)]); return (uint8_t)(readBuf[((uint8_t)a + 6)]);
} }
uint16_t PS3USB::getSensor(Sensor a) { uint16_t PS3USB::getSensor(SensorEnum a) {
return ((readBuf[((uint16_t)a) - 9] << 8) | readBuf[((uint16_t)a + 1) - 9]); return ((readBuf[((uint16_t)a) - 9] << 8) | readBuf[((uint16_t)a + 1) - 9]);
} }
double PS3USB::getAngle(Angle a) { double PS3USB::getAngle(AngleEnum a) {
if(PS3Connected) { if(PS3Connected) {
double accXval; double accXval;
double accYval; double accYval;
@ -358,7 +358,7 @@ double PS3USB::getAngle(Angle a) {
return 0; return 0;
} }
bool PS3USB::getStatus(Status c) { bool PS3USB::getStatus(StatusEnum c) {
return (readBuf[((uint16_t)c >> 8) - 9] == ((uint8_t)c & 0xff)); return (readBuf[((uint16_t)c >> 8) - 9] == ((uint8_t)c & 0xff));
} }
@ -419,7 +419,7 @@ void PS3USB::setRumbleOff() {
PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
} }
void PS3USB::setRumbleOn(Rumble mode) { void PS3USB::setRumbleOn(RumbleEnum mode) {
if((mode & 0x30) > 0x00) { if((mode & 0x30) > 0x00) {
uint8_t power[2] = {0xff, 0x00}; // Defaults to RumbleLow uint8_t power[2] = {0xff, 0x00}; // Defaults to RumbleLow
if(mode == RumbleHigh) { if(mode == RumbleHigh) {
@ -443,18 +443,22 @@ void PS3USB::setLedRaw(uint8_t value) {
PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
} }
void PS3USB::setLedOff(LED a) { void PS3USB::setLedOff(LEDEnum a) {
writeBuf[9] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1)); writeBuf[9] &= ~((uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1));
PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
} }
void PS3USB::setLedOn(LED a) { void PS3USB::setLedOn(LEDEnum a) {
writeBuf[9] |= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); if(a == OFF)
setLedRaw(0);
else {
writeBuf[9] |= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
}
} }
void PS3USB::setLedToggle(LED a) { void PS3USB::setLedToggle(LEDEnum a) {
writeBuf[9] ^= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); writeBuf[9] ^= (uint8_t)((pgm_read_byte(&PS3_LEDS[(uint8_t)a]) & 0x0f) << 1);
PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE);
} }
@ -506,7 +510,7 @@ void PS3USB::moveSetBulb(uint8_t r, uint8_t g, uint8_t b) { // Use this to set t
Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE); Move_Command(writeBuf, MOVE_REPORT_BUFFER_SIZE);
} }
void PS3USB::moveSetBulb(Colors color) { // Use this to set the Color using the predefined colors in "enums.h" void PS3USB::moveSetBulb(ColorsEnum color) { // Use this to set the Color using the predefined colors in "enums.h"
moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color)); moveSetBulb((uint8_t)(color >> 16), (uint8_t)(color >> 8), (uint8_t)(color));
} }

View file

@ -146,68 +146,70 @@ public:
/** @name PS3 Controller functions */ /** @name PS3 Controller functions */
/** /**
* getButtonPress(Button b) will return true as long as the button is held down. * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
* *
* While getButtonClick(Button b) will only return it once. * While getButtonClick(ButtonEnum b) will only return it once.
* *
* So you instance if you need to increase a variable once you would use getButtonClick(Button b), * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(Button b). * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
* @param b ::ButtonEnum to read.
* @return getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press.
*/ */
bool getButtonPress(Button b); bool getButtonPress(ButtonEnum b);
bool getButtonClick(Button b); bool getButtonClick(ButtonEnum b);
/**@}*/ /**@}*/
/** @name PS3 Controller functions */ /** @name PS3 Controller functions */
/** /**
* Used to get the analog value from button presses. * Used to get the analog value from button presses.
* @param a The ::Button to read. * @param a The ::ButtonEnum to read.
* The supported buttons are: * The supported buttons are:
* ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2, * ::UP, ::RIGHT, ::DOWN, ::LEFT, ::L1, ::L2, ::R1, ::R2,
* ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T. * ::TRIANGLE, ::CIRCLE, ::CROSS, ::SQUARE, and ::T.
* @return Analog value in the range of 0-255. * @return Analog value in the range of 0-255.
*/ */
uint8_t getAnalogButton(Button a); uint8_t getAnalogButton(ButtonEnum a);
/** /**
* Used to read the analog joystick. * Used to read the analog joystick.
* @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY. * @param a ::LeftHatX, ::LeftHatY, ::RightHatX, and ::RightHatY.
* @return Return the analog value in the range of 0-255. * @return Return the analog value in the range of 0-255.
*/ */
uint8_t getAnalogHat(AnalogHat a); uint8_t getAnalogHat(AnalogHatEnum a);
/** /**
* Used to read the sensors inside the Dualshock 3 controller. * Used to read the sensors inside the Dualshock 3 controller.
* @param a * @param a
* The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside. * The Dualshock 3 has a 3-axis accelerometer and a 1-axis gyro inside.
* @return Return the raw sensor value. * @return Return the raw sensor value.
*/ */
uint16_t getSensor(Sensor a); uint16_t getSensor(SensorEnum a);
/** /**
* Use this to get ::Pitch and ::Roll calculated using the accelerometer. * Use this to get ::Pitch and ::Roll calculated using the accelerometer.
* @param a Either ::Pitch or ::Roll. * @param a Either ::Pitch or ::Roll.
* @return Return the angle in the range of 0-360. * @return Return the angle in the range of 0-360.
*/ */
double getAngle(Angle a); double getAngle(AngleEnum a);
/** /**
* Get the ::Status from the controller. * Get the ::StatusEnum from the controller.
* @param c The ::Status you want to read. * @param c The ::StatusEnum you want to read.
* @return True if correct and false if not. * @return True if correct and false if not.
*/ */
bool getStatus(Status c); bool getStatus(StatusEnum c);
/** /**
* Read all the available ::Status from the controller. * Read all the available ::StatusEnum from the controller.
* @return One large string with all the information. * @return One large string with all the information.
*/ */
String getStatusString(); String getStatusString();
/** Used to set all LEDs and ::Rumble off. */ /** Used to set all LEDs and rumble off. */
void setAllOff(); void setAllOff();
/** Turn off ::Rumble. */ /** Turn off rumble. */
void setRumbleOff(); void setRumbleOff();
/** /**
* Turn on ::Rumble. * Turn on rumble.
* @param mode Either ::RumbleHigh or ::RumbleLow. * @param mode Either ::RumbleHigh or ::RumbleLow.
*/ */
void setRumbleOn(Rumble mode); void setRumbleOn(RumbleEnum mode);
/** /**
* Turn on ::Rumble using custom duration and power. * Turn on rumble using custom duration and power.
* @param rightDuration The duration of the right/low rumble effect. * @param rightDuration The duration of the right/low rumble effect.
* @param rightPower The intensity of the right/low rumble effect. * @param rightPower The intensity of the right/low rumble effect.
* @param leftDuration The duration of the left/high rumble effect. * @param leftDuration The duration of the left/high rumble effect.
@ -216,8 +218,8 @@ public:
void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower); void setRumbleOn(uint8_t rightDuration, uint8_t rightPower, uint8_t leftDuration, uint8_t leftPower);
/** /**
* Set LED value without using the ::LED enum. * Set LED value without using the ::LEDEnum.
* @param value See: ::LED enum. * @param value See: ::LEDEnum.
*/ */
void setLedRaw(uint8_t value); void setLedRaw(uint8_t value);
@ -226,20 +228,20 @@ public:
setLedRaw(0); setLedRaw(0);
} }
/** /**
* Turn the specific ::LED off. * Turn the specific ::LEDEnum off.
* @param a The ::LED to turn off. * @param a The ::LEDEnum to turn off.
*/ */
void setLedOff(LED a); void setLedOff(LEDEnum a);
/** /**
* Turn the specific ::LED on. * Turn the specific ::LEDEnum on.
* @param a The ::LED to turn on. * @param a The ::LEDEnum to turn on.
*/ */
void setLedOn(LED a); void setLedOn(LEDEnum a);
/** /**
* Toggle the specific ::LED. * Toggle the specific ::LEDEnum.
* @param a The ::LED to toggle. * @param a The ::LEDEnum to toggle.
*/ */
void setLedToggle(LED a); void setLedToggle(LEDEnum a);
/** /**
* Use this to set the Color using RGB values. * Use this to set the Color using RGB values.
@ -247,10 +249,10 @@ public:
*/ */
void moveSetBulb(uint8_t r, uint8_t g, uint8_t b); void moveSetBulb(uint8_t r, uint8_t g, uint8_t b);
/** /**
* Use this to set the color using the predefined colors in ::Colors. * Use this to set the color using the predefined colors in ::ColorsEnum.
* @param color The desired color. * @param color The desired color.
*/ */
void moveSetBulb(Colors color); void moveSetBulb(ColorsEnum color);
/** /**
* Set the rumble value inside the Move controller. * Set the rumble value inside the Move controller.
* @param rumble The desired value in the range from 64-255. * @param rumble The desired value in the range from 64-255.

47
Wii.cpp
View file

@ -22,7 +22,8 @@
//#define EXTRADEBUG // Uncomment to get even more debugging data //#define EXTRADEBUG // Uncomment to get even more debugging data
//#define PRINTREPORT // Uncomment to print the report send by the Wii controllers //#define PRINTREPORT // Uncomment to print the report send by the Wii controllers
const uint8_t LEDS[] PROGMEM = { const uint8_t WII_LEDS[] PROGMEM = {
0x00, // OFF
0x10, // LED1 0x10, // LED1
0x20, // LED2 0x20, // LED2
0x40, // LED3 0x40, // LED3
@ -33,10 +34,10 @@ const uint8_t LEDS[] PROGMEM = {
0xC0, // LED7 0xC0, // LED7
0xD0, // LED8 0xD0, // LED8
0xE0, // LED9 0xE0, // LED9
0xF0 // LED10 0xF0, // LED10
}; };
const uint32_t BUTTONS[] PROGMEM = { const uint32_t WII_BUTTONS[] PROGMEM = {
0x00008, // UP 0x00008, // UP
0x00002, // RIGHT 0x00002, // RIGHT
0x00004, // DOWN 0x00004, // DOWN
@ -53,9 +54,9 @@ const uint32_t BUTTONS[] PROGMEM = {
0x20000, // C 0x20000, // C
0x00400, // B 0x00400, // B
0x00800 // A 0x00800, // A
}; };
const uint32_t PROCONTROLLERBUTTONS[] PROGMEM = { const uint32_t WII_PROCONTROLLER_BUTTONS[] PROGMEM = {
0x00100, // UP 0x00100, // UP
0x00080, // RIGHT 0x00080, // RIGHT
0x00040, // DOWN 0x00040, // DOWN
@ -78,7 +79,7 @@ const uint32_t PROCONTROLLERBUTTONS[] PROGMEM = {
0x00020, // L 0x00020, // L
0x00002, // R 0x00002, // R
0x08000, // ZL 0x08000, // ZL
0x00400 // ZR 0x00400, // ZR
}; };
WII::WII(BTD *p, bool pair) : WII::WII(BTD *p, bool pair) :
@ -845,7 +846,7 @@ void WII::Run() {
/************************************************************/ /************************************************************/
void WII::HID_Command(uint8_t* data, uint8_t nbytes) { void WII::HID_Command(uint8_t* data, uint8_t nbytes) {
if(motionPlusInside) if(motionPlusInside)
pBtd->L2CAP_Command(hci_handle, data, nbytes, interrupt_scid[0], interrupt_scid[1]); // It's the new wiimote with the Motion Plus Inside pBtd->L2CAP_Command(hci_handle, data, nbytes, interrupt_scid[0], interrupt_scid[1]); // It's the new Wiimote with the Motion Plus Inside or Wii U Pro controller
else else
pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]); pBtd->L2CAP_Command(hci_handle, data, nbytes, control_scid[0], control_scid[1]);
} }
@ -880,21 +881,25 @@ void WII::setLedRaw(uint8_t value) {
HID_Command(HIDBuffer, 3); HID_Command(HIDBuffer, 3);
} }
void WII::setLedOff(LED a) { void WII::setLedOff(LEDEnum a) {
HIDBuffer[1] = 0x11; HIDBuffer[1] = 0x11;
HIDBuffer[2] &= ~(pgm_read_byte(&LEDS[(uint8_t)a])); HIDBuffer[2] &= ~(pgm_read_byte(&WII_LEDS[(uint8_t)a]));
HID_Command(HIDBuffer, 3); HID_Command(HIDBuffer, 3);
} }
void WII::setLedOn(LED a) { void WII::setLedOn(LEDEnum a) {
if(a == OFF)
setLedRaw(0);
else {
HIDBuffer[1] = 0x11; HIDBuffer[1] = 0x11;
HIDBuffer[2] |= pgm_read_byte(&LEDS[(uint8_t)a]); HIDBuffer[2] |= pgm_read_byte(&WII_LEDS[(uint8_t)a]);
HID_Command(HIDBuffer, 3); HID_Command(HIDBuffer, 3);
}
} }
void WII::setLedToggle(LED a) { void WII::setLedToggle(LEDEnum a) {
HIDBuffer[1] = 0x11; HIDBuffer[1] = 0x11;
HIDBuffer[2] ^= pgm_read_byte(&LEDS[(uint8_t)a]); HIDBuffer[2] ^= pgm_read_byte(&WII_LEDS[(uint8_t)a]);
HID_Command(HIDBuffer, 3); HID_Command(HIDBuffer, 3);
} }
@ -1033,25 +1038,25 @@ void WII::checkMotionPresent() {
/************************************************************/ /************************************************************/
bool WII::getButtonPress(Button b) { // Return true when a button is pressed bool WII::getButtonPress(ButtonEnum b) { // Return true when a button is pressed
if(wiiUProControllerConnected) if(wiiUProControllerConnected)
return (ButtonState & pgm_read_dword(&PROCONTROLLERBUTTONS[(uint8_t)b])); return (ButtonState & pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[(uint8_t)b]));
else else
return (ButtonState & pgm_read_dword(&BUTTONS[(uint8_t)b])); return (ButtonState & pgm_read_dword(&WII_BUTTONS[(uint8_t)b]));
} }
bool WII::getButtonClick(Button b) { // Only return true when a button is clicked bool WII::getButtonClick(ButtonEnum b) { // Only return true when a button is clicked
uint32_t button; uint32_t button;
if(wiiUProControllerConnected) if(wiiUProControllerConnected)
button = pgm_read_dword(&PROCONTROLLERBUTTONS[(uint8_t)b]); button = pgm_read_dword(&WII_PROCONTROLLER_BUTTONS[(uint8_t)b]);
else else
button = pgm_read_dword(&BUTTONS[(uint8_t)b]); button = pgm_read_dword(&WII_BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // clear "click" event
return click; return click;
} }
uint8_t WII::getAnalogHat(Hat a) { uint8_t WII::getAnalogHat(HatEnum a) {
if(!nunchuckConnected) if(!nunchuckConnected)
return 127; // Return center position return 127; // Return center position
else { else {
@ -1063,7 +1068,7 @@ uint8_t WII::getAnalogHat(Hat a) {
} }
} }
uint16_t WII::getAnalogHat(AnalogHat a) { uint16_t WII::getAnalogHat(AnalogHatEnum a) {
if(!wiiUProControllerConnected) if(!wiiUProControllerConnected)
return 2000; return 2000;
else { else {

40
Wii.h
View file

@ -32,7 +32,7 @@
#define wii_clear_flag(flag) (wii_event_flag &= ~(flag)) #define wii_clear_flag(flag) (wii_event_flag &= ~(flag))
/** Enum used to read the joystick on the Nunchuck. */ /** Enum used to read the joystick on the Nunchuck. */
enum Hat { enum HatEnum {
/** Read the x-axis on the Nunchuck joystick. */ /** Read the x-axis on the Nunchuck joystick. */
HatX = 0, HatX = 0,
/** Read the y-axis on the Nunchuck joystick. */ /** Read the y-axis on the Nunchuck joystick. */
@ -76,9 +76,11 @@ public:
* *
* So you instance if you need to increase a variable once you would use getButtonClick(Button b), * So you instance if you need to increase a variable once you would use getButtonClick(Button b),
* but if you need to drive a robot forward you would use getButtonPress(Button b). * but if you need to drive a robot forward you would use getButtonPress(Button b).
* @param b ::ButtonEnum to read.
* @return getButtonPress(ButtonEnum b) will return a true as long as a button is held down, while getButtonClick(ButtonEnum b) will return true once for each button press.
*/ */
bool getButtonPress(Button b); bool getButtonPress(ButtonEnum b);
bool getButtonClick(Button b); bool getButtonClick(ButtonEnum b);
/**@}*/ /**@}*/
/** @name Wii Controller functions */ /** @name Wii Controller functions */
@ -93,13 +95,13 @@ public:
* @param a Either ::HatX or ::HatY. * @param a Either ::HatX or ::HatY.
* @return Return the analog value in the range from approximately 25-230. * @return Return the analog value in the range from approximately 25-230.
*/ */
uint8_t getAnalogHat(Hat a); uint8_t getAnalogHat(HatEnum a);
/** /**
* Used to read the joystick of the Wii U Pro Controller. * Used to read the joystick of the Wii U Pro Controller.
* @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
* @return Return the analog value in the range from approximately 800-3200. * @return Return the analog value in the range from approximately 800-3200.
*/ */
uint16_t getAnalogHat(AnalogHat a); uint16_t getAnalogHat(AnalogHatEnum a);
/** /**
* Pitch calculated from the Wiimote. A complimentary filter is used if the Motion Plus is connected. * Pitch calculated from the Wiimote. A complimentary filter is used if the Motion Plus is connected.
@ -141,8 +143,8 @@ public:
void setRumbleToggle(); void setRumbleToggle();
/** /**
* Set LED value without using the ::LED enum. * Set LED value without using the ::LEDEnum.
* @param value See: ::LED enum. * @param value See: ::LEDEnum.
*/ */
void setLedRaw(uint8_t value); void setLedRaw(uint8_t value);
@ -151,26 +153,26 @@ public:
setLedRaw(0); setLedRaw(0);
}; };
/** /**
* Turn the specific ::LED off. * Turn the specific ::LEDEnum off.
* @param a The ::LED to turn off. * @param a The ::LEDEnum to turn off.
*/ */
void setLedOff(LED a); void setLedOff(LEDEnum a);
/** /**
* Turn the specific ::LED on. * Turn the specific ::LEDEnum on.
* @param a The ::LED to turn on. * @param a The ::LEDEnum to turn on.
*/ */
void setLedOn(LED a); void setLedOn(LEDEnum a);
/** /**
* Toggle the specific ::LED. * Toggle the specific ::LEDEnum.
* @param a The ::LED to toggle. * @param a The ::LEDEnum to toggle.
*/ */
void setLedToggle(LED a); void setLedToggle(LEDEnum a);
/** /**
* This will set the LEDs, so the user can see which connections are active. * This will set the LEDs, so the user can see which connections are active.
* *
* The first ::LED indicate that the Wiimote is connected, * The first ::LEDEnum indicate that the Wiimote is connected,
* the second ::LED indicate indicate that a Motion Plus is also connected * the second ::LEDEnum indicate indicate that a Motion Plus is also connected
* the third ::LED will indicate that a Nunchuck controller is also connected. * the third ::LEDEnum will indicate that a Nunchuck controller is also connected.
*/ */
void setLedStatus(); void setLedStatus();

View file

@ -21,7 +21,7 @@
//#define PRINTREPORT // Uncomment to print the report send by the Xbox controller //#define PRINTREPORT // Uncomment to print the report send by the Xbox controller
/** Buttons on the controllers */ /** Buttons on the controllers */
const uint8_t XBOXOLDBUTTONS[] PROGMEM = { const uint8_t XBOXOLD_BUTTONS[] PROGMEM = {
0x01, // UP 0x01, // UP
0x08, // RIGHT 0x08, // RIGHT
0x02, // DOWN 0x02, // DOWN
@ -291,15 +291,15 @@ void XBOXOLD::printReport(uint16_t length) { //Uncomment "#define PRINTREPORT" t
#endif #endif
} }
uint8_t XBOXOLD::getButtonPress(Button b) { uint8_t XBOXOLD::getButtonPress(ButtonEnum b) {
uint8_t button = pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b]); uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]);
if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
return buttonValues[button]; // Analog buttons return buttonValues[button]; // Analog buttons
return (ButtonState & button); // Digital buttons return (ButtonState & button); // Digital buttons
} }
bool XBOXOLD::getButtonClick(Button b) { bool XBOXOLD::getButtonClick(ButtonEnum b) {
uint8_t button = pgm_read_byte(&XBOXOLDBUTTONS[(uint8_t)b]); uint8_t button = pgm_read_byte(&XBOXOLD_BUTTONS[(uint8_t)b]);
if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) { // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons if(b == A || b == B || b == X || b == Y || b == BLACK || b == WHITE || b == L1 || b == R1) { // A, B, X, Y, BLACK, WHITE, L1, and R1 are analog buttons
if(buttonClicked[button]) { if(buttonClicked[button]) {
buttonClicked[button] = false; buttonClicked[button] = false;
@ -313,7 +313,7 @@ bool XBOXOLD::getButtonClick(Button b) {
return click; return click;
} }
int16_t XBOXOLD::getAnalogHat(AnalogHat a) { int16_t XBOXOLD::getAnalogHat(AnalogHatEnum a) {
return hatValue[a]; return hatValue[a];
} }

View file

@ -106,18 +106,17 @@ public:
/** @name Xbox Controller functions */ /** @name Xbox Controller functions */
/** /**
* getButtonPress(Button b) will return true as long as the button is held down. * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
* *
* While getButtonClick(Button b) will only return it once. * While getButtonClick(ButtonEnum b) will only return it once.
* *
* So you instance if you need to increase a variable once you would use getButtonClick(Button b), * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(Button b). * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
* @param b ::Button to read. * @param b ::ButtonEnum to read.
* @return getButtonClick(Button b) will return a bool, but getButtonPress(Button b) * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
* will return a byte if reading ::L2 or ::R2.
*/ */
uint8_t getButtonPress(Button b); uint8_t getButtonPress(ButtonEnum b);
bool getButtonClick(Button b); bool getButtonClick(ButtonEnum b);
/**@}*/ /**@}*/
/** @name Xbox Controller functions */ /** @name Xbox Controller functions */
@ -126,7 +125,7 @@ public:
* @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
* @return Returns a signed 16-bit integer. * @return Returns a signed 16-bit integer.
*/ */
int16_t getAnalogHat(AnalogHat a); int16_t getAnalogHat(AnalogHatEnum a);
/** Turn rumble off the controller. */ /** Turn rumble off the controller. */
void setRumbleOff() { void setRumbleOff() {

View file

@ -406,15 +406,15 @@ void XBOXRECV::printReport(uint8_t controller, uint8_t nBytes) { //Uncomment "#d
#endif #endif
} }
uint8_t XBOXRECV::getButtonPress(Button b, uint8_t controller) { uint8_t XBOXRECV::getButtonPress(ButtonEnum b, uint8_t controller) {
if(b == L2) // These are analog buttons if(b == L2) // These are analog buttons
return (uint8_t)(ButtonState[controller] >> 8); return (uint8_t)(ButtonState[controller] >> 8);
else if(b == R2) else if(b == R2)
return (uint8_t)ButtonState[controller]; return (uint8_t)ButtonState[controller];
return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16)); return (bool)(ButtonState[controller] & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16));
} }
bool XBOXRECV::getButtonClick(Button b, uint8_t controller) { bool XBOXRECV::getButtonClick(ButtonEnum b, uint8_t controller) {
if(b == L2) { if(b == L2) {
if(L2Clicked[controller]) { if(L2Clicked[controller]) {
L2Clicked[controller] = false; L2Clicked[controller] = false;
@ -428,13 +428,13 @@ bool XBOXRECV::getButtonClick(Button b, uint8_t controller) {
} }
return false; return false;
} }
uint16_t button = pgm_read_word(&XBOXBUTTONS[(uint8_t)b]); uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState[controller] & button); bool click = (ButtonClickState[controller] & button);
ButtonClickState[controller] &= ~button; // clear "click" event ButtonClickState[controller] &= ~button; // clear "click" event
return click; return click;
} }
int16_t XBOXRECV::getAnalogHat(AnalogHat a, uint8_t controller) { int16_t XBOXRECV::getAnalogHat(AnalogHatEnum a, uint8_t controller) {
return hatValue[controller][a]; return hatValue[controller][a];
} }
@ -512,16 +512,18 @@ void XBOXRECV::setLedRaw(uint8_t value, uint8_t controller) {
XboxCommand(controller, writeBuf, 4); XboxCommand(controller, writeBuf, 4);
} }
void XBOXRECV::setLedOn(LED led, uint8_t controller) { void XBOXRECV::setLedOn(LEDEnum led, uint8_t controller) {
if(led != ALL) // All LEDs can't be on a the same time if(led == OFF)
setLedRaw(pgm_read_byte(&XBOXLEDS[(uint8_t)led]) + 4, controller); setLedRaw(0);
else if(led != ALL) // All LEDs can't be on a the same time
setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]) + 4);
} }
void XBOXRECV::setLedBlink(LED led, uint8_t controller) { void XBOXRECV::setLedBlink(LEDEnum led, uint8_t controller) {
setLedRaw(pgm_read_byte(&XBOXLEDS[(uint8_t)led]), controller); setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]), controller);
} }
void XBOXRECV::setLedMode(LEDMode ledMode, uint8_t controller) { // This function is used to do some speciel LED stuff the controller supports 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); setLedRaw((uint8_t)ledMode, controller);
} }
@ -567,7 +569,7 @@ void XBOXRECV::onInit(uint8_t controller) {
if(pFuncOnInit) if(pFuncOnInit)
pFuncOnInit(); // Call the user function pFuncOnInit(); // Call the user function
else { else {
LED led; LEDEnum led;
if(controller == 0) if(controller == 0)
led = LED1; led = LED1;
else if(controller == 1) else if(controller == 1)

View file

@ -120,19 +120,18 @@ public:
/** @name Xbox Controller functions */ /** @name Xbox Controller functions */
/** /**
* getButtonPress(uint8_t controller, Button b) will return true as long as the button is held down. * getButtonPress(uint8_t controller, ButtonEnum b) will return true as long as the button is held down.
* *
* While getButtonClick(uint8_t controller, Button b) will only return it once. * While getButtonClick(uint8_t controller, ButtonEnum b) will only return it once.
* *
* So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, Button b), * So you instance if you need to increase a variable once you would use getButtonClick(uint8_t controller, ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, Button b). * but if you need to drive a robot forward you would use getButtonPress(uint8_t controller, ButtonEnum b).
* @param b ::Button to read. * @param b ::ButtonEnum to read.
* @param controller The controller to read from. Default to 0. * @param controller The controller to read from. Default to 0.
* @return getButtonClick(uint8_t controller, Button b) will return a bool, but getButtonPress(uint8_t controller, Button b) * @return getButtonClick(uint8_t controller, ButtonEnum b) will return a bool, while getButtonPress(uint8_t controller, ButtonEnum b) will return a byte if reading ::L2 or ::R2.
* will return a byte if reading ::L2 or ::R2.
*/ */
uint8_t getButtonPress(Button b, uint8_t controller = 0); uint8_t getButtonPress(ButtonEnum b, uint8_t controller = 0);
bool getButtonClick(Button b, uint8_t controller = 0); bool getButtonClick(ButtonEnum b, uint8_t controller = 0);
/**@}*/ /**@}*/
/** @name Xbox Controller functions */ /** @name Xbox Controller functions */
@ -142,7 +141,7 @@ public:
* @param controller The controller to read from. Default to 0. * @param controller The controller to read from. Default to 0.
* @return Returns a signed 16-bit integer. * @return Returns a signed 16-bit integer.
*/ */
int16_t getAnalogHat(AnalogHat a, uint8_t controller = 0); int16_t getAnalogHat(AnalogHatEnum a, uint8_t controller = 0);
/** /**
* Used to disconnect any of the controllers. * Used to disconnect any of the controllers.
@ -174,7 +173,7 @@ public:
*/ */
void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0); void setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller = 0);
/** /**
* Set LED value. Without using the ::LED or ::LEDMode enum. * Set LED value. Without using the ::LEDEnum or ::LEDMode enum.
* @param value See: * @param value See:
* setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l), * setLedOff(uint8_t controller), setLedOn(uint8_t controller, LED l),
* setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm). * setLedBlink(uint8_t controller, LED l), and setLedMode(uint8_t controller, LEDMode lm).
@ -190,23 +189,23 @@ public:
setLedRaw(0, controller); setLedRaw(0, controller);
}; };
/** /**
* Turn on a LED by using the ::LED enum. * Turn on a LED by using ::LEDEnum.
* @param l ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
* @param controller The controller to write to. Default to 0. * @param controller The controller to write to. Default to 0.
*/ */
void setLedOn(LED l, uint8_t controller = 0); void setLedOn(LEDEnum l, uint8_t controller = 0);
/** /**
* Turn on a LED by using the ::LED enum. * Turn on a LED by using ::LEDEnum.
* @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
* @param controller The controller to write to. Default to 0. * @param controller The controller to write to. Default to 0.
*/ */
void setLedBlink(LED l, uint8_t controller = 0); void setLedBlink(LEDEnum l, uint8_t controller = 0);
/** /**
* Used to set special LED modes supported by the Xbox controller. * Used to set special LED modes supported by the Xbox controller.
* @param lm See ::LEDMode. * @param lm See ::LEDModeEnum.
* @param controller The controller to write to. Default to 0. * @param controller The controller to write to. Default to 0.
*/ */
void setLedMode(LEDMode lm, uint8_t controller = 0); void setLedMode(LEDModeEnum lm, uint8_t controller = 0);
/** /**
* Used to get the battery level from the controller. * Used to get the battery level from the controller.
* @param controller The controller to read from. Default to 0. * @param controller The controller to read from. Default to 0.

View file

@ -279,15 +279,15 @@ void XBOXUSB::printReport() { //Uncomment "#define PRINTREPORT" to print the rep
#endif #endif
} }
uint8_t XBOXUSB::getButtonPress(Button b) { uint8_t XBOXUSB::getButtonPress(ButtonEnum b) {
if(b == L2) // These are analog buttons if(b == L2) // These are analog buttons
return (uint8_t)(ButtonState >> 8); return (uint8_t)(ButtonState >> 8);
else if(b == R2) else if(b == R2)
return (uint8_t)ButtonState; return (uint8_t)ButtonState;
return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOXBUTTONS[(uint8_t)b]) << 16)); return (bool)(ButtonState & ((uint32_t)pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]) << 16));
} }
bool XBOXUSB::getButtonClick(Button b) { bool XBOXUSB::getButtonClick(ButtonEnum b) {
if(b == L2) { if(b == L2) {
if(L2Clicked) { if(L2Clicked) {
L2Clicked = false; L2Clicked = false;
@ -301,13 +301,13 @@ bool XBOXUSB::getButtonClick(Button b) {
} }
return false; return false;
} }
uint16_t button = pgm_read_word(&XBOXBUTTONS[(uint8_t)b]); uint16_t button = pgm_read_word(&XBOX_BUTTONS[(uint8_t)b]);
bool click = (ButtonClickState & button); bool click = (ButtonClickState & button);
ButtonClickState &= ~button; // clear "click" event ButtonClickState &= ~button; // clear "click" event
return click; return click;
} }
int16_t XBOXUSB::getAnalogHat(AnalogHat a) { int16_t XBOXUSB::getAnalogHat(AnalogHatEnum a) {
return hatValue[a]; return hatValue[a];
} }
@ -325,16 +325,18 @@ void XBOXUSB::setLedRaw(uint8_t value) {
XboxCommand(writeBuf, 3); XboxCommand(writeBuf, 3);
} }
void XBOXUSB::setLedOn(LED led) { void XBOXUSB::setLedOn(LEDEnum led) {
if(led != ALL) // All LEDs can't be on a the same time if(led == OFF)
setLedRaw((pgm_read_byte(&XBOXLEDS[(uint8_t)led])) + 4); setLedRaw(0);
else if(led != ALL) // All LEDs can't be on a the same time
setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]) + 4);
} }
void XBOXUSB::setLedBlink(LED led) { void XBOXUSB::setLedBlink(LEDEnum led) {
setLedRaw(pgm_read_byte(&XBOXLEDS[(uint8_t)led])); setLedRaw(pgm_read_byte(&XBOX_LEDS[(uint8_t)led]));
} }
void XBOXUSB::setLedMode(LEDMode ledMode) { // This function is used to do some speciel LED stuff the controller supports void XBOXUSB::setLedMode(LEDModeEnum ledMode) { // This function is used to do some special LED stuff the controller supports
setLedRaw((uint8_t)ledMode); setLedRaw((uint8_t)ledMode);
} }

View file

@ -111,18 +111,17 @@ public:
/** @name Xbox Controller functions */ /** @name Xbox Controller functions */
/** /**
* getButtonPress(Button b) will return true as long as the button is held down. * getButtonPress(ButtonEnum b) will return true as long as the button is held down.
* *
* While getButtonClick(Button b) will only return it once. * While getButtonClick(ButtonEnum b) will only return it once.
* *
* So you instance if you need to increase a variable once you would use getButtonClick(Button b), * So you instance if you need to increase a variable once you would use getButtonClick(ButtonEnum b),
* but if you need to drive a robot forward you would use getButtonPress(Button b). * but if you need to drive a robot forward you would use getButtonPress(ButtonEnum b).
* @param b ::Button to read. * @param b ::ButtonEnum to read.
* @return getButtonClick(Button b) will return a bool, but getButtonPress(Button b) * @return getButtonClick(ButtonEnum b) will return a bool, while getButtonPress(ButtonEnum b) will return a byte if reading ::L2 or ::R2.
* will return a byte if reading ::L2 or ::R2.
*/ */
uint8_t getButtonPress(Button b); uint8_t getButtonPress(ButtonEnum b);
bool getButtonClick(Button b); bool getButtonClick(ButtonEnum b);
/**@}*/ /**@}*/
/** @name Xbox Controller functions */ /** @name Xbox Controller functions */
@ -131,7 +130,7 @@ public:
* @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY. * @param a Either ::LeftHatX, ::LeftHatY, ::RightHatX or ::RightHatY.
* @return Returns a signed 16-bit integer. * @return Returns a signed 16-bit integer.
*/ */
int16_t getAnalogHat(AnalogHat a); int16_t getAnalogHat(AnalogHatEnum a);
/** Turn rumble off and all the LEDs on the controller. */ /** Turn rumble off and all the LEDs on the controller. */
void setAllOff() { void setAllOff() {
@ -150,10 +149,10 @@ public:
*/ */
void setRumbleOn(uint8_t lValue, uint8_t rValue); void setRumbleOn(uint8_t lValue, uint8_t rValue);
/** /**
* Set LED value. Without using the ::LED or ::LEDMode enum. * Set LED value. Without using the ::LEDEnum or ::LEDMode enum.
* @param value See: * @param value See:
* setLedOff(), setLedOn(LED l), * setLedOff(), setLedOn(LEDEnum l),
* setLedBlink(LED l), and setLedMode(LEDMode lm). * setLedBlink(LEDEnum l), and setLedMode(LEDModeEnum lm).
*/ */
void setLedRaw(uint8_t value); void setLedRaw(uint8_t value);
@ -162,20 +161,20 @@ public:
setLedRaw(0); setLedRaw(0);
}; };
/** /**
* Turn on a LED by using the ::LED enum. * Turn on a LED by using ::LEDEnum.
* @param l ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. * @param l ::OFF, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
*/ */
void setLedOn(LED l); void setLedOn(LEDEnum l);
/** /**
* Turn on a LED by using the ::LED enum. * Turn on a LED by using ::LEDEnum.
* @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller. * @param l ::ALL, ::LED1, ::LED2, ::LED3 and ::LED4 is supported by the Xbox controller.
*/ */
void setLedBlink(LED l); void setLedBlink(LEDEnum l);
/** /**
* Used to set special LED modes supported by the Xbox controller. * Used to set special LED modes supported by the Xbox controller.
* @param lm See ::LEDMode. * @param lm See ::LEDMode.
*/ */
void setLedMode(LEDMode lm); void setLedMode(LEDModeEnum lm);
/** /**
* Used to call your own function when the controller is successfully initialized. * Used to call your own function when the controller is successfully initialized.

View file

@ -24,24 +24,25 @@
*/ */
/** Enum used to turn on the LEDs on the different controllers. */ /** Enum used to turn on the LEDs on the different controllers. */
enum LED { enum LEDEnum {
LED1 = 0, OFF = 0,
LED2 = 1, LED1 = 1,
LED3 = 2, LED2 = 2,
LED4 = 3, LED3 = 3,
LED4 = 4,
LED5 = 4, LED5 = 5,
LED6 = 5, LED6 = 6,
LED7 = 6, LED7 = 7,
LED8 = 7, LED8 = 8,
LED9 = 8, LED9 = 9,
LED10 = 9, LED10 = 10,
/** Used to blink all LEDs on the Xbox controller */ /** Used to blink all LEDs on the Xbox controller */
ALL = 4, ALL = 5,
}; };
/** This enum is used to read all the different buttons on the different controllers */ /** This enum is used to read all the different buttons on the different controllers */
enum Button { enum ButtonEnum {
/**@{*/ /**@{*/
/** These buttons are available on all the the controllers */ /** These buttons are available on all the the controllers */
UP = 0, UP = 0,
@ -106,7 +107,7 @@ enum Button {
}; };
/** Joysticks on the PS3 and Xbox controllers. */ /** Joysticks on the PS3 and Xbox controllers. */
enum AnalogHat { enum AnalogHatEnum {
/** Left joystick x-axis */ /** Left joystick x-axis */
LeftHatX = 0, LeftHatX = 0,
/** Left joystick y-axis */ /** Left joystick y-axis */

View file

@ -139,7 +139,7 @@ void onInit() {
for (uint8_t i = 0; i < length; i++) { for (uint8_t i = 0; i < length; i++) {
if ((PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) && !oldControllerState[i]) { if ((PS3[i]->PS3Connected || PS3[i]->PS3NavigationConnected) && !oldControllerState[i]) {
oldControllerState[i] = true; // Used to check which is the new controller oldControllerState[i] = true; // Used to check which is the new controller
PS3[i]->setLedOn((LED)i); // Cast directly to LED enum - see: "controllerEnums.h" PS3[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h"
} }
} }
} }

View file

@ -122,7 +122,7 @@ void onInit() {
for (uint8_t i = 0; i < length; i++) { for (uint8_t i = 0; i < length; i++) {
if (Wii[i]->wiimoteConnected && !oldControllerState[i]) { if (Wii[i]->wiimoteConnected && !oldControllerState[i]) {
oldControllerState[i] = true; // Used to check which is the new controller oldControllerState[i] = true; // Used to check which is the new controller
Wii[i]->setLedOn((LED)i); // Cast directly to LED enum - see: "controllerEnums.h" Wii[i]->setLedOn((LEDEnum)(i + 1)); // Cast directly to LEDEnum - see: "controllerEnums.h"
} }
} }
} }

View file

@ -77,6 +77,7 @@ watingForConnection KEYWORD2
#################################################### ####################################################
# Constants and enums (LITERAL1) # Constants and enums (LITERAL1)
#################################################### ####################################################
OFF LITERAL1
LED1 LITERAL1 LED1 LITERAL1
LED2 LITERAL1 LED2 LITERAL1
LED3 LITERAL1 LED3 LITERAL1

View file

@ -21,7 +21,7 @@
#include "controllerEnums.h" #include "controllerEnums.h"
/** Enum used to set special LED modes supported by the Xbox controller. */ /** Enum used to set special LED modes supported by the Xbox controller. */
enum LEDMode { enum LEDModeEnum {
ROTATING = 0x0A, ROTATING = 0x0A,
FASTBLINK = 0x0B, FASTBLINK = 0x0B,
SLOWBLINK = 0x0C, SLOWBLINK = 0x0C,
@ -29,15 +29,16 @@ enum LEDMode {
}; };
/** Used to set the LEDs on the controllers */ /** Used to set the LEDs on the controllers */
const uint8_t XBOXLEDS[] PROGMEM = { const uint8_t XBOX_LEDS[] PROGMEM = {
0x00, // OFF
0x02, // LED1 0x02, // LED1
0x03, // LED2 0x03, // LED2
0x04, // LED3 0x04, // LED3
0x05, // LED4 0x05, // LED4
0x01 // ALL - Used to blink all LEDs 0x01, // ALL - Used to blink all LEDs
}; };
/** Buttons on the controllers */ /** Buttons on the controllers */
const uint16_t XBOXBUTTONS[] PROGMEM = { const uint16_t XBOX_BUTTONS[] PROGMEM = {
0x0100, // UP 0x0100, // UP
0x0800, // RIGHT 0x0800, // RIGHT
0x0200, // DOWN 0x0200, // DOWN
@ -58,7 +59,7 @@ const uint16_t XBOXBUTTONS[] PROGMEM = {
0x0080, // Y 0x0080, // Y
0x0004, // XBOX 0x0004, // XBOX
0x0008 // SYNC 0x0008, // SYNC
}; };
#endif #endif