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
2013-11-15 19:05:25 +01:00
For more information visit my blog : http : //blog.tkjelectronics.dk/ or
2013-01-20 22:46:04 +01:00
send me an e - mail : kristianl @ tkjelectronics . com
*/
# include <Wii.h>
2013-08-13 21:31:52 +02:00
# include <usbhub.h>
2014-08-03 00:57:37 +02:00
2015-03-03 19:08:39 +01:00
// Satisfy the IDE, which needs to see the include statment in the ino too.
# ifdef dobogusinclude
2013-12-05 03:56:06 +01:00
# include <spi4teensy3.h>
# endif
2017-08-14 09:39:38 +02:00
# include <SPI.h>
2013-08-13 21:31:52 +02:00
2013-01-20 22:46:04 +01:00
USB Usb ;
2013-11-24 15:17:19 +01: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-11-15 19:05:25 +01:00
const uint8_t length = sizeof ( Wii ) / sizeof ( Wii [ 0 ] ) ; // Get the lenght of the array
2015-02-19 09:14:39 +01:00
bool printAngle [ length ] ;
bool oldControllerState [ length ] ;
2013-01-20 22:46:04 +01:00
void setup ( ) {
2013-11-15 19:05:25 +01: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-11-15 19:05:25 +01:00
2013-01-20 22:46:04 +01:00
Serial . begin ( 115200 ) ;
2014-11-10 07:35:13 +01:00
# if !defined(__MIPSEL__)
2013-10-21 19:58:03 +02:00
while ( ! Serial ) ; // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
2014-11-10 07:35:13 +01:00
# endif
2013-01-20 22:46:04 +01:00
if ( Usb . Init ( ) = = - 1 ) {
Serial . print ( F ( " \r \n OSC did not start " ) ) ;
2013-11-15 19:05:25 +01:00
while ( 1 ) ; //halt
2013-01-20 22:46:04 +01:00
}
Serial . print ( F ( " \r \n Wiimote Bluetooth Library Started " ) ) ;
}
void loop ( ) {
Usb . Task ( ) ;
2013-11-15 19:05:25 +01:00
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
2013-01-20 22:46:04 +01:00
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-11-15 19:05:25 +01:00
}
2013-01-20 22:46:04 +01:00
else {
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( LEFT ) ) {
2013-11-15 23:19:10 +01:00
Wii [ i ] - > setLedOff ( ) ;
2013-01-20 22:46:04 +01:00
Wii [ i ] - > setLedOn ( LED1 ) ;
Serial . print ( F ( " \r \n Left " ) ) ;
}
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( RIGHT ) ) {
2013-11-15 23:19:10 +01:00
Wii [ i ] - > setLedOff ( ) ;
2013-01-20 22:46:04 +01:00
Wii [ i ] - > setLedOn ( LED3 ) ;
Serial . print ( F ( " \r \n Right " ) ) ;
}
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( DOWN ) ) {
2013-11-15 23:19:10 +01:00
Wii [ i ] - > setLedOff ( ) ;
2013-01-20 22:46:04 +01:00
Wii [ i ] - > setLedOn ( LED4 ) ;
Serial . print ( F ( " \r \n Down " ) ) ;
2013-11-15 19:05:25 +01:00
}
if ( Wii [ i ] - > getButtonClick ( UP ) ) {
2013-11-15 23:19:10 +01:00
Wii [ i ] - > setLedOff ( ) ;
2013-11-15 19:05:25 +01:00
Wii [ i ] - > setLedOn ( LED2 ) ;
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n Up " ) ) ;
}
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( PLUS ) )
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n Plus " ) ) ;
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( MINUS ) )
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n Minus " ) ) ;
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( ONE ) )
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n One " ) ) ;
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( TWO ) )
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n Two " ) ) ;
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( A ) ) {
2013-01-20 22:46:04 +01:00
printAngle [ i ] = ! printAngle [ i ] ;
Serial . print ( F ( " \r \n A " ) ) ;
2013-11-15 19:05:25 +01:00
}
if ( Wii [ i ] - > getButtonClick ( B ) ) {
2013-01-20 22:46:04 +01:00
Wii [ i ] - > setRumbleToggle ( ) ;
Serial . print ( F ( " \r \n B " ) ) ;
}
}
2013-11-15 19:05:25 +01:00
if ( printAngle [ i ] ) {
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n Pitch: " ) ) ;
Serial . print ( Wii [ i ] - > getPitch ( ) ) ;
Serial . print ( F ( " \t Roll: " ) ) ;
Serial . print ( Wii [ i ] - > getRoll ( ) ) ;
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > motionPlusConnected ) {
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \t Yaw: " ) ) ;
Serial . print ( Wii [ i ] - > getYaw ( ) ) ;
2013-11-15 19:05:25 +01:00
}
if ( Wii [ i ] - > nunchuckConnected ) {
2013-01-20 22:46:04 +01:00
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
}
}
}
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > nunchuckConnected ) {
if ( Wii [ i ] - > getButtonClick ( Z ) )
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n Z " ) ) ;
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getButtonClick ( C ) )
2013-01-20 22:46:04 +01:00
Serial . print ( F ( " \r \n C " ) ) ;
2013-11-15 19:05:25 +01:00
if ( Wii [ i ] - > getAnalogHat ( HatX ) > 137 | | Wii [ i ] - > getAnalogHat ( HatX ) < 117 | | Wii [ i ] - > getAnalogHat ( HatY ) > 137 | | Wii [ i ] - > getAnalogHat ( HatY ) < 117 ) {
2013-01-20 22:46:04 +01:00
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 ( ) {
2013-11-15 19:05:25 +01:00
for ( uint8_t i = 0 ; i < length ; i + + ) {
2013-07-14 01:28:47 +02:00
if ( Wii [ i ] - > wiimoteConnected & & ! oldControllerState [ i ] ) {
oldControllerState [ i ] = true ; // Used to check which is the new controller
2014-01-04 13:43:49 +01:00
Wii [ i ] - > setLedOn ( ( LEDEnum ) ( i + 1 ) ) ; // Cast directly to LEDEnum - see: "controllerEnums.h"
2013-07-14 01:28:47 +02:00
}
}
}