Based on Alfredo Guerrero's WiiClassicControllerReader although I had to do additional decoding of packet. KL25Z talking to Wii classic controller.

Dependencies:   CommonTypes WiiClassicControllerLib mbed

Files at this revision

API Documentation at this revision

Comitter:
RichardE
Date:
Sun Jun 30 16:51:43 2013 +0000
Parent:
3:21e262cee052
Commit message:
Now shows the calibrated analogue readings as well as the raw counts and allows operator to calibrate analogue inputs by pressing A and B. Baud rate to PC changed to 115200 baud 8N1.

Changed in this revision

WiiClassicControllerLib.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 21e262cee052 -r 71f5e3d2b0c6 WiiClassicControllerLib.lib
--- a/WiiClassicControllerLib.lib	Sun Jun 30 12:08:06 2013 +0000
+++ b/WiiClassicControllerLib.lib	Sun Jun 30 16:51:43 2013 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/RichardE/code/WiiClassicControllerLib/#52c4a0b3a509
+http://mbed.org/users/RichardE/code/WiiClassicControllerLib/#79d04d737f02
diff -r 21e262cee052 -r 71f5e3d2b0c6 main.cpp
--- a/main.cpp	Sun Jun 30 12:08:06 2013 +0000
+++ b/main.cpp	Sun Jun 30 16:51:43 2013 +0000
@@ -9,9 +9,9 @@
 #include "WiiClassicControllerWithCalibration.h"
 
 // Define this to dump packet in hex.
-#define DUMP_PACKET
+#undef DUMP_PACKET
 
-#define LOOP_DELAY    1    // seconds
+#define LOOP_DELAY (float)0.025 // seconds
 
 // global declarations
 Serial pc(USBTX, USBRX);
@@ -21,53 +21,92 @@
  * @param portname Name of port you are reading from.
  */
 static void ReadAndReport( WiiClassicControllerWithCalibration *ctrlr, const char *portname ) {
+    static int counter = 0;
     if( ctrlr->Read() ) {
 
-        pc.printf( "%c[H", 27 );                            // VT100 home cursor
-        pc.printf("%s: ", portname);
-    
-        #ifdef DUMP_PACKET
-            int bufSize = ctrlr->GetReadBufSize();
-            UInt8 *bufPtr = ctrlr->GetReadBuf();
-            for (int i = 0; i < bufSize; i++) {
-                pc.printf("%x ", bufPtr[i]);
-            }
-            pc.printf("\r\n");
-        #endif
+        // Display updated every tenth time.    
+        if( counter == 0 ) {
+
+            pc.printf( "%c[H", 27 );                            // VT100 home cursor
+            pc.printf("%s\r\n", portname);
         
-        pc.printf("A=%u\r\n", ctrlr->GetButtonA());
-        pc.printf("B=%u\r\n", ctrlr->GetButtonB());
-        pc.printf("X=%u\r\n", ctrlr->GetButtonX());
-        pc.printf("Y=%u\r\n", ctrlr->GetButtonY());
-        pc.printf("ZL=%u\r\n", ctrlr->GetButtonZL());
-        pc.printf("ZR=%u\r\n", ctrlr->GetButtonZR());
-        pc.printf("Up=%u\r\n", ctrlr->GetButtonUp());
-        pc.printf("Down=%u\r\n", ctrlr->GetButtonDown());
-        pc.printf("Left=%u\r\n", ctrlr->GetButtonLeft());
-        pc.printf("Right=%u\r\n", ctrlr->GetButtonRight());
-        pc.printf("Home=%u\r\n", ctrlr->GetButtonHome());
-        pc.printf("Select=%u\r\n", ctrlr->GetButtonSelect());
-        pc.printf("Start=%u\r\n", ctrlr->GetButtonStart());
-        pc.printf("LeftTrigger=%2u (%5.3f)\r\n", ctrlr->GetLeftTrigger(), ctrlr->GetCalLeftTrigger() );
-        pc.printf("ButtonLT=%u\r\n", ctrlr->GetButtonLT());
-        pc.printf("RightTrigger=%2u (%5.3f)\r\n", ctrlr->GetRightTrigger(), ctrlr->GetCalRightTrigger() );
-        pc.printf("ButtonRT=%u\r\n", ctrlr->GetButtonRT());
-        pc.printf("LJoyX=%2u (%5.3f)\r\n", ctrlr->GetLJoyX(), ctrlr->GetCalLJoyX() );
-        pc.printf("LJoyY=%2u (%5.3f)\r\n", ctrlr->GetLJoyY(), ctrlr->GetCalLJoyY() );
-        pc.printf("RJoyX=%2u (%5.3f)\r\n", ctrlr->GetRJoyX(), ctrlr->GetCalRJoyX() );
-        pc.printf("RJoyY=%2u (%5.3f)\r\n", ctrlr->GetRJoyY(), ctrlr->GetCalRJoyY() );
+            #ifdef DUMP_PACKET
+                int bufSize = ctrlr->GetReadBufSize();
+                UInt8 *bufPtr = ctrlr->GetReadBuf();
+                for (int i = 0; i < bufSize; i++) {
+                    pc.printf("%x ", bufPtr[i]);
+                }
+                pc.printf("\r\n");
+            #endif
+            
+            pc.printf("A=%u\r\n", ctrlr->GetButtonA());
+            pc.printf("B=%u\r\n", ctrlr->GetButtonB());
+            pc.printf("X=%u\r\n", ctrlr->GetButtonX());
+            pc.printf("Y=%u\r\n", ctrlr->GetButtonY());
+            pc.printf("ZL=%u\r\n", ctrlr->GetButtonZL());
+            pc.printf("ZR=%u\r\n", ctrlr->GetButtonZR());
+            pc.printf("Up=%u\r\n", ctrlr->GetButtonUp());
+            pc.printf("Down=%u\r\n", ctrlr->GetButtonDown());
+            pc.printf("Left=%u\r\n", ctrlr->GetButtonLeft());
+            pc.printf("Right=%u\r\n", ctrlr->GetButtonRight());
+            pc.printf("Home=%u\r\n", ctrlr->GetButtonHome());
+            pc.printf("Select=%u\r\n", ctrlr->GetButtonSelect());
+            pc.printf("Start=%u\r\n", ctrlr->GetButtonStart());
+            pc.printf("LeftTrigger=%2u (%6.3f)\r\n", ctrlr->GetLeftTrigger(), ctrlr->GetCalLeftTrigger() );
+            pc.printf("ButtonLT=%u\r\n", ctrlr->GetButtonLT());
+            pc.printf("RightTrigger=%2u (%6.3f)\r\n", ctrlr->GetRightTrigger(), ctrlr->GetCalRightTrigger() );
+            pc.printf("ButtonRT=%u\r\n", ctrlr->GetButtonRT());
+            pc.printf("LJoyX=%2u (%6.3f)\r\n", ctrlr->GetLJoyX(), ctrlr->GetCalLJoyX() );
+            pc.printf("LJoyY=%2u (%6.3f)\r\n", ctrlr->GetLJoyY(), ctrlr->GetCalLJoyY() );
+            pc.printf("RJoyX=%2u (%6.3f)\r\n", ctrlr->GetRJoyX(), ctrlr->GetCalRJoyX() );
+            pc.printf("RJoyY=%2u (%6.3f)\r\n", ctrlr->GetRJoyY(), ctrlr->GetCalRJoyY() );
+            if( ctrlr->IsCalibrating() ) {
+                pc.puts( "Wiggle joysticks and triggers. Press HOME button to end calibration." );
+            }
+            else {
+                pc.puts( "Press A and B buttons simultaneously to start calibration." );
+            }
+            
+        }
+
+        // Update counter.
+        // Display is updated every so often.
+        counter++;
+        counter %= 10;
+            
     }
     else {
         pc.puts( "READ FAILURE\r\n" );
     }
 }
 
+/** Clear the screen.
+ */
+static void ClearScreen( void ) {
+    pc.printf( "%c[2J", 27 );                                // VT100 clear screen
+}
+
 /** Main program.
  */
 int main() {
     WiiClassicControllerWithCalibration ctrlrA( PTE0, PTE1 );
+    pc.baud( 115200 );
+    pc.format( 8, Serial::None, 1 );
+    ClearScreen();
     while (true) {
         ReadAndReport( &ctrlrA, "PORT A" );
+        // If both A and B buttons are pressed and not already calibrating
+        // then start calibrating and clear the screen.
+        if( ctrlrA.GetButtonA() && ctrlrA.GetButtonB() && ! ctrlrA.IsCalibrating() ) {
+            ctrlrA.StartCalibrating();
+            ClearScreen();
+        }
+        // If HOME buttons is pressed and doing a calibration
+        // then stop calibrating and clear the screen.
+        if( ctrlrA.GetButtonHome() && ctrlrA.IsCalibrating() ) {
+            ctrlrA.StopCalibrating();
+            ClearScreen();
+        }
         wait(LOOP_DELAY);
     }
     // Never gets here.