This is an example program that shows how to use the RadioHeadLite library, and does it using threads.

Dependencies:   RadioHeadLite

Revision:
3:fd3a7f10eed3
Parent:
2:57fefd8ae87c
Child:
4:8fbdf64301c5
--- a/main.cpp	Thu May 16 15:21:41 2019 +0000
+++ b/main.cpp	Mon May 31 02:59:41 2021 +0000
@@ -1,83 +1,139 @@
 #include "mbed.h"
-#include "USBSerial.h"
-
-#define GNSSEN   PC_3
-#define GNSSTXD  PA_9
-#define GNSSRXD  PA_10
-#define GNSSBAUD 9600
+#include "Geneva.h"
+#include "RH_RF95.h"
+#include "SPI.h"
 
 /******************** Comms *********************/
-USBSerial pc;
-//FileHandle* mbed::mbed_override_console(int)
-//{   
-//    return &pc;
-//}
+#include "USBSerial.h"
+USBSerial USB; // This allows us to have a non-blocking USB serial thingy
+FileHandle* mbed::mbed_override_console(int)
+{   
+    return &USB;
+}
+DigitalIn userBtn(USER_BUTTON);
 
-UARTSerial radio(GNSSTXD, GNSSRXD, 9600);
-
-
-DigitalOut gpsEn(GNSSEN, 1);
+// Change to match RX's freq!
+#define RF95_FREQ 915.0
 
-
-Thread myThread;
-
-
+// Singleton instance of the radio driver
+RH_RF95 radio(RADIO_CS, RADIO_INT);
+DigitalInOut radio_cs(RADIO_CS, PIN_OUTPUT, OpenDrainPullUp, 1);
+DigitalOut radio_rst(RADIO_RST, 1);
+DigitalOut greenLed(LED_GREEN, 1);
+DigitalOut blueLed(LED_BLUE, 1);
+DigitalOut redLed(LED_RED, 1);
 
-void sendToModem(void)
+#define wait_ms(X) (wait_us(X*1000))
+
+void doLED(uint8_t mask){
+    redLed = ((mask & 4)>>2)^1;
+    greenLed = ((mask & 2)>>1)^1;
+    blueLed = ((mask & 1))^1; 
+}
+
+static void press_power_button(int time_ms)
 {
-    char c;
-    c = pc.getc();
-    radio.write(&c, 1);// push the charcter to the modem   
 }
 
-void sendToPC(void)
+bool initModem(void)
 {
-    char c;
-    radio.read(&c, 1);
-    pc.putc(c);    
+    bool success = true;
+    printf("Starting LoRa radio...\n");
+    radio_rst = 0;
+    delay(10);
+    radio_rst = 1;
+    delay(10);
+    if(!radio.init()){
+        printf("init failed, ");
+        success = false;
+    }
+    
+      // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
+    if (success && !radio.setFrequency(RF95_FREQ)) {
+        printf("setFrequency failed, ");
+        success = false;
+    }
+        
+    // The default transmitter power is 13dBm, using PA_BOOST.
+    // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then 
+    // you can set transmitter powers from 5 to 23 dBm:       
+    if(success){
+        radio.setTxPower(18);
+    }
+    else
+        printf("LoRa driver not functional\n");
+        
+    return success;
+}     
+
+void pdwnModem(void)
+{
+    radio_rst = 0;
 }
 
-
-static void passthroughThread(void)
-{
-//    pc.attach(&sendToModem);
-    
-    while (1)
+static Thread radioThread( osPriorityNormal, OS_STACK_SIZE / 2, NULL, "radio_thread" );
+void writeRadio(){
+    uint8_t inbuf[RH_RF95_MAX_MESSAGE_LEN];
+    uint8_t size = 0;
+    uint8_t len = 0;
+    while(1)
     {
-        // Housekeeping - check button
-        if (pc.readable())
-        {
-            sendToModem();
+        // store up chars in buffer
+        if(USB.readable()){
+            size = USB.available();
+            if(USB.receive(inbuf+len,size))
+            USB.send(inbuf+len, size);
+            len += size;
         }
-
-        if (radio.readable())
-        {
-            sendToPC();
+            
+        // Send data if newline is found
+        if(inbuf[len - 1] == '\n'){
+            inbuf[len++] = 0; // make sure we terminate the string
+            radio.send(inbuf,len);
+            radio.waitPacketSent();
+            len = 0;    
         }
     }
 }
 
-
-
+static Thread pcThread( osPriorityNormal, OS_STACK_SIZE / 2, NULL, "pc_thread" );
+void writePC(){
+    uint8_t rxBuf[RH_RF95_MAX_MESSAGE_LEN];
+    uint8_t size = sizeof(rxBuf);
+    printf("starting PC thread\n");
+    while(1)
+    {
+        if(radio.available()){
+            if(radio.recv(rxBuf,&size))
+                printf("Got: %s\n", rxBuf);
+//                USB.send(rxBuf, size);
+        }
+    }
+}
 
 void setup(void)
 {
-    wait(1); // wait just a bit for the USB to enumerate
-
-
-    pc.set_blocking(false);
-    radio.set_blocking(false);
-
-    wait(1);    
-}  
+    userBtn.mode(PullUp);
+    USB.connect();
+    wait_ms(1000);
+    printf("Lora Modem\n------------\n");
+    printf("* mbed-os: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);    
+    
+    wait_ms(10);
+    initModem();
+    radioThread.start(writeRadio);
+    pcThread.start(writePC);
+}
 
 int main(void)
 {
     setup();
 
-    myThread.start(passthroughThread);
-
-    return 1;
+    while(1){
+        ThisThread::sleep_for(1000);
+        blueLed = !blueLed;
+    }
+        
 }