ECE 4180 - Final Project Team / Mbed 2 deprecated WalkieTalkie

Dependencies:   mbed 4DGL-uLCD-SE mbed-rtos nRF24L01P

Committer:
drechtmann3
Date:
Wed Apr 18 23:09:40 2018 +0000
Revision:
11:a9f4237eaaa1
Parent:
10:a8fcfc869fa5
Child:
12:efcfe4c0d9f2
Made several additions. They are all timestamped by comments in the code.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nurchu 0:c35b54fb9c3c 1 #include "mbed.h"
Nurchu 7:0ac1f1ca8aa6 2 #include "rtos.h"
Nurchu 2:dc046ff72566 3 #include "Microphone.h"
drechtmann3 1:fc0a2c17e086 4 #include "nRF24L01P.h"
drechtmann3 11:a9f4237eaaa1 5 #define TRANSFER_SIZE 4 //added 5:54pm 4/18/18
drechtmann3 1:fc0a2c17e086 6 Serial pc(USBTX, USBRX); // tx, rx
drechtmann3 1:fc0a2c17e086 7
drechtmann3 1:fc0a2c17e086 8 nRF24L01P my_nrf24l01p(p5, p6, p7, p8, p9, p10); // mosi, miso, sck, csn, ce, irq
drechtmann3 1:fc0a2c17e086 9 DigitalOut myled1(LED1);
drechtmann3 1:fc0a2c17e086 10 DigitalOut myled2(LED2);
drechtmann3 1:fc0a2c17e086 11 DigitalOut myled3(LED3);
Nurchu 2:dc046ff72566 12
Nurchu 2:dc046ff72566 13 Microphone mymicrophone(p16);
drechtmann3 11:a9f4237eaaa1 14 InterruptIn Button(p20); //changed DitialIn to InterruptIn at 5:54 4/18/18
drechtmann3 11:a9f4237eaaa1 15 AnalogOut speaker(p18); //added 6:34 pm at 4/18/18
drechtmann3 11:a9f4237eaaa1 16
drechtmann3 11:a9f4237eaaa1 17 char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; //making these usable by other voids.
drechtmann3 11:a9f4237eaaa1 18 int txDataCnt = 0;//and this.
drechtmann3 11:a9f4237eaaa1 19 int rxDataCnt = 0;//and this. //David Rechtmann 6.24 pm 4/18/18
drechtmann3 11:a9f4237eaaa1 20
drechtmann3 10:a8fcfc869fa5 21 void startup() {
drechtmann3 1:fc0a2c17e086 22
drechtmann3 1:fc0a2c17e086 23 // The nRF24L01+ supports transfers from 1 to 32 bytes, but Sparkfun's
drechtmann3 1:fc0a2c17e086 24 // "Nordic Serial Interface Board" (http://www.sparkfun.com/products/9019)
drechtmann3 1:fc0a2c17e086 25 // only handles 4 byte transfers in the ATMega code.
drechtmann3 11:a9f4237eaaa1 26 //define TRANSFER_SIZE 4
drechtmann3 1:fc0a2c17e086 27
drechtmann3 11:a9f4237eaaa1 28 // char txData[TRANSFER_SIZE], rxData[TRANSFER_SIZE]; //making these usable by other voids.
drechtmann3 11:a9f4237eaaa1 29 // int txDataCnt = 0;//and this.
drechtmann3 11:a9f4237eaaa1 30 // int rxDataCnt = 0;//and this. //David Rechtmann 6:24 pm 4/18/18
drechtmann3 1:fc0a2c17e086 31
drechtmann3 1:fc0a2c17e086 32 my_nrf24l01p.powerUp();
drechtmann3 1:fc0a2c17e086 33
drechtmann3 1:fc0a2c17e086 34 // Display the (default) setup of the nRF24L01+ chip
drechtmann3 1:fc0a2c17e086 35 pc.printf( "nRF24L01+ Frequency : %d MHz\r\n", my_nrf24l01p.getRfFrequency() );
drechtmann3 1:fc0a2c17e086 36 pc.printf( "nRF24L01+ Output power : %d dBm\r\n", my_nrf24l01p.getRfOutputPower() );
drechtmann3 1:fc0a2c17e086 37 pc.printf( "nRF24L01+ Data Rate : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
drechtmann3 1:fc0a2c17e086 38 pc.printf( "nRF24L01+ TX Address : 0x%010llX\r\n", my_nrf24l01p.getTxAddress() );
drechtmann3 1:fc0a2c17e086 39 pc.printf( "nRF24L01+ RX Address : 0x%010llX\r\n", my_nrf24l01p.getRxAddress() );
drechtmann3 1:fc0a2c17e086 40
drechtmann3 1:fc0a2c17e086 41 pc.printf( "Type keys to test transfers:\r\n (transfers are grouped into %d characters)\r\n", TRANSFER_SIZE );
drechtmann3 1:fc0a2c17e086 42
drechtmann3 1:fc0a2c17e086 43 my_nrf24l01p.setTransferSize( TRANSFER_SIZE );
drechtmann3 1:fc0a2c17e086 44
drechtmann3 1:fc0a2c17e086 45 my_nrf24l01p.setReceiveMode();
drechtmann3 1:fc0a2c17e086 46 my_nrf24l01p.enable();
drechtmann3 10:a8fcfc869fa5 47 }
drechtmann3 1:fc0a2c17e086 48
drechtmann3 1:fc0a2c17e086 49
drechtmann3 10:a8fcfc869fa5 50 void pctransmit() {
drechtmann3 1:fc0a2c17e086 51 // If we've received anything over the host serial link...
drechtmann3 1:fc0a2c17e086 52 if ( pc.readable() ) {
drechtmann3 1:fc0a2c17e086 53
drechtmann3 1:fc0a2c17e086 54 // ...add it to the transmit buffer
drechtmann3 1:fc0a2c17e086 55 txData[txDataCnt++] = pc.getc();
drechtmann3 1:fc0a2c17e086 56
drechtmann3 1:fc0a2c17e086 57 // If the transmit buffer is full
drechtmann3 1:fc0a2c17e086 58 if ( txDataCnt >= sizeof( txData ) ) {
drechtmann3 1:fc0a2c17e086 59
drechtmann3 1:fc0a2c17e086 60 // Send the transmitbuffer via the nRF24L01+
drechtmann3 1:fc0a2c17e086 61 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
drechtmann3 1:fc0a2c17e086 62
drechtmann3 1:fc0a2c17e086 63 txDataCnt = 0;
drechtmann3 1:fc0a2c17e086 64 }
drechtmann3 1:fc0a2c17e086 65
drechtmann3 1:fc0a2c17e086 66 // Toggle LED1 (to help debug Host -> nRF24L01+ communication)
drechtmann3 10:a8fcfc869fa5 67 myled3 = !myled3;
drechtmann3 1:fc0a2c17e086 68 }
drechtmann3 1:fc0a2c17e086 69
drechtmann3 1:fc0a2c17e086 70 // If we've received anything in the nRF24L01+...
drechtmann3 1:fc0a2c17e086 71 if ( my_nrf24l01p.readable() ) {
drechtmann3 1:fc0a2c17e086 72
drechtmann3 1:fc0a2c17e086 73 // ...read the data into the receive buffer
drechtmann3 1:fc0a2c17e086 74 rxDataCnt = my_nrf24l01p.read( NRF24L01P_PIPE_P0, rxData, sizeof( rxData ) );
drechtmann3 1:fc0a2c17e086 75
drechtmann3 1:fc0a2c17e086 76 // Display the receive buffer contents via the host serial link
drechtmann3 1:fc0a2c17e086 77 for ( int i = 0; rxDataCnt > 0; rxDataCnt--, i++ ) {
drechtmann3 1:fc0a2c17e086 78
drechtmann3 1:fc0a2c17e086 79 pc.putc( rxData[i] );
drechtmann3 1:fc0a2c17e086 80 }
drechtmann3 1:fc0a2c17e086 81
drechtmann3 1:fc0a2c17e086 82 // Toggle LED2 (to help debug nRF24L01+ -> Host communication)
drechtmann3 1:fc0a2c17e086 83 myled3 = !myled3;
drechtmann3 1:fc0a2c17e086 84 }
drechtmann3 1:fc0a2c17e086 85 }
drechtmann3 11:a9f4237eaaa1 86 //}
drechtmann3 11:a9f4237eaaa1 87
drechtmann3 11:a9f4237eaaa1 88 void receive() { //most of this function added by david at 6:02pm 4/18/18
drechtmann3 11:a9f4237eaaa1 89 float sample;
drechtmann3 11:a9f4237eaaa1 90 float average = 0.67/3.3;//initial DC bias level
drechtmann3 11:a9f4237eaaa1 91 while (Button == 0) {
drechtmann3 11:a9f4237eaaa1 92 if (my_nrf24l01p.readable(0)) {
drechtmann3 11:a9f4237eaaa1 93 my_nrf24l01p.read(0, rxData, 1 );//replace thrid val w/ 1?
drechtmann3 11:a9f4237eaaa1 94 sample = uint8_t (rxData);
drechtmann3 11:a9f4237eaaa1 95 average = 0.9999*average + 0.0001*sample; //This is in the mPA demo but I'm not sure how it works -david
drechtmann3 11:a9f4237eaaa1 96 speaker = 0.5 +((sample - average)*33.0);
drechtmann3 11:a9f4237eaaa1 97 }
drechtmann3 11:a9f4237eaaa1 98 if ( txDataCnt >= sizeof( txData ) ) { //i'm not sure what this does??? -david
drechtmann3 11:a9f4237eaaa1 99
drechtmann3 11:a9f4237eaaa1 100 // Send the transmitbuffer via the nRF24L01+
drechtmann3 11:a9f4237eaaa1 101 my_nrf24l01p.write( NRF24L01P_PIPE_P0, txData, txDataCnt );
drechtmann3 11:a9f4237eaaa1 102
drechtmann3 11:a9f4237eaaa1 103 txDataCnt = 0;
drechtmann3 11:a9f4237eaaa1 104 }
drechtmann3 11:a9f4237eaaa1 105
drechtmann3 11:a9f4237eaaa1 106 }
drechtmann3 11:a9f4237eaaa1 107 }
drechtmann3 11:a9f4237eaaa1 108
drechtmann3 10:a8fcfc869fa5 109 void transmit() {
drechtmann3 10:a8fcfc869fa5 110 // while (mymycrophone.getData()) {
drechtmann3 10:a8fcfc869fa5 111 // txData[txDataCnt++] = mymicrophone.getData();
drechtmann3 10:a8fcfc869fa5 112 // }
drechtmann3 10:a8fcfc869fa5 113 // txData[txDataCnt++] = ;
drechtmann3 11:a9f4237eaaa1 114 my_nrf24l01P.setTransmitMode(); // for latest revision 5:52pm 4/18/1, this doesn't compile and I don't know why.
drechtmann3 11:a9f4237eaaa1 115 speaker = 0;
drechtmann3 11:a9f4237eaaa1 116 while (Button == 1) {
drechtmann3 11:a9f4237eaaa1 117 my_nrf24l01P.write(0, mymicrophone.getData(), txDataCnt);
drechtmann3 10:a8fcfc869fa5 118 }
drechtmann3 10:a8fcfc869fa5 119 }
drechtmann3 11:a9f4237eaaa1 120 int main() {
drechtmann3 11:a9f4237eaaa1 121 Button.mode(PullUp);//added 6:23pm 4/18/18
drechtmann3 11:a9f4237eaaa1 122 Button.rise(&transmit);
drechtmann3 11:a9f4237eaaa1 123 Button.fall(&receive);
drechtmann3 1:fc0a2c17e086 124 while (1) {
drechtmann3 1:fc0a2c17e086 125
drechtmann3 1:fc0a2c17e086 126 }
drechtmann3 1:fc0a2c17e086 127 }