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

Dependencies:   RadioHeadLite

main.cpp

Committer:
rlanders73
Date:
2018-11-24
Revision:
0:f4015c8e84c3
Child:
1:75d533f15c95

File content as of revision 0:f4015c8e84c3:

#include "mbed.h"
#include "USBSerial.h"
#include "Geneva.h"

#define PASSTHROUGH 1

/******************** Comms *********************/
USBSerial pc; // moved the pc interface to the USB serial device
Serial modemSerial(MDMTXD, MDMRXD, 115200);


DigitalInOut cellRst(MDMRST);
DigitalInOut cellPwrKey(MDMPWRON);
DigitalOut cellVcc(MDMVCC, 0);
DigitalOut cellRts(MDMRTS, 0);  // Flow control not supported by 410, but RTS must be low
DigitalOut cellCts(MDMCTS, 0);

DigitalOut gpsEn(PC_3, 0);
DigitalOut bleRst(PB_5, 0);

DigitalOut greenLed(LED_GREEN, 1);
DigitalOut blueLed(LED_BLUE, 1);
DigitalOut redLed(LED_RED, 1);

InterruptIn buttonInt(USER_BUTTON);

InterruptIn *rxPin;
DigitalOut samplePin(PA_0);

Thread passThread;

 static char scanBuffer[512];
static bool buttonWasPushed = false;

typedef struct
{
    DigitalOut *resetPin;
    DigitalOut *enablePin;
    DigitalOut *dtrPin;
    FileHandle *fileHandle;
    ATCmdParser *rawParser;
    UARTSerial *serial;
} cellContext_t;

cellContext_t cell;

uint8_t buffer[64];

#if PASSTHROUGH
static void passthroughThread(void)
{
    
    while (1)
    {
        // Housekeeping - check button
        if (pc.readable())
        {
            char c = pc.getc(); // grab the available character
            pc.putc(c);         // provide local echo
            modemSerial.putc(c);// push the charcter to the modem
        }

        if (modemSerial.readable())
        {
            pc.putc(modemSerial.getc());
        }
    }
}
#endif

static void buttonPushed(void)
{
    buttonWasPushed = true;
}




int main(void)
{
    // Enable flow control on MDM uart
    //modemUartSerial.set_flow_control(SerialBase::RTSCTS, MDMRTS, MDMCTS);

    buttonInt.mode(PullUp);
    buttonInt.fall(buttonPushed);

    cellRst.mode(OpenDrainNoPull);
    cellRst.output();
    cellRst = 1;

    cellPwrKey.mode(OpenDrainNoPull);
    cellPwrKey.output();
    cellPwrKey = 1;
    
    wait(1); // wait just a bit for the USB to enumerate

    pc.printf("Modem Test\r\n", 12);

    pc.set_blocking(false);
    modemSerial.set_blocking(false);

    // Power up the modem!
    cellVcc = 1;
    pc.printf("Modem VCC UP\r\n", 14);
    wait(3);

    cellPwrKey = 0;
    wait(0.9);
    cellPwrKey = 1;
    pc.printf("Modem Power\r\n", 13);

    redLed = 0;

    wait(5);

#if PASSTHROUGH
    pc.printf("Passthrough mode\r\n", 18);
    blueLed = 0;
    passThread.start(passthroughThread);

#else
    pc.printf("Auto mode\r\n", 11);

    cell.fileHandle = &modemUartSerial;
    cell.rawParser = new ATCmdParser(cell.fileHandle);
    cell.rawParser->flush();
    cell.rawParser->debug_on(1);
    cell.rawParser->set_timeout(2000);

    while (1)
    {
        cell.rawParser->send("AT");
        if (cell.rawParser->recv("OK"))
            break;
        wait(1.5);
    }

    redLed = 1;
    blueLed = 0;
    pc.printf("Modem OK\r\n");
    wait(5);

    cell.rawParser->set_timeout(15000);

    while (1)
    {
        cell.rawParser->send("AT+CCID");
        if (cell.rawParser->recv("OK"))
        {
            break;
        }
        wait(4.5);
    }

    blueLed = 1;
    greenLed = 0;
    pc.printf("SIM OK\r\n");
    wait(2);

    int act;
    char plmn[8];
    bool result;

    cell.rawParser->set_timeout(20000);
    cell.rawParser->send("AT+CMEE=2") && cell.rawParser->recv("OK");

    while (1)
    {
        cell.rawParser->send("AT+COPS?");
        result = cell.rawParser->recv("+COPS: %[^\n]\nOK\n", scanBuffer);
        if (true == result)
        {
            // Parse the registration status
            if (sscanf(scanBuffer, "%*d, %*d, \"%[^\"]\", %d", plmn, &act) == 2)
            {
                if (act == 8)
                    break;
            }
        }
        wait(3);
    }
    
    pc.printf("Network OK\r\n");

    while (1)
    {
        greenLed = 1;
        wait(0.75);
        greenLed = 0;
        wait(0.75);
        if (buttonWasPushed)
            break;
    }

    cell.rawParser->send("AT+COPS=2") && cell.rawParser->recv("OK");

#endif

    Thread::wait(osWaitForever);

    return 1;
}