![](/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-08-08
- Revision:
- 12:2ead6bfd9f2a
- Parent:
- 11:9590f3c9d915
- Child:
- 14:25e836a5a7bf
File content as of revision 12:2ead6bfd9f2a:
#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(DEVICE_ID); 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; } /* * Format: * < :start flag * 02 :command * xx :length * xx: :00: get, 01: set * xx :index for parameters * ... * ff :checksum * > :end flag */ int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) { // read settings from radio #if USE_MODEM_LORA == 1 AlohaTransceiver::LoRaSettings_t *settings = aloha.getSettings(); #elif USE_MODEM_FSK == 1 AlohaTransceiver::FskSettings_t *settings = aloha.getSettings(); #else #error "Please define a modem in the compiler options." #endif if (payload_length < 2) { sprintf((char *) response, "Wrong Payload Length\r\n"); *response_length = 22; return 1; } // true is set, false is get bool isSet = (bool) payload[0]; uint8_t idx = payload[1]; switch(idx) { case 0x00: // Power { if (isSet) { int8_t Power = (int8_t) payload[2]; settings->Power = Power; return 0; } else { response[0] = (uint8_t) settings->Power; *response_length = 1; return 0; } } case 0x01: // Bandwidth { if (isSet) { uint32_t Bandwidth = (payload[5]) | (payload[4] << 8) | (payload[3] << 16) | (payload[2] << 24); settings->Bandwidth = Bandwidth; return 0; } else { response[3] = (uint8_t) (settings->Bandwidth); response[2] = (uint8_t) (settings->Bandwidth >> 8); response[1] = (uint8_t) (settings->Bandwidth >> 16); response[0] = (uint8_t) (settings->Bandwidth >> 24); *response_length = 4; return 0; } } case 0x02: // Datarate, AKA Spreading Factor { if (isSet) { uint32_t Datarate = (payload[5]) | (payload[4] << 8) | (payload[3] << 16) | (payload[2] << 24); settings->Datarate = Datarate; return 0; } else { response[3] = (uint8_t) (settings->Datarate); response[2] = (uint8_t) (settings->Datarate >> 8); response[1] = (uint8_t) (settings->Datarate >> 16); response[0] = (uint8_t) (settings->Datarate >> 24); *response_length = 4; return 0; } } case 0x03: // Coderate { if (isSet) { uint8_t Coderate = payload[2]; settings->Coderate = Coderate; return 0; } else { response[0] = (uint8_t) settings->Coderate; *response_length = 1; return 0; } } case 0x04: //Preamble Length { if (isSet) { uint16_t PreambleLen = payload[3] | (payload[2] << 8); settings->PreambleLen = PreambleLen; return 0; } else { response[1] = (uint8_t) (settings->PreambleLen); response[0] = (uint8_t)(settings->PreambleLen >> 8); *response_length = 2; return 0; } } case 0x05: //Symbol Timeout { if (isSet) { uint16_t SymbolTimeout = payload[3] | (payload[2] << 8); settings->SymbolTimeout = SymbolTimeout; return 0; } else { response[1] = (uint8_t) (settings->SymbolTimeout); response[0] = (uint8_t) (settings->SymbolTimeout >> 8); *response_length = 2; return 0; } } case 0x06: //FixLen { if (isSet) { bool FixLen = payload[2]; settings->FixLen = FixLen; return 0; } else { response[0] = (bool) (settings->SymbolTimeout); *response_length = 1; return 0; } } case 0x07: //PayloadLen { if (isSet) { uint8_t PayloadLen = payload[2]; settings->PayloadLen = PayloadLen; return 0; } else { response[0] = (uint8_t) (settings->PayloadLen); return 0; } } case 0x08: //CrcOn { if (isSet) { bool CrcOn = payload[2]; settings->CrcOn = CrcOn; return 0; } else { response[0] = (bool) (settings->CrcOn); return 0; } } case 0x09: //FreqHopOn { if (isSet) { bool FreqHopOn = payload[2]; settings->FreqHopOn = FreqHopOn; return 0; } else { response[0] = (bool) (settings->FreqHopOn); return 0; } } case 0x0A: //HopPeriod { if (isSet) { uint8_t HopPeriod = payload[2]; settings->HopPeriod = HopPeriod; return 0; } else { response[0] = (uint8_t) (settings->HopPeriod); return 0; } } case 0x0B: //IqInverted { if (isSet) { bool IqInverted = payload[2]; settings->IqInverted = IqInverted; return 0; } else { response[0] = (bool) (settings->IqInverted); return 0; } } case 0x0C: //RxContinuous { if(isSet) { bool RxContinuous = payload[2]; settings->RxContinuous = RxContinuous; return 0; } else { response[0] = (bool) (settings->RxContinuous); return 0; } } case 0x0D: //TxTimeout { if (isSet) { uint32_t TxTimeout = (payload[5]) | (payload[4] << 8) | (payload[3] << 16) | (payload[2] << 24); settings->TxTimeout = TxTimeout; return 0; } else { response[3] = (uint8_t) (settings->TxTimeout); response[2] = (uint8_t) (settings->TxTimeout >> 8); response[1] = (uint8_t) (settings->TxTimeout >> 16); response[0] = (uint8_t) (settings->TxTimeout >> 24); *response_length = 4; return 0; } } default: { break; } //case } return 0; } int radioUpdateSettings(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) { aloha.updateSettings(); 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; } int userQuery(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) { // send query data to client // make some dummy response response[0] = 1; // temperature sensor sprintf((char *)response + 1, "22.4"); *response_length = 5; 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(aloha.getDeviceId()); txFrame.setDestinationAddress(frame->getSourceAddress()); txFrame.setFullMessageFlag(frame->getFullMessageFlag()); txFrame.setSequenceID(frame->getSequenceID() + 1); txFrame.setPayload(0, snr); txFrame.setPayload(1, rssi_h); txFrame.setPayload(2, rssi_l); 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); SIP.registerCommand(0x02, configureRadio); SIP.registerCommand(0x03, radioUpdateSettings); SIP.registerCommand(0x04, userQuery); // 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); } } }