Permalink
Browse files

fixed bug in c button, added feedback lights on pins 2 and 3 for butt…

…on presses on the Arduino
  • Loading branch information...
1 parent 519d7a2 commit a248d0e38aab357047844b37f4f66d6f5318f291 @pixelpusher committed May 8, 2012
@@ -84,7 +84,7 @@ class WiiChuck {
uint16_t holdCycles; // number of cycles button is held down, for re-calibration
- bool lastZ, lastC;
+ bool lastZ, lastC, zPress, cPress;
public:
@@ -96,6 +96,10 @@ class WiiChuck {
void begin()
{
+
+ pinMode(2,OUTPUT);
+ pinMode(3,OUTPUT);
+
//Set power pinds
DDRC |= _BV(pwrpin) | _BV(gndpin);
@@ -111,6 +115,8 @@ class WiiChuck {
Wire.begin();
cnt = 0;
averageCounter = 0;
+ zPress = cPress = false;
+
// instead of the common 0x40 -> 0x00 initialization, we
// use 0xF0 -> 0x55 followed by 0xFB -> 0x00.
// this lets us use 3rd party nunchucks (like cheap $4 ebay ones)
@@ -139,7 +145,8 @@ class WiiChuck {
}
- void calibrateJoy() {
+ void calibrateJoy()
+ {
zeroJoyX = joyX;
zeroJoyY = joyY;
}
@@ -154,9 +161,14 @@ class WiiChuck {
++cnt;
}
- if (cnt > 5) {
+ if (cnt > 5)
+ {
lastZ = buttonZ;
lastC = buttonC;
+
+ digitalWrite(2,buttonZ);
+ digitalWrite(3,buttonC);
+
lastJoyX = readJoyX();
lastJoyY = readJoyY();
@@ -185,6 +197,14 @@ class WiiChuck {
buttonZ = !( status[5] & B00000001);
buttonC = !((status[5] & B00000010) >> 1);
+
+ // changed these - presses stay true until they are received
+ if (buttonZ && !lastZ)
+ zPress = true;
+
+ if (buttonC && !lastC)
+ cPress = true;
+
_send_zero(); // send the request for next bytes
if (buttonZ && lastZ)
@@ -194,8 +214,6 @@ class WiiChuck {
}
else
holdCycles = 0;
-
-
}
}
@@ -204,8 +222,8 @@ class WiiChuck {
// return status;
//}
- float readAccelX() {
-
+ float readAccelX()
+ {
// total = 0;
// accelArray[xyz][averageCounter] * FAST_WEIGHT;
return (float)angles[0] - ZEROX;
@@ -221,11 +239,22 @@ class WiiChuck {
return (float)angles[2] - ZEROZ;
}
- bool zPressed() {
- return (buttonZ && ! lastZ);
+ bool zPressed()
+ {
+
+ bool result = zPress;
+ zPress = false; // make sure
+ return result;
+ // return (buttonZ && ! lastZ);
+
}
- bool cPressed() {
- return (buttonC && ! lastC);
+ bool cPressed()
+ {
+ bool result = cPress;
+ cPress = false; // ma
+ return result;
+
+ //return (buttonC && ! lastC);
}
// for using the joystick like a directional button
@@ -255,17 +284,17 @@ class WiiChuck {
// returns roll degrees
- int readRoll() {
+ inline int readRoll() {
return (int)(atan2(readAccelX(),readAccelZ())/ M_PI * 180.0);
}
// returns pitch in degrees
- int readPitch() {
+ inline int readPitch() {
return (int) (acos(readAccelY()/RADIUS)/ M_PI * 180.0); // optionally swap 'RADIUS' for 'R()'
}
private:
- uint8_t _nunchuk_decode_byte (uint8_t x)
+ inline uint8_t _nunchuk_decode_byte (uint8_t x)
{
//decode is only necessary with certain initializations
//x = (x ^ 0x17) + 0x17;
@@ -26,7 +26,7 @@ void setup() {
void loop() {
- delay(25);
+ delay(50);
chuck.update();
@@ -32,12 +32,12 @@ void setupWiiChuck()
chuck1.addListener( new IWiiChuckListener() {
public void zPressed()
{
- println("Z!!!");
+ println("Z!!! " + millis());
tapTempo();
}
public void cPressed()
{
- println("C!!!");
+ println("C!!!" + millis());
}
public void stateUpdated(WiiChuck chuck) { }
} );
@@ -22,10 +22,10 @@ class WiiChuck
// button states
final static int UP = 0;
- final int PRESSED = 1;
- final int HELD = 2; // anything greater than PRESSED means held (and we keep counting...)
+ final static int PRESSED = 1;
+ final static int HELD = 2; // anything greater than PRESSED means held (and we keep counting...)
- final int NUM_VALUES = 9;
+ final static int NUM_VALUES = 9;
int zButton, cButton, zPressed, cPressed; // should be above states only - could use enum but I'm lazy today
@@ -42,59 +42,66 @@ class WiiChuck
{
listeners.add(wiiLi);
}
-
+
void removeListener(IWiiChuckListener wiiLi)
{
listeners.remove(wiiLi);
}
-
+
void update(String values[]) // for converting from Serial object
{
if (values.length == NUM_VALUES)
- update(int(values[0]), int(values[1]), int(values[2]), int(values[3]), int(values[4]), int(values[5]),
- int(values[6]), int(values[7]), int(values[8]));
- }
-
- void update(int _roll, int _pitch, int _ax, int _ay, int _az, int _stickX, int _stickY, int _zPressed, int _cPressed)
- {
- roll = _roll * DEG_TO_RAD;
- pitch = _pitch * DEG_TO_RAD;
-
- ax = _ax;
- ay = _ay;
- az = _az;
-
- stickX = _stickX;
- stickY = _stickY;
-
- //zButton = _zButton; // if held, keep counting...
- //cButton = _cButton;
-
- zPressed = _zPressed;
- cPressed = _cPressed;
-
- for ( IWiiChuckListener wiiLi : listeners)
{
- wiiLi.stateUpdated( this );
-
- if (zPressed == 1)
+ /*
+ print("VALUES:" );
+ for (int i=0; i<values.length; ++i)
+ print(" ["+i+"]:"+values[i]);
+ println();
+ */
+
+ int _roll=int(values[0]);
+ int _pitch=int(values[1]);
+ int _ax=int(values[2]);
+ int _ay=int(values[3]);
+ int _az=int(values[4]);
+ int _stickX=int(values[5]);
+ int _stickY=int(values[6]);
+ int _zPressed=int(values[7]);
+ int _cPressed=int(trim(values[8])); // get rid of whitespace!
+
+ roll = _roll * DEG_TO_RAD;
+ pitch = _pitch * DEG_TO_RAD;
+
+ ax = _ax;
+ ay = _ay;
+ az = _az;
+
+ stickX = _stickX;
+ stickY = _stickY;
+
+ //zButton = _zButton; // if held, keep counting...
+ //cButton = _cButton;
+
+ zPressed = _zPressed;
+ cPressed = _cPressed;
+
+ for ( IWiiChuckListener wiiLi : listeners)
{
- zPressed = PRESSED;
- wiiLi.zPressed();
- }
- else
- zPressed = this.UP;
+ wiiLi.stateUpdated( this );
- if (cPressed == 1)
- {
- cPressed = PRESSED;
- wiiLi.cPressed();
+ if (zPressed == PRESSED)
+ {
+ wiiLi.zPressed();
+ }
+
+ if (cPressed == PRESSED)
+ {
+ wiiLi.cPressed();
+ }
}
- else
- cPressed = this.UP;
+ if (debug) println(this.toString());
}
- if (debug) println(this.toString());
}
void destroy()
@@ -48,7 +48,10 @@ void setup()
{
println("C!!!");
}
- public void stateUpdated(WiiChuck chuck) { }
+ public void stateUpdated(WiiChuck chuck) {
+ //println("CPRESSED:" + chuck.cPressed);
+
+ }
} );
}

0 comments on commit a248d0e

Please sign in to comment.