Aloha implementation of LoRa technology

Dependencies:   SX1276Lib mbed

Fork of SX1276PingPong by Semtech

Revision:
15:f790f35839db
Child:
16:c3c6b13c3c42
diff -r c7251480feb9 -r f790f35839db AlohaPacket.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/AlohaPacket.h	Tue May 31 09:52:21 2016 +0000
@@ -0,0 +1,73 @@
+#ifndef ALOHAPACKET_H_
+#define ALOHAPACKET_H_
+
+#include "stdint.h"
+#include "crc.h"
+
+typedef struct 
+{
+    uint8_t fid;
+    uint8_t no;
+} HeaderStruct;
+
+typedef struct
+{
+    uint8_t pd0;
+    uint8_t pd1;
+} DataStruct;
+
+inline void createAlohaPacket(uint8_t *output, HeaderStruct *header, DataStruct *data)
+{
+    // assume user has already allocated memory for output
+    
+    // create header
+    if (header)
+    {
+        output[0] = header->no;
+        output[0] |= header->fid << 4;
+    }
+    
+    // fit data
+    if (data)
+    {
+        output[1] = data->pd0;
+        output[2] = data->pd1;
+    }
+    
+    // calculate CRC
+    if (header && data)
+    {
+        output[3] = crc8(output, 3);
+    }
+}
+
+inline bool dissectAlohaPacket(uint8_t *input, HeaderStruct *header, DataStruct *data)
+{
+    // assume user has already prepare memory in *in
+    
+    // get header
+    if (header)
+    {
+        header->fid = input[0] >> 4; // higher four bits
+        header->no = input[0] & 0x0f; // lower four bits
+    }
+    
+    // get data
+    if (data)
+    {
+        data->pd0 = input[1];
+        data->pd1 = input[2];
+    }
+    
+    // check crc
+    if (header && data)
+    {
+        return input[3] == crc8(input, 3);
+    }
+    else
+    {
+        return false;
+    }
+}
+
+#endif
\ No newline at end of file