diff --git a/PS3BT.cpp b/PS3BT.cpp index 06945966..0a320b3f 100644 --- a/PS3BT.cpp +++ b/PS3BT.cpp @@ -61,11 +61,12 @@ pBtd(p) // pointer to USB class instance - mandatory Reset(); } bool PS3BT::getButtonPress(Button b) { - return (ButtonState & (uint32_t)b); + return (ButtonState & pgm_read_dword(&BUTTONS[(uint8_t)b])); } bool PS3BT::getButtonClick(Button b) { - bool click = (ButtonClickState & (uint32_t)b); - ButtonClickState &= ~((uint32_t)b); // clear "click" event + uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]); + bool click = (ButtonClickState & button); + ButtonClickState &= ~button; // clear "click" event return click; } uint8_t PS3BT::getAnalogButton(AnalogButton a) { @@ -602,16 +603,16 @@ void PS3BT::setRumbleOn(Rumble mode) { } } void PS3BT::setLedOff(LED a) { - HIDBuffer[11] &= ~((uint8_t)(((uint16_t)a & 0x0f) << 1)); - HID_Command(HIDBuffer, HID_BUFFERSIZE); + HIDBuffer[11] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1)); + HID_Command(HIDBuffer, HID_BUFFERSIZE); } void PS3BT::setLedOn(LED a) { - HIDBuffer[11] |= (uint8_t)(((uint16_t)a & 0x0f) << 1); - HID_Command(HIDBuffer, HID_BUFFERSIZE); + HIDBuffer[11] |= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); + HID_Command(HIDBuffer, HID_BUFFERSIZE); } void PS3BT::setLedToggle(LED a) { - HIDBuffer[11] ^= (uint8_t)(((uint16_t)a & 0x0f) << 1); - HID_Command(HIDBuffer, HID_BUFFERSIZE); + HIDBuffer[11] ^= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); + HID_Command(HIDBuffer, HID_BUFFERSIZE); } void PS3BT::enable_sixaxis() { //Command used to enable the Dualshock 3 and Navigation controller to send data via USB uint8_t cmd_buf[6]; diff --git a/PS3Enums.h b/PS3Enums.h index d5e1906f..b8f10c3a 100644 --- a/PS3Enums.h +++ b/PS3Enums.h @@ -18,19 +18,48 @@ #ifndef _ps3enums_h #define _ps3enums_h -enum LED { - LED1 = 0x01, - LED2 = 0x02, - LED3 = 0x04, - LED4 = 0x08, +#include "controllerEnums.h" + +const uint8_t LEDS[] PROGMEM = { + 0x01, // LED1 + 0x02, // LED2 + 0x04, // LED3 + 0x08, // LED4 - LED5 = 0x09, - LED6 = 0x0A, - LED7 = 0x0C, - LED8 = 0x0D, - LED9 = 0x0E, - LED10 = 0x0F, + 0x09, // LED5 + 0x0A, // LED6 + 0x0C, // LED7 + 0x0D, // LED8 + 0x0E, // LED9 + 0x0F // LED10 }; +const uint32_t BUTTONS[] PROGMEM = { + 0x10, // UP + 0x20, // RIGHT + 0x40, // DOWN + 0x80, // LEFT + 0,0,0,0,0,0,0,0,0, // Skip buttons used by Wii library + + 0x01, // SELECT + 0x02, // L3 + 0x04, // R3 + 0x08, // START + + 0x0100, // L2 + 0x0200, // R2 + 0x0400, // L1 + 0x0800, // R1 + 0x1000, // TRIANGLE + 0x2000, // CIRCLE + 0x4000, // CROSS + 0x8000, // SQUARE + + 0x010000, // PS + + 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 +}; + enum Colors { // Used to set the colors of the move controller Red = 0xFF0000, // r = 255, g = 0, b = 0 @@ -44,30 +73,6 @@ enum Colors { White = 0xFFFFFF, // r = 255, g = 255, b = 255 Off = 0x00, // r = 0, g = 0, b = 0 }; -enum Button { - SELECT = 0x01, - L3 = 0x02, - R3 = 0x04, - START = 0x08, - UP = 0x10, - RIGHT = 0x20, - DOWN = 0x40, - LEFT = 0x80, - - L2 = 0x0100, - R2 = 0x0200, - L1 = 0x0400, - R1 = 0x0800, - TRIANGLE = 0x1000, - CIRCLE = 0x2000, - CROSS = 0x4000, - SQUARE = 0x8000, - - PS = 0x010000, - - MOVE = 0x080000, // covers 12 bits - we only need to read the top 8 - T = 0x100000, // covers 12 bits - we only need to read the top 8 -}; enum AnalogButton { // Note that the location is shiftet 9 when it's connected via USB // Sixaxis Dualshcock 3 & Navigation controller diff --git a/PS3USB.cpp b/PS3USB.cpp index 4136b318..79cd1cd4 100644 --- a/PS3USB.cpp +++ b/PS3USB.cpp @@ -327,11 +327,12 @@ void PS3USB::printReport() { //Uncomment "#define PRINTREPORT" to print the repo } bool PS3USB::getButtonPress(Button b) { - return (ButtonState & (uint32_t)b); + return (ButtonState & pgm_read_dword(&BUTTONS[(uint8_t)b])); } bool PS3USB::getButtonClick(Button b) { - bool click = (ButtonClickState & (uint32_t)b); - ButtonClickState &= ~((uint32_t)b); // clear "click" event + uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]); + bool click = (ButtonClickState & button); + ButtonClickState &= ~button; // clear "click" event return click; } uint8_t PS3USB::getAnalogButton(AnalogButton a) { @@ -456,16 +457,16 @@ void PS3USB::setRumbleOn(Rumble mode) { } } void PS3USB::setLedOff(LED a) { - writeBuf[9] &= ~((uint8_t)(((uint16_t)a & 0x0f) << 1)); - PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); + writeBuf[9] &= ~((uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1)); + PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); } void PS3USB::setLedOn(LED a) { - writeBuf[9] |= (uint8_t)(((uint16_t)a & 0x0f) << 1); - PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); + writeBuf[9] |= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); + PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); } void PS3USB::setLedToggle(LED a) { - writeBuf[9] ^= (uint8_t)(((uint16_t)a & 0x0f) << 1); - PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); + writeBuf[9] ^= (uint8_t)((pgm_read_byte(&LEDS[(uint8_t)a]) & 0x0f) << 1); + PS3_Command(writeBuf, PS3_REPORT_BUFFER_SIZE); } void PS3USB::setBdaddr(uint8_t* BDADDR) { /* Set the internal bluetooth address */ diff --git a/Wii.cpp b/Wii.cpp index 61a25492..81528095 100755 --- a/Wii.cpp +++ b/Wii.cpp @@ -24,6 +24,40 @@ //#define EXTRADEBUG // Uncomment to get even more debugging data //#define PRINTREPORT // Uncomment to print the report send by the Wii controllers +const uint8_t LEDS[] PROGMEM = { + 0x10, // LED1 + 0x20, // LED2 + 0x40, // LED3 + 0x80, // LED4 + + 0x90, // LED5 + 0xA0, // LED6 + 0xC0, // LED7 + 0xD0, // LED8 + 0xE0, // LED9 + 0xF0 // LED10 + }; + +const uint32_t BUTTONS[] PROGMEM = { + 0x00008, // UP + 0x00002, // RIGHT + 0x00004, // DOWN + 0x00001, // LEFT + + + 0x00010, // PLUS + + 0x00100, // TWO + 0x00200, // ONE + 0x00400, // B + 0x00800, // A + 0x01000, // MINUS + 0x08000, // HOME + + 0x10000, // Z + 0x20000 // C +}; + WII::WII(BTD *p, bool pair): pBtd(p) // pointer to USB class instance - mandatory { @@ -789,17 +823,17 @@ void WII::setRumbleToggle() { } void WII::setLedOff(LED a) { HIDBuffer[1] = 0x11; - HIDBuffer[2] &= ~((uint8_t)a); + HIDBuffer[2] &= ~(pgm_read_byte(&LEDS[(uint8_t)a])); HID_Command(HIDBuffer, 3); } void WII::setLedOn(LED a) { HIDBuffer[1] = 0x11; - HIDBuffer[2] |= (uint8_t)a; + HIDBuffer[2] |= pgm_read_byte(&LEDS[(uint8_t)a]); HID_Command(HIDBuffer, 3); } void WII::setLedToggle(LED a) { HIDBuffer[1] = 0x11; - HIDBuffer[2] ^= (uint8_t)a; + HIDBuffer[2] ^= pgm_read_byte(&LEDS[(uint8_t)a]); HID_Command(HIDBuffer, 3); } void WII::setLedStatus() { @@ -916,14 +950,15 @@ void WII::checkMotionPresent() { /************************************************************/ bool WII::getButtonPress(Button b) { // Return true when a button is pressed - return (ButtonState & (uint32_t)b); + return (ButtonState & pgm_read_dword(&BUTTONS[(uint8_t)b])); } bool WII::getButtonClick(Button b) { // Only return true when a button is clicked - bool click = (ButtonClickState & (uint32_t)b); - ButtonClickState &= ~((uint32_t)b); // clear "click" event + uint32_t button = pgm_read_dword(&BUTTONS[(uint8_t)b]); + bool click = (ButtonClickState & button); + ButtonClickState &= ~button; // clear "click" event return click; } -uint8_t WII::getAnalogHat(AnalogHat a) { +uint8_t WII::getAnalogHat(Hat a) { if(!nunchuckConnected) return 127; // Return center position else { diff --git a/Wii.h b/Wii.h index adb15289..e8f7ca5b 100755 --- a/Wii.h +++ b/Wii.h @@ -23,6 +23,7 @@ #define _wii_h_ #include "BTD.h" +#include "controllerEnums.h" //#define WIICAMERA //uncomment to enable IR camera @@ -78,38 +79,7 @@ #define PAIR 1 -enum LED { - LED1 = 0x10, - LED2 = 0x20, - LED3 = 0x40, - LED4 = 0x80, - - LED5 = 0x90, - LED6 = 0xA0, - LED7 = 0xC0, - LED8 = 0xD0, - LED9 = 0xE0, - LED10 = 0xF0, -}; - -enum Button { - LEFT = 0x00001, - RIGHT = 0x00002, - DOWN = 0x00004, - UP = 0x00008, - PLUS = 0x00010, - - TWO = 0x00100, - ONE = 0x00200, - B = 0x00400, - A = 0x00800, - MINUS = 0x01000, - HOME = 0x08000, - - Z = 0x10000, - C = 0x20000, -}; -enum AnalogHat { +enum Hat { HatX = 0, HatY = 1, }; @@ -133,7 +103,7 @@ public: bool getButtonPress(Button b); // This will read true as long as the button is held down bool getButtonClick(Button b); // This will only be true when the button is clicked the first time - uint8_t getAnalogHat(AnalogHat a); // Used to read the joystick of the Nunchuck + uint8_t getAnalogHat(Hat a); // Used to read the joystick of the Nunchuck double getPitch() { return pitch; }; // Fusioned angle using a complimentary filter if the Motion Plus is connected double getRoll() { return roll; }; // Fusioned angle using a complimentary filter if the Motion Plus is connected diff --git a/controllerEnums.h b/controllerEnums.h new file mode 100644 index 00000000..9fdbdae0 --- /dev/null +++ b/controllerEnums.h @@ -0,0 +1,74 @@ +/* Copyright (C) 2012 Kristian Lauszus, TKJ Electronics. All rights reserved. + + This software may be distributed and modified under the terms of the GNU + General Public License version 2 (GPL2) as published by the Free Software + Foundation and appearing in the file GPL2.TXT included in the packaging of + this file. Please note that GPL2 Section 2[b] requires that all works based + on this software must also be made publicly available under the terms of + the GPL2 ("Copyleft"). + + Contact information + ------------------- + + Kristian Lauszus, TKJ Electronics + Web : http://www.tkjelectronics.com + e-mail : kristianl@tkjelectronics.com + */ + +#ifndef _controllerenums_h +#define _controllerenums_h + + +enum LED { + /* Enum used to turn on the LEDs on the different controllers */ + LED1 = 0, + LED2 = 1, + LED3 = 2, + LED4 = 3, + + LED5 = 4, + LED6 = 5, + LED7 = 6, + LED8 = 7, + LED9 = 8, + LED10 = 9, +}; +enum Button { + UP = 0, + RIGHT = 1, + DOWN = 2, + LEFT = 3, + + /* Wii buttons */ + PLUS = 4, + TWO = 5, + ONE = 6, + B = 7, + A = 8, + MINUS = 9, + HOME = 10, + Z = 11, + C = 12, + + /* PS3 controllers buttons */ + SELECT = 13, + L3 = 14, + R3 = 15, + START = 16, + + L2 = 17, + R2 = 18, + L1 = 19, + R1 = 20, + TRIANGLE = 21, + CIRCLE = 22, + CROSS = 23, + SQUARE = 24, + + PS = 25, + + MOVE = 26, // covers 12 bits - we only need to read the top 8 + T = 27, // covers 12 bits - we only need to read the top 8 +}; + +#endif \ No newline at end of file