Ananya Iyer / Mbed 2 deprecated NRF_Wireless_mouse

Dependencies:   USBDevice mbed nRF24L01P

Fork of kl25Z_nRF_RX by Ganesh Gore

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Receiver.cpp Source File

Receiver.cpp

00001 #include "mbed.h"
00002 #include "nRF24L01P.h"
00003 #include "USBMouse.h"
00004 Serial pc(USBTX, USBRX); // tx, rx
00005 
00006 nRF24L01P my_nrf24l01p(PTD6, PTE3, PTE2, PTB8, PTB9, PTD0);    // mosi, miso, sck, csn, ce, irq
00007 DigitalOut GreenLED(LED2);
00008 USBMouse mouse;
00009 char count[3] = {'\0'};
00010 int main() {
00011     
00012     
00013     char RxDataCnt;
00014     char temp;
00015 
00016     my_nrf24l01p.powerUp();
00017     my_nrf24l01p.setRfFrequency(2510);
00018 
00019     // Display the (default) setup of the nRF24L01+ chip
00020     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00021     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00022     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00023     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00024     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00025 
00026     pc.printf( "Simple 2 Byte Receiver\r\n" );
00027     
00028     RxDataCnt = 3;
00029     my_nrf24l01p.setTransferSize( RxDataCnt );
00030     
00031     my_nrf24l01p.setReceiveMode();
00032     my_nrf24l01p.enable();
00033 
00034     while (1) {
00035 
00036         // If we've received anything in the nRF24L01+...
00037         if ( my_nrf24l01p.readable() ) {
00038 
00039             // ...read the data into the receive buffer
00040             temp = my_nrf24l01p.read( NRF24L01P_PIPE_P0, count, RxDataCnt );
00041 
00042 
00043             pc.printf( "%d %d %d\r\n",count[0],count[1],count[2]);
00044 
00045 int xaxis = int(count[0]);
00046 int yaxis = int(count[1]);
00047 int value = int(count[2]);
00048 
00049 
00050             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00051             if( xaxis >=0 && xaxis <=50 )
00052     mouse.move(0,-3);
00053     
00054     if( xaxis >=150 && xaxis <=200 )
00055     mouse.move(0,3);
00056     
00057     if( xaxis >50 && xaxis <=80 )
00058     mouse.move(0,-1);
00059     
00060     if( xaxis >=120 && xaxis <150 )
00061     mouse.move(0,1);
00062     
00063     
00064     
00065     if( yaxis >50 && yaxis <=80 )
00066     mouse.move(1,0);
00067     
00068     if( yaxis >=120 && yaxis <150 )
00069     mouse.move(-1,0);
00070     
00071     if( yaxis >=0 && yaxis <=50 )
00072     mouse.move(3,0);
00073     
00074     if( yaxis >=150 && yaxis <=200 )
00075     mouse.move(-3,0);
00076     
00077     if( xaxis >=0 && xaxis <=80 && yaxis >=0 && yaxis <=80 )
00078     mouse.move(2,-2);
00079     
00080     if( xaxis >=120 && xaxis <=200 && yaxis >=0 && yaxis <=80 )
00081     mouse.move(2,2);
00082     
00083     if( xaxis >=0 && xaxis <=80 && yaxis >=120 && yaxis <=200 )
00084     mouse.move(-2,-2);
00085     
00086     if( xaxis >=120 && xaxis <=200 && yaxis >=120 && yaxis <=200)
00087     mouse.move(-2,2);
00088     
00089     if(value>50)
00090     {mouse.press(MOUSE_LEFT);
00091      wait(0.1);
00092      mouse.release(MOUSE_LEFT);
00093     }
00094     
00095     if(value<50 && value>0)
00096     {mouse.press(MOUSE_RIGHT);
00097      wait(0.1);
00098      mouse.release(MOUSE_RIGHT);
00099     }
00100     else
00101      mouse.move(0,0);
00102         }
00103     }
00104 }