A wireless accelerometer based joypad using FRDM-KL25Z for the Gameduino based space invaders.

Dependencies:   MMA8451Q mbed nRF2401A

Fork of nRF2401A_Hello_World by Chris Dick

nRF2401A connected to the KL25Z board:

/media/uploads/TheChrisyd/2014-03-08_22.55.16.jpg

Revision:
4:4a84fcba7bd4
Parent:
3:34ae527e9d41
Child:
5:8e11050f1464
--- a/main.cpp	Sun Oct 06 16:19:52 2013 +0000
+++ b/main.cpp	Sat Oct 26 22:43:56 2013 +0000
@@ -1,33 +1,25 @@
 #include "mbed.h"
 #include "nRF2401A.h"
+#include "MMA8451Q.h"
 
-// comment these out depending on the job of the mbed. If your only using one mbed leave both uncommented.
-#define TX
-#define RX
-
-Serial pc(USBTX, USBRX);
-DigitalOut  myled(LED1);
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+#define STICK_LEFT_BIT    0x01
+#define STICK_RIGHT_BIT   0x02
+#define STICK_UP_BIT      0x04
+#define STICK_DOWN_BIT    0x08
 
-#ifdef TX
-nRF2401A    rf1(p10, p11, p12, p13, p14);
-#endif
-
-#ifdef RX
-nRF2401A    rf2(p21, p22, p23, p24, p25);
+MMA8451Q  acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
+Serial    pc(USBTX, USBRX);
+nRF2401A  rf1(PTD0, PTD5, PTA13, PTC12, PTC13);
+PwmOut    rled(LED_RED);
+PwmOut    gled(LED_GREEN);
+PwmOut    bled(LED_BLUE);
 
-bool rx_recieved = false;
-
-void nRF2401A_rx (void *arg) 
-{
-     rx_recieved = true;
-}
-#endif
 int main() 
 {
     wait(0.005);
     pc.printf("Hello nRF2401A\n\r");
-    
-#ifdef TX    
+     
     rf1.setDataPayloadLength(4 << 3)
        .setAddress(0x0, 0x0, 0xa6, 0xa6, 0xa6, 3 << 3)
        .setCRCMode(nRF2401A::NO_CRC)
@@ -38,42 +30,47 @@
     rf1.flushControlPacket();
     
     nRF2401A::address_t rf2_addr = {0x0, 0x0, 0x53, 0x53, 0x53};
-    uint8_t msg[] = {0x01, 0x01, 0x01, 0x01};
-    uint32_t *msg32 = (uint32_t *) msg;
-#endif
-
-#ifdef RX   
-    rf2.setDataPayloadLength(4 << 3)
-       .setAddress(0x0, 0x0, 0x53, 0x53, 0x53, 3 << 3)
-       .setCRCMode(nRF2401A::NO_CRC)
-       .setDataRate(nRF2401A::BIT_RATE_250KBITS)
-       .setChannel(0x02);
-       
-    rf2.printControlPacket(pc);
-    rf2.flushControlPacket();   
-    rf2.attachRXHandler(&nRF2401A_rx, 0);
-#endif
+    uint8_t msg[] = {0x00, 0x00, 0x00, 0x00, 0x00};
       
     while(1) 
     {
-    
-#ifdef TX             
-        rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
-        *msg32 += 1;
-#endif
-
-        myled = 1;
-        wait(0.25);
+        #if 0
+        if ( acc.getAccZ() < -0.1)
+        {
+            msg[0] |= STICK_UP_BIT;
+        }
+        else if ( acc.getAccZ() > 0.1)
+        {
+            msg[0] |= STICK_DOWN_BIT;
+        }
+        #endif
+        if ( acc.getAccY() < -0.1)
+        {
+            msg[0] |= STICK_LEFT_BIT;
+        }
+        else if ( acc.getAccY() > 0.1)
+        {
+            msg[0] |= STICK_RIGHT_BIT;
+        }
+        if ( ( acc.getAccX() > 0.1) || (acc.getAccY() < -0.1) )
+        {
+            msg[1] |= 0x01;
+        }
         
-#ifdef RX  
-        if (rx_recieved)
-        {      
-            rf2.printDataPacket(pc);
-            rx_recieved = false;
-        }
-#endif 
-      
-        myled = 0;
-        wait(0.25);
+        
+        msg[2] = (uint8_t) ((acc.getAccX() + 1) * 127);
+        msg[3] = (uint8_t) ((acc.getAccY() + 1) * 127);
+        msg[4] = (uint8_t) ((acc.getAccZ() + 1) * 127);
+        
+        rf1.sendMsg(rf2_addr, 3 << 3, msg, 4 << 3);
+        
+        msg[0] = 0;
+        msg[1] = 0;
+        
+        rled = 1.0 - abs(acc.getAccX());
+        gled = 1.0 - abs(acc.getAccY());
+        bled = 1.0 - abs(acc.getAccZ());
+        wait(0.1);
     }
+    
 }