Permalink
Browse files

updated ClassicController code for arduino 1.0, and the arduino mega

  • Loading branch information...
1 parent 6988cad commit 560910e4f715b9388e404cb3c1e61b5414b7f96d @matthewbeckler matthewbeckler committed Aug 18, 2012
View
29 ClassicController/ClassicController.cpp
@@ -2,7 +2,9 @@
ClassicController.cpp - Library for the Nintendo Wii Classic Controller
Created by Adam Wolf and Matthew Beckler of Wayne and Layne, LLC
http://wayneandlayne.com/projects/video-game-shield/
- Last updated: January 18, 2012 - Arduino 1.0 compatability fix
+ Recent updates:
+ August 11, 2012 - Changed initialization bytes to support knock-off nunchucks
+ January 18, 2012 - Arduino 1.0 compatability fix
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -70,7 +72,7 @@ unsigned char ClassicController::begin(unsigned char which_controller)
joy_right_y_scaled_max = CLASSICCONTROLLER_JOY_Y_SCALED_MAX;
- DDRB |= 0x10; // set DDRB4 bit = make it an output (used for controller select)
+ WII_SELECT_SET_OUTPUT_DIR;
this_controller = which_controller;
if (this_controller == WII_PLAYER_2)
@@ -84,9 +86,19 @@ unsigned char ClassicController::begin(unsigned char which_controller)
// could not access device
return 1;
}
- i2cmaster::i2c_write(0x40);
+
+ // this new init bytes come from this arduino.cc forum post:
+ // http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1264805255/4#4
+ i2cmaster::i2c_write(0xF0);
+ i2cmaster::i2c_write(0x55);
+ i2cmaster::i2c_stop();
+ delay(1);
+
+ i2cmaster::i2c_start_wait(WII_I2C_ADDR + I2C_WRITE, 10);
+ i2cmaster::i2c_write(0xFB);
i2cmaster::i2c_write(0x00);
i2cmaster::i2c_stop();
+ delay(1);
return 0;
}
@@ -107,9 +119,9 @@ void ClassicController::update()
i2cmaster::i2c_start_wait(WII_I2C_ADDR + I2C_READ, 0xFFFF);
for (unsigned char i = 0; i < 5; i++)
{
- data[i] = _decode_byte(i2cmaster::i2c_readAck());
+ data[i] = i2cmaster::i2c_readAck();
}
- data[5] = _decode_byte(i2cmaster::i2c_readNak());
+ data[5] = i2cmaster::i2c_readNak();
i2cmaster::i2c_stop();
}
@@ -330,10 +342,3 @@ unsigned char ClassicController::joy_right_y_scaled()
return map(constrain(joy_right_y(), joy_right_y_min, joy_right_y_max), joy_right_y_min, joy_right_y_max, joy_right_y_scaled_min, joy_right_y_scaled_max);
}
-// Internal function to decode data from controller
-unsigned char ClassicController::_decode_byte (unsigned char x)
-{
- x = (x ^ 0x17) + 0x17;
- return x;
-}
-
View
34 ClassicController/ClassicController.h
@@ -2,7 +2,9 @@
ClassicController.h - Library for the Nintendo Wii Classic Controller
Created by: Adam Wolf and Matthew Beckler of Wayne and Layne, LLC
http://wayneandlayne.com/projects/video-game-shield/
- Last updated: January 18, 2012 - Arduino 1.0 compatability fix
+ Recent updates:
+ August 11, 2012 - Changed initialization bytes to support knock-off nunchucks
+ January 18, 2012 - Arduino 1.0 compatability fixes
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -19,6 +21,7 @@
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Some information taken from http://www.arduino.cc/playground/Main/WiiClassicController
+
*/
#ifndef CLASSICCONTROLLER_H
#define CLASSICCONTROLLER_H
@@ -29,11 +32,22 @@
#include "WProgram.h"
#endif
-#define WII_I2C_ADDR 0xA4
-#define WII_SELECT_P1 PORTB &= 0xEF // set PB4 = low
-#define WII_SELECT_P2 PORTB |= 0x10 // set PB4 = high
-#define WII_PLAYER_1 0
-#define WII_PLAYER_2 1
+#define WII_I2C_ADDR 0xA4
+#define WII_PLAYER_1 0
+#define WII_PLAYER_2 1
+// The VGS board has the nunchuck/cc-select signal line connected to Arduino pin D12.
+// On the UNO and compatible (atmega328p et al) D12 is PB4.
+// On the MEGA 2560 and compatible (atmega2560 et al) D12 is PB6.
+// We use an ifdef here to make our WII_SELECT_P{1,2} macros be correct
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
+ #define WII_SELECT_SET_OUTPUT_DIR DDRB |= 0x40 // set DDRB4 bit = make it an output (used for nunchuck select)
+ #define WII_SELECT_P1 PORTB &= 0xBF // set PB6 = low
+ #define WII_SELECT_P2 PORTB |= 0x40 // set PB6 = high
+#else
+ #define WII_SELECT_SET_OUTPUT_DIR DDRB |= 0x10 // set DDRB4 bit = make it an output (used for nunchuck select)
+ #define WII_SELECT_P1 PORTB &= 0xEF // set PB4 = low
+ #define WII_SELECT_P2 PORTB |= 0x10 // set PB4 = high
+#endif
// These are default values:
#define CLASSICCONTROLLER_JOY_LEFT_MAX 22
@@ -56,9 +70,9 @@
class ClassicController
{
public:
- // returns 0=success, 1=could not access device
- // Provide a 0 or 1 to which_controller to specify which player this is
- // 0 means "Player 1", 1 means "Player 2"
+ // Returns 0=success, 1=could not access device
+ // Argument specifies which_nunchuck to initialize,
+ // pass in either WII_PLAYER_1 or WII_PLAYER_2
unsigned char begin(unsigned char which_controller);
// Call this function to query the Controller and update the data
@@ -172,8 +186,6 @@ class ClassicController
unsigned char joy_right_x_scaled_max;
unsigned char joy_right_y_scaled_min;
unsigned char joy_right_y_scaled_max;
-
- unsigned char _decode_byte(unsigned char);
};
#endif
View
4 ...ntroller_demo/classic_controller_demo.pde → ...ntroller_demo/classic_controller_demo.ino
@@ -2,7 +2,9 @@
Arduino sketch to demonstrate interfacing with a Nintendo Wii Classic Controller.
Created by Adam Wolf and Matthew Beckler
Wayne and Layne, LLC - http://wayneandlayne.com/projects/video-game-shield/
- Last updated: January 16, 2011
+ Recent updates:
+ August 18, 2012 - Updated filename extension to .ino for arduino 1.0 compatability
+ January 16, 2011 - Initial release
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

0 comments on commit 560910e

Please sign in to comment.