![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mouse transmitter
Dependencies: MMA8451Q TSI USBDevice mbed nRF24L01P
Fork of kl25Z_nRF_TX by
Transmitter.cpp@2:36a1ad8ff689, 2015-10-11 (annotated)
- Committer:
- aji052
- Date:
- Sun Oct 11 10:17:20 2015 +0000
- Revision:
- 2:36a1ad8ff689
- Parent:
- 1:2c6781177599
blah
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ganeshgore | 0:19a03658bf70 | 1 | #include "mbed.h" |
ganeshgore | 0:19a03658bf70 | 2 | #include "nRF24L01P.h" |
aji052 | 2:36a1ad8ff689 | 3 | #include "USBMouse.h" |
aji052 | 1:2c6781177599 | 4 | #include "TSISensor.h" |
aji052 | 1:2c6781177599 | 5 | #include "MMA8451Q.h" |
aji052 | 1:2c6781177599 | 6 | #define MMA8451_I2C_ADDRESS (0x1d<<1) |
aji052 | 1:2c6781177599 | 7 | MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS); |
ganeshgore | 0:19a03658bf70 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
aji052 | 1:2c6781177599 | 9 | |
aji052 | 1:2c6781177599 | 10 | nRF24L01P my_nrf24l01p(PTD6, PTE3, PTE2, PTB8, PTB9, PTD0); // mosi, miso, sck, csn, ce, irq |
ganeshgore | 0:19a03658bf70 | 11 | |
aji052 | 1:2c6781177599 | 12 | |
aji052 | 1:2c6781177599 | 13 | TSISensor tsi; |
aji052 | 2:36a1ad8ff689 | 14 | USBMouse mouse; |
aji052 | 1:2c6781177599 | 15 | float value; |
aji052 | 1:2c6781177599 | 16 | int xaxis, yaxis, zaxis; |
ganeshgore | 0:19a03658bf70 | 17 | |
ganeshgore | 0:19a03658bf70 | 18 | int main() |
aji052 | 2:36a1ad8ff689 | 19 | { |
aji052 | 2:36a1ad8ff689 | 20 | char count[4]; |
ganeshgore | 0:19a03658bf70 | 21 | char TxDataCnt; |
aji052 | 2:36a1ad8ff689 | 22 | |
aji052 | 2:36a1ad8ff689 | 23 | char RxDataCnt; |
ganeshgore | 0:19a03658bf70 | 24 | char temp; |
ganeshgore | 0:19a03658bf70 | 25 | |
aji052 | 2:36a1ad8ff689 | 26 | |
ganeshgore | 0:19a03658bf70 | 27 | my_nrf24l01p.powerUp(); |
aji052 | 1:2c6781177599 | 28 | my_nrf24l01p.setRfFrequency(2510); |
aji052 | 1:2c6781177599 | 29 | |
aji052 | 2:36a1ad8ff689 | 30 | if(count[3]==0) |
aji052 | 2:36a1ad8ff689 | 31 | { |
ganeshgore | 0:19a03658bf70 | 32 | // Display the (default) setup of the nRF24L01+ chip |
ganeshgore | 0:19a03658bf70 | 33 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
ganeshgore | 0:19a03658bf70 | 34 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
ganeshgore | 0:19a03658bf70 | 35 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
ganeshgore | 0:19a03658bf70 | 36 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
ganeshgore | 0:19a03658bf70 | 37 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
ganeshgore | 0:19a03658bf70 | 38 | |
ganeshgore | 0:19a03658bf70 | 39 | pc.printf( "Simple Transmitter (0 - 9 Counter) \r\n"); |
ganeshgore | 0:19a03658bf70 | 40 | |
aji052 | 2:36a1ad8ff689 | 41 | TxDataCnt = 4; |
ganeshgore | 0:19a03658bf70 | 42 | my_nrf24l01p.setTransferSize(TxDataCnt); |
ganeshgore | 0:19a03658bf70 | 43 | |
ganeshgore | 0:19a03658bf70 | 44 | my_nrf24l01p.enable(); |
ganeshgore | 0:19a03658bf70 | 45 | |
aji052 | 1:2c6781177599 | 46 | |
aji052 | 1:2c6781177599 | 47 | |
ganeshgore | 0:19a03658bf70 | 48 | while (1) { |
aji052 | 1:2c6781177599 | 49 | count[2]=tsi.readPercentage()*100; |
aji052 | 1:2c6781177599 | 50 | count[0]=(acc.getAccX()+1)*100; |
aji052 | 1:2c6781177599 | 51 | count[1]=(acc.getAccY()+1)*100; |
ganeshgore | 0:19a03658bf70 | 52 | // Send the Transmit buffer via the nRF24L01+ |
ganeshgore | 0:19a03658bf70 | 53 | temp = my_nrf24l01p.write( NRF24L01P_PIPE_P0,count, TxDataCnt ); |
ganeshgore | 0:19a03658bf70 | 54 | |
aji052 | 2:36a1ad8ff689 | 55 | pc.printf( "Sending %d - %d %d %d\r\n",temp,count[0],count[1],count[2],count[3]); |
ganeshgore | 0:19a03658bf70 | 56 | |
ganeshgore | 0:19a03658bf70 | 57 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
aji052 | 1:2c6781177599 | 58 | |
ganeshgore | 0:19a03658bf70 | 59 | |
aji052 | 2:36a1ad8ff689 | 60 | if ( count[0] >=280 || count[1] >= 280 ) |
aji052 | 2:36a1ad8ff689 | 61 | {count[3]++; |
aji052 | 2:36a1ad8ff689 | 62 | break;} |
ganeshgore | 0:19a03658bf70 | 63 | |
aji052 | 1:2c6781177599 | 64 | |
ganeshgore | 0:19a03658bf70 | 65 | } |
aji052 | 2:36a1ad8ff689 | 66 | |
aji052 | 2:36a1ad8ff689 | 67 | |
aji052 | 2:36a1ad8ff689 | 68 | |
aji052 | 2:36a1ad8ff689 | 69 | } |
aji052 | 2:36a1ad8ff689 | 70 | |
aji052 | 2:36a1ad8ff689 | 71 | |
aji052 | 2:36a1ad8ff689 | 72 | if( count[3]==1) |
aji052 | 2:36a1ad8ff689 | 73 | { |
aji052 | 2:36a1ad8ff689 | 74 | // Display the (default) setup of the nRF24L01+ chip |
aji052 | 2:36a1ad8ff689 | 75 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
aji052 | 2:36a1ad8ff689 | 76 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
aji052 | 2:36a1ad8ff689 | 77 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
aji052 | 2:36a1ad8ff689 | 78 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
aji052 | 2:36a1ad8ff689 | 79 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
aji052 | 2:36a1ad8ff689 | 80 | |
aji052 | 2:36a1ad8ff689 | 81 | pc.printf( "Simple 2 Byte Receiver\r\n" ); |
aji052 | 2:36a1ad8ff689 | 82 | |
aji052 | 2:36a1ad8ff689 | 83 | RxDataCnt = 3; |
aji052 | 2:36a1ad8ff689 | 84 | my_nrf24l01p.setTransferSize( RxDataCnt ); |
aji052 | 2:36a1ad8ff689 | 85 | |
aji052 | 2:36a1ad8ff689 | 86 | my_nrf24l01p.setReceiveMode(); |
aji052 | 2:36a1ad8ff689 | 87 | my_nrf24l01p.enable(); |
aji052 | 2:36a1ad8ff689 | 88 | |
aji052 | 2:36a1ad8ff689 | 89 | while (1) { |
aji052 | 2:36a1ad8ff689 | 90 | |
aji052 | 2:36a1ad8ff689 | 91 | // If we've received anything in the nRF24L01+... |
aji052 | 2:36a1ad8ff689 | 92 | if ( my_nrf24l01p.readable() ) { |
aji052 | 2:36a1ad8ff689 | 93 | |
aji052 | 2:36a1ad8ff689 | 94 | // ...read the data into the receive buffer |
aji052 | 2:36a1ad8ff689 | 95 | temp = my_nrf24l01p.read( NRF24L01P_PIPE_P0, count, RxDataCnt ); |
aji052 | 2:36a1ad8ff689 | 96 | |
aji052 | 2:36a1ad8ff689 | 97 | |
aji052 | 2:36a1ad8ff689 | 98 | pc.printf( "%d %d %d\r\n",count[0],count[1],count[2]); |
aji052 | 2:36a1ad8ff689 | 99 | |
aji052 | 2:36a1ad8ff689 | 100 | int xaxis = int(count[0]); |
aji052 | 2:36a1ad8ff689 | 101 | int yaxis = int(count[1]); |
aji052 | 2:36a1ad8ff689 | 102 | int value = int(count[2]); |
aji052 | 2:36a1ad8ff689 | 103 | |
aji052 | 2:36a1ad8ff689 | 104 | |
aji052 | 2:36a1ad8ff689 | 105 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
aji052 | 2:36a1ad8ff689 | 106 | if( xaxis >=0 && xaxis <=50 ) |
aji052 | 2:36a1ad8ff689 | 107 | mouse.move(0,-3); |
aji052 | 2:36a1ad8ff689 | 108 | |
aji052 | 2:36a1ad8ff689 | 109 | if( xaxis >=150 && xaxis <=200 ) |
aji052 | 2:36a1ad8ff689 | 110 | mouse.move(0,3); |
aji052 | 2:36a1ad8ff689 | 111 | |
aji052 | 2:36a1ad8ff689 | 112 | if( xaxis >50 && xaxis <=80 ) |
aji052 | 2:36a1ad8ff689 | 113 | mouse.move(0,-1); |
aji052 | 2:36a1ad8ff689 | 114 | |
aji052 | 2:36a1ad8ff689 | 115 | if( xaxis >=120 && xaxis <150 ) |
aji052 | 2:36a1ad8ff689 | 116 | mouse.move(0,1); |
aji052 | 2:36a1ad8ff689 | 117 | |
aji052 | 2:36a1ad8ff689 | 118 | |
aji052 | 2:36a1ad8ff689 | 119 | |
aji052 | 2:36a1ad8ff689 | 120 | if( yaxis >50 && yaxis <=80 ) |
aji052 | 2:36a1ad8ff689 | 121 | mouse.move(1,0); |
aji052 | 2:36a1ad8ff689 | 122 | |
aji052 | 2:36a1ad8ff689 | 123 | if( yaxis >=120 && yaxis <150 ) |
aji052 | 2:36a1ad8ff689 | 124 | mouse.move(-1,0); |
aji052 | 2:36a1ad8ff689 | 125 | |
aji052 | 2:36a1ad8ff689 | 126 | if( yaxis >=0 && yaxis <=50 ) |
aji052 | 2:36a1ad8ff689 | 127 | mouse.move(3,0); |
aji052 | 2:36a1ad8ff689 | 128 | |
aji052 | 2:36a1ad8ff689 | 129 | if( yaxis >=150 && yaxis <=200 ) |
aji052 | 2:36a1ad8ff689 | 130 | mouse.move(-3,0); |
aji052 | 2:36a1ad8ff689 | 131 | |
aji052 | 2:36a1ad8ff689 | 132 | if( xaxis >=0 && xaxis <=80 && yaxis >=0 && yaxis <=80 ) |
aji052 | 2:36a1ad8ff689 | 133 | mouse.move(2,-2); |
aji052 | 2:36a1ad8ff689 | 134 | |
aji052 | 2:36a1ad8ff689 | 135 | if( xaxis >=120 && xaxis <=200 && yaxis >=0 && yaxis <=80 ) |
aji052 | 2:36a1ad8ff689 | 136 | mouse.move(2,2); |
aji052 | 2:36a1ad8ff689 | 137 | |
aji052 | 2:36a1ad8ff689 | 138 | if( xaxis >=0 && xaxis <=80 && yaxis >=120 && yaxis <=200 ) |
aji052 | 2:36a1ad8ff689 | 139 | mouse.move(-2,-2); |
aji052 | 2:36a1ad8ff689 | 140 | |
aji052 | 2:36a1ad8ff689 | 141 | if( xaxis >=120 && xaxis <=200 && yaxis >=120 && yaxis <=200) |
aji052 | 2:36a1ad8ff689 | 142 | mouse.move(-2,2); |
aji052 | 2:36a1ad8ff689 | 143 | |
aji052 | 2:36a1ad8ff689 | 144 | if(value>50) |
aji052 | 2:36a1ad8ff689 | 145 | {mouse.press(MOUSE_LEFT); |
aji052 | 2:36a1ad8ff689 | 146 | wait(0.1); |
aji052 | 2:36a1ad8ff689 | 147 | mouse.release(MOUSE_LEFT); |
aji052 | 2:36a1ad8ff689 | 148 | } |
aji052 | 2:36a1ad8ff689 | 149 | |
aji052 | 2:36a1ad8ff689 | 150 | if(value<50 && value>0) |
aji052 | 2:36a1ad8ff689 | 151 | {mouse.press(MOUSE_RIGHT); |
aji052 | 2:36a1ad8ff689 | 152 | wait(0.1); |
aji052 | 2:36a1ad8ff689 | 153 | mouse.release(MOUSE_RIGHT); |
aji052 | 2:36a1ad8ff689 | 154 | } |
aji052 | 2:36a1ad8ff689 | 155 | else |
aji052 | 2:36a1ad8ff689 | 156 | mouse.move(0,0); |
aji052 | 2:36a1ad8ff689 | 157 | |
aji052 | 2:36a1ad8ff689 | 158 | if ( xaxis >=280 || yaxis >= 280 ) |
aji052 | 2:36a1ad8ff689 | 159 | {count[3]=0; |
aji052 | 2:36a1ad8ff689 | 160 | break;} |
aji052 | 2:36a1ad8ff689 | 161 | } |
aji052 | 2:36a1ad8ff689 | 162 | } |
aji052 | 2:36a1ad8ff689 | 163 | } |
aji052 | 2:36a1ad8ff689 | 164 | |
ganeshgore | 0:19a03658bf70 | 165 | } |