WIRELESS MOUSE CODE FOR RECEIVER USING NRF MODULE

Dependencies:   MMA8451Q TSI USBDevice mbed nRF24L01P

Fork of kl25Z_nRF_RX by Eduvance SIT2017

Revision:
4:47d9d6c2d7e9
Parent:
3:f3e27d1b37ad
--- a/Receiver.cpp	Sat Jun 04 05:35:08 2016 +0000
+++ b/Receiver.cpp	Sun Mar 18 16:05:36 2018 +0000
@@ -1,51 +1,57 @@
 #include "mbed.h"
 #include "nRF24L01P.h"
-
-Serial pc(USBTX, USBRX); // tx, rx
-
-nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTD5, PTD4);    // mosi, miso, sck, csn, ce, irq
-DigitalOut GreenLED(PTA12);
-
-
-int main() {
-    
-    char count[2];
-    char RxDataCnt;
-    char temp;
+#include "MMA8451Q.h"
+#include "TSISensor.h"
+#include "USBMouse.h"  
+#define MMA8451_addr (0X1d<<1)
+USBMouse mouse;
+MMA8451Q acc(PTE25,PTE24,MMA8451_addr);
+TSISensor tsi;
+Serial pc(USBTX, USBRX); 
+nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTD5, PTD4); 
 
+int main() 
+{
+    char count[4];
+    char RxDataCnt;
+    float x,y;
+    char temp;
     my_nrf24l01p.powerUp();
-    my_nrf24l01p.setRfFrequency(2410);
-
-    // Display the (default) setup of the nRF24L01+ chip
-    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
-    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
-    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
-    pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
-    pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
-
-    pc.printf( "Simple 2 Byte Receiver\r\n" );
+    my_nrf24l01p.setRfFrequency(2400);
     
-    RxDataCnt = 2;
+    RxDataCnt = 4;
     my_nrf24l01p.setTransferSize( RxDataCnt );
     
     my_nrf24l01p.setReceiveMode();
     my_nrf24l01p.enable();
 
-    while (1) {
-
-        // If we've received anything in the nRF24L01+...
-        if ( my_nrf24l01p.readable() ) {
-
-            // ...read the data into the receive buffer
-            temp = my_nrf24l01p.read( NRF24L01P_PIPE_P0, count, RxDataCnt );
+    while (1)
+     {
 
-
-            pc.printf( "Cnt %d = %d %d\r\n",temp,count[0],count[1]);
-
-
-            // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
-            GreenLED = !GreenLED;
-            wait_ms(10);
+        if ( my_nrf24l01p.readable() ) 
+        {
+            temp = my_nrf24l01p.read( NRF24L01P_PIPE_P0, count, RxDataCnt );
+            //  pc.printf( "Cnt %d = %d %d %d %d\r\n",temp,count[0],count[1],count[2],count[3]);
+            y=count[0];
+            x=count[1];
+            y=(y/50.0f)-(float)2;
+            x=(x/50.0f)-(float)2;
+            mouse.move(-1.0f*y*20.0f,x*20.0f);
+            if(count[2]==1)
+                {
+                mouse.press(MOUSE_LEFT);
+                }
+            else if(count[3]==1)
+                {
+                mouse.press(MOUSE_RIGHT);
+                }
+            else
+                {
+                 mouse.release(MOUSE_LEFT);
+                 mouse.release(MOUSE_RIGHT);
+                 }
+                 
+            wait_ms(50);
         }
     }
 }