ECE 4180 - Final Project Team / Mbed 2 deprecated WalkieTalkie

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

Revision:
45:805f92c68d55
Parent:
44:a4f81588fcd5
--- a/main.cpp	Sun Apr 29 16:32:23 2018 +0000
+++ b/main.cpp	Sun Apr 29 20:18:09 2018 +0000
@@ -26,6 +26,7 @@
 
 Speaker spkr(p18);
 Microphone mic(p16);
+Mutex u;
 
 uLCD_4DGL uLCD(p28, p27, p29);                      // serial tx, serial rx, reset pin;
 
@@ -41,7 +42,7 @@
 int dataRate;
 unsigned long long rxAddr, txAddr;
 int pipe = 0;
-int channelNum = 0;
+int channelNum = -1;
 
 enum operatingMode {
     RECEIVE = 0,
@@ -51,14 +52,16 @@
 operatingMode mode;
 
 // Cheap nonbranching min function
-int min(int a, int b) {
+int min(int a, int b)
+{
     return a * (int)(a <= b) + b * (int)(b < a);
 }
 
 // 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 setChannel() {
+void setChannel()
+{
     int oldChannel = channelNum;
     //int oldPipe = pipe;
     //int width = 5;
@@ -72,6 +75,13 @@
                 //my_nrf24l01p.setTxAddress(txAddr, width);
                 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
                 my_nrf24l01p.setRfFrequency(rfFreq);
+                u.lock();
+                uLCD.locate(0, 0);
+                uLCD.printf("Freq: %d MHz", rfFreq);
+                uLCD.locate(0, 2);
+                uLCD.printf("Channel: %d ", channelNum);
+                uLCD.locate(0, 10);
+                u.unlock();
             }
             break;
         case 1:      // Channel 1
@@ -83,6 +93,12 @@
                 //my_nrf24l01p.setTxAddress(txAddr, width);
                 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
                 my_nrf24l01p.setRfFrequency(rfFreq);
+                uLCD.locate(0, 0);
+                uLCD.printf("Freq: %d MHz", rfFreq);
+                uLCD.locate(0, 2);
+                uLCD.printf("Channel: %d ", channelNum);
+                uLCD.locate(0, 10);
+                u.unlock();
             }
             break;
         case 2:      // Channel 2
@@ -94,6 +110,13 @@
                 //my_nrf24l01p.setTxAddress(txAddr, width);
                 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
                 my_nrf24l01p.setRfFrequency(rfFreq);
+                u.lock();
+                uLCD.locate(0, 0);
+                uLCD.printf("Freq: %d MHz", rfFreq);
+                uLCD.locate(0, 2);
+                uLCD.printf("Channel: %d ", channelNum);
+                uLCD.locate(0, 10);
+                u.unlock();
             }
             break;
         case 4:      // Channel 3
@@ -105,6 +128,13 @@
                 //my_nrf24l01p.setTxAddress(txAddr, width);
                 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
                 my_nrf24l01p.setRfFrequency(rfFreq);
+                u.lock();
+                uLCD.locate(0, 0);
+                uLCD.printf("Freq: %d MHz", rfFreq);
+                uLCD.locate(0, 2);
+                uLCD.printf("Channel: %d ", channelNum);
+                uLCD.locate(0, 10);
+                u.unlock();
             }
             break;
         case 8:      // Channel 4
@@ -116,6 +146,13 @@
                 //my_nrf24l01p.setTxAddress(txAddr, width);
                 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
                 my_nrf24l01p.setRfFrequency(rfFreq);
+                u.lock();
+                uLCD.locate(0, 0);
+                uLCD.printf("Freq: %d MHz", rfFreq);
+                uLCD.locate(0, 2);
+                uLCD.printf("Channel: %d ", channelNum);
+                uLCD.locate(0, 10);
+                u.unlock();
             }
             break;
         case 16:     // Channel 5
@@ -127,30 +164,47 @@
                 //my_nrf24l01p.setTxAddress(txAddr, width);
                 rfFreq = channelNum + NRF24L01P_MIN_RF_FREQUENCY;
                 my_nrf24l01p.setRfFrequency(rfFreq);
+                u.lock();
+                uLCD.locate(0, 0);
+                uLCD.printf("Freq: %d MHz", rfFreq);
+                uLCD.locate(0, 2);
+                uLCD.printf("Channel: %d ", channelNum);
+                uLCD.locate(0, 10);
+                u.unlock();
             }
             break;
         default:
             break;
     }
-    
-    //pc.printf("Pipe = %d\r\n", pipe); 
-            
+
+    //pc.printf("Pipe = %d\r\n", pipe);
+
     // TODO: Don't force it to the default RF frequency
     //channelNum = 2;
-    
+
     //my_nrf24l01p.setRfFrequency(channelNum + NRF24L01P_MIN_RF_FREQUENCY);
 }
 
 // Callback interrupt from the button to shift into transmit mode
-void enterTransmitMode() {
+void enterTransmitMode()
+{
     mode = TRANSMIT;
     txbuff.clear();
+//   u.lock();
+//    uLCD.locate(0, 10);
+//   uLCD.printf("Mode:    Transmitting");
+//     u.unlock();
 }
 
 // Callback interrupt from the button to shift into receive mode
-void enterRecieveMode() {
+void enterRecieveMode()
+{
     mode = RECEIVE;
     rxbuff.clear();
+    //    u.lock();
+    //  uLCD.locate(0, 10);
+//   uLCD.printf("Mode:    Receiving");
+    // u.unlock();
 }
 
 // Called every SAMPLE_PERIOD ms to sample the mic or output data into the speaker
@@ -162,7 +216,7 @@
         // If there is no data in the buffer, it will just output 0 to the write function
         uint8_t speakerSample = 0;
         rxbuff.pop(&speakerSample, 1);
-        
+
         // Output into the actual speaker
         spkr.write(speakerSample);
     } else {
@@ -173,7 +227,7 @@
         // Make sure the speaker is actually off
         spkr.turnOff();
     }
-    
+
     // TODO: This will have to be removed later on once we actually crank up the sample rate
     myled3 = !myled3;
 }
@@ -190,25 +244,25 @@
             // Make sure something is there to read
             if (my_nrf24l01p.readable( NRF24L01P_PIPE_P0 )) {
                 uint8_t spkrPacket[DATA_PACKET_SIZE];
-            
+
                 // Remove entire packet of data from the bus
                 int numReceived =  my_nrf24l01p.read( NRF24L01P_PIPE_P0, (char*) spkrPacket, DATA_PACKET_SIZE );
-                
+
                 // Place into buffer to play speaker in another thread
                 // 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
             if (txbuff.size() >= DATA_PACKET_SIZE) {
                 uint8_t micPacket[DATA_PACKET_SIZE];
-                
+
                 // Pull an entire packet of data from the mic sample buffer
                 int numPopped = txbuff.pop(micPacket, DATA_PACKET_SIZE);
-             //   rxbuff.push(micPacket, DATA_PACKET_SIZE);
-                
+                //   rxbuff.push(micPacket, DATA_PACKET_SIZE);
+
                 // Send the entire buffer to the other device
                 // TODO: We just assume that DATA_PACKET_SIZE bytes were popped, this may
                 //       not be the case
@@ -217,7 +271,7 @@
                 myled1 = !myled1;
             }
         }
-        
+
         //pc.printf("TX Size %d RX Size%d Mode %d\n\r", txbuff.size(), rxbuff.size(), mode);
         Thread::yield();
         //Thread::wait(10);
@@ -238,7 +292,7 @@
         //uLCD.printf("RX: 0x%010llX", rxAddr);
         uLCD.printf("Channel: %d", channelNum);
         uLCD.locate(0, 10);
-        
+
         switch (mode) {
             case RECEIVE:
                 uLCD.printf("Mode:    Receiving");
@@ -248,7 +302,7 @@
                 uLCD.printf("Mode: Transmitting");
                 break;
         }
-        
+
         // Maybe add some graphics too idk
         Thread::wait(1000);
     }
@@ -257,51 +311,74 @@
 int main()
 {
     //Thread lcd;
-    Thread comm;    
-    
+    Thread comm;
+
     // Set up the nrf24l01p
     my_nrf24l01p.setAirDataRate(2000);  // 2Mbs
     rfFreq = my_nrf24l01p.getRfFrequency();
     dataRate = my_nrf24l01p.getAirDataRate();
     rxAddr = my_nrf24l01p.getRxAddress();
     txAddr = my_nrf24l01p.getTxAddress();
-    
+
     my_nrf24l01p.setTransferSize(DATA_PACKET_SIZE);
 
     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
     uLCD.baudrate(3000000);
     uLCD.background_color(BLACK);
-    
+
     // Spawn threads
     //lcd.start(lcdThread);
     comm.start(commThread);
-    
+
     // Setup the button to enter transmit mode when pushed down
     // and recieve when release
     button.mode(PullUp);
     button.fall(&enterTransmitMode);
     button.rise(&enterRecieveMode);
-    
+
     // Setup sampler to sample at a specific frequency
     sampler.attach(&sampleData, SAMPLE_PERIOD);
+
+    // Heartbeat thread
+    u.lock();
+    uLCD.locate(0, 0);
+    uLCD.printf("Freq: %d MHz", rfFreq);
+    uLCD.locate(0, 2);
+    uLCD.printf("Channel: %d", channelNum);
+    uLCD.locate(0, 4);
+    uLCD.printf("Addr: 0x%010llX", txAddr);
+    uLCD.locate(0, 6);
+    uLCD.printf("RX: 0x%010llX", rxAddr);
+    uLCD.locate(0, 10);
+    uLCD.printf("Mode: Receiving");
+    u.unlock();
+    int oldmode = -1;
     
-    // Heartbeat thread
     while (1) {
         myled4 = !myled4;
         setChannel(); // Poll the DIP switch and change channels accordingly
+        if (mode == 0 && not(oldmode == mode)) {
+            uLCD.locate(6, 10);
+            uLCD.printf("Rx");
+            oldmode = mode;
+        } else if  (mode == 1 && not(oldmode == mode)) {
+            uLCD.locate(6, 10);
+            uLCD.printf("Tx");
+            oldmode = mode;
+        }
         Thread::wait(100);
     }
 }
\ No newline at end of file