Wireless interface using LoRa technology

Dependencies:   AlohaTransceiver RingBuffer SX1276Lib_inAir SerialInterfaceProtocol mbed L3PDU

Committer:
rba90
Date:
Thu Aug 11 03:44:11 2016 +0000
Revision:
14:80cee3991860
Parent:
11:c7c0036efdbd
Child:
15:cfc7d817e444
integration test

Who changed what in which revision?

UserRevisionLine numberNew 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 14:80cee3991860 6 #include "ControlPacket.h"
rba90 14:80cee3991860 7 #include "DataBlockPacket.h"
rba90 0:7d8e24cb62c1 8
rba90 0:7d8e24cb62c1 9 Serial pc(USBTX, USBRX);
rba90 0:7d8e24cb62c1 10
rba90 0:7d8e24cb62c1 11 // sip uses two buffer queues
rba90 0:7d8e24cb62c1 12 CircularBuffer<uint8_t> SerialInputBuffer;
rba90 0:7d8e24cb62c1 13 CircularBuffer<uint8_t> SerialOutputBuffer;
rba90 0:7d8e24cb62c1 14 SerialInterfaceProtocol SIP(&SerialInputBuffer, &SerialOutputBuffer);
rba90 0:7d8e24cb62c1 15
rba90 0:7d8e24cb62c1 16 // aloha transceiver
rba90 9:86d95e095bb0 17 AlohaTransceiver aloha(DEVICE_ID);
rba90 0:7d8e24cb62c1 18 AlohaFrame txFrame;
rba90 1:101cf4fca4e5 19 Timer timer;
rba90 2:5a74ae8be594 20 InterruptIn button(USER_BUTTON);
rba90 0:7d8e24cb62c1 21
rba90 14:80cee3991860 22 // sensors
rba90 14:80cee3991860 23 #define SUPPLY_VOLTAGE 3.3;
rba90 14:80cee3991860 24 AnalogIn TempSensor(PA_0);
rba90 14:80cee3991860 25
rba90 14:80cee3991860 26 float getTemperature()
rba90 14:80cee3991860 27 {
rba90 14:80cee3991860 28 float reading = TempSensor.read();
rba90 14:80cee3991860 29 float output_voltage = reading * SUPPLY_VOLTAGE;
rba90 14:80cee3991860 30 return (output_voltage - 0.25) / 0.028;
rba90 14:80cee3991860 31 }
rba90 14:80cee3991860 32
rba90 0:7d8e24cb62c1 33 void serialInterruptHandler() {
rba90 0:7d8e24cb62c1 34 // Note: you need to actually read from the serial to clear the RX interrupt
rba90 0:7d8e24cb62c1 35 int c = pc.getc();
rba90 0:7d8e24cb62c1 36
rba90 0:7d8e24cb62c1 37 // add to buffer
rba90 0:7d8e24cb62c1 38 if (SerialInputBuffer.isLocked())
rba90 0:7d8e24cb62c1 39 {
rba90 0:7d8e24cb62c1 40 printf("Mutex Locked\r\n");
rba90 0:7d8e24cb62c1 41 }
rba90 0:7d8e24cb62c1 42 else
rba90 0:7d8e24cb62c1 43 {
rba90 0:7d8e24cb62c1 44 SerialInputBuffer.enqueue((uint8_t) c);
rba90 0:7d8e24cb62c1 45 }
rba90 0:7d8e24cb62c1 46 }
rba90 0:7d8e24cb62c1 47
rba90 0:7d8e24cb62c1 48 int toggleChecksum(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 0:7d8e24cb62c1 49 {
rba90 0:7d8e24cb62c1 50 // one payload
rba90 0:7d8e24cb62c1 51 if (payload_length != 1)
rba90 0:7d8e24cb62c1 52 {
rba90 0:7d8e24cb62c1 53 sprintf((char *) response, "Wrong Payload Length\r\n");
rba90 0:7d8e24cb62c1 54 *response_length = 22;
rba90 0:7d8e24cb62c1 55 return 1;
rba90 0:7d8e24cb62c1 56 }
rba90 0:7d8e24cb62c1 57
rba90 0:7d8e24cb62c1 58 if ((bool) payload[0])
rba90 0:7d8e24cb62c1 59 {
rba90 0:7d8e24cb62c1 60 SIP.enableChecksum();
rba90 0:7d8e24cb62c1 61 }
rba90 0:7d8e24cb62c1 62 else
rba90 0:7d8e24cb62c1 63 {
rba90 0:7d8e24cb62c1 64 SIP.disableChecksum();
rba90 0:7d8e24cb62c1 65 }
rba90 0:7d8e24cb62c1 66
rba90 0:7d8e24cb62c1 67 return 0;
rba90 0:7d8e24cb62c1 68 }
rba90 0:7d8e24cb62c1 69
rba90 4:9151697dfa70 70 /*
rba90 4:9151697dfa70 71 * Format:
rba90 4:9151697dfa70 72 * < :start flag
rba90 4:9151697dfa70 73 * 02 :command
rba90 4:9151697dfa70 74 * xx :length
rba90 4:9151697dfa70 75 * xx: :00: get, 01: set
rba90 4:9151697dfa70 76 * xx :index for parameters
rba90 4:9151697dfa70 77 * ...
rba90 4:9151697dfa70 78 * ff :checksum
rba90 4:9151697dfa70 79 * > :end flag
rba90 4:9151697dfa70 80 */
rba90 3:7bb50ee42cba 81 int configureRadio(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 3:7bb50ee42cba 82 {
rba90 3:7bb50ee42cba 83 // read settings from radio
rba90 3:7bb50ee42cba 84 #if USE_MODEM_LORA == 1
rba90 3:7bb50ee42cba 85 AlohaTransceiver::LoRaSettings_t *settings = aloha.getSettings();
rba90 3:7bb50ee42cba 86 #elif USE_MODEM_FSK == 1
rba90 3:7bb50ee42cba 87 AlohaTransceiver::FskSettings_t *settings = aloha.getSettings();
rba90 3:7bb50ee42cba 88 #else
rba90 3:7bb50ee42cba 89 #error "Please define a modem in the compiler options."
rba90 3:7bb50ee42cba 90 #endif
rba90 3:7bb50ee42cba 91
rba90 3:7bb50ee42cba 92 if (payload_length < 2)
rba90 3:7bb50ee42cba 93 {
rba90 3:7bb50ee42cba 94 sprintf((char *) response, "Wrong Payload Length\r\n");
rba90 3:7bb50ee42cba 95 *response_length = 22;
rba90 3:7bb50ee42cba 96 return 1;
rba90 3:7bb50ee42cba 97 }
rba90 3:7bb50ee42cba 98
rba90 3:7bb50ee42cba 99 // true is set, false is get
rba90 3:7bb50ee42cba 100 bool isSet = (bool) payload[0];
rba90 3:7bb50ee42cba 101 uint8_t idx = payload[1];
rba90 3:7bb50ee42cba 102
rba90 3:7bb50ee42cba 103 switch(idx)
rba90 3:7bb50ee42cba 104 {
rba90 3:7bb50ee42cba 105 case 0x00: // Power
rba90 3:7bb50ee42cba 106 {
rba90 3:7bb50ee42cba 107 if (isSet)
rba90 3:7bb50ee42cba 108 {
rba90 3:7bb50ee42cba 109 int8_t Power = (int8_t) payload[2];
rba90 3:7bb50ee42cba 110 settings->Power = Power;
rba90 3:7bb50ee42cba 111
rba90 3:7bb50ee42cba 112 return 0;
rba90 3:7bb50ee42cba 113 }
rba90 3:7bb50ee42cba 114 else
rba90 3:7bb50ee42cba 115 {
rba90 3:7bb50ee42cba 116 response[0] = (uint8_t) settings->Power;
rba90 3:7bb50ee42cba 117 *response_length = 1;
rba90 1:101cf4fca4e5 118
rba90 3:7bb50ee42cba 119 return 0;
rba90 3:7bb50ee42cba 120 }
rba90 3:7bb50ee42cba 121 }
rba90 3:7bb50ee42cba 122
rba90 3:7bb50ee42cba 123 case 0x01: // Bandwidth
rba90 3:7bb50ee42cba 124 {
rba90 3:7bb50ee42cba 125 if (isSet)
rba90 3:7bb50ee42cba 126 {
Razorfoot 8:e30610bf6f79 127 uint32_t Bandwidth = (payload[5]) |
Razorfoot 8:e30610bf6f79 128 (payload[4] << 8) |
Razorfoot 8:e30610bf6f79 129 (payload[3] << 16) |
Razorfoot 8:e30610bf6f79 130 (payload[2] << 24);
rba90 3:7bb50ee42cba 131 settings->Bandwidth = Bandwidth;
rba90 3:7bb50ee42cba 132
rba90 3:7bb50ee42cba 133 return 0;
rba90 3:7bb50ee42cba 134 }
rba90 3:7bb50ee42cba 135 else
rba90 3:7bb50ee42cba 136 {
rba90 3:7bb50ee42cba 137 response[3] = (uint8_t) (settings->Bandwidth);
rba90 3:7bb50ee42cba 138 response[2] = (uint8_t) (settings->Bandwidth >> 8);
rba90 3:7bb50ee42cba 139 response[1] = (uint8_t) (settings->Bandwidth >> 16);
rba90 3:7bb50ee42cba 140 response[0] = (uint8_t) (settings->Bandwidth >> 24);
rba90 3:7bb50ee42cba 141 *response_length = 4;
rba90 3:7bb50ee42cba 142
rba90 3:7bb50ee42cba 143 return 0;
rba90 3:7bb50ee42cba 144 }
rba90 3:7bb50ee42cba 145
rba90 3:7bb50ee42cba 146 }
rba90 3:7bb50ee42cba 147
Razorfoot 8:e30610bf6f79 148 case 0x02: // Datarate, AKA Spreading Factor
rba90 3:7bb50ee42cba 149 {
rba90 3:7bb50ee42cba 150 if (isSet)
rba90 3:7bb50ee42cba 151 {
Razorfoot 8:e30610bf6f79 152 uint32_t Datarate = (payload[5]) |
Razorfoot 8:e30610bf6f79 153 (payload[4] << 8) |
Razorfoot 8:e30610bf6f79 154 (payload[3] << 16) |
Razorfoot 8:e30610bf6f79 155 (payload[2] << 24);
rba90 3:7bb50ee42cba 156 settings->Datarate = Datarate;
rba90 3:7bb50ee42cba 157
rba90 3:7bb50ee42cba 158 return 0;
rba90 3:7bb50ee42cba 159 }
rba90 3:7bb50ee42cba 160 else
rba90 3:7bb50ee42cba 161 {
rba90 3:7bb50ee42cba 162 response[3] = (uint8_t) (settings->Datarate);
rba90 3:7bb50ee42cba 163 response[2] = (uint8_t) (settings->Datarate >> 8);
rba90 3:7bb50ee42cba 164 response[1] = (uint8_t) (settings->Datarate >> 16);
rba90 3:7bb50ee42cba 165 response[0] = (uint8_t) (settings->Datarate >> 24);
rba90 3:7bb50ee42cba 166 *response_length = 4;
rba90 3:7bb50ee42cba 167
rba90 3:7bb50ee42cba 168 return 0;
rba90 3:7bb50ee42cba 169 }
rba90 3:7bb50ee42cba 170 }
rba90 3:7bb50ee42cba 171
rba90 3:7bb50ee42cba 172 case 0x03: // Coderate
rba90 3:7bb50ee42cba 173 {
rba90 3:7bb50ee42cba 174 if (isSet)
rba90 3:7bb50ee42cba 175 {
Razorfoot 8:e30610bf6f79 176 uint8_t Coderate = payload[2];
rba90 3:7bb50ee42cba 177 settings->Coderate = Coderate;
rba90 3:7bb50ee42cba 178
rba90 3:7bb50ee42cba 179 return 0;
rba90 3:7bb50ee42cba 180 }
rba90 3:7bb50ee42cba 181 else
rba90 3:7bb50ee42cba 182 {
rba90 3:7bb50ee42cba 183 response[0] = (uint8_t) settings->Coderate;
rba90 3:7bb50ee42cba 184 *response_length = 1;
rba90 3:7bb50ee42cba 185
rba90 3:7bb50ee42cba 186 return 0;
rba90 3:7bb50ee42cba 187 }
rba90 3:7bb50ee42cba 188 }
rba90 3:7bb50ee42cba 189
Razorfoot 8:e30610bf6f79 190 case 0x04: //Preamble Length
Razorfoot 8:e30610bf6f79 191 {
Razorfoot 8:e30610bf6f79 192 if (isSet)
Razorfoot 8:e30610bf6f79 193 {
Razorfoot 8:e30610bf6f79 194 uint16_t PreambleLen = payload[3] | (payload[2] << 8);
Razorfoot 8:e30610bf6f79 195 settings->PreambleLen = PreambleLen;
Razorfoot 8:e30610bf6f79 196 return 0;
Razorfoot 8:e30610bf6f79 197 }
Razorfoot 8:e30610bf6f79 198 else
Razorfoot 8:e30610bf6f79 199 {
Razorfoot 8:e30610bf6f79 200 response[1] = (uint8_t) (settings->PreambleLen);
Razorfoot 8:e30610bf6f79 201 response[0] = (uint8_t)(settings->PreambleLen >> 8);
Razorfoot 8:e30610bf6f79 202 *response_length = 2;
Razorfoot 8:e30610bf6f79 203
Razorfoot 8:e30610bf6f79 204 return 0;
Razorfoot 8:e30610bf6f79 205 }
Razorfoot 8:e30610bf6f79 206 }
Razorfoot 8:e30610bf6f79 207
Razorfoot 8:e30610bf6f79 208 case 0x05: //Symbol Timeout
Razorfoot 8:e30610bf6f79 209 {
rba90 11:c7c0036efdbd 210 if (isSet)
rba90 11:c7c0036efdbd 211 {
rba90 11:c7c0036efdbd 212 uint16_t SymbolTimeout = payload[3] | (payload[2] << 8);
rba90 11:c7c0036efdbd 213 settings->SymbolTimeout = SymbolTimeout;
rba90 11:c7c0036efdbd 214 return 0;
rba90 11:c7c0036efdbd 215 }
rba90 11:c7c0036efdbd 216 else
rba90 11:c7c0036efdbd 217 {
rba90 11:c7c0036efdbd 218 response[1] = (uint8_t) (settings->SymbolTimeout);
rba90 11:c7c0036efdbd 219 response[0] = (uint8_t) (settings->SymbolTimeout >> 8);
rba90 11:c7c0036efdbd 220 *response_length = 2;
rba90 11:c7c0036efdbd 221
rba90 11:c7c0036efdbd 222 return 0;
rba90 11:c7c0036efdbd 223 }
Razorfoot 8:e30610bf6f79 224 }
Razorfoot 8:e30610bf6f79 225
Razorfoot 8:e30610bf6f79 226 case 0x06: //FixLen
Razorfoot 8:e30610bf6f79 227 {
rba90 11:c7c0036efdbd 228 if (isSet)
rba90 11:c7c0036efdbd 229 {
rba90 11:c7c0036efdbd 230 bool FixLen = payload[2];
rba90 11:c7c0036efdbd 231 settings->FixLen = FixLen;
rba90 11:c7c0036efdbd 232 return 0;
rba90 11:c7c0036efdbd 233 }
rba90 11:c7c0036efdbd 234 else
rba90 11:c7c0036efdbd 235 {
rba90 11:c7c0036efdbd 236 response[0] = (bool) (settings->SymbolTimeout);
rba90 11:c7c0036efdbd 237 *response_length = 1;
rba90 11:c7c0036efdbd 238
rba90 11:c7c0036efdbd 239 return 0;
rba90 11:c7c0036efdbd 240 }
Razorfoot 8:e30610bf6f79 241 }
Razorfoot 8:e30610bf6f79 242
Razorfoot 8:e30610bf6f79 243 case 0x07: //PayloadLen
Razorfoot 8:e30610bf6f79 244 {
rba90 11:c7c0036efdbd 245 if (isSet)
rba90 11:c7c0036efdbd 246 {
rba90 11:c7c0036efdbd 247 uint8_t PayloadLen = payload[2];
rba90 11:c7c0036efdbd 248 settings->PayloadLen = PayloadLen;
rba90 11:c7c0036efdbd 249 return 0;
rba90 11:c7c0036efdbd 250 }
rba90 11:c7c0036efdbd 251 else
rba90 11:c7c0036efdbd 252 {
rba90 11:c7c0036efdbd 253 response[0] = (uint8_t) (settings->PayloadLen);
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 0x08: //CrcOn
Razorfoot 8:e30610bf6f79 260 {
rba90 11:c7c0036efdbd 261 if (isSet) {
rba90 11:c7c0036efdbd 262 bool CrcOn = payload[2];
rba90 11:c7c0036efdbd 263 settings->CrcOn = CrcOn;
rba90 11:c7c0036efdbd 264 return 0;
rba90 11:c7c0036efdbd 265 }
rba90 11:c7c0036efdbd 266 else
rba90 11:c7c0036efdbd 267 {
rba90 11:c7c0036efdbd 268 response[0] = (bool) (settings->CrcOn);
rba90 11:c7c0036efdbd 269
rba90 11:c7c0036efdbd 270 return 0;
rba90 11:c7c0036efdbd 271 }
Razorfoot 8:e30610bf6f79 272 }
Razorfoot 8:e30610bf6f79 273
Razorfoot 8:e30610bf6f79 274 case 0x09: //FreqHopOn
Razorfoot 8:e30610bf6f79 275 {
rba90 11:c7c0036efdbd 276 if (isSet) {
rba90 11:c7c0036efdbd 277 bool FreqHopOn = payload[2];
rba90 11:c7c0036efdbd 278 settings->FreqHopOn = FreqHopOn;
rba90 11:c7c0036efdbd 279 return 0;
rba90 11:c7c0036efdbd 280 }
rba90 11:c7c0036efdbd 281 else
rba90 11:c7c0036efdbd 282 {
rba90 11:c7c0036efdbd 283 response[0] = (bool) (settings->FreqHopOn);
rba90 11:c7c0036efdbd 284
rba90 11:c7c0036efdbd 285 return 0;
rba90 11:c7c0036efdbd 286 }
Razorfoot 8:e30610bf6f79 287 }
Razorfoot 8:e30610bf6f79 288
Razorfoot 8:e30610bf6f79 289 case 0x0A: //HopPeriod
Razorfoot 8:e30610bf6f79 290 {
rba90 11:c7c0036efdbd 291 if (isSet) {
rba90 11:c7c0036efdbd 292 uint8_t HopPeriod = payload[2];
rba90 11:c7c0036efdbd 293 settings->HopPeriod = HopPeriod;
rba90 11:c7c0036efdbd 294 return 0;
rba90 11:c7c0036efdbd 295 }
rba90 11:c7c0036efdbd 296 else
rba90 11:c7c0036efdbd 297 {
rba90 11:c7c0036efdbd 298 response[0] = (uint8_t) (settings->HopPeriod);
rba90 11:c7c0036efdbd 299 return 0;
rba90 11:c7c0036efdbd 300 }
Razorfoot 8:e30610bf6f79 301 }
Razorfoot 8:e30610bf6f79 302
Razorfoot 8:e30610bf6f79 303 case 0x0B: //IqInverted
Razorfoot 8:e30610bf6f79 304 {
rba90 11:c7c0036efdbd 305 if (isSet) {
rba90 11:c7c0036efdbd 306 bool IqInverted = payload[2];
rba90 11:c7c0036efdbd 307 settings->IqInverted = IqInverted;
rba90 11:c7c0036efdbd 308 return 0;
rba90 11:c7c0036efdbd 309 }
rba90 11:c7c0036efdbd 310 else
rba90 11:c7c0036efdbd 311 {
rba90 11:c7c0036efdbd 312 response[0] = (bool) (settings->IqInverted);
rba90 11:c7c0036efdbd 313 return 0;
rba90 11:c7c0036efdbd 314 }
Razorfoot 8:e30610bf6f79 315 }
Razorfoot 8:e30610bf6f79 316
Razorfoot 8:e30610bf6f79 317 case 0x0C: //RxContinuous
Razorfoot 8:e30610bf6f79 318 {
rba90 11:c7c0036efdbd 319 if(isSet)
rba90 11:c7c0036efdbd 320 {
rba90 11:c7c0036efdbd 321 bool RxContinuous = payload[2];
rba90 11:c7c0036efdbd 322 settings->RxContinuous = RxContinuous;
rba90 11:c7c0036efdbd 323 return 0;
rba90 11:c7c0036efdbd 324 }
rba90 11:c7c0036efdbd 325 else
rba90 11:c7c0036efdbd 326 {
rba90 11:c7c0036efdbd 327 response[0] = (bool) (settings->RxContinuous);
rba90 11:c7c0036efdbd 328 return 0;
rba90 11:c7c0036efdbd 329 }
Razorfoot 8:e30610bf6f79 330 }
Razorfoot 8:e30610bf6f79 331
Razorfoot 8:e30610bf6f79 332 case 0x0D: //TxTimeout
Razorfoot 8:e30610bf6f79 333 {
rba90 11:c7c0036efdbd 334 if (isSet)
rba90 11:c7c0036efdbd 335 {
rba90 11:c7c0036efdbd 336 uint32_t TxTimeout = (payload[5]) |
rba90 11:c7c0036efdbd 337 (payload[4] << 8) |
rba90 11:c7c0036efdbd 338 (payload[3] << 16) |
rba90 11:c7c0036efdbd 339 (payload[2] << 24);
rba90 11:c7c0036efdbd 340 settings->TxTimeout = TxTimeout;
rba90 11:c7c0036efdbd 341 return 0;
rba90 11:c7c0036efdbd 342 }
rba90 11:c7c0036efdbd 343 else
rba90 11:c7c0036efdbd 344 {
rba90 11:c7c0036efdbd 345 response[3] = (uint8_t) (settings->TxTimeout);
rba90 11:c7c0036efdbd 346 response[2] = (uint8_t) (settings->TxTimeout >> 8);
rba90 11:c7c0036efdbd 347 response[1] = (uint8_t) (settings->TxTimeout >> 16);
rba90 11:c7c0036efdbd 348 response[0] = (uint8_t) (settings->TxTimeout >> 24);
rba90 11:c7c0036efdbd 349 *response_length = 4;
rba90 11:c7c0036efdbd 350
rba90 11:c7c0036efdbd 351 return 0;
rba90 11:c7c0036efdbd 352 }
Razorfoot 8:e30610bf6f79 353 }
Razorfoot 8:e30610bf6f79 354
Razorfoot 8:e30610bf6f79 355
rba90 3:7bb50ee42cba 356 default:
rba90 3:7bb50ee42cba 357 {
rba90 3:7bb50ee42cba 358 break;
rba90 3:7bb50ee42cba 359 }
Razorfoot 8:e30610bf6f79 360
Razorfoot 8:e30610bf6f79 361 //case
rba90 3:7bb50ee42cba 362 }
rba90 3:7bb50ee42cba 363
rba90 3:7bb50ee42cba 364
rba90 3:7bb50ee42cba 365 return 0;
rba90 1:101cf4fca4e5 366 }
rba90 1:101cf4fca4e5 367
rba90 11:c7c0036efdbd 368 int radioUpdateSettings(uint8_t *payload, uint8_t payload_length, uint8_t *response, uint8_t *response_length)
rba90 11:c7c0036efdbd 369 {
rba90 11:c7c0036efdbd 370 aloha.updateSettings();
rba90 11:c7c0036efdbd 371
rba90 11:c7c0036efdbd 372 return 0;
rba90 11:c7c0036efdbd 373 }
rba90 11:c7c0036efdbd 374
rba90 14:80cee3991860 375
rba90 14:80cee3991860 376 void AlohaDataPacketHandler(uint8_t *payload, uint8_t payload_length, uint8_t src_addr)
rba90 1:101cf4fca4e5 377 {
rba90 14:80cee3991860 378 // try to decode packet
rba90 14:80cee3991860 379 BasicPacket packet(payload);
rba90 14:80cee3991860 380
rba90 14:80cee3991860 381 // verify crc
rba90 14:80cee3991860 382 // skip for current development
rba90 14:80cee3991860 383
rba90 14:80cee3991860 384 // process the packet based on different feature id
rba90 14:80cee3991860 385 BasicPacket::L3Fid_t fid = (BasicPacket::L3Fid_t) packet.getFid();
rba90 1:101cf4fca4e5 386
rba90 14:80cee3991860 387 // we don't care about the type conversion. just create a new one.
rba90 14:80cee3991860 388 switch (fid)
rba90 0:7d8e24cb62c1 389 {
rba90 14:80cee3991860 390 case BasicPacket::L3ControlPacket:
rba90 14:80cee3991860 391 {
rba90 14:80cee3991860 392 printf("Control Packet\r\n");
rba90 14:80cee3991860 393 ControlPacket controlPacket(payload);
rba90 14:80cee3991860 394
rba90 14:80cee3991860 395 // execute command
rba90 14:80cee3991860 396 uint8_t command = controlPacket.getCommand();
rba90 14:80cee3991860 397 switch(command)
rba90 14:80cee3991860 398 {
rba90 14:80cee3991860 399 case 0x0: // query
rba90 14:80cee3991860 400 {
rba90 14:80cee3991860 401 uint8_t sensor_type = controlPacket.getData(0);
rba90 14:80cee3991860 402 switch (sensor_type)
rba90 14:80cee3991860 403 {
rba90 14:80cee3991860 404 case 0x0: // temperature sensor
rba90 14:80cee3991860 405 {
rba90 14:80cee3991860 406 uint8_t temp = getTemperature();
rba90 14:80cee3991860 407
rba90 14:80cee3991860 408 // create a response
rba90 14:80cee3991860 409 DataBlockPacket response;
rba90 14:80cee3991860 410
rba90 14:80cee3991860 411 // set sequence id
rba90 14:80cee3991860 412 response.setSequenceID(0x0);
rba90 14:80cee3991860 413
rba90 14:80cee3991860 414 // set source id
rba90 14:80cee3991860 415 response.setSourceID(aloha.getDeviceID());
rba90 14:80cee3991860 416
rba90 14:80cee3991860 417 // set destination id
rba90 14:80cee3991860 418 response.setDestinationID(src_addr);
rba90 14:80cee3991860 419
rba90 14:80cee3991860 420 // set source type (temperature)
rba90 14:80cee3991860 421 response.setSourceType(0x0);
rba90 14:80cee3991860 422
rba90 14:80cee3991860 423 // set payload type
rba90 14:80cee3991860 424 response.setPayloadType(0x0);
rba90 14:80cee3991860 425
rba90 14:80cee3991860 426 // copy temperature
rba90 14:80cee3991860 427 response.setData(0, temp);
rba90 14:80cee3991860 428
rba90 14:80cee3991860 429 // calculate crc
rba90 14:80cee3991860 430 response.generateCrc();
rba90 14:80cee3991860 431
rba90 14:80cee3991860 432 // serialize and send it
rba90 14:80cee3991860 433 uint8_t buffer[8];
rba90 14:80cee3991860 434 memset(buffer, 0x0, sizeof(buffer));
rba90 14:80cee3991860 435
rba90 14:80cee3991860 436 // copy bytes into buffer
rba90 14:80cee3991860 437 response.serialize(buffer);
rba90 14:80cee3991860 438
rba90 14:80cee3991860 439 // send to aloha transceiver
rba90 14:80cee3991860 440 aloha.send(buffer, 8, src_addr);
rba90 14:80cee3991860 441
rba90 14:80cee3991860 442 break;
rba90 14:80cee3991860 443 }
rba90 14:80cee3991860 444 case 0x1: // door sensor
rba90 14:80cee3991860 445 {
rba90 14:80cee3991860 446 break;
rba90 14:80cee3991860 447 }
rba90 14:80cee3991860 448 default:
rba90 14:80cee3991860 449 break;
rba90 14:80cee3991860 450 }
rba90 14:80cee3991860 451
rba90 14:80cee3991860 452 break;
rba90 14:80cee3991860 453 }
rba90 14:80cee3991860 454 default:
rba90 14:80cee3991860 455 break;
rba90 14:80cee3991860 456 }
rba90 14:80cee3991860 457
rba90 14:80cee3991860 458 break;
rba90 14:80cee3991860 459 }
rba90 14:80cee3991860 460 case BasicPacket::L3DataBlockPacket:
rba90 14:80cee3991860 461 {
rba90 14:80cee3991860 462 printf("Data Packet\r\n");
rba90 14:80cee3991860 463 DataBlockPacket dataBlockPacket(payload);
rba90 14:80cee3991860 464
rba90 14:80cee3991860 465 // do something
rba90 14:80cee3991860 466 break;
rba90 14:80cee3991860 467 }
rba90 14:80cee3991860 468 default:
rba90 14:80cee3991860 469 break;
rba90 0:7d8e24cb62c1 470 }
rba90 0:7d8e24cb62c1 471 }
rba90 0:7d8e24cb62c1 472
Razorfoot 10:7dcb951ecabd 473 void enqueueString(char* s, int len) {
Razorfoot 10:7dcb951ecabd 474 for (int i = 0; i < len; i++) {
Razorfoot 10:7dcb951ecabd 475 SerialInputBuffer.enqueue((uint8_t) s[i]);
Razorfoot 10:7dcb951ecabd 476 }
Razorfoot 10:7dcb951ecabd 477 }
Razorfoot 10:7dcb951ecabd 478
rba90 2:5a74ae8be594 479 void automaticPacketTransmit()
rba90 2:5a74ae8be594 480 {
Razorfoot 10:7dcb951ecabd 481 /*SerialInputBuffer.enqueue((uint8_t) '<');
rba90 2:5a74ae8be594 482 SerialInputBuffer.enqueue((uint8_t) '0');
rba90 2:5a74ae8be594 483 SerialInputBuffer.enqueue((uint8_t) '1');
rba90 2:5a74ae8be594 484 SerialInputBuffer.enqueue((uint8_t) '0');
rba90 2:5a74ae8be594 485 SerialInputBuffer.enqueue((uint8_t) '0');
rba90 2:5a74ae8be594 486 SerialInputBuffer.enqueue((uint8_t) 'f');
rba90 2:5a74ae8be594 487 SerialInputBuffer.enqueue((uint8_t) 'f');
Razorfoot 10:7dcb951ecabd 488 SerialInputBuffer.enqueue((uint8_t) '>');*/
Razorfoot 10:7dcb951ecabd 489 enqueueString("<0100ff>", 8);
rba90 2:5a74ae8be594 490 }
rba90 2:5a74ae8be594 491
rba90 2:5a74ae8be594 492
Razorfoot 10:7dcb951ecabd 493
rba90 0:7d8e24cb62c1 494 int main() {
rba90 0:7d8e24cb62c1 495 // initialize radio module
rba90 3:7bb50ee42cba 496 aloha.boardInit();
rba90 3:7bb50ee42cba 497 aloha.updateSettings();
rba90 3:7bb50ee42cba 498 aloha.enable();
rba90 0:7d8e24cb62c1 499
rba90 0:7d8e24cb62c1 500 // attach serial interrupt handler
rba90 0:7d8e24cb62c1 501 pc.attach(&serialInterruptHandler);
rba90 0:7d8e24cb62c1 502
rba90 0:7d8e24cb62c1 503 // register callback functions for SIP
rba90 0:7d8e24cb62c1 504 SIP.registerCommand(0x00, toggleChecksum);
rba90 14:80cee3991860 505 SIP.registerCommand(0x01, NULL); // disable sendMessage for now
rba90 3:7bb50ee42cba 506 SIP.registerCommand(0x02, configureRadio);
rba90 11:c7c0036efdbd 507 SIP.registerCommand(0x03, radioUpdateSettings);
rba90 0:7d8e24cb62c1 508
rba90 0:7d8e24cb62c1 509 // register callback functions for aloha transceiver
rba90 14:80cee3991860 510 aloha.registerType(AlohaFrame::Aloha_Data, AlohaDataPacketHandler);
rba90 2:5a74ae8be594 511
rba90 2:5a74ae8be594 512 // configure button interrupt
rba90 2:5a74ae8be594 513 button.fall(automaticPacketTransmit);
rba90 0:7d8e24cb62c1 514
rba90 0:7d8e24cb62c1 515 while(1) {
rba90 0:7d8e24cb62c1 516 SIP.poll();
rba90 0:7d8e24cb62c1 517 aloha.poll();
rba90 0:7d8e24cb62c1 518
rba90 0:7d8e24cb62c1 519 while (SerialOutputBuffer.getCounter() > 0)
rba90 0:7d8e24cb62c1 520 {
rba90 0:7d8e24cb62c1 521 uint8_t ch;
rba90 0:7d8e24cb62c1 522 ch = SerialOutputBuffer.dequeue();
rba90 0:7d8e24cb62c1 523 pc.putc(ch);
rba90 0:7d8e24cb62c1 524 }
rba90 0:7d8e24cb62c1 525 }
rba90 0:7d8e24cb62c1 526 }