![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Wireless interface using LoRa technology
Dependencies: AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU
main.cpp@5:445f35444a6a, 2016-07-23 (annotated)
- Committer:
- rba90
- Date:
- Sat Jul 23 07:42:58 2016 +0000
- Revision:
- 5:445f35444a6a
- Parent:
- 3:a4f6abad9378
- Child:
- 6:19457108c899
rename BoardInit() to boardInit() with respect to the change in library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rba90 | 0:1e42d79b42e1 | 1 | #include "mbed.h" |
rba90 | 0:1e42d79b42e1 | 2 | #include "AlohaTransceiver.h" |
rba90 | 0:1e42d79b42e1 | 3 | #include "buffer.h" |
rba90 | 0:1e42d79b42e1 | 4 | #include "SerialInterfaceProtocol.h" |
rba90 | 0:1e42d79b42e1 | 5 | #include "AlohaFrame.h" |
rba90 | 0:1e42d79b42e1 | 6 | |
rba90 | 0:1e42d79b42e1 | 7 | Serial pc(USBTX, USBRX); |
rba90 | 0:1e42d79b42e1 | 8 | |
rba90 | 0:1e42d79b42e1 | 9 | // sip uses two buffer queues |
rba90 | 0:1e42d79b42e1 | 10 | CircularBuffer<uint8_t> SerialInputBuffer; |
rba90 | 0:1e42d79b42e1 | 11 | CircularBuffer<uint8_t> SerialOutputBuffer; |
rba90 | 0:1e42d79b42e1 | 12 | SerialInterfaceProtocol SIP(&SerialInputBuffer, &SerialOutputBuffer); |
rba90 | 0:1e42d79b42e1 | 13 | |
rba90 | 0:1e42d79b42e1 | 14 | // aloha transceiver |
rba90 | 0:1e42d79b42e1 | 15 | AlohaTransceiver aloha; |
rba90 | 0:1e42d79b42e1 | 16 | AlohaFrame txFrame; |
rba90 | 0:1e42d79b42e1 | 17 | |
rba90 | 0:1e42d79b42e1 | 18 | void serialInterruptHandler() { |
rba90 | 0:1e42d79b42e1 | 19 | // Note: you need to actually read from the serial to clear the RX interrupt |
rba90 | 0:1e42d79b42e1 | 20 | int c = pc.getc(); |
rba90 | 0:1e42d79b42e1 | 21 | |
rba90 | 0:1e42d79b42e1 | 22 | // add to buffer |
rba90 | 0:1e42d79b42e1 | 23 | if (SerialInputBuffer.isLocked()) |
rba90 | 0:1e42d79b42e1 | 24 | { |
rba90 | 0:1e42d79b42e1 | 25 | printf("Mutex Locked\r\n"); |
rba90 | 0:1e42d79b42e1 | 26 | } |
rba90 | 0:1e42d79b42e1 | 27 | else |
rba90 | 0:1e42d79b42e1 | 28 | { |
rba90 | 0:1e42d79b42e1 | 29 | SerialInputBuffer.enqueue((uint8_t) c); |
rba90 | 0:1e42d79b42e1 | 30 | } |
rba90 | 0:1e42d79b42e1 | 31 | } |
rba90 | 0:1e42d79b42e1 | 32 | |
rba90 | 0:1e42d79b42e1 | 33 | int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 0:1e42d79b42e1 | 34 | { |
rba90 | 0:1e42d79b42e1 | 35 | // one payload |
rba90 | 0:1e42d79b42e1 | 36 | if (payload_length != 1) |
rba90 | 0:1e42d79b42e1 | 37 | { |
rba90 | 0:1e42d79b42e1 | 38 | sprintf((char *) response, "Wrong Payload Length\r\n"); |
rba90 | 0:1e42d79b42e1 | 39 | *response_length = 22; |
rba90 | 0:1e42d79b42e1 | 40 | return 1; |
rba90 | 0:1e42d79b42e1 | 41 | } |
rba90 | 0:1e42d79b42e1 | 42 | |
rba90 | 0:1e42d79b42e1 | 43 | if ((bool) payload[0]) |
rba90 | 0:1e42d79b42e1 | 44 | { |
rba90 | 0:1e42d79b42e1 | 45 | SIP.enableChecksum(); |
rba90 | 0:1e42d79b42e1 | 46 | } |
rba90 | 0:1e42d79b42e1 | 47 | else |
rba90 | 0:1e42d79b42e1 | 48 | { |
rba90 | 0:1e42d79b42e1 | 49 | SIP.disableChecksum(); |
rba90 | 0:1e42d79b42e1 | 50 | } |
rba90 | 0:1e42d79b42e1 | 51 | |
rba90 | 0:1e42d79b42e1 | 52 | return 0; |
rba90 | 0:1e42d79b42e1 | 53 | } |
rba90 | 0:1e42d79b42e1 | 54 | |
rba90 | 2:ae85337d4ca7 | 55 | void AlohaDataEcho(AlohaFrame *frame) |
rba90 | 3:a4f6abad9378 | 56 | { |
rba90 | 2:ae85337d4ca7 | 57 | // get rssi and snr |
rba90 | 2:ae85337d4ca7 | 58 | uint16_t rssi = aloha.getRssi(); |
rba90 | 2:ae85337d4ca7 | 59 | uint8_t snr = aloha.getSnr(); |
rba90 | 2:ae85337d4ca7 | 60 | |
rba90 | 2:ae85337d4ca7 | 61 | uint8_t rssi_h = (rssi & 0xff00) >> 8; |
rba90 | 2:ae85337d4ca7 | 62 | uint8_t rssi_l = rssi & 0xff; |
rba90 | 2:ae85337d4ca7 | 63 | |
rba90 | 2:ae85337d4ca7 | 64 | // Transmit RSSI and SNR back to sender |
rba90 | 2:ae85337d4ca7 | 65 | txFrame.setType(AlohaFrame::Aloha_Data); |
rba90 | 2:ae85337d4ca7 | 66 | txFrame.setPayloadLength(3); |
rba90 | 2:ae85337d4ca7 | 67 | txFrame.setSourceAddress(frame->getDestinationAddress()); |
rba90 | 2:ae85337d4ca7 | 68 | txFrame.setDestinationAddress(frame->getSourceAddress()); |
rba90 | 2:ae85337d4ca7 | 69 | txFrame.setFullMessageFlag(frame->getFullMessageFlag()); |
rba90 | 2:ae85337d4ca7 | 70 | txFrame.setSequenceID(frame->getSequenceID() + 1); |
rba90 | 2:ae85337d4ca7 | 71 | txFrame.setPayload(snr, 0); |
rba90 | 2:ae85337d4ca7 | 72 | txFrame.setPayload(rssi_h, 1); |
rba90 | 2:ae85337d4ca7 | 73 | txFrame.setPayload(rssi_l, 2); |
rba90 | 2:ae85337d4ca7 | 74 | txFrame.generateCrc(); |
rba90 | 2:ae85337d4ca7 | 75 | |
rba90 | 2:ae85337d4ca7 | 76 | |
rba90 | 2:ae85337d4ca7 | 77 | uint8_t buffer[20]; |
rba90 | 2:ae85337d4ca7 | 78 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 2:ae85337d4ca7 | 79 | txFrame.serialize(buffer); |
rba90 | 2:ae85337d4ca7 | 80 | |
rba90 | 2:ae85337d4ca7 | 81 | aloha.send(buffer, 20); |
rba90 | 2:ae85337d4ca7 | 82 | |
rba90 | 3:a4f6abad9378 | 83 | // print received message |
rba90 | 3:a4f6abad9378 | 84 | printf("-----------------------------------------\r\n"); |
rba90 | 3:a4f6abad9378 | 85 | printf(">Received Frame\r\n"); |
rba90 | 3:a4f6abad9378 | 86 | printf("> Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength()); |
rba90 | 3:a4f6abad9378 | 87 | printf("> SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress()); |
rba90 | 3:a4f6abad9378 | 88 | printf("> FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID()); |
rba90 | 3:a4f6abad9378 | 89 | for (uint8_t i = 0; i < frame->getPayloadLength(); i++) |
rba90 | 3:a4f6abad9378 | 90 | { |
rba90 | 3:a4f6abad9378 | 91 | printf("> Payload[%d]: 0x%x\r\n", i, frame->getPayload(i)); |
rba90 | 3:a4f6abad9378 | 92 | } |
rba90 | 3:a4f6abad9378 | 93 | printf("> CRC: 0x%x\r\n", frame->getCrc()); |
rba90 | 3:a4f6abad9378 | 94 | printf("-----------------------------------------\r\n"); |
rba90 | 0:1e42d79b42e1 | 95 | } |
rba90 | 0:1e42d79b42e1 | 96 | |
rba90 | 0:1e42d79b42e1 | 97 | int main() { |
rba90 | 0:1e42d79b42e1 | 98 | // initialize radio module |
rba90 | 5:445f35444a6a | 99 | aloha.boardInit(); |
rba90 | 0:1e42d79b42e1 | 100 | |
rba90 | 0:1e42d79b42e1 | 101 | // attach serial interrupt handler |
rba90 | 0:1e42d79b42e1 | 102 | pc.attach(&serialInterruptHandler); |
rba90 | 0:1e42d79b42e1 | 103 | |
rba90 | 0:1e42d79b42e1 | 104 | // register callback functions for SIP |
rba90 | 0:1e42d79b42e1 | 105 | SIP.registerCommand(0x00, toggleChecksum); |
rba90 | 0:1e42d79b42e1 | 106 | |
rba90 | 0:1e42d79b42e1 | 107 | // register callback functions for aloha transceiver |
rba90 | 2:ae85337d4ca7 | 108 | aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataEcho); |
rba90 | 0:1e42d79b42e1 | 109 | |
rba90 | 0:1e42d79b42e1 | 110 | while(1) { |
rba90 | 0:1e42d79b42e1 | 111 | SIP.poll(); |
rba90 | 0:1e42d79b42e1 | 112 | aloha.poll(); |
rba90 | 0:1e42d79b42e1 | 113 | |
rba90 | 0:1e42d79b42e1 | 114 | while (SerialOutputBuffer.getCounter() > 0) |
rba90 | 0:1e42d79b42e1 | 115 | { |
rba90 | 0:1e42d79b42e1 | 116 | uint8_t ch; |
rba90 | 0:1e42d79b42e1 | 117 | ch = SerialOutputBuffer.dequeue(); |
rba90 | 0:1e42d79b42e1 | 118 | pc.putc(ch); |
rba90 | 0:1e42d79b42e1 | 119 | } |
rba90 | 0:1e42d79b42e1 | 120 | } |
rba90 | 0:1e42d79b42e1 | 121 | } |