Wireless interface using LoRa technology
Dependencies: AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU
main.cpp
- Committer:
- rba90
- Date:
- 2016-07-14
- Revision:
- 1:101cf4fca4e5
- Parent:
- 0:7d8e24cb62c1
- Child:
- 2:5a74ae8be594
File content as of revision 1:101cf4fca4e5:
#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; Timer timer; 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; // prepare for the frame txFrame.setType(AlohaFrame::Aloha_Data); txFrame.setPayloadLength(0x0); txFrame.setSourceAddress(0x1); txFrame.setDestinationAddress(0x2); txFrame.setFullMessageFlag(0x1); txFrame.setSequenceID(seqid); txFrame.generateCrc(); uint8_t buffer[20]; memset(buffer, 0x0, sizeof(buffer)); txFrame.serialize(buffer); aloha.send(buffer, 20); // start the timer to measure round trip time timer.reset(); timer.start(); seqid += 1; return 0; } void AlohaDataHandler(AlohaFrame *frame) { // stop the timer to measure round trip time timer.stop(); printf("Received Frame\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()); // decode payload (range test only) int8_t snr = (int8_t) frame->getPayload(0); uint16_t rssi_h = frame->getPayload(1); uint8_t rssi_l = frame->getPayload(2); int16_t rssi = (int16_t) (rssi_h << 8 | rssi_l); printf("Base Station respond:\r\n"); printf("Rssi: %d, Snr: %d\r\n", rssi, snr); printf("Round trip time: %d ms\r\n", timer.read_ms()); } 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); } } }