AJINKYA MALI / Mbed 2 deprecated changedtransmitter

Dependencies:   MMA8451Q TSI USBDevice mbed nRF24L01P

Fork of kl25Z_nRF_TX by Ganesh Gore

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Transmitter.cpp Source File

Transmitter.cpp

00001 #include "mbed.h"
00002 #include "nRF24L01P.h"
00003 #include "USBMouse.h"
00004 #include "TSISensor.h"
00005 #include "MMA8451Q.h"
00006 #define MMA8451_I2C_ADDRESS (0x1d<<1) 
00007 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
00008 Serial pc(USBTX, USBRX); // tx, rx
00009  
00010 nRF24L01P my_nrf24l01p(PTD6, PTE3, PTE2, PTB8, PTB9, PTD0);    // mosi, miso, sck, csn, ce, irq
00011 
00012  
00013  TSISensor tsi;
00014 USBMouse mouse;
00015 float value;
00016 int xaxis, yaxis, zaxis;
00017 
00018 int main()
00019 {   
00020     char count[4];
00021     char TxDataCnt;
00022     
00023     char RxDataCnt;
00024     char temp;
00025 
00026     
00027     my_nrf24l01p.powerUp();
00028     my_nrf24l01p.setRfFrequency(2510);
00029       
00030    if(count[3]==0)
00031    {
00032     // Display the (default) setup of the nRF24L01+ chip
00033     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00034     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00035     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00036     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00037     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00038 
00039     pc.printf( "Simple Transmitter (0 - 9 Counter) \r\n");
00040 
00041     TxDataCnt = 4;
00042     my_nrf24l01p.setTransferSize(TxDataCnt);
00043 
00044     my_nrf24l01p.enable();
00045 
00046     
00047         
00048     while (1) {
00049 count[2]=tsi.readPercentage()*100;
00050     count[0]=(acc.getAccX()+1)*100;
00051         count[1]=(acc.getAccY()+1)*100;
00052         // Send the Transmit buffer via the nRF24L01+
00053         temp = my_nrf24l01p.write( NRF24L01P_PIPE_P0,count, TxDataCnt );
00054 
00055         pc.printf( "Sending %d - %d %d %d\r\n",temp,count[0],count[1],count[2],count[3]);
00056 
00057         // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
00058         
00059             
00060         if ( count[0] >=280 || count[1] >= 280 )
00061         {count[3]++;
00062          break;}
00063         
00064         
00065     }
00066     
00067     
00068     
00069     }
00070     
00071     
00072     if( count[3]==1)
00073     {
00074         // Display the (default) setup of the nRF24L01+ chip
00075     pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  my_nrf24l01p.getRfFrequency() );
00076     pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
00077     pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
00078     pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
00079     pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
00080 
00081     pc.printf( "Simple 2 Byte Receiver\r\n" );
00082     
00083     RxDataCnt = 3;
00084     my_nrf24l01p.setTransferSize( RxDataCnt );
00085     
00086     my_nrf24l01p.setReceiveMode();
00087     my_nrf24l01p.enable();
00088 
00089     while (1) {
00090 
00091         // If we've received anything in the nRF24L01+...
00092         if ( my_nrf24l01p.readable() ) {
00093 
00094             // ...read the data into the receive buffer
00095             temp = my_nrf24l01p.read( NRF24L01P_PIPE_P0, count, RxDataCnt );
00096 
00097 
00098             pc.printf( "%d %d %d\r\n",count[0],count[1],count[2]);
00099 
00100 int xaxis = int(count[0]);
00101 int yaxis = int(count[1]);
00102 int value = int(count[2]);
00103 
00104 
00105             // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
00106             if( xaxis >=0 && xaxis <=50 )
00107     mouse.move(0,-3);
00108     
00109     if( xaxis >=150 && xaxis <=200 )
00110     mouse.move(0,3);
00111     
00112     if( xaxis >50 && xaxis <=80 )
00113     mouse.move(0,-1);
00114     
00115     if( xaxis >=120 && xaxis <150 )
00116     mouse.move(0,1);
00117     
00118     
00119     
00120     if( yaxis >50 && yaxis <=80 )
00121     mouse.move(1,0);
00122     
00123     if( yaxis >=120 && yaxis <150 )
00124     mouse.move(-1,0);
00125     
00126     if( yaxis >=0 && yaxis <=50 )
00127     mouse.move(3,0);
00128     
00129     if( yaxis >=150 && yaxis <=200 )
00130     mouse.move(-3,0);
00131     
00132     if( xaxis >=0 && xaxis <=80 && yaxis >=0 && yaxis <=80 )
00133     mouse.move(2,-2);
00134     
00135     if( xaxis >=120 && xaxis <=200 && yaxis >=0 && yaxis <=80 )
00136     mouse.move(2,2);
00137     
00138     if( xaxis >=0 && xaxis <=80 && yaxis >=120 && yaxis <=200 )
00139     mouse.move(-2,-2);
00140     
00141     if( xaxis >=120 && xaxis <=200 && yaxis >=120 && yaxis <=200)
00142     mouse.move(-2,2);
00143     
00144     if(value>50)
00145     {mouse.press(MOUSE_LEFT);
00146      wait(0.1);
00147      mouse.release(MOUSE_LEFT);
00148     }
00149     
00150     if(value<50 && value>0)
00151     {mouse.press(MOUSE_RIGHT);
00152      wait(0.1);
00153      mouse.release(MOUSE_RIGHT);
00154     }
00155     else
00156      mouse.move(0,0);
00157      
00158      if ( xaxis >=280 || yaxis >= 280 )
00159         {count[3]=0;
00160          break;}
00161         }
00162     }
00163     }
00164     
00165 }