Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU

main.cpp

Committer:
rba90
Date:
2016-07-14
Revision:
0:7d8e24cb62c1
Child:
1:101cf4fca4e5

File content as of revision 0:7d8e24cb62c1:

#include "mbed.h"
#include "AlohaTransceiver.h"
#include "buffer.h"
#include "SerialInterfaceProtocol.h"
#include "AlohaFrame.h"

Serial pc(USBTX, USBRX);

// sip uses two buffer queues
CircularBuffer<uint8_t> SerialInputBuffer;
CircularBuffer<uint8_t> SerialOutputBuffer;
SerialInterfaceProtocol SIP(&SerialInputBuffer, &SerialOutputBuffer);

// aloha transceiver
AlohaTransceiver aloha;
AlohaFrame txFrame;

void serialInterruptHandler() {
    // Note: you need to actually read from the serial to clear the RX interrupt
    int c = pc.getc();
    
    // add to buffer
    if (SerialInputBuffer.isLocked())
    {
        printf("Mutex Locked\r\n");
    }
    else
    {
        SerialInputBuffer.enqueue((uint8_t) c);   
    }
}

int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
{
    // one payload
    if (payload_length != 1)
    {
        sprintf((char *) response, "Wrong Payload Length\r\n");
        *response_length = 22;
        return 1;
    } 
    
    if ((bool) payload[0])
    {
        SIP.enableChecksum();
    }
    else
    {
        SIP.disableChecksum();
    }
    
    return 0;
}

void AlohaDataEcho(AlohaFrame *frame)
{    
    // print received message
    printf("AlohaDataHandler invoked\r\n");
    printf("    Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength());
    printf("    SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress());
    printf("    FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID());
    for (uint8_t i = 0; i < frame->getPayloadLength(); i++)
    {
        printf("    Payload[%d]: 0x%x\r\n", i, frame->getPayload(i));
    }
    printf("    CRC: 0x%x\r\n", frame->getCrc());
    
    // echo back to the sender
    txFrame.setType(frame->getType());
    txFrame.setPayloadLength(frame->getPayloadLength());
    txFrame.setSourceAddress(frame->getDestinationAddress());
    txFrame.setDestinationAddress(frame->getSourceAddress());
    txFrame.setFullMessageFlag(frame->getFullMessageFlag());
    txFrame.setSequenceID(frame->getSequenceID() + 1);
    for (uint8_t i = 0; i < frame->getPayloadLength(); i++)
    {
        txFrame.setPayload(frame->getPayload(i), i);   
    }
    txFrame.generateCrc();
    
    
    uint8_t buffer[20];
    memset(buffer, 0x0, sizeof(buffer));
    txFrame.serialize(buffer);
    
    aloha.send(buffer, 20);
}

int main() {
    // initialize radio module
    aloha.BoardInit();
    
    // attach serial interrupt handler
    pc.attach(&serialInterruptHandler);
    
    // register callback functions for SIP
    SIP.registerCommand(0x00, toggleChecksum);
    
    // register callback functions for aloha transceiver
    aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataEcho);
    
    while(1) {
        SIP.poll();
        aloha.poll();
        
        while (SerialOutputBuffer.getCounter() > 0)
        {
            uint8_t ch;
            ch = SerialOutputBuffer.dequeue();
            pc.putc(ch);
        }
    }
}