ECE 4180 - Final Project Team / Mbed 2 deprecated WalkieTalkie

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

Revision:
41:9ed924a1f2e0
Parent:
34:d73e95bbdbed
Parent:
40:4f7d95c68d29
Child:
44:a4f81588fcd5
--- a/main.cpp	Fri Apr 27 22:01:10 2018 +0000
+++ b/main.cpp	Sun Apr 29 15:34:20 2018 +0000
@@ -15,10 +15,10 @@
 // How maybe bytes to send over RF
 #define DATA_PACKET_SIZE   32
 // How quickly to sample the mic / play the speaker
-#define SAMPLE_PERIOD   1/16000.0
+#define SAMPLE_PERIOD   1/8000.0
 
 
-//Serial pc(USBTX, USBRX);
+Serial pc(USBTX, USBRX);
 DigitalOut myled1(LED1); // Mic data sent over RF
 DigitalOut myled2(LED2); // Speaker data recieved over RF
 DigitalOut myled3(LED3); // Sampled mic / played the speaker
@@ -35,11 +35,12 @@
 Ticker sampler; //10:41 am 4/20
 
 InterruptIn button(p12); //changed DitialIn to InterruptIn at 5:54 4/18/18
-BusIn channel(p21, p22, p23, p24); // TODO: CHANGE THESE TO THE ACTUAL PINS NEEDED
+BusIn channel(p21, p22, p23, p24, p25);
 
 int rfFreq;
 int dataRate;
 unsigned long long rxAddr, txAddr;
+int pipe = 0;
 
 enum operatingMode {
     RECEIVE = 0,
@@ -53,15 +54,71 @@
     return a * (int)(a <= b) + b * (int)(b < a);
 }
 
-// Sets the frequency of the RF device based on which swtiches are flipped
+// Sets the channel of the RF device based on which swtiches are flipped
+// by changing RX/TX pipes
 // TODO: Make sure we don't have to restart the device or anything to change this
-void setRFFrequency() {
-    int channelNum = 0;//channel.read();
+void setChannel() {
+    int oldPipe = pipe;
+    int width = 5;
+    switch (channel) {
+        case 0:      // Channel 0
+            rxAddr = txAddr = 0xC2C2C2C2C0;
+            pipe = NRF24L01P_PIPE_P0;
+            if (pipe != oldPipe) {
+                my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
+                my_nrf24l01p.setTxAddress(txAddr, width);
+            }
+            break;
+        case 1:      // Channel 1
+            rxAddr = txAddr = 0xC2C2C2C2C1;
+            pipe = NRF24L01P_PIPE_P1;
+            if (pipe != oldPipe) {
+                my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
+                my_nrf24l01p.setTxAddress(txAddr, width);
+            }
+            break;
+        case 2:      // Channel 2
+            rxAddr = txAddr = 0xC2C2C2C2C2;
+            pipe = NRF24L01P_PIPE_P2;
+            if (pipe != oldPipe) {
+                my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
+                my_nrf24l01p.setTxAddress(txAddr, width);
+            }
+            break;
+        case 4:      // Channel 3
+            rxAddr = txAddr = 0xC2C2C2C2C3;
+            pipe = NRF24L01P_PIPE_P3;
+            if (pipe != oldPipe) {
+                my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
+                my_nrf24l01p.setTxAddress(txAddr, width);
+            }
+            break;
+        case 8:      // Channel 4
+            rxAddr = txAddr = 0xC2C2C2C2C4;
+            pipe = NRF24L01P_PIPE_P4;
+            if (pipe != oldPipe) {
+                my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
+                my_nrf24l01p.setTxAddress(txAddr, width);
+            }
+            break;
+        case 16:     // Channel 5
+            rxAddr = txAddr = 0xC2C2C2C2C5;
+            pipe = NRF24L01P_PIPE_P5;
+            if (pipe != oldPipe) {
+                my_nrf24l01p.setRxAddress(rxAddr, width, pipe);
+                my_nrf24l01p.setTxAddress(txAddr, width);
+            }
+            break;
+        default:
+            break;
+    }
     
+    //pc.printf("Pipe = %d\r\n", pipe); 
+            
     // TODO: Don't force it to the default RF frequency
-    channelNum = 2;
+    //channelNum = 2;
     
-    my_nrf24l01p.setRfFrequency(channelNum + NRF24L01P_MIN_RF_FREQUENCY);
+    //my_nrf24l01p.setRfFrequency(channelNum + NRF24L01P_MIN_RF_FREQUENCY);
 }
 
 // Callback interrupt from the button to shift into transmit mode
@@ -121,6 +178,7 @@
                 // Only place into the buffer the number of bytes received
                 rxbuff.push(spkrPacket, min(DATA_PACKET_SIZE, numReceived));
                 
+                //pc.printf("Receiviing....\n\r");
                 myled2 = !myled2;
             }
         } else { // mode == TRANSMIT
@@ -151,18 +209,18 @@
 {
     while (1) {
         uLCD.locate(0, 0);
-        uLCD.printf("Frequency: %d MHz", rfFreq);
+        uLCD.printf("Freq: %d MHz", rfFreq);
         uLCD.locate(0, 2);
-        uLCD.printf("Data Rate: %d kbps", dataRate);
+        uLCD.printf("Rate: %d kbps", dataRate);
         uLCD.locate(0, 4);
-        uLCD.printf("TX Address: 0x%010llX", txAddr);
+        uLCD.printf("TX: 0x%010llX", txAddr);
         uLCD.locate(0, 6);
-        uLCD.printf("RX Address: 0x%010llX", rxAddr);
+        uLCD.printf("RX: 0x%010llX", rxAddr);
         uLCD.locate(0, 8);
         
         switch (mode) {
             case RECEIVE:
-                uLCD.printf("Mode: Receiving");
+                uLCD.printf("Mode:    Receiving");
                 break;
             case TRANSMIT:
                 uLCD.printf("Mode: Transmitting");
@@ -180,7 +238,7 @@
     Thread comm;    
     
     // Set up the nrf24l01p
-    my_nrf24l01p.setAirDataRate(2000);
+    my_nrf24l01p.setAirDataRate(2000);  // 2Mbs
     rfFreq = my_nrf24l01p.getRfFrequency();
     dataRate = my_nrf24l01p.getAirDataRate();
     rxAddr = my_nrf24l01p.getRxAddress();
@@ -191,6 +249,14 @@
     my_nrf24l01p.setReceiveMode();
     my_nrf24l01p.enable();
     
+    pc.printf( "nRF24L01+ Frequency    : %d MHz\r\n",  rfFreq );
+    pc.printf( "nRF24L01+ Output power : %d dBm\r\n",  my_nrf24l01p.getRfOutputPower() );
+    pc.printf( "nRF24L01+ Data Rate    : %d kbps\r\n", my_nrf24l01p.getAirDataRate() );
+    pc.printf( "nRF24L01+ TX Address   : 0x%010llX\r\n", txAddr );
+    pc.printf( "nRF24L01+ RX Address   : 0x%010llX\r\n", rxAddr );
+    
+    pc.printf("Finished starting up....\n\r");
+    
     mode = RECEIVE;
     
     // Initialize the uLCD
@@ -213,6 +279,7 @@
     // Heartbeat thread
     while (1) {
         myled4 = !myled4;
+        setChannel(); // Poll the DIP switch and change channels accordingly
         Thread::wait(100);
     }
 }
\ No newline at end of file