Point Labs / Mbed OS Threaded_LoRa_Modem

Dependencies:   RadioHeadLite

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Geneva.h"
00003 #include "RH_RF95.h"
00004 #include "SPI.h"
00005 
00006 /******************** Comms *********************/
00007 #include "USBSerial.h"
00008 USBSerial USB; // This allows us to have a non-blocking USB serial thingy
00009 FileHandle* mbed::mbed_override_console(int)
00010 {   
00011     return &USB;
00012 }
00013 DigitalIn userBtn(USER_BUTTON);
00014 
00015 // Change to match RX's freq!
00016 #define RF95_FREQ 915.0
00017 
00018 // Singleton instance of the radio driver
00019 RH_RF95 radio(RADIO_CS, RADIO_INT);
00020 DigitalInOut radio_cs(RADIO_CS, PIN_OUTPUT, OpenDrainPullUp, 1);
00021 DigitalOut radio_rst(RADIO_RST, 1);
00022 DigitalOut greenLed(LED_GREEN, 1);
00023 DigitalOut blueLed(LED_BLUE, 1);
00024 DigitalOut redLed(LED_RED, 1);
00025 
00026 #define wait_ms(X) (wait_us(X*1000))
00027 
00028 void doLED(uint8_t mask){
00029     redLed = ((mask & 4)>>2)^1;
00030     greenLed = ((mask & 2)>>1)^1;
00031     blueLed = ((mask & 1))^1; 
00032 }
00033 
00034 static void press_power_button(int time_ms)
00035 {
00036 }
00037 
00038 bool initModem(void)
00039 {
00040     bool success = true;
00041     printf("Starting LoRa radio...\n");
00042     radio_rst = 0;
00043     delay(10);
00044     radio_rst = 1;
00045     delay(10);
00046     if(!radio.init()){
00047         printf("init failed, ");
00048         success = false;
00049     }
00050     
00051       // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM
00052     if (success && !radio.setFrequency(RF95_FREQ)) {
00053         printf("setFrequency failed, ");
00054         success = false;
00055     }
00056         
00057     // The default transmitter power is 13dBm, using PA_BOOST.
00058     // If you are using RFM95/96/97/98 modules which uses the PA_BOOST transmitter pin, then 
00059     // you can set transmitter powers from 5 to 23 dBm:       
00060     if(success){
00061         radio.setTxPower(18);
00062     }
00063     else
00064         printf("LoRa driver not functional\n");
00065         
00066     return success;
00067 }     
00068 
00069 void pdwnModem(void)
00070 {
00071     radio_rst = 0;
00072 }
00073 
00074 static Thread radioThread( osPriorityNormal, OS_STACK_SIZE / 2, NULL, "radio_thread" );
00075 void writeRadio(){
00076     uint8_t inbuf[RH_RF95_MAX_MESSAGE_LEN];
00077     uint8_t size = 0;
00078     uint8_t len = 0;
00079     while(1)
00080     {
00081         // store up chars in buffer
00082         if(USB.readable()){
00083             size = USB.available();
00084             if(USB.receive(inbuf+len,size)){
00085                 USB.send(inbuf+len, size);
00086                 len += size;
00087             }
00088         }
00089             
00090         // Send data if newline is found
00091         if(inbuf[len - 1] == '\n'){
00092             len -= 2; // strip off the newline stuff
00093 //            inbuf[len++] = 0; // make sure we terminate the string
00094             radio.send(inbuf,len);
00095             radio.waitPacketSent();
00096             len = 0;    
00097         }
00098     }
00099 }
00100 
00101 static Thread pcThread( osPriorityNormal, OS_STACK_SIZE / 2, NULL, "pc_thread" );
00102 void writePC(){
00103     uint8_t rxBuf[RH_RF95_MAX_MESSAGE_LEN];
00104     uint8_t size;
00105     printf("starting PC thread\n");
00106     while(1)
00107     {
00108         if(radio.available()){
00109             size = sizeof(rxBuf);
00110             if(radio.recv(rxBuf,&size))
00111                 rxBuf[size] = 0; // make sure to null terminate the string
00112                 printf("Got: %s", rxBuf);
00113                 if(rxBuf[size-1]!= '\n')
00114                     USB.putc('\n'); // put a new line in just in case
00115                 printf("RSSI: %d\n",radio.lastRssi());
00116         }
00117     }
00118 }
00119 void trigger()
00120 {
00121     blueLed = !blueLed;
00122 }
00123 
00124 void setup(void)
00125 {
00126     userBtn.mode(PullUp);
00127     USB.connect();
00128     wait_ms(1000);
00129     printf("Lora Modem\n------------\n");
00130     printf("* mbed-os: %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);    
00131     
00132     wait_ms(10);
00133     initModem();
00134     radioThread.start(writeRadio);
00135     pcThread.start(writePC);
00136 }
00137 
00138 int main(void)
00139 {
00140     setup();
00141 
00142     while(1){
00143         ThisThread::sleep_for(100);
00144     }
00145         
00146 }
00147 
00148