Wireless interface using LoRa technology
Dependencies: AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU
main.cpp
- Committer:
- rba90
- Date:
- 2016-07-27
- Revision:
- 6:19457108c899
- Parent:
- 5:445f35444a6a
- Child:
- 8:332099ece6e9
File content as of revision 6:19457108c899:
#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; // 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); seqid += 1; return 0; } void AlohaDataEcho(AlohaFrame *frame) { // get rssi and snr uint16_t rssi = aloha.getRssi(); uint8_t snr = aloha.getSnr(); uint8_t rssi_h = (rssi & 0xff00) >> 8; uint8_t rssi_l = rssi & 0xff; // Transmit RSSI and SNR back to sender txFrame.setType(AlohaFrame::Aloha_Data); txFrame.setPayloadLength(3); txFrame.setSourceAddress(frame->getDestinationAddress()); txFrame.setDestinationAddress(frame->getSourceAddress()); txFrame.setFullMessageFlag(frame->getFullMessageFlag()); txFrame.setSequenceID(frame->getSequenceID() + 1); txFrame.setPayload(snr, 0); txFrame.setPayload(rssi_h, 1); txFrame.setPayload(rssi_l, 2); txFrame.generateCrc(); uint8_t buffer[20]; memset(buffer, 0x0, sizeof(buffer)); txFrame.serialize(buffer); aloha.send(buffer, 20); // print received message printf("-----------------------------------------\r\n"); 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()); printf("-----------------------------------------\r\n"); } int main() { // initialize radio module aloha.boardInit(); aloha.updateSettings(); aloha.enable(); // 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, AlohaDataEcho); while(1) { SIP.poll(); aloha.poll(); while (SerialOutputBuffer.getCounter() > 0) { uint8_t ch; ch = SerialOutputBuffer.dequeue(); pc.putc(ch); } } }