public
Created

DealExtreme Wii Nunchuk

  • Download Gist
ArduinoNunchuk.cpp
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
/*
* ArduinoNunchuk.cpp - Improved Wii Nunchuk library for Arduino
*
* Copyright 2011-2012 Gabriel Bianconi, http://www.gabrielbianconi.com/
*
* Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
*
* Based on the following projects/websites:
* http://www.windmeadow.com/node/42
* http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
* http://wiibrew.org/wiki/Wiimote/Extension_Controllers
*
*/
 
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
 
#include <Wire.h>
#include "ArduinoNunchuk.h"
 
void ArduinoNunchuk::init()
{
Wire.begin();
ArduinoNunchuk::_sendByte(0x55, 0xF0, 0xA4);
ArduinoNunchuk::_sendByte(0x00, 0xFB0, 0xA4);
ArduinoNunchuk::update();
}
void ArduinoNunchuk::update()
{
int count = 0;
int values[5];
Wire.requestFrom (0xA5, 6);
while(Wire.available())
{
values[count] = Wire.read();
count++;
}
ArduinoNunchuk::analogX = values[0];
ArduinoNunchuk::analogY = values[1];
ArduinoNunchuk::accelX = values[2] * 2 * 2 + ((values[5] >> 2) & 1) * 2 + ((values[5] >> 3) & 1);
ArduinoNunchuk::accelY = values[3] * 2 * 2 + ((values[5] >> 4) & 1) * 2 + ((values[5] >> 5) & 1);
ArduinoNunchuk::accelZ = values[4] * 2 * 2 + ((values[5] >> 6) & 1) * 2 + ((values[5] >> 7) & 1);
ArduinoNunchuk::zButton = !((values[5] >> 0) & 1);
ArduinoNunchuk::cButton = !((values[5] >> 1) & 1);
ArduinoNunchuk::_sendByte(0x00, 0x00, 0xA5);
}
void ArduinoNunchuk::_sendByte(byte data, byte location, byte address)
{
Wire.beginTransmission(address);
#if (ARDUINO >= 100)
Wire.write(location);
Wire.write(data);
#else
Wire.send(location);
Wire.send(data);
#endif
Wire.endTransmission();
delay(10);
}
ArduinoNunchuk.h
C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
/*
* ArduinoNunchuk.h - Improved Wii Nunchuk library for Arduino
*
* Copyright 2011-2012 Gabriel Bianconi, http://www.gabrielbianconi.com/
*
* Project URL: http://www.gabrielbianconi.com/projects/arduinonunchuk/
*
* Based on the following projects/websites:
* http://www.windmeadow.com/node/42
* http://todbot.com/blog/2008/02/18/wiichuck-wii-nunchuck-adapter-available/
* http://wiibrew.org/wiki/Wiimote/Extension_Controllers
*
*/
 
#ifndef ArduinoNunchuk_H
#define ArduinoNunchuk_H
 
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
 
class ArduinoNunchuk
{
public:
int analogX;
int analogY;
int accelX;
int accelY;
int accelZ;
int zButton;
int cButton;
void init();
void update();
private:
void _sendByte(byte data, byte location, byte address);
};
 
#endif

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.