Wii Nunchuk via RFM69HW to Duplo 9203 Remote Control Car Kit using ARM mbed on a FRDM-KL25Z

Dependencies:   CRC FastPWM RFM69 USBDevice WakeUp WiiChuk_compat mbed-rtos mbed tlc59108

Fork of wiiNunchuk_compat by Greg Brush

Revision:
3:9091adbed369
Parent:
2:04fdd571a385
Child:
4:c9711f0cd097
--- a/main.cpp	Mon Jun 08 19:53:42 2015 +0000
+++ b/main.cpp	Tue Jun 09 08:48:01 2015 +0000
@@ -17,6 +17,8 @@
 #define FREQUENCY     RF69_868MHZ
 //#define FREQUENCY     RF69_915MHZ
 
+const char *directions[8] = { "XR", "RR", "RX", "FR", "FX", "FF", "XF", "RF" };
+    
 #ifndef USBSerial
     Serial pc(USBTX, USBRX);
 #endif
@@ -32,55 +34,62 @@
     g = 1.0f;
     PwmOut b(LED_BLUE);
     b = 1.0f;
-    //WiiChuck nun(PTE0, PTE1, pc);
+    WiiChuck nun(PTE0, PTE1, pc);
     RFM69 radio(PTD2, PTD3, PTC5, PTD0, PTA13);
 #else
     WiiChuck nun(p9, p10, pc);
 #endif
 
-    pc.printf("-- %d --\r\n", radio.initialize(FREQUENCY, NODE_ID, NETWORKID));
-    radio.encrypt("0123456789012345");
+    radio.initialize(FREQUENCY, NODE_ID, NETWORKID);
+    radio.encrypt("0123456789054321");
     //radio.promiscuous(false);
-    //radio.setHighPower(true);
-    radio.setPowerLevel(31);
-    //radio.rcCalibration();
+    radio.setHighPower(true);
+    radio.setPowerLevel(20);
+    radio.rcCalibration();
     //radio.readAllRegs();
     pc.printf("temp %d\r\n", radio.readTemperature(-1));
-    pc.printf("wii test begin                                             \r\n");
 
-
+    bool central = true;
     nunchuk n;
-     
     while(1) {
-        bool read = true;//nun.Read(&n.X, &n.Y, &n.aX, &n.aY, &n.aZ, &n.C, &n.Z);
+        bool read = nun.Read(&n.X, &n.Y, &n.aX, &n.aY, &n.aZ, &n.C, &n.Z);
         if(read)
         {
             float x = n.X - 128, y = n.Y - 128;
-            float R = 1000, p = 90.0f;//x*x + y*y, p = atan2(y, x) * 4 / M_PI - 0.5;
+            float R = x*x + y*y, p = atan2(y, x) * 4 / M_PI - 0.5;
             int c = 0;
-            const char *d = "RF";
-            if (p > -4) {c = 0; d = "XR";}
-            if (p > -3) {c = 1; d = "RR";}
-            if (p > -2) {c = 2; d = "RX";}
-            if (p > -1) {c = 3; d = "FR";}
-            if (p >  0) {c = 4; d = "FX";}
-            if (p >  1) {c = 5; d = "FF";}
-            if (p >  2) {c = 6; d = "XF";}
-            if (p >  3) {c = 7; d = "RF";}
-            
-            //pc.printf("x%3d y%3d c%1d z%1d --", n.X, n.Y, n.C, n.Z);
-            //pc.printf("x%d y%d z%d -- %.3f %s                   \r\n", n.aX, n.aY, n.aZ, R, d);
+            if (p > -4) c = 0;
+            if (p > -3) c = 1;
+            if (p > -2) c = 2;
+            if (p > -1) c = 3;
+            if (p >  0) c = 4;
+            if (p >  1) c = 5;
+            if (p >  2) c = 6;
+            if (p >  3) c = 7;
 
-            radio.send(GATEWAY_ID, (const void*)"A", 1, false);
-            //radio.send(GATEWAY_ID, (const void*)&n, sizeof(nunchuk), false);
-            pc.printf("A");
+#ifdef DEBUG
+            pc.printf("x%3d y%3d c%1d z%1d --", n.X, n.Y, n.C, n.Z);
+            pc.printf("x%d y%d z%d -- %.3f %s                   \r\n", n.aX, n.aY, n.aZ, R, directions[c]);
+
+            //radio.send(GATEWAY_ID, (const void*)"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", 50, false);
+#endif
+            radio.send(GATEWAY_ID, (const void*)&n, sizeof(struct nunchuk), false);
+
 #ifdef TARGET_KL25Z
-            if (R < 100) {
+            if (R < 20) {
+                if (!central) {
+                    pc.printf("central\r\n");
+                    central = true;
+                }
                 r = 1.0f;
                 g = 1.0f;
                 b = 1.0f;
             } else {
-                R = R/60000;
+                if (central) {
+                    pc.printf("go\r\n");
+                    central = false;
+                }
+                R = R/20000;
                 float pal[8][3] = {
                     {   0,   0,   1 },
                     {   0,   1,   1 },
@@ -100,8 +109,8 @@
         else
         {
             pc.printf("Error\r\n");
-            wait(1);
+            return 0;
         }
-        wait(0.1);
+        wait(0.01);
     }
 }