tim003 tim003
/
G6-tim003-Projekat-Wireless
Hasković Nedžad Džafić Faris
main.cpp@0:b285e41a1176, 2014-06-05 (annotated)
- Committer:
- tim003
- Date:
- Thu Jun 05 16:04:37 2014 +0000
- Revision:
- 0:b285e41a1176
G6-tim003-chat;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tim003 | 0:b285e41a1176 | 1 | #include "mbed.h" |
tim003 | 0:b285e41a1176 | 2 | #include "nRF24L01P.h" |
tim003 | 0:b285e41a1176 | 3 | #include <string> |
tim003 | 0:b285e41a1176 | 4 | #define TRANSFER_SIZE 4 |
tim003 | 0:b285e41a1176 | 5 | |
tim003 | 0:b285e41a1176 | 6 | Serial pc(USBTX, USBRX); // tx, rx |
tim003 | 0:b285e41a1176 | 7 | |
tim003 | 0:b285e41a1176 | 8 | nRF24L01P my_nrf24l01p(PTD2, PTD3, PTD1, PTD0, PTD5, PTA13); // mosi, miso, sck, csn, ce, irq |
tim003 | 0:b285e41a1176 | 9 | |
tim003 | 0:b285e41a1176 | 10 | DigitalOut myled1(LED1); |
tim003 | 0:b285e41a1176 | 11 | DigitalOut myled2(LED2); |
tim003 | 0:b285e41a1176 | 12 | |
tim003 | 0:b285e41a1176 | 13 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
tim003 | 0:b285e41a1176 | 14 | int txDataCnt = 0; |
tim003 | 0:b285e41a1176 | 15 | int rxDataCnt = 0; |
tim003 | 0:b285e41a1176 | 16 | |
tim003 | 0:b285e41a1176 | 17 | string zaPoslat; |
tim003 | 0:b285e41a1176 | 18 | int brZaPoslat(0); |
tim003 | 0:b285e41a1176 | 19 | |
tim003 | 0:b285e41a1176 | 20 | void primiPoruku() { |
tim003 | 0:b285e41a1176 | 21 | |
tim003 | 0:b285e41a1176 | 22 | } |
tim003 | 0:b285e41a1176 | 23 | |
tim003 | 0:b285e41a1176 | 24 | |
tim003 | 0:b285e41a1176 | 25 | void saljiPoruku() { |
tim003 | 0:b285e41a1176 | 26 | for(int i = 0; i<zaPoslat.size(); i+=4) if(i<zaPoslat.size()-4) |
tim003 | 0:b285e41a1176 | 27 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, (char*)zaPoslat.substr(i,4).c_str(), zaPoslat.substr(i,4).size()); |
tim003 | 0:b285e41a1176 | 28 | zaPoslat.clear(); |
tim003 | 0:b285e41a1176 | 29 | } |
tim003 | 0:b285e41a1176 | 30 | |
tim003 | 0:b285e41a1176 | 31 | int main() { |
tim003 | 0:b285e41a1176 | 32 | my_nrf24l01p.powerUp(); |
tim003 | 0:b285e41a1176 | 33 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
tim003 | 0:b285e41a1176 | 34 | my_nrf24l01p.setReceiveMode(); |
tim003 | 0:b285e41a1176 | 35 | my_nrf24l01p.enable(); |
tim003 | 0:b285e41a1176 | 36 | while (1) { |
tim003 | 0:b285e41a1176 | 37 | if ( pc.readable() ) { |
tim003 | 0:b285e41a1176 | 38 | |
tim003 | 0:b285e41a1176 | 39 | char znak = pc.getc(); |
tim003 | 0:b285e41a1176 | 40 | if(znak == 'x') saljiPoruku(); |
tim003 | 0:b285e41a1176 | 41 | else zaPoslat += znak; |
tim003 | 0:b285e41a1176 | 42 | myled1 = !myled1; |
tim003 | 0:b285e41a1176 | 43 | } |
tim003 | 0:b285e41a1176 | 44 | |
tim003 | 0:b285e41a1176 | 45 | // If we've received anything in the nRF24L01+... |
tim003 | 0:b285e41a1176 | 46 | if ( my_nrf24l01p.readable() ) { |
tim003 | 0:b285e41a1176 | 47 | |
tim003 | 0:b285e41a1176 | 48 | // ...read the data into the receive buffer |
tim003 | 0:b285e41a1176 | 49 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
tim003 | 0:b285e41a1176 | 50 | |
tim003 | 0:b285e41a1176 | 51 | // Display the receive buffer contents via the host serial link |
tim003 | 0:b285e41a1176 | 52 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
tim003 | 0:b285e41a1176 | 53 | |
tim003 | 0:b285e41a1176 | 54 | pc.putc( rxData[i] ); |
tim003 | 0:b285e41a1176 | 55 | } |
tim003 | 0:b285e41a1176 | 56 | |
tim003 | 0:b285e41a1176 | 57 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
tim003 | 0:b285e41a1176 | 58 | myled2 = !myled2; |
tim003 | 0:b285e41a1176 | 59 | } |
tim003 | 0:b285e41a1176 | 60 | } |
tim003 | 0:b285e41a1176 | 61 | |
tim003 | 0:b285e41a1176 | 62 | } |