Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU

Revision:
0:1e42d79b42e1
Child:
2:ae85337d4ca7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Jul 14 04:49:35 2016 +0000
@@ -0,0 +1,120 @@
+#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;
+}
+
+int sendMessage(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
+{
+    static uint8_t seqid = 0;
+    
+    txFrame.setType(AlohaFrame::Aloha_Data);
+    txFrame.setPayloadLength(0x2);
+    txFrame.setSourceAddress(0x1);
+    txFrame.setDestinationAddress(0x2);
+    txFrame.setFullMessageFlag(0x1);
+    txFrame.setSequenceID(seqid);
+    txFrame.setPayload(0xff, 0);
+    txFrame.setPayload(0xfe, 1);
+    txFrame.generateCrc();
+    
+    
+    uint8_t buffer[20];
+    memset(buffer, 0x0, sizeof(buffer));
+    txFrame.serialize(buffer);
+    
+    aloha.send(buffer, 20);
+    
+    seqid += 1;
+    
+    return 0;   
+}
+
+void AlohaDataHandler(AlohaFrame *frame)
+{
+    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());
+    
+}
+
+int main() {
+    // initialize radio module
+    aloha.BoardInit();
+    
+    // attach serial interrupt handler
+    pc.attach(&serialInterruptHandler);
+    
+    // register callback functions for SIP
+    SIP.registerCommand(0x00, toggleChecksum);
+    SIP.registerCommand(0x01, sendMessage);
+    
+    // register callback functions for aloha transceiver
+    aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataHandler);
+    
+    while(1) {
+        SIP.poll();
+        aloha.poll();
+        
+        while (SerialOutputBuffer.getCounter() > 0)
+        {
+            uint8_t ch;
+            ch = SerialOutputBuffer.dequeue();
+            pc.putc(ch);
+        }
+    }
+}