![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Wireless interface using LoRa technology
Dependencies: AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU
main.cpp
- Committer:
- rba90
- Date:
- 2016-07-14
- Revision:
- 2:ae85337d4ca7
- Parent:
- 0:1e42d79b42e1
- Child:
- 3:a4f6abad9378
File content as of revision 2:ae85337d4ca7:
#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()); // 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); } 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); } } }