59 lines
1.4 KiB
Arduino
59 lines
1.4 KiB
Arduino
|
/*
|
||
|
WiiNunSerial
|
||
|
(c)2012 kou029w - MIT License [http://kou029w.appspot.com/mit-license.txt]
|
||
|
|
||
|
WiiNun <= I2C => Arduino
|
||
|
CLK A5 CLK
|
||
|
DAT A4 DAT
|
||
|
PWR A3
|
||
|
GND A2
|
||
|
*/
|
||
|
|
||
|
#define PIN_PWR A3
|
||
|
#define PIN_GND A2
|
||
|
|
||
|
#include <Wire.h>
|
||
|
#include <WiiNun.h>
|
||
|
|
||
|
WiiNun WiiNun;
|
||
|
|
||
|
void setup(){
|
||
|
pinMode(PIN_PWR, OUTPUT);
|
||
|
pinMode(PIN_GND, OUTPUT);
|
||
|
digitalWrite(PIN_PWR, HIGH);
|
||
|
digitalWrite(PIN_GND, LOW);
|
||
|
delay(100);
|
||
|
Serial.begin(9600);
|
||
|
WiiNun.begin();
|
||
|
}
|
||
|
|
||
|
void loop(){
|
||
|
WiiNun.get();
|
||
|
byte joyx = WiiNun.data[0];
|
||
|
byte joyy = WiiNun.data[1];
|
||
|
byte accx = WiiNun.data[2];
|
||
|
byte accy = WiiNun.data[3];
|
||
|
byte accz = WiiNun.data[4];
|
||
|
if((WiiNun.data[5] >> 2) & 1)accx += 2;
|
||
|
if((WiiNun.data[5] >> 3) & 1)accx += 1;
|
||
|
if((WiiNun.data[5] >> 4) & 1)accy += 2;
|
||
|
if((WiiNun.data[5] >> 5) & 1)accy += 1;
|
||
|
if((WiiNun.data[5] >> 6) & 1)accz += 2;
|
||
|
if((WiiNun.data[5] >> 7) & 1)accz += 1;
|
||
|
Serial.print("Joy(x,y):(");
|
||
|
Serial.print(joyx, DEC); // 0x00:左, 0xFF:右
|
||
|
Serial.print(",");
|
||
|
Serial.print(joyy, DEC); // 0x00:下, 0xFF:上
|
||
|
Serial.print(") ");
|
||
|
Serial.print("Acc(x,y,z):(");
|
||
|
Serial.print(accx, DEC); //
|
||
|
Serial.print(",");
|
||
|
Serial.print(accy, DEC); //
|
||
|
Serial.print(",");
|
||
|
Serial.print(accz, DEC); //
|
||
|
Serial.print(") ");
|
||
|
if(~WiiNun.data[5] & 0x01<<0)Serial.print("Z ");
|
||
|
if(~WiiNun.data[5] & 0x01<<1)Serial.print("C ");
|
||
|
Serial.println();
|
||
|
delay(16);
|
||
|
}
|