Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU
main.cpp@2:ae85337d4ca7, 2016-07-14 (annotated)
- Committer:
- rba90
- Date:
- Thu Jul 14 11:48:52 2016 +0000
- Revision:
- 2:ae85337d4ca7
- Parent:
- 0:1e42d79b42e1
- Child:
- 3:a4f6abad9378
response the client with rssi and snr information
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 | 2:ae85337d4ca7 | 56 | { |
| rba90 | 2:ae85337d4ca7 | 57 | // print received message |
| rba90 | 0:1e42d79b42e1 | 58 | printf("AlohaDataHandler invoked\r\n"); |
| rba90 | 0:1e42d79b42e1 | 59 | printf(" Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength()); |
| rba90 | 0:1e42d79b42e1 | 60 | printf(" SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress()); |
| rba90 | 0:1e42d79b42e1 | 61 | printf(" FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID()); |
| rba90 | 0:1e42d79b42e1 | 62 | for (uint8_t i = 0; i < frame->getPayloadLength(); i++) |
| rba90 | 0:1e42d79b42e1 | 63 | { |
| rba90 | 0:1e42d79b42e1 | 64 | printf(" Payload[%d]: 0x%x\r\n", i, frame->getPayload(i)); |
| rba90 | 0:1e42d79b42e1 | 65 | } |
| rba90 | 0:1e42d79b42e1 | 66 | printf(" CRC: 0x%x\r\n", frame->getCrc()); |
| rba90 | 0:1e42d79b42e1 | 67 | |
| rba90 | 2:ae85337d4ca7 | 68 | // get rssi and snr |
| rba90 | 2:ae85337d4ca7 | 69 | uint16_t rssi = aloha.getRssi(); |
| rba90 | 2:ae85337d4ca7 | 70 | uint8_t snr = aloha.getSnr(); |
| rba90 | 2:ae85337d4ca7 | 71 | |
| rba90 | 2:ae85337d4ca7 | 72 | uint8_t rssi_h = (rssi & 0xff00) >> 8; |
| rba90 | 2:ae85337d4ca7 | 73 | uint8_t rssi_l = rssi & 0xff; |
| rba90 | 2:ae85337d4ca7 | 74 | |
| rba90 | 2:ae85337d4ca7 | 75 | // Transmit RSSI and SNR back to sender |
| rba90 | 2:ae85337d4ca7 | 76 | txFrame.setType(AlohaFrame::Aloha_Data); |
| rba90 | 2:ae85337d4ca7 | 77 | txFrame.setPayloadLength(3); |
| rba90 | 2:ae85337d4ca7 | 78 | txFrame.setSourceAddress(frame->getDestinationAddress()); |
| rba90 | 2:ae85337d4ca7 | 79 | txFrame.setDestinationAddress(frame->getSourceAddress()); |
| rba90 | 2:ae85337d4ca7 | 80 | txFrame.setFullMessageFlag(frame->getFullMessageFlag()); |
| rba90 | 2:ae85337d4ca7 | 81 | txFrame.setSequenceID(frame->getSequenceID() + 1); |
| rba90 | 2:ae85337d4ca7 | 82 | txFrame.setPayload(snr, 0); |
| rba90 | 2:ae85337d4ca7 | 83 | txFrame.setPayload(rssi_h, 1); |
| rba90 | 2:ae85337d4ca7 | 84 | txFrame.setPayload(rssi_l, 2); |
| rba90 | 2:ae85337d4ca7 | 85 | txFrame.generateCrc(); |
| rba90 | 2:ae85337d4ca7 | 86 | |
| rba90 | 2:ae85337d4ca7 | 87 | |
| rba90 | 2:ae85337d4ca7 | 88 | uint8_t buffer[20]; |
| rba90 | 2:ae85337d4ca7 | 89 | memset(buffer, 0x0, sizeof(buffer)); |
| rba90 | 2:ae85337d4ca7 | 90 | txFrame.serialize(buffer); |
| rba90 | 2:ae85337d4ca7 | 91 | |
| rba90 | 2:ae85337d4ca7 | 92 | aloha.send(buffer, 20); |
| rba90 | 2:ae85337d4ca7 | 93 | |
| rba90 | 0:1e42d79b42e1 | 94 | } |
| rba90 | 0:1e42d79b42e1 | 95 | |
| rba90 | 0:1e42d79b42e1 | 96 | int main() { |
| rba90 | 0:1e42d79b42e1 | 97 | // initialize radio module |
| rba90 | 0:1e42d79b42e1 | 98 | aloha.BoardInit(); |
| rba90 | 0:1e42d79b42e1 | 99 | |
| rba90 | 0:1e42d79b42e1 | 100 | // attach serial interrupt handler |
| rba90 | 0:1e42d79b42e1 | 101 | pc.attach(&serialInterruptHandler); |
| rba90 | 0:1e42d79b42e1 | 102 | |
| rba90 | 0:1e42d79b42e1 | 103 | // register callback functions for SIP |
| rba90 | 0:1e42d79b42e1 | 104 | SIP.registerCommand(0x00, toggleChecksum); |
| rba90 | 0:1e42d79b42e1 | 105 | |
| rba90 | 0:1e42d79b42e1 | 106 | // register callback functions for aloha transceiver |
| rba90 | 2:ae85337d4ca7 | 107 | aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataEcho); |
| rba90 | 0:1e42d79b42e1 | 108 | |
| rba90 | 0:1e42d79b42e1 | 109 | while(1) { |
| rba90 | 0:1e42d79b42e1 | 110 | SIP.poll(); |
| rba90 | 0:1e42d79b42e1 | 111 | aloha.poll(); |
| rba90 | 0:1e42d79b42e1 | 112 | |
| rba90 | 0:1e42d79b42e1 | 113 | while (SerialOutputBuffer.getCounter() > 0) |
| rba90 | 0:1e42d79b42e1 | 114 | { |
| rba90 | 0:1e42d79b42e1 | 115 | uint8_t ch; |
| rba90 | 0:1e42d79b42e1 | 116 | ch = SerialOutputBuffer.dequeue(); |
| rba90 | 0:1e42d79b42e1 | 117 | pc.putc(ch); |
| rba90 | 0:1e42d79b42e1 | 118 | } |
| rba90 | 0:1e42d79b42e1 | 119 | } |
| rba90 | 0:1e42d79b42e1 | 120 | } |