Wireless interface using LoRa technology
Dependencies: AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU
main.cpp@11:c7c0036efdbd, 2016-07-29 (annotated)
- Committer:
- rba90
- Date:
- Fri Jul 29 04:03:24 2016 +0000
- Revision:
- 11:c7c0036efdbd
- Parent:
- 10:7dcb951ecabd
- Child:
- 14:80cee3991860
update radio settings on the fly
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rba90 | 0:7d8e24cb62c1 | 1 | #include "mbed.h" |
rba90 | 0:7d8e24cb62c1 | 2 | #include "AlohaTransceiver.h" |
rba90 | 0:7d8e24cb62c1 | 3 | #include "buffer.h" |
rba90 | 0:7d8e24cb62c1 | 4 | #include "SerialInterfaceProtocol.h" |
rba90 | 0:7d8e24cb62c1 | 5 | #include "AlohaFrame.h" |
rba90 | 0:7d8e24cb62c1 | 6 | |
rba90 | 0:7d8e24cb62c1 | 7 | Serial pc(USBTX, USBRX); |
rba90 | 0:7d8e24cb62c1 | 8 | |
rba90 | 0:7d8e24cb62c1 | 9 | // sip uses two buffer queues |
rba90 | 0:7d8e24cb62c1 | 10 | CircularBuffer<uint8_t> SerialInputBuffer; |
rba90 | 0:7d8e24cb62c1 | 11 | CircularBuffer<uint8_t> SerialOutputBuffer; |
rba90 | 0:7d8e24cb62c1 | 12 | SerialInterfaceProtocol SIP(&SerialInputBuffer, &SerialOutputBuffer); |
rba90 | 0:7d8e24cb62c1 | 13 | |
rba90 | 0:7d8e24cb62c1 | 14 | // aloha transceiver |
rba90 | 9:86d95e095bb0 | 15 | AlohaTransceiver aloha(DEVICE_ID); |
rba90 | 0:7d8e24cb62c1 | 16 | AlohaFrame txFrame; |
rba90 | 1:101cf4fca4e5 | 17 | Timer timer; |
rba90 | 2:5a74ae8be594 | 18 | InterruptIn button(USER_BUTTON); |
rba90 | 0:7d8e24cb62c1 | 19 | |
rba90 | 0:7d8e24cb62c1 | 20 | void serialInterruptHandler() { |
rba90 | 0:7d8e24cb62c1 | 21 | // Note: you need to actually read from the serial to clear the RX interrupt |
rba90 | 0:7d8e24cb62c1 | 22 | int c = pc.getc(); |
rba90 | 0:7d8e24cb62c1 | 23 | |
rba90 | 0:7d8e24cb62c1 | 24 | // add to buffer |
rba90 | 0:7d8e24cb62c1 | 25 | if (SerialInputBuffer.isLocked()) |
rba90 | 0:7d8e24cb62c1 | 26 | { |
rba90 | 0:7d8e24cb62c1 | 27 | printf("Mutex Locked\r\n"); |
rba90 | 0:7d8e24cb62c1 | 28 | } |
rba90 | 0:7d8e24cb62c1 | 29 | else |
rba90 | 0:7d8e24cb62c1 | 30 | { |
rba90 | 0:7d8e24cb62c1 | 31 | SerialInputBuffer.enqueue((uint8_t) c); |
rba90 | 0:7d8e24cb62c1 | 32 | } |
rba90 | 0:7d8e24cb62c1 | 33 | } |
rba90 | 0:7d8e24cb62c1 | 34 | |
rba90 | 0:7d8e24cb62c1 | 35 | int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 0:7d8e24cb62c1 | 36 | { |
rba90 | 0:7d8e24cb62c1 | 37 | // one payload |
rba90 | 0:7d8e24cb62c1 | 38 | if (payload_length != 1) |
rba90 | 0:7d8e24cb62c1 | 39 | { |
rba90 | 0:7d8e24cb62c1 | 40 | sprintf((char *) response, "Wrong Payload Length\r\n"); |
rba90 | 0:7d8e24cb62c1 | 41 | *response_length = 22; |
rba90 | 0:7d8e24cb62c1 | 42 | return 1; |
rba90 | 0:7d8e24cb62c1 | 43 | } |
rba90 | 0:7d8e24cb62c1 | 44 | |
rba90 | 0:7d8e24cb62c1 | 45 | if ((bool) payload[0]) |
rba90 | 0:7d8e24cb62c1 | 46 | { |
rba90 | 0:7d8e24cb62c1 | 47 | SIP.enableChecksum(); |
rba90 | 0:7d8e24cb62c1 | 48 | } |
rba90 | 0:7d8e24cb62c1 | 49 | else |
rba90 | 0:7d8e24cb62c1 | 50 | { |
rba90 | 0:7d8e24cb62c1 | 51 | SIP.disableChecksum(); |
rba90 | 0:7d8e24cb62c1 | 52 | } |
rba90 | 0:7d8e24cb62c1 | 53 | |
rba90 | 0:7d8e24cb62c1 | 54 | return 0; |
rba90 | 0:7d8e24cb62c1 | 55 | } |
rba90 | 0:7d8e24cb62c1 | 56 | |
rba90 | 1:101cf4fca4e5 | 57 | int sendMessage(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 1:101cf4fca4e5 | 58 | { |
rba90 | 1:101cf4fca4e5 | 59 | static uint8_t seqid = 0; |
rba90 | 1:101cf4fca4e5 | 60 | |
rba90 | 1:101cf4fca4e5 | 61 | // prepare for the frame |
rba90 | 1:101cf4fca4e5 | 62 | txFrame.setType(AlohaFrame::Aloha_Data); |
rba90 | 1:101cf4fca4e5 | 63 | txFrame.setPayloadLength(0x0); |
rba90 | 9:86d95e095bb0 | 64 | txFrame.setSourceAddress(aloha.getDeviceId()); |
rba90 | 9:86d95e095bb0 | 65 | txFrame.setDestinationAddress(0x0); |
rba90 | 1:101cf4fca4e5 | 66 | txFrame.setFullMessageFlag(0x1); |
rba90 | 1:101cf4fca4e5 | 67 | txFrame.setSequenceID(seqid); |
rba90 | 1:101cf4fca4e5 | 68 | txFrame.generateCrc(); |
rba90 | 1:101cf4fca4e5 | 69 | |
rba90 | 1:101cf4fca4e5 | 70 | |
rba90 | 1:101cf4fca4e5 | 71 | uint8_t buffer[20]; |
rba90 | 1:101cf4fca4e5 | 72 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 1:101cf4fca4e5 | 73 | txFrame.serialize(buffer); |
rba90 | 1:101cf4fca4e5 | 74 | |
rba90 | 3:7bb50ee42cba | 75 | aloha.send(buffer, 20); |
rba90 | 1:101cf4fca4e5 | 76 | |
rba90 | 3:7bb50ee42cba | 77 | seqid += 1; |
rba90 | 1:101cf4fca4e5 | 78 | |
rba90 | 1:101cf4fca4e5 | 79 | // start the timer to measure round trip time |
rba90 | 1:101cf4fca4e5 | 80 | timer.reset(); |
rba90 | 1:101cf4fca4e5 | 81 | timer.start(); |
rba90 | 1:101cf4fca4e5 | 82 | |
rba90 | 3:7bb50ee42cba | 83 | return 0; |
rba90 | 3:7bb50ee42cba | 84 | } |
rba90 | 3:7bb50ee42cba | 85 | |
rba90 | 4:9151697dfa70 | 86 | /* |
rba90 | 4:9151697dfa70 | 87 | * Format: |
rba90 | 4:9151697dfa70 | 88 | * < :start flag |
rba90 | 4:9151697dfa70 | 89 | * 02 :command |
rba90 | 4:9151697dfa70 | 90 | * xx :length |
rba90 | 4:9151697dfa70 | 91 | * xx: :00: get, 01: set |
rba90 | 4:9151697dfa70 | 92 | * xx :index for parameters |
rba90 | 4:9151697dfa70 | 93 | * ... |
rba90 | 4:9151697dfa70 | 94 | * ff :checksum |
rba90 | 4:9151697dfa70 | 95 | * > :end flag |
rba90 | 4:9151697dfa70 | 96 | */ |
rba90 | 3:7bb50ee42cba | 97 | int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 3:7bb50ee42cba | 98 | { |
rba90 | 3:7bb50ee42cba | 99 | // read settings from radio |
rba90 | 3:7bb50ee42cba | 100 | #if USE_MODEM_LORA == 1 |
rba90 | 3:7bb50ee42cba | 101 | AlohaTransceiver::LoRaSettings_t *settings = aloha.getSettings(); |
rba90 | 3:7bb50ee42cba | 102 | #elif USE_MODEM_FSK == 1 |
rba90 | 3:7bb50ee42cba | 103 | AlohaTransceiver::FskSettings_t *settings = aloha.getSettings(); |
rba90 | 3:7bb50ee42cba | 104 | #else |
rba90 | 3:7bb50ee42cba | 105 | #error "Please define a modem in the compiler options." |
rba90 | 3:7bb50ee42cba | 106 | #endif |
rba90 | 3:7bb50ee42cba | 107 | |
rba90 | 3:7bb50ee42cba | 108 | if (payload_length < 2) |
rba90 | 3:7bb50ee42cba | 109 | { |
rba90 | 3:7bb50ee42cba | 110 | sprintf((char *) response, "Wrong Payload Length\r\n"); |
rba90 | 3:7bb50ee42cba | 111 | *response_length = 22; |
rba90 | 3:7bb50ee42cba | 112 | return 1; |
rba90 | 3:7bb50ee42cba | 113 | } |
rba90 | 3:7bb50ee42cba | 114 | |
rba90 | 3:7bb50ee42cba | 115 | // true is set, false is get |
rba90 | 3:7bb50ee42cba | 116 | bool isSet = (bool) payload[0]; |
rba90 | 3:7bb50ee42cba | 117 | uint8_t idx = payload[1]; |
rba90 | 3:7bb50ee42cba | 118 | |
rba90 | 3:7bb50ee42cba | 119 | switch(idx) |
rba90 | 3:7bb50ee42cba | 120 | { |
rba90 | 3:7bb50ee42cba | 121 | case 0x00: // Power |
rba90 | 3:7bb50ee42cba | 122 | { |
rba90 | 3:7bb50ee42cba | 123 | if (isSet) |
rba90 | 3:7bb50ee42cba | 124 | { |
rba90 | 3:7bb50ee42cba | 125 | int8_t Power = (int8_t) payload[2]; |
rba90 | 3:7bb50ee42cba | 126 | settings->Power = Power; |
rba90 | 3:7bb50ee42cba | 127 | |
rba90 | 3:7bb50ee42cba | 128 | return 0; |
rba90 | 3:7bb50ee42cba | 129 | } |
rba90 | 3:7bb50ee42cba | 130 | else |
rba90 | 3:7bb50ee42cba | 131 | { |
rba90 | 3:7bb50ee42cba | 132 | response[0] = (uint8_t) settings->Power; |
rba90 | 3:7bb50ee42cba | 133 | *response_length = 1; |
rba90 | 1:101cf4fca4e5 | 134 | |
rba90 | 3:7bb50ee42cba | 135 | return 0; |
rba90 | 3:7bb50ee42cba | 136 | } |
rba90 | 3:7bb50ee42cba | 137 | } |
rba90 | 3:7bb50ee42cba | 138 | |
rba90 | 3:7bb50ee42cba | 139 | case 0x01: // Bandwidth |
rba90 | 3:7bb50ee42cba | 140 | { |
rba90 | 3:7bb50ee42cba | 141 | if (isSet) |
rba90 | 3:7bb50ee42cba | 142 | { |
Razorfoot | 8:e30610bf6f79 | 143 | uint32_t Bandwidth = (payload[5]) | |
Razorfoot | 8:e30610bf6f79 | 144 | (payload[4] << 8) | |
Razorfoot | 8:e30610bf6f79 | 145 | (payload[3] << 16) | |
Razorfoot | 8:e30610bf6f79 | 146 | (payload[2] << 24); |
rba90 | 3:7bb50ee42cba | 147 | settings->Bandwidth = Bandwidth; |
rba90 | 3:7bb50ee42cba | 148 | |
rba90 | 3:7bb50ee42cba | 149 | return 0; |
rba90 | 3:7bb50ee42cba | 150 | } |
rba90 | 3:7bb50ee42cba | 151 | else |
rba90 | 3:7bb50ee42cba | 152 | { |
rba90 | 3:7bb50ee42cba | 153 | response[3] = (uint8_t) (settings->Bandwidth); |
rba90 | 3:7bb50ee42cba | 154 | response[2] = (uint8_t) (settings->Bandwidth >> 8); |
rba90 | 3:7bb50ee42cba | 155 | response[1] = (uint8_t) (settings->Bandwidth >> 16); |
rba90 | 3:7bb50ee42cba | 156 | response[0] = (uint8_t) (settings->Bandwidth >> 24); |
rba90 | 3:7bb50ee42cba | 157 | *response_length = 4; |
rba90 | 3:7bb50ee42cba | 158 | |
rba90 | 3:7bb50ee42cba | 159 | return 0; |
rba90 | 3:7bb50ee42cba | 160 | } |
rba90 | 3:7bb50ee42cba | 161 | |
rba90 | 3:7bb50ee42cba | 162 | } |
rba90 | 3:7bb50ee42cba | 163 | |
Razorfoot | 8:e30610bf6f79 | 164 | case 0x02: // Datarate, AKA Spreading Factor |
rba90 | 3:7bb50ee42cba | 165 | { |
rba90 | 3:7bb50ee42cba | 166 | if (isSet) |
rba90 | 3:7bb50ee42cba | 167 | { |
Razorfoot | 8:e30610bf6f79 | 168 | uint32_t Datarate = (payload[5]) | |
Razorfoot | 8:e30610bf6f79 | 169 | (payload[4] << 8) | |
Razorfoot | 8:e30610bf6f79 | 170 | (payload[3] << 16) | |
Razorfoot | 8:e30610bf6f79 | 171 | (payload[2] << 24); |
rba90 | 3:7bb50ee42cba | 172 | settings->Datarate = Datarate; |
rba90 | 3:7bb50ee42cba | 173 | |
rba90 | 3:7bb50ee42cba | 174 | return 0; |
rba90 | 3:7bb50ee42cba | 175 | } |
rba90 | 3:7bb50ee42cba | 176 | else |
rba90 | 3:7bb50ee42cba | 177 | { |
rba90 | 3:7bb50ee42cba | 178 | response[3] = (uint8_t) (settings->Datarate); |
rba90 | 3:7bb50ee42cba | 179 | response[2] = (uint8_t) (settings->Datarate >> 8); |
rba90 | 3:7bb50ee42cba | 180 | response[1] = (uint8_t) (settings->Datarate >> 16); |
rba90 | 3:7bb50ee42cba | 181 | response[0] = (uint8_t) (settings->Datarate >> 24); |
rba90 | 3:7bb50ee42cba | 182 | *response_length = 4; |
rba90 | 3:7bb50ee42cba | 183 | |
rba90 | 3:7bb50ee42cba | 184 | return 0; |
rba90 | 3:7bb50ee42cba | 185 | } |
rba90 | 3:7bb50ee42cba | 186 | } |
rba90 | 3:7bb50ee42cba | 187 | |
rba90 | 3:7bb50ee42cba | 188 | case 0x03: // Coderate |
rba90 | 3:7bb50ee42cba | 189 | { |
rba90 | 3:7bb50ee42cba | 190 | if (isSet) |
rba90 | 3:7bb50ee42cba | 191 | { |
Razorfoot | 8:e30610bf6f79 | 192 | uint8_t Coderate = payload[2]; |
rba90 | 3:7bb50ee42cba | 193 | settings->Coderate = Coderate; |
rba90 | 3:7bb50ee42cba | 194 | |
rba90 | 3:7bb50ee42cba | 195 | return 0; |
rba90 | 3:7bb50ee42cba | 196 | } |
rba90 | 3:7bb50ee42cba | 197 | else |
rba90 | 3:7bb50ee42cba | 198 | { |
rba90 | 3:7bb50ee42cba | 199 | response[0] = (uint8_t) settings->Coderate; |
rba90 | 3:7bb50ee42cba | 200 | *response_length = 1; |
rba90 | 3:7bb50ee42cba | 201 | |
rba90 | 3:7bb50ee42cba | 202 | return 0; |
rba90 | 3:7bb50ee42cba | 203 | } |
rba90 | 3:7bb50ee42cba | 204 | } |
rba90 | 3:7bb50ee42cba | 205 | |
Razorfoot | 8:e30610bf6f79 | 206 | case 0x04: //Preamble Length |
Razorfoot | 8:e30610bf6f79 | 207 | { |
Razorfoot | 8:e30610bf6f79 | 208 | if (isSet) |
Razorfoot | 8:e30610bf6f79 | 209 | { |
Razorfoot | 8:e30610bf6f79 | 210 | uint16_t PreambleLen = payload[3] | (payload[2] << 8); |
Razorfoot | 8:e30610bf6f79 | 211 | settings->PreambleLen = PreambleLen; |
Razorfoot | 8:e30610bf6f79 | 212 | return 0; |
Razorfoot | 8:e30610bf6f79 | 213 | } |
Razorfoot | 8:e30610bf6f79 | 214 | else |
Razorfoot | 8:e30610bf6f79 | 215 | { |
Razorfoot | 8:e30610bf6f79 | 216 | response[1] = (uint8_t) (settings->PreambleLen); |
Razorfoot | 8:e30610bf6f79 | 217 | response[0] = (uint8_t)(settings->PreambleLen >> 8); |
Razorfoot | 8:e30610bf6f79 | 218 | *response_length = 2; |
Razorfoot | 8:e30610bf6f79 | 219 | |
Razorfoot | 8:e30610bf6f79 | 220 | return 0; |
Razorfoot | 8:e30610bf6f79 | 221 | } |
Razorfoot | 8:e30610bf6f79 | 222 | } |
Razorfoot | 8:e30610bf6f79 | 223 | |
Razorfoot | 8:e30610bf6f79 | 224 | case 0x05: //Symbol Timeout |
Razorfoot | 8:e30610bf6f79 | 225 | { |
rba90 | 11:c7c0036efdbd | 226 | if (isSet) |
rba90 | 11:c7c0036efdbd | 227 | { |
rba90 | 11:c7c0036efdbd | 228 | uint16_t SymbolTimeout = payload[3] | (payload[2] << 8); |
rba90 | 11:c7c0036efdbd | 229 | settings->SymbolTimeout = SymbolTimeout; |
rba90 | 11:c7c0036efdbd | 230 | return 0; |
rba90 | 11:c7c0036efdbd | 231 | } |
rba90 | 11:c7c0036efdbd | 232 | else |
rba90 | 11:c7c0036efdbd | 233 | { |
rba90 | 11:c7c0036efdbd | 234 | response[1] = (uint8_t) (settings->SymbolTimeout); |
rba90 | 11:c7c0036efdbd | 235 | response[0] = (uint8_t) (settings->SymbolTimeout >> 8); |
rba90 | 11:c7c0036efdbd | 236 | *response_length = 2; |
rba90 | 11:c7c0036efdbd | 237 | |
rba90 | 11:c7c0036efdbd | 238 | return 0; |
rba90 | 11:c7c0036efdbd | 239 | } |
Razorfoot | 8:e30610bf6f79 | 240 | } |
Razorfoot | 8:e30610bf6f79 | 241 | |
Razorfoot | 8:e30610bf6f79 | 242 | case 0x06: //FixLen |
Razorfoot | 8:e30610bf6f79 | 243 | { |
rba90 | 11:c7c0036efdbd | 244 | if (isSet) |
rba90 | 11:c7c0036efdbd | 245 | { |
rba90 | 11:c7c0036efdbd | 246 | bool FixLen = payload[2]; |
rba90 | 11:c7c0036efdbd | 247 | settings->FixLen = FixLen; |
rba90 | 11:c7c0036efdbd | 248 | return 0; |
rba90 | 11:c7c0036efdbd | 249 | } |
rba90 | 11:c7c0036efdbd | 250 | else |
rba90 | 11:c7c0036efdbd | 251 | { |
rba90 | 11:c7c0036efdbd | 252 | response[0] = (bool) (settings->SymbolTimeout); |
rba90 | 11:c7c0036efdbd | 253 | *response_length = 1; |
rba90 | 11:c7c0036efdbd | 254 | |
rba90 | 11:c7c0036efdbd | 255 | return 0; |
rba90 | 11:c7c0036efdbd | 256 | } |
Razorfoot | 8:e30610bf6f79 | 257 | } |
Razorfoot | 8:e30610bf6f79 | 258 | |
Razorfoot | 8:e30610bf6f79 | 259 | case 0x07: //PayloadLen |
Razorfoot | 8:e30610bf6f79 | 260 | { |
rba90 | 11:c7c0036efdbd | 261 | if (isSet) |
rba90 | 11:c7c0036efdbd | 262 | { |
rba90 | 11:c7c0036efdbd | 263 | uint8_t PayloadLen = payload[2]; |
rba90 | 11:c7c0036efdbd | 264 | settings->PayloadLen = PayloadLen; |
rba90 | 11:c7c0036efdbd | 265 | return 0; |
rba90 | 11:c7c0036efdbd | 266 | } |
rba90 | 11:c7c0036efdbd | 267 | else |
rba90 | 11:c7c0036efdbd | 268 | { |
rba90 | 11:c7c0036efdbd | 269 | response[0] = (uint8_t) (settings->PayloadLen); |
rba90 | 11:c7c0036efdbd | 270 | |
rba90 | 11:c7c0036efdbd | 271 | return 0; |
rba90 | 11:c7c0036efdbd | 272 | } |
Razorfoot | 8:e30610bf6f79 | 273 | } |
Razorfoot | 8:e30610bf6f79 | 274 | |
Razorfoot | 8:e30610bf6f79 | 275 | case 0x08: //CrcOn |
Razorfoot | 8:e30610bf6f79 | 276 | { |
rba90 | 11:c7c0036efdbd | 277 | if (isSet) { |
rba90 | 11:c7c0036efdbd | 278 | bool CrcOn = payload[2]; |
rba90 | 11:c7c0036efdbd | 279 | settings->CrcOn = CrcOn; |
rba90 | 11:c7c0036efdbd | 280 | return 0; |
rba90 | 11:c7c0036efdbd | 281 | } |
rba90 | 11:c7c0036efdbd | 282 | else |
rba90 | 11:c7c0036efdbd | 283 | { |
rba90 | 11:c7c0036efdbd | 284 | response[0] = (bool) (settings->CrcOn); |
rba90 | 11:c7c0036efdbd | 285 | |
rba90 | 11:c7c0036efdbd | 286 | return 0; |
rba90 | 11:c7c0036efdbd | 287 | } |
Razorfoot | 8:e30610bf6f79 | 288 | } |
Razorfoot | 8:e30610bf6f79 | 289 | |
Razorfoot | 8:e30610bf6f79 | 290 | case 0x09: //FreqHopOn |
Razorfoot | 8:e30610bf6f79 | 291 | { |
rba90 | 11:c7c0036efdbd | 292 | if (isSet) { |
rba90 | 11:c7c0036efdbd | 293 | bool FreqHopOn = payload[2]; |
rba90 | 11:c7c0036efdbd | 294 | settings->FreqHopOn = FreqHopOn; |
rba90 | 11:c7c0036efdbd | 295 | return 0; |
rba90 | 11:c7c0036efdbd | 296 | } |
rba90 | 11:c7c0036efdbd | 297 | else |
rba90 | 11:c7c0036efdbd | 298 | { |
rba90 | 11:c7c0036efdbd | 299 | response[0] = (bool) (settings->FreqHopOn); |
rba90 | 11:c7c0036efdbd | 300 | |
rba90 | 11:c7c0036efdbd | 301 | return 0; |
rba90 | 11:c7c0036efdbd | 302 | } |
Razorfoot | 8:e30610bf6f79 | 303 | } |
Razorfoot | 8:e30610bf6f79 | 304 | |
Razorfoot | 8:e30610bf6f79 | 305 | case 0x0A: //HopPeriod |
Razorfoot | 8:e30610bf6f79 | 306 | { |
rba90 | 11:c7c0036efdbd | 307 | if (isSet) { |
rba90 | 11:c7c0036efdbd | 308 | uint8_t HopPeriod = payload[2]; |
rba90 | 11:c7c0036efdbd | 309 | settings->HopPeriod = HopPeriod; |
rba90 | 11:c7c0036efdbd | 310 | return 0; |
rba90 | 11:c7c0036efdbd | 311 | } |
rba90 | 11:c7c0036efdbd | 312 | else |
rba90 | 11:c7c0036efdbd | 313 | { |
rba90 | 11:c7c0036efdbd | 314 | response[0] = (uint8_t) (settings->HopPeriod); |
rba90 | 11:c7c0036efdbd | 315 | return 0; |
rba90 | 11:c7c0036efdbd | 316 | } |
Razorfoot | 8:e30610bf6f79 | 317 | } |
Razorfoot | 8:e30610bf6f79 | 318 | |
Razorfoot | 8:e30610bf6f79 | 319 | case 0x0B: //IqInverted |
Razorfoot | 8:e30610bf6f79 | 320 | { |
rba90 | 11:c7c0036efdbd | 321 | if (isSet) { |
rba90 | 11:c7c0036efdbd | 322 | bool IqInverted = payload[2]; |
rba90 | 11:c7c0036efdbd | 323 | settings->IqInverted = IqInverted; |
rba90 | 11:c7c0036efdbd | 324 | return 0; |
rba90 | 11:c7c0036efdbd | 325 | } |
rba90 | 11:c7c0036efdbd | 326 | else |
rba90 | 11:c7c0036efdbd | 327 | { |
rba90 | 11:c7c0036efdbd | 328 | response[0] = (bool) (settings->IqInverted); |
rba90 | 11:c7c0036efdbd | 329 | return 0; |
rba90 | 11:c7c0036efdbd | 330 | } |
Razorfoot | 8:e30610bf6f79 | 331 | } |
Razorfoot | 8:e30610bf6f79 | 332 | |
Razorfoot | 8:e30610bf6f79 | 333 | case 0x0C: //RxContinuous |
Razorfoot | 8:e30610bf6f79 | 334 | { |
rba90 | 11:c7c0036efdbd | 335 | if(isSet) |
rba90 | 11:c7c0036efdbd | 336 | { |
rba90 | 11:c7c0036efdbd | 337 | bool RxContinuous = payload[2]; |
rba90 | 11:c7c0036efdbd | 338 | settings->RxContinuous = RxContinuous; |
rba90 | 11:c7c0036efdbd | 339 | return 0; |
rba90 | 11:c7c0036efdbd | 340 | } |
rba90 | 11:c7c0036efdbd | 341 | else |
rba90 | 11:c7c0036efdbd | 342 | { |
rba90 | 11:c7c0036efdbd | 343 | response[0] = (bool) (settings->RxContinuous); |
rba90 | 11:c7c0036efdbd | 344 | return 0; |
rba90 | 11:c7c0036efdbd | 345 | } |
Razorfoot | 8:e30610bf6f79 | 346 | } |
Razorfoot | 8:e30610bf6f79 | 347 | |
Razorfoot | 8:e30610bf6f79 | 348 | case 0x0D: //TxTimeout |
Razorfoot | 8:e30610bf6f79 | 349 | { |
rba90 | 11:c7c0036efdbd | 350 | if (isSet) |
rba90 | 11:c7c0036efdbd | 351 | { |
rba90 | 11:c7c0036efdbd | 352 | uint32_t TxTimeout = (payload[5]) | |
rba90 | 11:c7c0036efdbd | 353 | (payload[4] << 8) | |
rba90 | 11:c7c0036efdbd | 354 | (payload[3] << 16) | |
rba90 | 11:c7c0036efdbd | 355 | (payload[2] << 24); |
rba90 | 11:c7c0036efdbd | 356 | settings->TxTimeout = TxTimeout; |
rba90 | 11:c7c0036efdbd | 357 | return 0; |
rba90 | 11:c7c0036efdbd | 358 | } |
rba90 | 11:c7c0036efdbd | 359 | else |
rba90 | 11:c7c0036efdbd | 360 | { |
rba90 | 11:c7c0036efdbd | 361 | response[3] = (uint8_t) (settings->TxTimeout); |
rba90 | 11:c7c0036efdbd | 362 | response[2] = (uint8_t) (settings->TxTimeout >> 8); |
rba90 | 11:c7c0036efdbd | 363 | response[1] = (uint8_t) (settings->TxTimeout >> 16); |
rba90 | 11:c7c0036efdbd | 364 | response[0] = (uint8_t) (settings->TxTimeout >> 24); |
rba90 | 11:c7c0036efdbd | 365 | *response_length = 4; |
rba90 | 11:c7c0036efdbd | 366 | |
rba90 | 11:c7c0036efdbd | 367 | return 0; |
rba90 | 11:c7c0036efdbd | 368 | } |
Razorfoot | 8:e30610bf6f79 | 369 | } |
Razorfoot | 8:e30610bf6f79 | 370 | |
Razorfoot | 8:e30610bf6f79 | 371 | |
rba90 | 3:7bb50ee42cba | 372 | default: |
rba90 | 3:7bb50ee42cba | 373 | { |
rba90 | 3:7bb50ee42cba | 374 | break; |
rba90 | 3:7bb50ee42cba | 375 | } |
Razorfoot | 8:e30610bf6f79 | 376 | |
Razorfoot | 8:e30610bf6f79 | 377 | //case |
rba90 | 3:7bb50ee42cba | 378 | } |
rba90 | 3:7bb50ee42cba | 379 | |
rba90 | 3:7bb50ee42cba | 380 | |
rba90 | 3:7bb50ee42cba | 381 | return 0; |
rba90 | 1:101cf4fca4e5 | 382 | } |
rba90 | 1:101cf4fca4e5 | 383 | |
rba90 | 11:c7c0036efdbd | 384 | int radioUpdateSettings(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 11:c7c0036efdbd | 385 | { |
rba90 | 11:c7c0036efdbd | 386 | aloha.updateSettings(); |
rba90 | 11:c7c0036efdbd | 387 | |
rba90 | 11:c7c0036efdbd | 388 | return 0; |
rba90 | 11:c7c0036efdbd | 389 | } |
rba90 | 11:c7c0036efdbd | 390 | |
rba90 | 1:101cf4fca4e5 | 391 | void AlohaDataHandler(AlohaFrame *frame) |
rba90 | 1:101cf4fca4e5 | 392 | { |
rba90 | 1:101cf4fca4e5 | 393 | // stop the timer to measure round trip time |
rba90 | 1:101cf4fca4e5 | 394 | timer.stop(); |
rba90 | 1:101cf4fca4e5 | 395 | |
rba90 | 2:5a74ae8be594 | 396 | printf("-----------------------------------------\r\n"); |
rba90 | 2:5a74ae8be594 | 397 | printf(">Received Frame\r\n"); |
rba90 | 2:5a74ae8be594 | 398 | printf("> Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength()); |
rba90 | 2:5a74ae8be594 | 399 | printf("> SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress()); |
rba90 | 2:5a74ae8be594 | 400 | printf("> FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID()); |
rba90 | 0:7d8e24cb62c1 | 401 | for (uint8_t i = 0; i < frame->getPayloadLength(); i++) |
rba90 | 0:7d8e24cb62c1 | 402 | { |
rba90 | 2:5a74ae8be594 | 403 | printf("> Payload[%d]: 0x%x\r\n", i, frame->getPayload(i)); |
rba90 | 0:7d8e24cb62c1 | 404 | } |
rba90 | 2:5a74ae8be594 | 405 | printf("> CRC: 0x%x\r\n", frame->getCrc()); |
rba90 | 0:7d8e24cb62c1 | 406 | |
rba90 | 1:101cf4fca4e5 | 407 | // decode payload (range test only) |
rba90 | 1:101cf4fca4e5 | 408 | int8_t snr = (int8_t) frame->getPayload(0); |
rba90 | 1:101cf4fca4e5 | 409 | |
rba90 | 1:101cf4fca4e5 | 410 | uint16_t rssi_h = frame->getPayload(1); |
rba90 | 1:101cf4fca4e5 | 411 | uint8_t rssi_l = frame->getPayload(2); |
rba90 | 1:101cf4fca4e5 | 412 | int16_t rssi = (int16_t) (rssi_h << 8 | rssi_l); |
rba90 | 0:7d8e24cb62c1 | 413 | |
rba90 | 0:7d8e24cb62c1 | 414 | |
rba90 | 3:7bb50ee42cba | 415 | printf(">About this transmission:\r\n"); |
rba90 | 3:7bb50ee42cba | 416 | printf("> Base Station:: Rssi: %d, Snr: %d\r\n", rssi, snr); |
rba90 | 3:7bb50ee42cba | 417 | printf("> Terminal:: Rssi: %d, Snr: %d\r\n", aloha.getRssi(), aloha.getSnr()); |
rba90 | 2:5a74ae8be594 | 418 | printf("> Round trip time: %d ms\r\n", timer.read_ms()); |
rba90 | 2:5a74ae8be594 | 419 | printf("-----------------------------------------\r\n"); |
rba90 | 0:7d8e24cb62c1 | 420 | } |
rba90 | 0:7d8e24cb62c1 | 421 | |
Razorfoot | 10:7dcb951ecabd | 422 | void enqueueString(char* s, int len) { |
Razorfoot | 10:7dcb951ecabd | 423 | for (int i = 0; i < len; i++) { |
Razorfoot | 10:7dcb951ecabd | 424 | SerialInputBuffer.enqueue((uint8_t) s[i]); |
Razorfoot | 10:7dcb951ecabd | 425 | } |
Razorfoot | 10:7dcb951ecabd | 426 | } |
Razorfoot | 10:7dcb951ecabd | 427 | |
rba90 | 2:5a74ae8be594 | 428 | void automaticPacketTransmit() |
rba90 | 2:5a74ae8be594 | 429 | { |
Razorfoot | 10:7dcb951ecabd | 430 | /*SerialInputBuffer.enqueue((uint8_t) '<'); |
rba90 | 2:5a74ae8be594 | 431 | SerialInputBuffer.enqueue((uint8_t) '0'); |
rba90 | 2:5a74ae8be594 | 432 | SerialInputBuffer.enqueue((uint8_t) '1'); |
rba90 | 2:5a74ae8be594 | 433 | SerialInputBuffer.enqueue((uint8_t) '0'); |
rba90 | 2:5a74ae8be594 | 434 | SerialInputBuffer.enqueue((uint8_t) '0'); |
rba90 | 2:5a74ae8be594 | 435 | SerialInputBuffer.enqueue((uint8_t) 'f'); |
rba90 | 2:5a74ae8be594 | 436 | SerialInputBuffer.enqueue((uint8_t) 'f'); |
Razorfoot | 10:7dcb951ecabd | 437 | SerialInputBuffer.enqueue((uint8_t) '>');*/ |
Razorfoot | 10:7dcb951ecabd | 438 | enqueueString("<0100ff>", 8); |
rba90 | 2:5a74ae8be594 | 439 | } |
rba90 | 2:5a74ae8be594 | 440 | |
rba90 | 2:5a74ae8be594 | 441 | |
Razorfoot | 10:7dcb951ecabd | 442 | |
rba90 | 0:7d8e24cb62c1 | 443 | int main() { |
rba90 | 0:7d8e24cb62c1 | 444 | // initialize radio module |
rba90 | 3:7bb50ee42cba | 445 | aloha.boardInit(); |
rba90 | 3:7bb50ee42cba | 446 | aloha.updateSettings(); |
rba90 | 3:7bb50ee42cba | 447 | aloha.enable(); |
rba90 | 0:7d8e24cb62c1 | 448 | |
rba90 | 0:7d8e24cb62c1 | 449 | // attach serial interrupt handler |
rba90 | 0:7d8e24cb62c1 | 450 | pc.attach(&serialInterruptHandler); |
rba90 | 0:7d8e24cb62c1 | 451 | |
rba90 | 0:7d8e24cb62c1 | 452 | // register callback functions for SIP |
rba90 | 0:7d8e24cb62c1 | 453 | SIP.registerCommand(0x00, toggleChecksum); |
rba90 | 1:101cf4fca4e5 | 454 | SIP.registerCommand(0x01, sendMessage); |
rba90 | 3:7bb50ee42cba | 455 | SIP.registerCommand(0x02, configureRadio); |
rba90 | 11:c7c0036efdbd | 456 | SIP.registerCommand(0x03, radioUpdateSettings); |
rba90 | 0:7d8e24cb62c1 | 457 | |
rba90 | 0:7d8e24cb62c1 | 458 | // register callback functions for aloha transceiver |
rba90 | 1:101cf4fca4e5 | 459 | aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataHandler); |
rba90 | 2:5a74ae8be594 | 460 | |
rba90 | 2:5a74ae8be594 | 461 | // configure button interrupt |
rba90 | 2:5a74ae8be594 | 462 | button.fall(automaticPacketTransmit); |
rba90 | 0:7d8e24cb62c1 | 463 | |
rba90 | 0:7d8e24cb62c1 | 464 | while(1) { |
rba90 | 0:7d8e24cb62c1 | 465 | SIP.poll(); |
rba90 | 0:7d8e24cb62c1 | 466 | aloha.poll(); |
rba90 | 0:7d8e24cb62c1 | 467 | |
rba90 | 0:7d8e24cb62c1 | 468 | while (SerialOutputBuffer.getCounter() > 0) |
rba90 | 0:7d8e24cb62c1 | 469 | { |
rba90 | 0:7d8e24cb62c1 | 470 | uint8_t ch; |
rba90 | 0:7d8e24cb62c1 | 471 | ch = SerialOutputBuffer.dequeue(); |
rba90 | 0:7d8e24cb62c1 | 472 | pc.putc(ch); |
rba90 | 0:7d8e24cb62c1 | 473 | } |
rba90 | 0:7d8e24cb62c1 | 474 | } |
rba90 | 0:7d8e24cb62c1 | 475 | } |