Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P
main.cpp@9:268d096562f1, 2018-04-18 (annotated)
- Committer:
- drechtmann3
- Date:
- Wed Apr 18 15:23:23 2018 +0000
- Revision:
- 9:268d096562f1
- Parent:
- 2:dc046ff72566
feeling it up around
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Nurchu | 0:c35b54fb9c3c | 1 | #include "mbed.h" |
Nurchu | 2:dc046ff72566 | 2 | #include "Microphone.h" |
drechtmann3 | 1:fc0a2c17e086 | 3 | #include "nRF24L01P.h" |
drechtmann3 | 1:fc0a2c17e086 | 4 | #include "rtos.h" |
Nurchu | 2:dc046ff72566 | 5 | |
Nurchu | 2:dc046ff72566 | 6 | |
drechtmann3 | 1:fc0a2c17e086 | 7 | Serial pc(USBTX, USBRX); // tx, rx |
drechtmann3 | 1:fc0a2c17e086 | 8 | |
drechtmann3 | 1:fc0a2c17e086 | 9 | nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq |
drechtmann3 | 1:fc0a2c17e086 | 10 | DigitalOut myled1(LED1); |
drechtmann3 | 1:fc0a2c17e086 | 11 | DigitalOut myled2(LED2); |
drechtmann3 | 1:fc0a2c17e086 | 12 | DigitalOut myled3(LED3); |
Nurchu | 2:dc046ff72566 | 13 | |
Nurchu | 0:c35b54fb9c3c | 14 | |
Nurchu | 2:dc046ff72566 | 15 | Microphone mymicrophone(p16); |
drechtmann3 | 9:268d096562f1 | 16 | Mutex mux; |
drechtmann3 | 9:268d096562f1 | 17 | void nrfpcstartup() { //should be called once |
drechtmann3 | 1:fc0a2c17e086 | 18 | |
drechtmann3 | 1:fc0a2c17e086 | 19 | // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's |
drechtmann3 | 1:fc0a2c17e086 | 20 | // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019) |
drechtmann3 | 1:fc0a2c17e086 | 21 | // only handles 4 byte transfers in the ATMega code. |
drechtmann3 | 1:fc0a2c17e086 | 22 | #define TRANSFER_SIZE 4 |
drechtmann3 | 1:fc0a2c17e086 | 23 | |
drechtmann3 | 1:fc0a2c17e086 | 24 | char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; |
drechtmann3 | 1:fc0a2c17e086 | 25 | int txDataCnt = 0; |
drechtmann3 | 1:fc0a2c17e086 | 26 | int rxDataCnt = 0; |
drechtmann3 | 1:fc0a2c17e086 | 27 | |
drechtmann3 | 1:fc0a2c17e086 | 28 | my_nrf24l01p.powerUp(); |
drechtmann3 | 1:fc0a2c17e086 | 29 | |
drechtmann3 | 1:fc0a2c17e086 | 30 | // Display the (default) setup of the nRF24L01+ chip |
drechtmann3 | 1:fc0a2c17e086 | 31 | pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() ); |
drechtmann3 | 1:fc0a2c17e086 | 32 | pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() ); |
drechtmann3 | 1:fc0a2c17e086 | 33 | pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() ); |
drechtmann3 | 1:fc0a2c17e086 | 34 | pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() ); |
drechtmann3 | 1:fc0a2c17e086 | 35 | pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() ); |
drechtmann3 | 1:fc0a2c17e086 | 36 | |
drechtmann3 | 1:fc0a2c17e086 | 37 | pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE ); |
drechtmann3 | 1:fc0a2c17e086 | 38 | |
drechtmann3 | 1:fc0a2c17e086 | 39 | my_nrf24l01p.setTransferSize( TRANSFER_SIZE ); |
drechtmann3 | 1:fc0a2c17e086 | 40 | |
drechtmann3 | 1:fc0a2c17e086 | 41 | my_nrf24l01p.setReceiveMode(); |
drechtmann3 | 1:fc0a2c17e086 | 42 | my_nrf24l01p.enable(); |
drechtmann3 | 9:268d096562f1 | 43 | while (1) { |
drechtmann3 | 9:268d096562f1 | 44 | |
drechtmann3 | 9:268d096562f1 | 45 | } |
drechtmann3 | 9:268d096562f1 | 46 | } |
drechtmann3 | 1:fc0a2c17e086 | 47 | |
drechtmann3 | 9:268d096562f1 | 48 | |
drechtmann3 | 9:268d096562f1 | 49 | |
drechtmann3 | 9:268d096562f1 | 50 | void michello(void const *args) { |
drechtmann3 | 9:268d096562f1 | 51 | mux.lock(); |
drechtmann3 | 1:fc0a2c17e086 | 52 | //read in, subtract 0.67 DC bias, take absolute value, and scale up .1Vpp to 15 for builtin LED display |
drechtmann3 | 1:fc0a2c17e086 | 53 | myleds = int(abs((mymicrophone - (0.67/3.3)))*500.0); |
drechtmann3 | 1:fc0a2c17e086 | 54 | //Use an 8kHz audio sample rate (phone quality audio); |
drechtmann3 | 1:fc0a2c17e086 | 55 | wait(1.0/8000.0); |
drechtmann3 | 9:268d096562f1 | 56 | mux.unlock(); |
drechtmann3 | 1:fc0a2c17e086 | 57 | } |
drechtmann3 | 1:fc0a2c17e086 | 58 | |
drechtmann3 | 9:268d096562f1 | 59 | void nrfctransmit() { |
drechtmann3 | 9:268d096562f1 | 60 | my_nrf24l01p.write(0, (uint8_t)mymicrophone, txDataCnt); |
Nurchu | 0:c35b54fb9c3c | 61 | } |
drechtmann3 | 1:fc0a2c17e086 | 62 | |
drechtmann3 | 9:268d096562f1 | 63 | void pchello(void const *args) { |
drechtmann3 | 1:fc0a2c17e086 | 64 | // If we've received anything over the host serial link... |
drechtmann3 | 1:fc0a2c17e086 | 65 | if ( pc.readable() ) { |
drechtmann3 | 1:fc0a2c17e086 | 66 | |
drechtmann3 | 1:fc0a2c17e086 | 67 | // ...add it to the transmit buffer |
drechtmann3 | 1:fc0a2c17e086 | 68 | txData[txDataCnt++] = pc.getc(); |
drechtmann3 | 1:fc0a2c17e086 | 69 | |
drechtmann3 | 1:fc0a2c17e086 | 70 | // If the transmit buffer is full |
drechtmann3 | 1:fc0a2c17e086 | 71 | if ( txDataCnt >= sizeof( txData ) ) { |
drechtmann3 | 1:fc0a2c17e086 | 72 | |
drechtmann3 | 1:fc0a2c17e086 | 73 | // Send the transmitbuffer via the nRF24L01+ |
drechtmann3 | 1:fc0a2c17e086 | 74 | my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt ); |
drechtmann3 | 1:fc0a2c17e086 | 75 | |
drechtmann3 | 1:fc0a2c17e086 | 76 | txDataCnt = 0; |
drechtmann3 | 1:fc0a2c17e086 | 77 | } |
drechtmann3 | 1:fc0a2c17e086 | 78 | |
drechtmann3 | 1:fc0a2c17e086 | 79 | // Toggle LED1 (to help debug Host -> nRF24L01+ communication) |
drechtmann3 | 1:fc0a2c17e086 | 80 | myled1 = !myled1; |
drechtmann3 | 1:fc0a2c17e086 | 81 | } |
drechtmann3 | 1:fc0a2c17e086 | 82 | |
drechtmann3 | 1:fc0a2c17e086 | 83 | // If we've received anything in the nRF24L01+... |
drechtmann3 | 1:fc0a2c17e086 | 84 | if ( my_nrf24l01p.readable() ) { |
drechtmann3 | 1:fc0a2c17e086 | 85 | |
drechtmann3 | 1:fc0a2c17e086 | 86 | // ...read the data into the receive buffer |
drechtmann3 | 1:fc0a2c17e086 | 87 | rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) ); |
drechtmann3 | 1:fc0a2c17e086 | 88 | |
drechtmann3 | 1:fc0a2c17e086 | 89 | // Display the receive buffer contents via the host serial link |
drechtmann3 | 1:fc0a2c17e086 | 90 | for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) { |
drechtmann3 | 1:fc0a2c17e086 | 91 | |
drechtmann3 | 1:fc0a2c17e086 | 92 | pc.putc( rxData[i] ); |
drechtmann3 | 1:fc0a2c17e086 | 93 | } |
drechtmann3 | 1:fc0a2c17e086 | 94 | |
drechtmann3 | 1:fc0a2c17e086 | 95 | // Toggle LED2 (to help debug nRF24L01+ -> Host communication) |
drechtmann3 | 1:fc0a2c17e086 | 96 | myled3 = !myled3; |
drechtmann3 | 1:fc0a2c17e086 | 97 | } |
drechtmann3 | 1:fc0a2c17e086 | 98 | } |
drechtmann3 | 1:fc0a2c17e086 | 99 | } |
drechtmann3 | 1:fc0a2c17e086 | 100 | |
drechtmann3 | 1:fc0a2c17e086 | 101 | Mutex mux; |
drechtmann3 | 9:268d096562f1 | 102 | //Thread pcthread; |
drechtmann3 | 9:268d096562f1 | 103 | //Thread micthread; |
drechtmann3 | 9:268d096562f1 | 104 | int main() { |
drechtmann3 | 9:268d096562f1 | 105 | Thread t1(pchello); |
drechtmann3 | 9:268d096562f1 | 106 | Thread t2(michello); |
drechtmann3 | 1:fc0a2c17e086 | 107 | while (1) { |
drechtmann3 | 1:fc0a2c17e086 | 108 | |
drechtmann3 | 1:fc0a2c17e086 | 109 | } |
drechtmann3 | 1:fc0a2c17e086 | 110 | } |