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:
13:ab524cb4f768
Parent:
12:eec17d54a646
Child:
14:579137c8ceac
--- a/main.cpp	Mon Jun 27 21:53:15 2016 +0000
+++ b/main.cpp	Tue Jun 28 10:17:09 2016 +0000
@@ -1,5 +1,6 @@
 #include "mbed.h"
 #include "rtos.h"
+#include "WakeUp.h"
 #include "FastPWM.h"
 #ifndef M_PI
 #define M_PI           3.14159265358979323846
@@ -128,7 +129,7 @@
     while(1) {
         //if (!central)
             for(int x = 0; x < 5; x++) {
-                pc.printf(central? "stop %s %d\r\n" : "direction %s %d\r\n", directions[direction], stops_sent);
+                //pc.printf(central? "stop %s %d\r\n" : "direction %s %d\r\n", directions[direction], stops_sent);
                 if (central) {
                     if (stops_sent < 50) {
                         stops_sent++;
@@ -247,6 +248,7 @@
     WiiChuck nun(p9, p10, pc);
 #endif
 
+Timer rx_last_contact;
 bool rx_to_snooze = true;
 bool rx_snoozing = false;
 bool rx_snoozed() {
@@ -259,11 +261,12 @@
 }
 
 void rx_snoozer(void const *mainThreadID) {
-    pc.printf("snooze rx\r\n");
+    pc.printf("snooze rx %f\r\n", rx_last_contact.read());
     
     rx_snoozing = true;
-    radio.send(GATEWAY_ID, (const void*)"hello?", 6, false);
-    Thread::wait(5000);
+    radio.sleep();
+    WakeUp::set(5);//rx_last_contact.read() < 10? 1 : 5);
+    deepsleep();
     rx_snoozing = false;
     rx_to_snooze = true;
     pc.printf("unsnooze rx\r\n");
@@ -272,12 +275,14 @@
 
 int main()
 {
+    WakeUp::calibrate();
+    RtosTimer rx_snooze(&rx_snoozer, osTimerOnce, (void*)osThreadGetId());
 
-    RtosTimer rx_snooze(&rx_snoozer, osTimerOnce, (void*)osThreadGetId());
 #ifndef USBSerial
     pc.baud(115200);
 #endif
 
+    r = g = b = 1;
     gnd = 0;
     ir.period_us(1000/38);
 
@@ -335,6 +340,7 @@
             // LFO
             wave[127-k] = 1.0/(1400+200*sin(k/128.0*6.28318530717959));
         }
+        rx_last_contact.start();
    }    
     radio.encrypt("0123456789054321");
     //radio.promiscuous(false);
@@ -353,6 +359,7 @@
             n->sum = calculate_crc8((char*)n, sizeof(struct nunchuk));
         }
         else if (rx_snoozed() && radio.receiveDone()) {
+            rx_last_contact.reset();
             rx_snooze.stop();
             rx_to_snooze = true;
             pc.printf("rssi %d\r\n", radio.RSSI);
@@ -372,7 +379,7 @@
                 pc.printf("len %d\r\n", radio.DATALEN);
         } else if (rx_to_snooze) {
             pc.printf("to snooze\r\n");
-            rx_snooze.start(1000);
+            rx_snooze.start(200);
             rx_to_snooze = false;
         }
         if(read)