![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Wireless interface using LoRa technology
Dependencies: AlohaTransceiver RingBuffer SX1276Lib SerialInterfaceProtocol mbed L3PDU
main.cpp@12:2ead6bfd9f2a, 2016-08-08 (annotated)
- Committer:
- rba90
- Date:
- Mon Aug 08 09:38:12 2016 +0000
- Revision:
- 12:2ead6bfd9f2a
- Parent:
- 11:9590f3c9d915
- Child:
- 14:25e836a5a7bf
update submodule
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 | 8:332099ece6e9 | 15 | AlohaTransceiver aloha(DEVICE_ID); |
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 | 9:e13e02aa4a2f | 55 | /* |
rba90 | 9:e13e02aa4a2f | 56 | * Format: |
rba90 | 9:e13e02aa4a2f | 57 | * < :start flag |
rba90 | 9:e13e02aa4a2f | 58 | * 02 :command |
rba90 | 9:e13e02aa4a2f | 59 | * xx :length |
rba90 | 9:e13e02aa4a2f | 60 | * xx: :00: get, 01: set |
rba90 | 9:e13e02aa4a2f | 61 | * xx :index for parameters |
rba90 | 9:e13e02aa4a2f | 62 | * ... |
rba90 | 9:e13e02aa4a2f | 63 | * ff :checksum |
rba90 | 9:e13e02aa4a2f | 64 | * > :end flag |
rba90 | 9:e13e02aa4a2f | 65 | */ |
rba90 | 9:e13e02aa4a2f | 66 | int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 9:e13e02aa4a2f | 67 | { |
rba90 | 9:e13e02aa4a2f | 68 | // read settings from radio |
rba90 | 9:e13e02aa4a2f | 69 | #if USE_MODEM_LORA == 1 |
rba90 | 9:e13e02aa4a2f | 70 | AlohaTransceiver::LoRaSettings_t *settings = aloha.getSettings(); |
rba90 | 9:e13e02aa4a2f | 71 | #elif USE_MODEM_FSK == 1 |
rba90 | 9:e13e02aa4a2f | 72 | AlohaTransceiver::FskSettings_t *settings = aloha.getSettings(); |
rba90 | 9:e13e02aa4a2f | 73 | #else |
rba90 | 9:e13e02aa4a2f | 74 | #error "Please define a modem in the compiler options." |
rba90 | 9:e13e02aa4a2f | 75 | #endif |
rba90 | 9:e13e02aa4a2f | 76 | |
rba90 | 9:e13e02aa4a2f | 77 | if (payload_length < 2) |
rba90 | 9:e13e02aa4a2f | 78 | { |
rba90 | 9:e13e02aa4a2f | 79 | sprintf((char *) response, "Wrong Payload Length\r\n"); |
rba90 | 9:e13e02aa4a2f | 80 | *response_length = 22; |
rba90 | 9:e13e02aa4a2f | 81 | return 1; |
rba90 | 9:e13e02aa4a2f | 82 | } |
rba90 | 9:e13e02aa4a2f | 83 | |
rba90 | 9:e13e02aa4a2f | 84 | // true is set, false is get |
rba90 | 9:e13e02aa4a2f | 85 | bool isSet = (bool) payload[0]; |
rba90 | 9:e13e02aa4a2f | 86 | uint8_t idx = payload[1]; |
rba90 | 9:e13e02aa4a2f | 87 | |
rba90 | 9:e13e02aa4a2f | 88 | switch(idx) |
rba90 | 9:e13e02aa4a2f | 89 | { |
rba90 | 9:e13e02aa4a2f | 90 | case 0x00: // Power |
rba90 | 9:e13e02aa4a2f | 91 | { |
rba90 | 9:e13e02aa4a2f | 92 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 93 | { |
rba90 | 9:e13e02aa4a2f | 94 | int8_t Power = (int8_t) payload[2]; |
rba90 | 9:e13e02aa4a2f | 95 | settings->Power = Power; |
rba90 | 9:e13e02aa4a2f | 96 | |
rba90 | 9:e13e02aa4a2f | 97 | return 0; |
rba90 | 9:e13e02aa4a2f | 98 | } |
rba90 | 9:e13e02aa4a2f | 99 | else |
rba90 | 9:e13e02aa4a2f | 100 | { |
rba90 | 9:e13e02aa4a2f | 101 | response[0] = (uint8_t) settings->Power; |
rba90 | 9:e13e02aa4a2f | 102 | *response_length = 1; |
rba90 | 9:e13e02aa4a2f | 103 | |
rba90 | 9:e13e02aa4a2f | 104 | return 0; |
rba90 | 9:e13e02aa4a2f | 105 | } |
rba90 | 9:e13e02aa4a2f | 106 | } |
rba90 | 9:e13e02aa4a2f | 107 | |
rba90 | 9:e13e02aa4a2f | 108 | case 0x01: // Bandwidth |
rba90 | 9:e13e02aa4a2f | 109 | { |
rba90 | 9:e13e02aa4a2f | 110 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 111 | { |
rba90 | 9:e13e02aa4a2f | 112 | uint32_t Bandwidth = (payload[5]) | |
rba90 | 9:e13e02aa4a2f | 113 | (payload[4] << 8) | |
rba90 | 9:e13e02aa4a2f | 114 | (payload[3] << 16) | |
rba90 | 9:e13e02aa4a2f | 115 | (payload[2] << 24); |
rba90 | 9:e13e02aa4a2f | 116 | settings->Bandwidth = Bandwidth; |
rba90 | 9:e13e02aa4a2f | 117 | |
rba90 | 9:e13e02aa4a2f | 118 | return 0; |
rba90 | 9:e13e02aa4a2f | 119 | } |
rba90 | 9:e13e02aa4a2f | 120 | else |
rba90 | 9:e13e02aa4a2f | 121 | { |
rba90 | 9:e13e02aa4a2f | 122 | response[3] = (uint8_t) (settings->Bandwidth); |
rba90 | 9:e13e02aa4a2f | 123 | response[2] = (uint8_t) (settings->Bandwidth >> 8); |
rba90 | 9:e13e02aa4a2f | 124 | response[1] = (uint8_t) (settings->Bandwidth >> 16); |
rba90 | 9:e13e02aa4a2f | 125 | response[0] = (uint8_t) (settings->Bandwidth >> 24); |
rba90 | 9:e13e02aa4a2f | 126 | *response_length = 4; |
rba90 | 9:e13e02aa4a2f | 127 | |
rba90 | 9:e13e02aa4a2f | 128 | return 0; |
rba90 | 9:e13e02aa4a2f | 129 | } |
rba90 | 9:e13e02aa4a2f | 130 | |
rba90 | 9:e13e02aa4a2f | 131 | } |
rba90 | 9:e13e02aa4a2f | 132 | |
rba90 | 9:e13e02aa4a2f | 133 | case 0x02: // Datarate, AKA Spreading Factor |
rba90 | 9:e13e02aa4a2f | 134 | { |
rba90 | 9:e13e02aa4a2f | 135 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 136 | { |
rba90 | 9:e13e02aa4a2f | 137 | uint32_t Datarate = (payload[5]) | |
rba90 | 9:e13e02aa4a2f | 138 | (payload[4] << 8) | |
rba90 | 9:e13e02aa4a2f | 139 | (payload[3] << 16) | |
rba90 | 9:e13e02aa4a2f | 140 | (payload[2] << 24); |
rba90 | 9:e13e02aa4a2f | 141 | settings->Datarate = Datarate; |
rba90 | 9:e13e02aa4a2f | 142 | |
rba90 | 9:e13e02aa4a2f | 143 | return 0; |
rba90 | 9:e13e02aa4a2f | 144 | } |
rba90 | 9:e13e02aa4a2f | 145 | else |
rba90 | 9:e13e02aa4a2f | 146 | { |
rba90 | 9:e13e02aa4a2f | 147 | response[3] = (uint8_t) (settings->Datarate); |
rba90 | 9:e13e02aa4a2f | 148 | response[2] = (uint8_t) (settings->Datarate >> 8); |
rba90 | 9:e13e02aa4a2f | 149 | response[1] = (uint8_t) (settings->Datarate >> 16); |
rba90 | 9:e13e02aa4a2f | 150 | response[0] = (uint8_t) (settings->Datarate >> 24); |
rba90 | 9:e13e02aa4a2f | 151 | *response_length = 4; |
rba90 | 9:e13e02aa4a2f | 152 | |
rba90 | 9:e13e02aa4a2f | 153 | return 0; |
rba90 | 9:e13e02aa4a2f | 154 | } |
rba90 | 9:e13e02aa4a2f | 155 | } |
rba90 | 9:e13e02aa4a2f | 156 | |
rba90 | 9:e13e02aa4a2f | 157 | case 0x03: // Coderate |
rba90 | 9:e13e02aa4a2f | 158 | { |
rba90 | 9:e13e02aa4a2f | 159 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 160 | { |
rba90 | 9:e13e02aa4a2f | 161 | uint8_t Coderate = payload[2]; |
rba90 | 9:e13e02aa4a2f | 162 | settings->Coderate = Coderate; |
rba90 | 9:e13e02aa4a2f | 163 | |
rba90 | 9:e13e02aa4a2f | 164 | return 0; |
rba90 | 9:e13e02aa4a2f | 165 | } |
rba90 | 9:e13e02aa4a2f | 166 | else |
rba90 | 9:e13e02aa4a2f | 167 | { |
rba90 | 9:e13e02aa4a2f | 168 | response[0] = (uint8_t) settings->Coderate; |
rba90 | 9:e13e02aa4a2f | 169 | *response_length = 1; |
rba90 | 9:e13e02aa4a2f | 170 | |
rba90 | 9:e13e02aa4a2f | 171 | return 0; |
rba90 | 9:e13e02aa4a2f | 172 | } |
rba90 | 9:e13e02aa4a2f | 173 | } |
rba90 | 9:e13e02aa4a2f | 174 | |
rba90 | 9:e13e02aa4a2f | 175 | case 0x04: //Preamble Length |
rba90 | 9:e13e02aa4a2f | 176 | { |
rba90 | 9:e13e02aa4a2f | 177 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 178 | { |
rba90 | 9:e13e02aa4a2f | 179 | uint16_t PreambleLen = payload[3] | (payload[2] << 8); |
rba90 | 9:e13e02aa4a2f | 180 | settings->PreambleLen = PreambleLen; |
rba90 | 9:e13e02aa4a2f | 181 | return 0; |
rba90 | 9:e13e02aa4a2f | 182 | } |
rba90 | 9:e13e02aa4a2f | 183 | else |
rba90 | 9:e13e02aa4a2f | 184 | { |
rba90 | 9:e13e02aa4a2f | 185 | response[1] = (uint8_t) (settings->PreambleLen); |
rba90 | 9:e13e02aa4a2f | 186 | response[0] = (uint8_t)(settings->PreambleLen >> 8); |
rba90 | 9:e13e02aa4a2f | 187 | *response_length = 2; |
rba90 | 9:e13e02aa4a2f | 188 | |
rba90 | 9:e13e02aa4a2f | 189 | return 0; |
rba90 | 9:e13e02aa4a2f | 190 | } |
rba90 | 9:e13e02aa4a2f | 191 | } |
rba90 | 9:e13e02aa4a2f | 192 | |
rba90 | 9:e13e02aa4a2f | 193 | case 0x05: //Symbol Timeout |
rba90 | 9:e13e02aa4a2f | 194 | { |
rba90 | 9:e13e02aa4a2f | 195 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 196 | { |
rba90 | 9:e13e02aa4a2f | 197 | uint16_t SymbolTimeout = payload[3] | (payload[2] << 8); |
rba90 | 9:e13e02aa4a2f | 198 | settings->SymbolTimeout = SymbolTimeout; |
rba90 | 9:e13e02aa4a2f | 199 | return 0; |
rba90 | 9:e13e02aa4a2f | 200 | } |
rba90 | 9:e13e02aa4a2f | 201 | else |
rba90 | 9:e13e02aa4a2f | 202 | { |
rba90 | 9:e13e02aa4a2f | 203 | response[1] = (uint8_t) (settings->SymbolTimeout); |
rba90 | 9:e13e02aa4a2f | 204 | response[0] = (uint8_t) (settings->SymbolTimeout >> 8); |
rba90 | 9:e13e02aa4a2f | 205 | *response_length = 2; |
rba90 | 9:e13e02aa4a2f | 206 | |
rba90 | 9:e13e02aa4a2f | 207 | return 0; |
rba90 | 9:e13e02aa4a2f | 208 | } |
rba90 | 9:e13e02aa4a2f | 209 | } |
rba90 | 9:e13e02aa4a2f | 210 | |
rba90 | 9:e13e02aa4a2f | 211 | case 0x06: //FixLen |
rba90 | 9:e13e02aa4a2f | 212 | { |
rba90 | 9:e13e02aa4a2f | 213 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 214 | { |
rba90 | 9:e13e02aa4a2f | 215 | bool FixLen = payload[2]; |
rba90 | 9:e13e02aa4a2f | 216 | settings->FixLen = FixLen; |
rba90 | 9:e13e02aa4a2f | 217 | return 0; |
rba90 | 9:e13e02aa4a2f | 218 | } |
rba90 | 9:e13e02aa4a2f | 219 | else |
rba90 | 9:e13e02aa4a2f | 220 | { |
rba90 | 9:e13e02aa4a2f | 221 | response[0] = (bool) (settings->SymbolTimeout); |
rba90 | 9:e13e02aa4a2f | 222 | *response_length = 1; |
rba90 | 9:e13e02aa4a2f | 223 | |
rba90 | 9:e13e02aa4a2f | 224 | return 0; |
rba90 | 9:e13e02aa4a2f | 225 | } |
rba90 | 9:e13e02aa4a2f | 226 | } |
rba90 | 9:e13e02aa4a2f | 227 | |
rba90 | 9:e13e02aa4a2f | 228 | case 0x07: //PayloadLen |
rba90 | 9:e13e02aa4a2f | 229 | { |
rba90 | 9:e13e02aa4a2f | 230 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 231 | { |
rba90 | 9:e13e02aa4a2f | 232 | uint8_t PayloadLen = payload[2]; |
rba90 | 9:e13e02aa4a2f | 233 | settings->PayloadLen = PayloadLen; |
rba90 | 9:e13e02aa4a2f | 234 | return 0; |
rba90 | 9:e13e02aa4a2f | 235 | } |
rba90 | 9:e13e02aa4a2f | 236 | else |
rba90 | 9:e13e02aa4a2f | 237 | { |
rba90 | 9:e13e02aa4a2f | 238 | response[0] = (uint8_t) (settings->PayloadLen); |
rba90 | 9:e13e02aa4a2f | 239 | |
rba90 | 9:e13e02aa4a2f | 240 | return 0; |
rba90 | 9:e13e02aa4a2f | 241 | } |
rba90 | 9:e13e02aa4a2f | 242 | } |
rba90 | 9:e13e02aa4a2f | 243 | |
rba90 | 9:e13e02aa4a2f | 244 | case 0x08: //CrcOn |
rba90 | 9:e13e02aa4a2f | 245 | { |
rba90 | 9:e13e02aa4a2f | 246 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 247 | bool CrcOn = payload[2]; |
rba90 | 9:e13e02aa4a2f | 248 | settings->CrcOn = CrcOn; |
rba90 | 9:e13e02aa4a2f | 249 | return 0; |
rba90 | 9:e13e02aa4a2f | 250 | } |
rba90 | 9:e13e02aa4a2f | 251 | else |
rba90 | 9:e13e02aa4a2f | 252 | { |
rba90 | 9:e13e02aa4a2f | 253 | response[0] = (bool) (settings->CrcOn); |
rba90 | 9:e13e02aa4a2f | 254 | |
rba90 | 9:e13e02aa4a2f | 255 | return 0; |
rba90 | 9:e13e02aa4a2f | 256 | } |
rba90 | 9:e13e02aa4a2f | 257 | } |
rba90 | 9:e13e02aa4a2f | 258 | |
rba90 | 9:e13e02aa4a2f | 259 | case 0x09: //FreqHopOn |
rba90 | 9:e13e02aa4a2f | 260 | { |
rba90 | 9:e13e02aa4a2f | 261 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 262 | bool FreqHopOn = payload[2]; |
rba90 | 9:e13e02aa4a2f | 263 | settings->FreqHopOn = FreqHopOn; |
rba90 | 9:e13e02aa4a2f | 264 | return 0; |
rba90 | 9:e13e02aa4a2f | 265 | } |
rba90 | 9:e13e02aa4a2f | 266 | else |
rba90 | 9:e13e02aa4a2f | 267 | { |
rba90 | 9:e13e02aa4a2f | 268 | response[0] = (bool) (settings->FreqHopOn); |
rba90 | 9:e13e02aa4a2f | 269 | |
rba90 | 9:e13e02aa4a2f | 270 | return 0; |
rba90 | 9:e13e02aa4a2f | 271 | } |
rba90 | 9:e13e02aa4a2f | 272 | } |
rba90 | 9:e13e02aa4a2f | 273 | |
rba90 | 9:e13e02aa4a2f | 274 | case 0x0A: //HopPeriod |
rba90 | 9:e13e02aa4a2f | 275 | { |
rba90 | 9:e13e02aa4a2f | 276 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 277 | uint8_t HopPeriod = payload[2]; |
rba90 | 9:e13e02aa4a2f | 278 | settings->HopPeriod = HopPeriod; |
rba90 | 9:e13e02aa4a2f | 279 | return 0; |
rba90 | 9:e13e02aa4a2f | 280 | } |
rba90 | 9:e13e02aa4a2f | 281 | else |
rba90 | 9:e13e02aa4a2f | 282 | { |
rba90 | 9:e13e02aa4a2f | 283 | response[0] = (uint8_t) (settings->HopPeriod); |
rba90 | 9:e13e02aa4a2f | 284 | return 0; |
rba90 | 9:e13e02aa4a2f | 285 | } |
rba90 | 9:e13e02aa4a2f | 286 | } |
rba90 | 9:e13e02aa4a2f | 287 | |
rba90 | 9:e13e02aa4a2f | 288 | case 0x0B: //IqInverted |
rba90 | 9:e13e02aa4a2f | 289 | { |
rba90 | 9:e13e02aa4a2f | 290 | if (isSet) { |
rba90 | 9:e13e02aa4a2f | 291 | bool IqInverted = payload[2]; |
rba90 | 9:e13e02aa4a2f | 292 | settings->IqInverted = IqInverted; |
rba90 | 9:e13e02aa4a2f | 293 | return 0; |
rba90 | 9:e13e02aa4a2f | 294 | } |
rba90 | 9:e13e02aa4a2f | 295 | else |
rba90 | 9:e13e02aa4a2f | 296 | { |
rba90 | 9:e13e02aa4a2f | 297 | response[0] = (bool) (settings->IqInverted); |
rba90 | 9:e13e02aa4a2f | 298 | return 0; |
rba90 | 9:e13e02aa4a2f | 299 | } |
rba90 | 9:e13e02aa4a2f | 300 | } |
rba90 | 9:e13e02aa4a2f | 301 | |
rba90 | 9:e13e02aa4a2f | 302 | case 0x0C: //RxContinuous |
rba90 | 9:e13e02aa4a2f | 303 | { |
rba90 | 9:e13e02aa4a2f | 304 | if(isSet) |
rba90 | 9:e13e02aa4a2f | 305 | { |
rba90 | 9:e13e02aa4a2f | 306 | bool RxContinuous = payload[2]; |
rba90 | 9:e13e02aa4a2f | 307 | settings->RxContinuous = RxContinuous; |
rba90 | 9:e13e02aa4a2f | 308 | return 0; |
rba90 | 9:e13e02aa4a2f | 309 | } |
rba90 | 9:e13e02aa4a2f | 310 | else |
rba90 | 9:e13e02aa4a2f | 311 | { |
rba90 | 9:e13e02aa4a2f | 312 | response[0] = (bool) (settings->RxContinuous); |
rba90 | 9:e13e02aa4a2f | 313 | return 0; |
rba90 | 9:e13e02aa4a2f | 314 | } |
rba90 | 9:e13e02aa4a2f | 315 | } |
rba90 | 9:e13e02aa4a2f | 316 | |
rba90 | 9:e13e02aa4a2f | 317 | case 0x0D: //TxTimeout |
rba90 | 9:e13e02aa4a2f | 318 | { |
rba90 | 9:e13e02aa4a2f | 319 | if (isSet) |
rba90 | 9:e13e02aa4a2f | 320 | { |
rba90 | 9:e13e02aa4a2f | 321 | uint32_t TxTimeout = (payload[5]) | |
rba90 | 9:e13e02aa4a2f | 322 | (payload[4] << 8) | |
rba90 | 9:e13e02aa4a2f | 323 | (payload[3] << 16) | |
rba90 | 9:e13e02aa4a2f | 324 | (payload[2] << 24); |
rba90 | 9:e13e02aa4a2f | 325 | settings->TxTimeout = TxTimeout; |
rba90 | 9:e13e02aa4a2f | 326 | return 0; |
rba90 | 9:e13e02aa4a2f | 327 | } |
rba90 | 9:e13e02aa4a2f | 328 | else |
rba90 | 9:e13e02aa4a2f | 329 | { |
rba90 | 9:e13e02aa4a2f | 330 | response[3] = (uint8_t) (settings->TxTimeout); |
rba90 | 9:e13e02aa4a2f | 331 | response[2] = (uint8_t) (settings->TxTimeout >> 8); |
rba90 | 9:e13e02aa4a2f | 332 | response[1] = (uint8_t) (settings->TxTimeout >> 16); |
rba90 | 9:e13e02aa4a2f | 333 | response[0] = (uint8_t) (settings->TxTimeout >> 24); |
rba90 | 9:e13e02aa4a2f | 334 | *response_length = 4; |
rba90 | 9:e13e02aa4a2f | 335 | |
rba90 | 9:e13e02aa4a2f | 336 | return 0; |
rba90 | 9:e13e02aa4a2f | 337 | } |
rba90 | 9:e13e02aa4a2f | 338 | } |
rba90 | 9:e13e02aa4a2f | 339 | |
rba90 | 9:e13e02aa4a2f | 340 | |
rba90 | 9:e13e02aa4a2f | 341 | default: |
rba90 | 9:e13e02aa4a2f | 342 | { |
rba90 | 9:e13e02aa4a2f | 343 | break; |
rba90 | 9:e13e02aa4a2f | 344 | } |
rba90 | 9:e13e02aa4a2f | 345 | |
rba90 | 9:e13e02aa4a2f | 346 | //case |
rba90 | 9:e13e02aa4a2f | 347 | } |
rba90 | 9:e13e02aa4a2f | 348 | |
rba90 | 9:e13e02aa4a2f | 349 | |
rba90 | 9:e13e02aa4a2f | 350 | return 0; |
rba90 | 9:e13e02aa4a2f | 351 | } |
rba90 | 9:e13e02aa4a2f | 352 | |
rba90 | 9:e13e02aa4a2f | 353 | int radioUpdateSettings(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 9:e13e02aa4a2f | 354 | { |
rba90 | 9:e13e02aa4a2f | 355 | aloha.updateSettings(); |
rba90 | 9:e13e02aa4a2f | 356 | |
rba90 | 9:e13e02aa4a2f | 357 | return 0; |
rba90 | 9:e13e02aa4a2f | 358 | } |
rba90 | 9:e13e02aa4a2f | 359 | |
rba90 | 6:19457108c899 | 360 | int sendMessage(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 6:19457108c899 | 361 | { |
rba90 | 6:19457108c899 | 362 | static uint8_t seqid = 0; |
rba90 | 6:19457108c899 | 363 | |
rba90 | 6:19457108c899 | 364 | // prepare for the frame |
rba90 | 6:19457108c899 | 365 | txFrame.setType(AlohaFrame::Aloha_Data); |
rba90 | 6:19457108c899 | 366 | txFrame.setPayloadLength(0x0); |
rba90 | 6:19457108c899 | 367 | txFrame.setSourceAddress(0x1); |
rba90 | 6:19457108c899 | 368 | txFrame.setDestinationAddress(0x2); |
rba90 | 6:19457108c899 | 369 | txFrame.setFullMessageFlag(0x1); |
rba90 | 6:19457108c899 | 370 | txFrame.setSequenceID(seqid); |
rba90 | 6:19457108c899 | 371 | txFrame.generateCrc(); |
rba90 | 6:19457108c899 | 372 | |
rba90 | 6:19457108c899 | 373 | |
rba90 | 6:19457108c899 | 374 | uint8_t buffer[20]; |
rba90 | 6:19457108c899 | 375 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 6:19457108c899 | 376 | txFrame.serialize(buffer); |
rba90 | 6:19457108c899 | 377 | |
rba90 | 6:19457108c899 | 378 | aloha.send(buffer, 20); |
rba90 | 6:19457108c899 | 379 | |
rba90 | 6:19457108c899 | 380 | seqid += 1; |
rba90 | 6:19457108c899 | 381 | |
rba90 | 6:19457108c899 | 382 | return 0; |
rba90 | 6:19457108c899 | 383 | } |
rba90 | 6:19457108c899 | 384 | |
rba90 | 11:9590f3c9d915 | 385 | int userQuery(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length) |
rba90 | 11:9590f3c9d915 | 386 | { |
rba90 | 11:9590f3c9d915 | 387 | // send query data to client |
rba90 | 11:9590f3c9d915 | 388 | // make some dummy response |
rba90 | 11:9590f3c9d915 | 389 | response[0] = 1; // temperature sensor |
rba90 | 11:9590f3c9d915 | 390 | sprintf((char *)response + 1, "22.4"); |
rba90 | 11:9590f3c9d915 | 391 | *response_length = 5; |
rba90 | 11:9590f3c9d915 | 392 | |
rba90 | 11:9590f3c9d915 | 393 | return 0; |
rba90 | 11:9590f3c9d915 | 394 | } |
rba90 | 11:9590f3c9d915 | 395 | |
rba90 | 2:ae85337d4ca7 | 396 | void AlohaDataEcho(AlohaFrame *frame) |
rba90 | 3:a4f6abad9378 | 397 | { |
rba90 | 2:ae85337d4ca7 | 398 | // get rssi and snr |
rba90 | 2:ae85337d4ca7 | 399 | uint16_t rssi = aloha.getRssi(); |
rba90 | 2:ae85337d4ca7 | 400 | uint8_t snr = aloha.getSnr(); |
rba90 | 2:ae85337d4ca7 | 401 | |
rba90 | 2:ae85337d4ca7 | 402 | uint8_t rssi_h = (rssi & 0xff00) >> 8; |
rba90 | 2:ae85337d4ca7 | 403 | uint8_t rssi_l = rssi & 0xff; |
rba90 | 2:ae85337d4ca7 | 404 | |
rba90 | 2:ae85337d4ca7 | 405 | // Transmit RSSI and SNR back to sender |
rba90 | 2:ae85337d4ca7 | 406 | txFrame.setType(AlohaFrame::Aloha_Data); |
rba90 | 2:ae85337d4ca7 | 407 | txFrame.setPayloadLength(3); |
rba90 | 8:332099ece6e9 | 408 | txFrame.setSourceAddress(aloha.getDeviceId()); |
rba90 | 2:ae85337d4ca7 | 409 | txFrame.setDestinationAddress(frame->getSourceAddress()); |
rba90 | 2:ae85337d4ca7 | 410 | txFrame.setFullMessageFlag(frame->getFullMessageFlag()); |
rba90 | 2:ae85337d4ca7 | 411 | txFrame.setSequenceID(frame->getSequenceID() + 1); |
rba90 | 12:2ead6bfd9f2a | 412 | txFrame.setPayload(0, snr); |
rba90 | 12:2ead6bfd9f2a | 413 | txFrame.setPayload(1, rssi_h); |
rba90 | 12:2ead6bfd9f2a | 414 | txFrame.setPayload(2, rssi_l); |
rba90 | 2:ae85337d4ca7 | 415 | txFrame.generateCrc(); |
rba90 | 2:ae85337d4ca7 | 416 | |
rba90 | 2:ae85337d4ca7 | 417 | |
rba90 | 2:ae85337d4ca7 | 418 | uint8_t buffer[20]; |
rba90 | 2:ae85337d4ca7 | 419 | memset(buffer, 0x0, sizeof(buffer)); |
rba90 | 2:ae85337d4ca7 | 420 | txFrame.serialize(buffer); |
rba90 | 2:ae85337d4ca7 | 421 | |
rba90 | 2:ae85337d4ca7 | 422 | aloha.send(buffer, 20); |
rba90 | 2:ae85337d4ca7 | 423 | |
rba90 | 3:a4f6abad9378 | 424 | // print received message |
rba90 | 3:a4f6abad9378 | 425 | printf("-----------------------------------------\r\n"); |
rba90 | 3:a4f6abad9378 | 426 | printf(">Received Frame\r\n"); |
rba90 | 3:a4f6abad9378 | 427 | printf("> Type: 0x%x, PayloadLength: 0x%x\r\n", frame->getType(), frame->getPayloadLength()); |
rba90 | 3:a4f6abad9378 | 428 | printf("> SrcAddr: 0x%x, DestAddr: 0x%x\r\n", frame->getSourceAddress(), frame->getDestinationAddress()); |
rba90 | 3:a4f6abad9378 | 429 | printf("> FMF: 0x%x, SequenceID: 0x%x\r\n", frame->getFullMessageFlag(), frame->getSequenceID()); |
rba90 | 3:a4f6abad9378 | 430 | for (uint8_t i = 0; i < frame->getPayloadLength(); i++) |
rba90 | 3:a4f6abad9378 | 431 | { |
rba90 | 3:a4f6abad9378 | 432 | printf("> Payload[%d]: 0x%x\r\n", i, frame->getPayload(i)); |
rba90 | 3:a4f6abad9378 | 433 | } |
rba90 | 3:a4f6abad9378 | 434 | printf("> CRC: 0x%x\r\n", frame->getCrc()); |
rba90 | 3:a4f6abad9378 | 435 | printf("-----------------------------------------\r\n"); |
rba90 | 0:1e42d79b42e1 | 436 | } |
rba90 | 0:1e42d79b42e1 | 437 | |
rba90 | 0:1e42d79b42e1 | 438 | int main() { |
rba90 | 0:1e42d79b42e1 | 439 | // initialize radio module |
rba90 | 5:445f35444a6a | 440 | aloha.boardInit(); |
rba90 | 6:19457108c899 | 441 | aloha.updateSettings(); |
rba90 | 6:19457108c899 | 442 | aloha.enable(); |
rba90 | 0:1e42d79b42e1 | 443 | |
rba90 | 0:1e42d79b42e1 | 444 | // attach serial interrupt handler |
rba90 | 0:1e42d79b42e1 | 445 | pc.attach(&serialInterruptHandler); |
rba90 | 0:1e42d79b42e1 | 446 | |
rba90 | 0:1e42d79b42e1 | 447 | // register callback functions for SIP |
rba90 | 0:1e42d79b42e1 | 448 | SIP.registerCommand(0x00, toggleChecksum); |
rba90 | 6:19457108c899 | 449 | SIP.registerCommand(0x01, sendMessage); |
rba90 | 9:e13e02aa4a2f | 450 | SIP.registerCommand(0x02, configureRadio); |
rba90 | 9:e13e02aa4a2f | 451 | SIP.registerCommand(0x03, radioUpdateSettings); |
rba90 | 11:9590f3c9d915 | 452 | SIP.registerCommand(0x04, userQuery); |
rba90 | 0:1e42d79b42e1 | 453 | |
rba90 | 0:1e42d79b42e1 | 454 | // register callback functions for aloha transceiver |
rba90 | 2:ae85337d4ca7 | 455 | aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataEcho); |
rba90 | 0:1e42d79b42e1 | 456 | |
rba90 | 0:1e42d79b42e1 | 457 | while(1) { |
rba90 | 0:1e42d79b42e1 | 458 | SIP.poll(); |
rba90 | 0:1e42d79b42e1 | 459 | aloha.poll(); |
rba90 | 0:1e42d79b42e1 | 460 | |
rba90 | 0:1e42d79b42e1 | 461 | while (SerialOutputBuffer.getCounter() > 0) |
rba90 | 0:1e42d79b42e1 | 462 | { |
rba90 | 0:1e42d79b42e1 | 463 | uint8_t ch; |
rba90 | 0:1e42d79b42e1 | 464 | ch = SerialOutputBuffer.dequeue(); |
rba90 | 0:1e42d79b42e1 | 465 | pc.putc(ch); |
rba90 | 0:1e42d79b42e1 | 466 | } |
rba90 | 0:1e42d79b42e1 | 467 | } |
rba90 | 0:1e42d79b42e1 | 468 | } |