Added attachOnInit

This will allow the user to call a function when the controller is initialized
This commit is contained in:
Kristian Sloth Lauszus 2013-07-14 00:43:35 +02:00
parent c3c80924ec
commit 2dc817f84a
11 changed files with 160 additions and 38 deletions

View file

@ -516,25 +516,23 @@ void PS3BT::Run() {
case L2CAP_HID_PS3_LED: case L2CAP_HID_PS3_LED:
if (millis() - timer > 1000) { // loop 1 second before sending the command if (millis() - timer > 1000) { // loop 1 second before sending the command
if (remote_name[0] == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P') if (remote_name[0] == 'P') { // First letter in PLAYSTATION(R)3 Controller ('P')
setLedOn(LED1);
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nDualshock 3 Controller Enabled\r\n"), 0x80); Notify(PSTR("\r\nDualshock 3 Controller Enabled\r\n"), 0x80);
#endif #endif
PS3Connected = true; PS3Connected = true;
} else if (remote_name[0] == 'N') { // First letter in Navigation Controller ('N') } else if (remote_name[0] == 'N') { // First letter in Navigation Controller ('N')
setLedOn(LED1); // This just turns LED constantly on, on the Navigation controller
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nNavigation Controller Enabled\r\n"), 0x80); Notify(PSTR("\r\nNavigation Controller Enabled\r\n"), 0x80);
#endif #endif
PS3NavigationConnected = true; PS3NavigationConnected = true;
} else if (remote_name[0] == 'M') { // First letter in Motion Controller ('M') } else if (remote_name[0] == 'M') { // First letter in Motion Controller ('M')
moveSetBulb(Red);
timerBulbRumble = millis(); timerBulbRumble = millis();
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nMotion Controller Enabled\r\n"), 0x80); Notify(PSTR("\r\nMotion Controller Enabled\r\n"), 0x80);
#endif #endif
PS3MoveConnected = true; PS3MoveConnected = true;
} }
onInit(); // Turn on the LED on the controller
l2cap_state = L2CAP_DONE; l2cap_state = L2CAP_DONE;
} }
break; break;
@ -659,3 +657,14 @@ void PS3BT::moveSetRumble(uint8_t rumble) {
HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE); HIDMove_Command(HIDMoveBuffer, HID_BUFFERSIZE);
} }
void PS3BT::onInit() {
if (pFuncOnInit)
pFuncOnInit(); // Call the user function
else {
if (PS3MoveConnected)
moveSetBulb(Red);
else // Dualshock 3 or Navigation controller
setLedOn(LED1);
}
}

26
PS3BT.h
View file

@ -207,19 +207,35 @@ public:
* @param rumble The desired value in the range from 64-255. * @param rumble The desired value in the range from 64-255.
*/ */
void moveSetRumble(uint8_t rumble); void moveSetRumble(uint8_t rumble);
/**
* Used to call your own function when the controller is successfully initialized.
* @param funcOnInit Function to call.
*/
void attachOnInit(void (*funcOnInit)(void)) {
pFuncOnInit = funcOnInit;
};
/**@}*/ /**@}*/
/** Variable used to indicate if the normal playstation controller is successfully connected. */ /** Variable used to indicate if the normal Playstation controller is successfully connected. */
bool PS3Connected; bool PS3Connected;
/** Variable used to indicate if the move controller is successfully connected. */ /** Variable used to indicate if the Move controller is successfully connected. */
bool PS3MoveConnected; bool PS3MoveConnected;
/** Variable used to indicate if the navigation controller is successfully connected. */ /** Variable used to indicate if the Navigation controller is successfully connected. */
bool PS3NavigationConnected; bool PS3NavigationConnected;
private: private:
/* mandatory members */ /* Mandatory members */
BTD *pBtd; BTD *pBtd;
/**
* Called when the controller is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
* This is useful for instance if you want to set the LEDs in a specific way.
*/
void onInit();
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
void L2CAP_task(); // L2CAP state machine void L2CAP_task(); // L2CAP state machine
/* Variables filled from HCI event management */ /* Variables filled from HCI event management */
@ -229,7 +245,7 @@ private:
/* variables used by high level L2CAP task */ /* variables used by high level L2CAP task */
uint8_t l2cap_state; uint8_t l2cap_state;
uint16_t l2cap_event_flag; // L2CAP flags of received bluetooth events uint16_t l2cap_event_flag; // L2CAP flags of received Bluetooth events
unsigned long timer; unsigned long timer;

View file

@ -183,10 +183,9 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
#endif #endif
PS3NavigationConnected = true; PS3NavigationConnected = true;
} }
/* Set internal bluetooth address and request for data */ /* Set internal Bluetooth address and request for data */
setBdaddr(my_bdaddr); setBdaddr(my_bdaddr);
enable_sixaxis(); enable_sixaxis();
setLedOn(LED1);
// Needed for PS3 Dualshock and Navigation commands to work // Needed for PS3 Dualshock and Navigation commands to work
for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++) for (uint8_t i = 0; i < PS3_REPORT_BUFFER_SIZE; i++)
@ -199,11 +198,11 @@ uint8_t PS3USB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
Notify(PSTR("\r\nMotion Controller Connected"), 0x80); Notify(PSTR("\r\nMotion Controller Connected"), 0x80);
#endif #endif
PS3MoveConnected = true; PS3MoveConnected = true;
setMoveBdaddr(my_bdaddr); // Set internal bluetooth address setMoveBdaddr(my_bdaddr); // Set internal Bluetooth address
moveSetBulb(Red);
writeBuf[0] = 0x02; // Set report ID, this is needed for Move commands to work writeBuf[0] = 0x02; // Set report ID, this is needed for Move commands to work
} }
onInit();
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80); Notify(PSTR("\r\nBluetooth Address was set to: "), 0x80);
@ -538,3 +537,14 @@ void PS3USB::setMoveBdaddr(uint8_t* BDADDR) {
//bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data) //bmRequest = Host to device (0x00) | Class (0x20) | Interface (0x01) = 0x21, bRequest = Set Report (0x09), Report ID (0x05), Report Type (Feature 0x03), interface (0x00), datalength, datalength, data)
pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL); pUsb->ctrlReq(bAddress, epInfo[PS3_CONTROL_PIPE].epAddr, bmREQ_HID_OUT, HID_REQUEST_SET_REPORT, 0x05, 0x03, 0x00, 11, 11, buf, NULL);
} }
void PS3USB::onInit() {
if (pFuncOnInit)
pFuncOnInit(); // Call the user function
else {
if (PS3MoveConnected)
moveSetBulb(Red);
else // Dualshock 3 or Navigation controller
setLedOn(LED1);
}
}

View file

@ -224,6 +224,14 @@ public:
* @param rumble The desired value in the range from 64-255. * @param rumble The desired value in the range from 64-255.
*/ */
void moveSetRumble(uint8_t rumble); void moveSetRumble(uint8_t rumble);
/**
* Used to call your own function when the controller is successfully initialized.
* @param funcOnInit Function to call.
*/
void attachOnInit(void (*funcOnInit)(void)) {
pFuncOnInit = funcOnInit;
};
/**@}*/ /**@}*/
/** Variable used to indicate if the normal playstation controller is successfully connected. */ /** Variable used to indicate if the normal playstation controller is successfully connected. */
@ -242,6 +250,14 @@ protected:
EpInfo epInfo[PS3_MAX_ENDPOINTS]; EpInfo epInfo[PS3_MAX_ENDPOINTS];
private: private:
/**
* Called when the controller is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
* This is useful for instance if you want to set the LEDs in a specific way.
*/
void onInit();
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
bool bPollEnable; bool bPollEnable;
uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values uint32_t timer; // used to continuously set PS3 Move controller Bulb and rumble values

14
Wii.cpp
View file

@ -326,7 +326,7 @@ void WII::ACLData(uint8_t* l2capinbuf) {
#endif #endif
nunchuckConnected = false; // It must be the Nunchuck controller then nunchuckConnected = false; // It must be the Nunchuck controller then
l2cap_event_flag &= ~WII_FLAG_NUNCHUCK_CONNECTED; l2cap_event_flag &= ~WII_FLAG_NUNCHUCK_CONNECTED;
setLedStatus(); onInit();
setReportMode(false, 0x31); // If there is no extension connected we will read the button and accelerometer setReportMode(false, 0x31); // If there is no extension connected we will read the button and accelerometer
} else { } else {
setReportMode(false, 0x31); // If there is no extension connected we will read the button and accelerometer setReportMode(false, 0x31); // If there is no extension connected we will read the button and accelerometer
@ -779,7 +779,7 @@ void WII::Run() {
case L2CAP_LED_STATE: case L2CAP_LED_STATE:
if (nunchuck_connected_flag) if (nunchuck_connected_flag)
nunchuckConnected = true; nunchuckConnected = true;
setLedStatus(); onInit();
l2cap_state = L2CAP_DONE; l2cap_state = L2CAP_DONE;
break; break;
@ -824,7 +824,7 @@ void WII::Run() {
else if (stateCounter == 400) else if (stateCounter == 400)
readExtensionType(); // Check if it has been activated readExtensionType(); // Check if it has been activated
else if (stateCounter == 450) { else if (stateCounter == 450) {
//setLedStatus(); onInit();
stateCounter = 0; stateCounter = 0;
unknownExtensionConnected = false; unknownExtensionConnected = false;
} }
@ -1062,6 +1062,14 @@ uint16_t WII::getAnalogHat(AnalogHat a) {
return output; return output;
} }
} }
void WII::onInit() {
if (pFuncOnInit)
pFuncOnInit(); // Call the user function
else
setLedStatus();
}
/************************************************************/ /************************************************************/
/* The following functions are for the IR camera */ /* The following functions are for the IR camera */
/************************************************************/ /************************************************************/

23
Wii.h
View file

@ -233,6 +233,14 @@ public:
uint8_t getWiiState() { uint8_t getWiiState() {
return wiiState; return wiiState;
}; };
/**
* Used to call your own function when the controller is successfully initialized.
* @param funcOnInit Function to call.
*/
void attachOnInit(void (*funcOnInit)(void)) {
pFuncOnInit = funcOnInit;
};
/**@}*/ /**@}*/
/**@{*/ /**@{*/
@ -418,8 +426,15 @@ public:
#endif #endif
private: private:
/* Mandatory members */ BTD *pBtd; // Pointer to BTD instance
BTD *pBtd;
/**
* Called when the controller is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
* This is useful for instance if you want to set the LEDs in a specific way.
*/
void onInit();
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
void L2CAP_task(); // L2CAP state machine void L2CAP_task(); // L2CAP state machine
@ -427,9 +442,9 @@ private:
uint16_t hci_handle; uint16_t hci_handle;
bool activeConnection; // Used to indicate if it's already has established a connection bool activeConnection; // Used to indicate if it's already has established a connection
/* variables used by high level L2CAP task */ /* Variables used by high level L2CAP task */
uint8_t l2cap_state; uint8_t l2cap_state;
uint16_t l2cap_event_flag; // l2cap flags of received bluetooth events uint16_t l2cap_event_flag; // l2cap flags of received Bluetooth events
uint32_t ButtonState; uint32_t ButtonState;
uint32_t OldButtonState; uint32_t OldButtonState;

View file

@ -310,7 +310,7 @@ void XBOXRECV::readReport(uint8_t controller) {
#endif #endif
if (Xbox360Connected[controller]) { if (Xbox360Connected[controller]) {
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
char* str = 0; const char* str = 0;
switch (readBuf[1]) { switch (readBuf[1]) {
case 0x80: str = PSTR(" as controller\r\n"); case 0x80: str = PSTR(" as controller\r\n");
break; break;
@ -322,18 +322,7 @@ void XBOXRECV::readReport(uint8_t controller) {
Notify(PSTR(": connected"), 0x80); Notify(PSTR(": connected"), 0x80);
Notify(str, 0x80); Notify(str, 0x80);
#endif #endif
LED led; onInit(controller);
switch (controller) {
case 0: led = LED1;
break;
case 1: led = LED2;
break;
case 2: led = LED3;
break;
case 3: led = LED4;
break;
}
setLedOn(led, controller);
} }
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
else else
@ -529,3 +518,20 @@ void XBOXRECV::setRumbleOn(uint8_t lValue, uint8_t rValue, uint8_t controller) {
XboxCommand(controller, writeBuf, 7); XboxCommand(controller, writeBuf, 7);
} }
void XBOXRECV::onInit(uint8_t controller) {
if (pFuncOnInit)
pFuncOnInit(); // Call the user function
else {
LED led;
if (controller == 0)
led = LED1;
else if (controller == 1)
led = LED2;
if (controller == 2)
led = LED3;
else
led = LED4;
setLedOn(led, controller);
}
}

View file

@ -200,6 +200,14 @@ public:
* @return True if a button has changed. * @return True if a button has changed.
*/ */
bool buttonChanged(uint8_t controller = 0); bool buttonChanged(uint8_t controller = 0);
/**
* Used to call your own function when the controller is successfully initialized.
* @param funcOnInit Function to call.
*/
void attachOnInit(void (*funcOnInit)(void)) {
pFuncOnInit = funcOnInit;
};
/**@}*/ /**@}*/
/** True if a wireless receiver is connected. */ /** True if a wireless receiver is connected. */
@ -216,6 +224,15 @@ protected:
EpInfo epInfo[XBOX_MAX_ENDPOINTS]; EpInfo epInfo[XBOX_MAX_ENDPOINTS];
private: private:
/**
* Called when the controller is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
* This is useful for instance if you want to set the LEDs in a specific way.
* @param controller The initialized controller.
*/
void onInit(uint8_t controller);
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
bool bPollEnable; bool bPollEnable;
/* Variables to store the buttons */ /* Variables to store the buttons */

View file

@ -167,7 +167,7 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
if (rcode) if (rcode)
goto FailSetDevTblEntry; goto FailSetDevTblEntry;
delay(200); //Give time for address change delay(200); // Give time for address change
rcode = pUsb->setConf(bAddress, epInfo[ XBOX_CONTROL_PIPE ].epAddr, 1); rcode = pUsb->setConf(bAddress, epInfo[ XBOX_CONTROL_PIPE ].epAddr, 1);
if (rcode) if (rcode)
@ -176,12 +176,12 @@ uint8_t XBOXUSB::Init(uint8_t parent, uint8_t port, bool lowspeed) {
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
Notify(PSTR("\r\nXbox 360 Controller Connected\r\n"), 0x80); Notify(PSTR("\r\nXbox 360 Controller Connected\r\n"), 0x80);
#endif #endif
setLedOn(LED1); onInit();
Xbox360Connected = true; Xbox360Connected = true;
bPollEnable = true; bPollEnable = true;
return 0; // successful configuration return 0; // Successful configuration
/* diagnostic messages */ /* Diagnostic messages */
FailGetDevDescr: FailGetDevDescr:
#ifdef DEBUG_USB_HOST #ifdef DEBUG_USB_HOST
NotifyFailGetDevDescr(); NotifyFailGetDevDescr();
@ -345,3 +345,10 @@ void XBOXUSB::setRumbleOn(uint8_t lValue, uint8_t rValue) {
XboxCommand(writeBuf, 8); XboxCommand(writeBuf, 8);
} }
void XBOXUSB::onInit() {
if (pFuncOnInit)
pFuncOnInit(); // Call the user function
else
setLedOn(LED1);
}

View file

@ -168,6 +168,14 @@ public:
* @param lm See ::LEDMode. * @param lm See ::LEDMode.
*/ */
void setLedMode(LEDMode lm); void setLedMode(LEDMode lm);
/**
* Used to call your own function when the controller is successfully initialized.
* @param funcOnInit Function to call.
*/
void attachOnInit(void (*funcOnInit)(void)) {
pFuncOnInit = funcOnInit;
};
/**@}*/ /**@}*/
/** True if a Xbox 360 controller is connected. */ /** True if a Xbox 360 controller is connected. */
@ -182,6 +190,14 @@ protected:
EpInfo epInfo[XBOX_MAX_ENDPOINTS]; EpInfo epInfo[XBOX_MAX_ENDPOINTS];
private: private:
/**
* Called when the controller is successfully initialized.
* Use attachOnInit(void (*funcOnInit)(void)) to call your own function.
* This is useful for instance if you want to set the LEDs in a specific way.
*/
void onInit();
void (*pFuncOnInit)(void); // Pointer to function called in onInit()
bool bPollEnable; bool bPollEnable;
/* Variables to store the buttons */ /* Variables to store the buttons */

View file

@ -56,6 +56,8 @@ setLedToggle KEYWORD2
moveSetBulb KEYWORD2 moveSetBulb KEYWORD2
moveSetRumble KEYWORD2 moveSetRumble KEYWORD2
attachOnInit KEYWORD2
PS3Connected KEYWORD2 PS3Connected KEYWORD2
PS3MoveConnected KEYWORD2 PS3MoveConnected KEYWORD2
PS3NavigationConnected KEYWORD2 PS3NavigationConnected KEYWORD2