2013-01-20 22:46:04 +01:00
/*
Example sketch for the Wiimote Bluetooth library - developed by Kristian Lauszus
This example show how one can use multiple controllers with the library
For more information visit my blog : http : //blog.tkjelectronics.dk/ or
send me an e - mail : kristianl @ tkjelectronics . com
*/
# include <Wii.h>
2013-08-13 21:31:52 +02:00
# include <usbhub.h>
2013-01-20 22:46:04 +01:00
USB Usb ;
2013-08-13 21:31:52 +02:00
USBHub Hub1 ( & Usb ) ; // Some dongles have a hub inside
2013-01-20 22:46:04 +01:00
BTD Btd ( & Usb ) ; // You have to create the Bluetooth Dongle instance like so
2013-07-14 01:28:47 +02:00
WII * Wii [ 2 ] ; // We will use this pointer to store the two instance, you can easily make it larger if you like, but it will use a lot of RAM!
2013-01-20 22:46:04 +01:00
const uint8_t length = sizeof ( Wii ) / sizeof ( Wii [ 0 ] ) ; // Get the lenght of the array
2013-07-14 01:28:47 +02:00
boolean printAngle [ length ] ;
boolean oldControllerState [ length ] ;
2013-01-20 22:46:04 +01:00
void setup ( ) {
2013-07-14 01:28:47 +02:00
for ( uint8_t i = 0 ; i < length ; i + + ) {
2013-06-12 05:11:43 +02:00
Wii [ i ] = new WII ( & Btd ) ; // You will have to pair each controller with the dongle before you can define the instances like so, just add PAIR as the second argument
2013-07-14 01:28:47 +02:00
Wii [ i ] - > attachOnInit ( onInit ) ; // onInit() is called upon a new connection - you can call the function whatever you like
}
2013-01-20 22:46:04 +01:00
Serial . begin ( 115200 ) ;
if ( Usb . Init ( ) = = - 1 ) {
Serial . print ( F ( " \r \n OSC did not start " ) ) ;
while ( 1 ) ; //halt
}
Serial . print ( F ( " \r \n Wiimote Bluetooth Library Started " ) ) ;
}
void loop ( ) {
Usb . Task ( ) ;
for ( uint8_t i = 0 ; i < length ; i + + ) {
if ( Wii [ i ] - > wiimoteConnected ) {
if ( Wii [ i ] - > getButtonClick ( HOME ) ) { // You can use getButtonPress to see if the button is held down
Serial . print ( F ( " \r \n HOME " ) ) ;
2013-01-23 22:47:47 +01:00
Wii [ i ] - > disconnect ( ) ;
2013-07-14 01:28:47 +02:00
oldControllerState [ i ] = false ; // Reset value
2013-01-20 22:46:04 +01:00
}
else {
if ( Wii [ i ] - > getButtonClick ( LEFT ) ) {
Wii [ i ] - > setAllOff ( ) ;
Wii [ i ] - > setLedOn ( LED1 ) ;
Serial . print ( F ( " \r \n Left " ) ) ;
}
if ( Wii [ i ] - > getButtonClick ( RIGHT ) ) {
Wii [ i ] - > setAllOff ( ) ;
Wii [ i ] - > setLedOn ( LED3 ) ;
Serial . print ( F ( " \r \n Right " ) ) ;
}
if ( Wii [ i ] - > getButtonClick ( DOWN ) ) {
Wii [ i ] - > setAllOff ( ) ;
Wii [ i ] - > setLedOn ( LED4 ) ;
Serial . print ( F ( " \r \n Down " ) ) ;
}
if ( Wii [ i ] - > getButtonClick ( UP ) ) {
Wii [ i ] - > setAllOff ( ) ;
Wii [ i ] - > setLedOn ( LED2 ) ;
Serial . print ( F ( " \r \n Up " ) ) ;
}
if ( Wii [ i ] - > getButtonClick ( PLUS ) )
Serial . print ( F ( " \r \n Plus " ) ) ;
if ( Wii [ i ] - > getButtonClick ( MINUS ) )
Serial . print ( F ( " \r \n Minus " ) ) ;
if ( Wii [ i ] - > getButtonClick ( ONE ) )
Serial . print ( F ( " \r \n One " ) ) ;
if ( Wii [ i ] - > getButtonClick ( TWO ) )
Serial . print ( F ( " \r \n Two " ) ) ;
if ( Wii [ i ] - > getButtonClick ( A ) ) {
printAngle [ i ] = ! printAngle [ i ] ;
Serial . print ( F ( " \r \n A " ) ) ;
}
if ( Wii [ i ] - > getButtonClick ( B ) ) {
Wii [ i ] - > setRumbleToggle ( ) ;
Serial . print ( F ( " \r \n B " ) ) ;
}
}
if ( printAngle [ i ] ) {
Serial . print ( F ( " \r \n Pitch: " ) ) ;
Serial . print ( Wii [ i ] - > getPitch ( ) ) ;
Serial . print ( F ( " \t Roll: " ) ) ;
Serial . print ( Wii [ i ] - > getRoll ( ) ) ;
if ( Wii [ i ] - > motionPlusConnected ) {
Serial . print ( F ( " \t Yaw: " ) ) ;
Serial . print ( Wii [ i ] - > getYaw ( ) ) ;
}
if ( Wii [ i ] - > nunchuckConnected ) {
Serial . print ( F ( " \t Nunchuck Pitch: " ) ) ;
2013-10-21 18:41:47 +02:00
Serial . print ( Wii [ i ] - > getNunchuckPitch ( ) ) ;
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \t Nunchuck Roll: " ) ) ;
2013-10-21 18:41:47 +02:00
Serial . print ( Wii [ i ] - > getNunchuckRoll ( ) ) ;
2013-01-20 22:46:04 +01:00
}
}
}
if ( Wii [ i ] - > nunchuckConnected ) {
if ( Wii [ i ] - > getButtonClick ( Z ) )
Serial . print ( F ( " \r \n Z " ) ) ;
if ( Wii [ i ] - > getButtonClick ( C ) )
Serial . print ( F ( " \r \n C " ) ) ;
if ( Wii [ i ] - > getAnalogHat ( HatX ) > 137 | | Wii [ i ] - > getAnalogHat ( HatX ) < 117 | | Wii [ i ] - > getAnalogHat ( HatY ) > 137 | | Wii [ i ] - > getAnalogHat ( HatY ) < 117 ) {
Serial . print ( F ( " \r \n HatX: " ) ) ;
Serial . print ( Wii [ i ] - > getAnalogHat ( HatX ) ) ;
Serial . print ( F ( " \t HatY: " ) ) ;
Serial . print ( Wii [ i ] - > getAnalogHat ( HatY ) ) ;
}
}
}
}
2013-07-14 01:28:47 +02:00
void onInit ( ) {
for ( uint8_t i = 0 ; i < length ; i + + ) {
if ( Wii [ i ] - > wiimoteConnected & & ! oldControllerState [ i ] ) {
oldControllerState [ i ] = true ; // Used to check which is the new controller
Wii [ i ] - > setLedOn ( ( LED ) i ) ; // Cast directly to LED enum - see: "controllerEnums.h"
}
}
}