ping pong with modifiable parameters

Dependencies:   mbed SX126xLib

Revision:
4:99bfe3b61a6a
Parent:
3:c704345b0c9f
Child:
5:dc53029f4c02
--- a/main.cpp	Wed Jun 19 16:25:56 2019 +0000
+++ b/main.cpp	Thu Jun 20 22:04:10 2019 +0000
@@ -1,40 +1,19 @@
-/*
-  ______                              _
- / _____)             _              | |
-( (____  _____ ____ _| |_ _____  ____| |__
- \____ \| ___ |    (_   _) ___ |/ ___)  _ \
- _____) ) ____| | | || |_| ____( (___| | | |
-(______/|_____)_|_|_| \__)_____)\____)_| |_|
-    (C)2017 Semtech
-
-Description: Main program
-
-Maintainer: Gregory Cristian & Gilbert Menth
-*/
-
 #include "mbed.h"
 #include "radio.h"
 #include "sx126x-hal.h"
 
-
-/*!
- * \brief Used to display firmware version on RS232
- */
-#define FIRMWARE_VERSION ( ( char* )"Firmware Version: 170314A" )
-
-/*!
- * Use the Ping Ping in uncommented Mode
- */
-//#define MODE_BLE
-#define MODE_LORA
-//#define MODE_GENERIC
-//#define MODE_FLRC
+#define buffer_size 256             // incoming buffer size
+#define buffer_fill buffer_size+1   // number, when buffer is ready
 
 
-/*!
- * \brief Defines the nominal frequency
- */
-#define RF_FREQUENCY                                350000000 // Hz
+long unsigned RF_FREQUENCY = 350000000; // Hz
+
+Serial s( USBTX, USBRX );
+// additional variables for incoming data
+char serial_buffer[buffer_size];    // buffer to save incoming data
+int serial_buffer_index = 0;          // index array for buffer
+bool serial_end_line = false;       // searching for end line
+
 
 /*!
  * \brief Defines the output power in dBm
@@ -89,35 +68,18 @@
 
 int8_t RssiValue = 0;
 int8_t SnrValue = 0;
-
-/*!
- * \brief Function to be executed on Radio Tx Done event
- */
-void OnTxDone( void );
-
-/*!
- * \brief Function to be executed on Radio Rx Done event
- */
-void OnRxDone( void );
+ModulationParams_t modulationParams;
 
-/*!
- * \brief Function executed on Radio Tx Timeout event
- */
+void OnTxDone( void );
+void OnRxDone( void );
 void OnTxTimeout( void );
-
-/*!
- * \brief Function executed on Radio Rx Timeout event
- */
 void OnRxTimeout( void );
-
-/*!
- * \brief Function executed on Radio Rx Error event
- */
 void OnRxError( IrqErrorCode_t );
+void parser();
+void LoRa_init(ModulationParams_t modulationParams);
 
 
-RadioCallbacks_t callbacks =
-{
+RadioCallbacks_t callbacks = {
     &OnTxDone,        // txDone
     &OnRxDone,        // rxDone
     NULL,             // rxPreambleDetect
@@ -165,45 +127,60 @@
 
 /*!
  * \brief Locals parameters and status for radio API
- * NEED TO BE OPTIMIZED, COPY OF STUCTURE ALREADY EXISTING
  */
 PacketParams_t PacketParams;
 PacketStatus_t PacketStatus;
 
+
+void serialRx()
+{
+    int j;
+    while(s.readable()) {
+        char character=s.getc();                                           
+        if(( (int)character==10 || (int)character==13) && serial_end_line) {
+            serial_end_line=true; 
+            continue;
+        } else {
+            serial_end_line=false; 
+        }
+        if(serial_buffer_index != buffer_fill) {
+            serial_buffer[serial_buffer_index++] = character;
+            if(serial_buffer_index == buffer_size) {
+                serial_buffer[buffer_size-1] = '\0';
+                serial_buffer_index=buffer_fill;
+                continue;
+            }
+            if(character == 13 || character == 10) {
+                serial_buffer[serial_buffer_index-1] = '\0';
+                serial_buffer_index = buffer_fill;
+                serial_end_line = true;
+            }
+        } 
+    }
+    if(serial_buffer_index==buffer_fill && serial_end_line != true) {
+        printf("Input too long to be parsed\n\r");
+        serial_buffer_index = 0;
+    }
+    else if(serial_end_line == true) {
+        serial_buffer_index = 0;
+        if(sscanf (serial_buffer,"%d",&j)) {  // sscanf for searching integer
+            printf("From PC:%d.\n\r" ,j);  // write to serial incoming integer
+        }
+        parser();
+    }
+    
+}
+
 /*!
  * \brief Specify serial datarate for UART debug output
  */
 void baud( int baudrate )
-{
-    Serial s( USBTX, USBRX );
-
-    s.baud( baudrate );
+{    
+    s.baud(baudrate);
+    s.attach(&serialRx,Serial::RxIrq);  
 }
 
-int main( )
-{
-    bool isMaster = true;
-    ModulationParams_t modulationParams;
-
-    baud( 115200 );
-
-    F_CS   = 1;
-    SD_CS  = 1;
-    RxLed  = 1;
-    TxLed  = 1;
-    ANT_SW = 1;
-
-    wait_ms( 500 ); // wait for on board DC/DC start-up time
-
-    Radio.Init( );
-    Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
-
-    memset( &Buffer, 0x00, BufferSize );
-
-    printf( "\n\n\r     SX1262 Ping Pong Demo Application (%s)\n\n\r", "nimita" );
-
-
-    printf( "\nPing Pong running in LORA mode\n\r");
+void LoRa_init() {
     modulationParams.PacketType                  = PACKET_TYPE_LORA;
     modulationParams.Params.LoRa.SpreadingFactor = LORA_SF7;
     modulationParams.Params.LoRa.Bandwidth       = LORA_BW_500;
@@ -235,6 +212,32 @@
     AppState = APP_LOWPOWER;
     
     Radio.ProcessIrqs( );
+}
+
+int main( )
+{
+    bool isMaster = true;
+
+    baud( 115200 );
+
+    F_CS   = 1;
+    SD_CS  = 1;
+    RxLed  = 1;
+    TxLed  = 1;
+    ANT_SW = 1;
+
+    wait_ms( 500 ); // wait for on board DC/DC start-up time
+
+    Radio.Init( );
+    Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
+
+    memset( &Buffer, 0x00, BufferSize );
+
+    printf( "\n\n\r     SX1262 Whitespace Ping Pong Application \n\n\r");
+
+    LoRa_init();
+    printf( "\nPing Pong running in LORA mode\n\r");
+    
 
     while( 1 )
     {
@@ -361,13 +364,11 @@
     }
 }
 
-void OnTxDone( void )
-{
+void OnTxDone( void ) {
     AppState = APP_TX;
 }
 
-void OnRxDone( void )
-{
+void OnRxDone( void ) {
     AppState = APP_RX;
     PacketStatus_t packetStatus;
     Radio.GetPacketStatus(&packetStatus);
@@ -376,27 +377,42 @@
     printf("rssi: %d; snr: %d\n\r", RssiValue, SnrValue );
 }
 
-void OnTxTimeout( void )
-{
+void OnTxTimeout( void ) {
     AppState = APP_TX_TIMEOUT;
     printf( "<>>>>>>>>TXE\r\n" );
 }
 
-void OnRxTimeout( void )
-{
+void OnRxTimeout( void ) {
     AppState = APP_RX_TIMEOUT;
 }
 
-void OnRxError( IrqErrorCode_t errorCode )
-{
+void OnRxError( IrqErrorCode_t errorCode ) {
     AppState = APP_RX_ERROR;
     printf( "RXE<>>>>>>>>\r\n" );
 }
-//
-//void OnRangingDone( IrqRangingCode_t val )
-//{
-//}
+
+void OnCadDone( bool channelActivityDetected ) {
+}
+
 
-void OnCadDone( bool channelActivityDetected )
-{
+void parser() {
+    printf("%s\n\r", serial_buffer);
+    char command[10];
+    unsigned long val;
+    if(sscanf(serial_buffer, "%10s %lu", command, &val) != 2){
+        printf("Invalid Input\n\r");
+        return;
+    }
+    if(strcmp(command, "FREQ") == 0) {
+        if((125000000<=val) && (val<=960000000)) {
+            RF_FREQUENCY = val;
+            printf("Frequency set to: %lu\n\r", val);
+            LoRa_init();
+            
+        }
+    }
+    else
+        printf("Invalid command\n\r");
+    
+        
 }