mdot UDK & STMicro MEMS Shield Sensor packet example

Dependencies:   libmDot-mbed5 DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B X_NUCLEO_IKS01A1 Senet_Packet

Fork of MTDOT-UDKDemo_Senet by canuck lehead

Committer:
Shaun Nelson
Date:
Fri Aug 25 10:04:01 2017 -0400
Revision:
32:5873d0638277
Parent:
26:752359e19a97
Parent:
29:055824db068a
Child:
36:cd1077f40dbf
Merge with develop

This encompasses switch to mbed 5 and latest libmDot

Library versions:
- mbed-os 5.4.7
- libmDot-mbed5 3.0.0

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Shaun Nelson 32:5873d0638277 1 /* _____ _
Shaun Nelson 32:5873d0638277 2 * / ____| | |
Shaun Nelson 32:5873d0638277 3 * | (___ ___ _ __ ___ | |_
Shaun Nelson 32:5873d0638277 4 * \___ \ / _ \ | '_ \ / _ \ | __|
Shaun Nelson 32:5873d0638277 5 * ____) | | __/ | | | | | __/ | |_
Shaun Nelson 32:5873d0638277 6 * |_____/ \___| |_| |_| \___| \__|
Shaun Nelson 32:5873d0638277 7 * (C) 2016 Senet, Inc
Shaun Nelson 32:5873d0638277 8 *
Shaun Nelson 32:5873d0638277 9 */
Shaun Nelson 32:5873d0638277 10
Shaun Nelson 32:5873d0638277 11 #include "board.h"
Shaun Nelson 32:5873d0638277 12 #include "senet_packet.h"
Shaun Nelson 32:5873d0638277 13
Shaun Nelson 32:5873d0638277 14
Shaun Nelson 32:5873d0638277 15 /******************************************************************************
Shaun Nelson 32:5873d0638277 16 * LoRaWAN Configuration *
Shaun Nelson 32:5873d0638277 17 ******************************************************************************/
Shaun Nelson 32:5873d0638277 18 // Senet Developer Portal Application EUI
Shaun Nelson 32:5873d0638277 19 static uint8_t APP_EUI[8] = {0x00,0x25,0x0C,0x00,0x00,0x01,0x00,0x01};
Shaun Nelson 32:5873d0638277 20
Shaun Nelson 32:5873d0638277 21 // Get Application Key from Senet Developer Portal Device Edit page
Shaun Nelson 32:5873d0638277 22 static uint8_t APP_KEY[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
Shaun Nelson 32:5873d0638277 23
Shaun Nelson 32:5873d0638277 24 #define DATARATE mDot::DR0
Shaun Nelson 32:5873d0638277 25 #define TXPOWER 20
Shaun Nelson 32:5873d0638277 26 #define JOIN_RETRIES 1
Shaun Nelson 32:5873d0638277 27
Shaun Nelson 32:5873d0638277 28 static std::vector<uint8_t> appEUI(APP_EUI,APP_EUI+sizeof(APP_EUI)/sizeof(uint8_t));
Shaun Nelson 32:5873d0638277 29 static std::vector<uint8_t> appKey(APP_KEY,APP_KEY+sizeof(APP_KEY)/sizeof(uint8_t));
Shaun Nelson 32:5873d0638277 30 static uint8_t fsb = 0;
Shaun Nelson 32:5873d0638277 31 static bool adrOn = true;
Shaun Nelson 32:5873d0638277 32
Shaun Nelson 32:5873d0638277 33 /******************************************************************************/
Shaun Nelson 32:5873d0638277 34
Shaun Nelson 32:5873d0638277 35 #define APP_TX_DUTY_CYCLE_NORMAL 300000 // 5 min
Shaun Nelson 32:5873d0638277 36 #define APP_TX_DUTY_CYCLE_ALARM 15000 // 15 s
Shaun Nelson 32:5873d0638277 37
Shaun Nelson 32:5873d0638277 38 // Backend configured state. Set true to enable alarm rate transmits until backend response
Shaun Nelson 32:5873d0638277 39 static bool BackendEnabled = false;
Shaun Nelson 32:5873d0638277 40
Shaun Nelson 32:5873d0638277 41 #define HORIZONTAL_ORIENTATION_VALUE 1 // transmitted value when device is horizontal
Shaun Nelson 32:5873d0638277 42 #define VERTICAL_ORIENTATION_VALUE 2 // transmitted value when device is vertical
Shaun Nelson 32:5873d0638277 43
Shaun Nelson 32:5873d0638277 44 // Transmit rate related variables
Shaun Nelson 32:5873d0638277 45 static bool NextTx = true;
Shaun Nelson 32:5873d0638277 46 static uint32_t AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL;
Shaun Nelson 32:5873d0638277 47
Shaun Nelson 32:5873d0638277 48 static Ticker joinTicker;
Shaun Nelson 32:5873d0638277 49 static Ticker nextTxTimer;
Shaun Nelson 32:5873d0638277 50 static BoardSensorData sensorData;
Shaun Nelson 32:5873d0638277 51 static BoardOrientation txOrientation;
Shaun Nelson 32:5873d0638277 52
Shaun Nelson 32:5873d0638277 53
Shaun Nelson 32:5873d0638277 54 // Backend service state (set to false if backend is not configured)
Shaun Nelson 32:5873d0638277 55 static bool BackendSynchronized = true;
Shaun Nelson 32:5873d0638277 56 BoardOrientation BackendOrientation;
Shaun Nelson 32:5873d0638277 57
Shaun Nelson 32:5873d0638277 58
Shaun Nelson 32:5873d0638277 59 // Board Orientation
Shaun Nelson 32:5873d0638277 60 enum EOrientationType
Shaun Nelson 32:5873d0638277 61 {
Shaun Nelson 32:5873d0638277 62 Orientation_Horizontal = 0,
Shaun Nelson 32:5873d0638277 63 Orientation_Vertical,
Shaun Nelson 32:5873d0638277 64 Orientation_Max,
Shaun Nelson 32:5873d0638277 65 };
Shaun Nelson 32:5873d0638277 66
Shaun Nelson 32:5873d0638277 67 //orientation change count
Shaun Nelson 32:5873d0638277 68 uint32_t orientationCount[Orientation_Max];
Shaun Nelson 32:5873d0638277 69 // orientation transmit count
Shaun Nelson 32:5873d0638277 70 uint32_t orientationTxCount[Orientation_Max];
Shaun Nelson 32:5873d0638277 71
Shaun Nelson 32:5873d0638277 72 // Forward
Shaun Nelson 32:5873d0638277 73 static void log_error(mDot* dot, const char* msg, int32_t retval);
Shaun Nelson 32:5873d0638277 74 static void joinLedToggle();
Shaun Nelson 32:5873d0638277 75 static void onNextTxTimerEvent();
Shaun Nelson 32:5873d0638277 76 static void ReceiveData(std::vector<uint8_t> frame);
Shaun Nelson 32:5873d0638277 77
Shaun Nelson 32:5873d0638277 78 void JoinNetwork()
Shaun Nelson 32:5873d0638277 79 {
Shaun Nelson 32:5873d0638277 80 bool ok;
Shaun Nelson 32:5873d0638277 81 int32_t mdot_ret;
Shaun Nelson 32:5873d0638277 82
Shaun Nelson 32:5873d0638277 83 do{
Shaun Nelson 32:5873d0638277 84 ok = true;
Shaun Nelson 32:5873d0638277 85
Shaun Nelson 32:5873d0638277 86 // reset to default config so we know what state we're in
Shaun Nelson 32:5873d0638277 87 mDotPtr->resetConfig();
Shaun Nelson 32:5873d0638277 88 mDotPtr->setLogLevel(6);
Shaun Nelson 32:5873d0638277 89 mDotPtr->setAntennaGain(-3);
Shaun Nelson 32:5873d0638277 90
Shaun Nelson 32:5873d0638277 91 // Read node ID
Shaun Nelson 32:5873d0638277 92 std::vector<uint8_t> mdot_EUI;
Shaun Nelson 32:5873d0638277 93 mdot_EUI = mDotPtr->getDeviceId();
Shaun Nelson 32:5873d0638277 94 printf("mDot EUI = ");
Shaun Nelson 32:5873d0638277 95
Shaun Nelson 32:5873d0638277 96 for (uint8_t i=0; i<mdot_EUI.size(); i++)
Shaun Nelson 32:5873d0638277 97 printf("%02x ", mdot_EUI[i]);
Shaun Nelson 32:5873d0638277 98 printf("\n\r");
Shaun Nelson 32:5873d0638277 99
Shaun Nelson 32:5873d0638277 100 /*
Shaun Nelson 32:5873d0638277 101 * This call sets up private or public mode on the MTDOT. Set the function to true if
Shaun Nelson 32:5873d0638277 102 * connecting to a public network
Shaun Nelson 32:5873d0638277 103 */
Shaun Nelson 32:5873d0638277 104 printf("setting Public Network Mode\r\n");
Shaun Nelson 32:5873d0638277 105 if ((mdot_ret = mDotPtr->setPublicNetwork(true)) != mDot::MDOT_OK)
Shaun Nelson 32:5873d0638277 106 log_error(mDotPtr, "failed to set Public Network Mode", mdot_ret);
Shaun Nelson 32:5873d0638277 107
Shaun Nelson 32:5873d0638277 108 mDotPtr->setTxDataRate(DATARATE);
Shaun Nelson 32:5873d0638277 109 mDotPtr->setTxPower(TXPOWER);
Shaun Nelson 32:5873d0638277 110 mDotPtr->setJoinRetries(JOIN_RETRIES);
Shaun Nelson 32:5873d0638277 111 mDotPtr->setJoinMode(mDot::OTA);
Shaun Nelson 32:5873d0638277 112
Shaun Nelson 32:5873d0638277 113 /*
Shaun Nelson 32:5873d0638277 114 * Frequency sub-band is valid for NAM only and for Private networks should be set to a value
Shaun Nelson 32:5873d0638277 115 * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only.
Shaun Nelson 32:5873d0638277 116 * This function can be commented out for EU networks
Shaun Nelson 32:5873d0638277 117 */
Shaun Nelson 32:5873d0638277 118 printf("setting frequency sub band\r\n");
Shaun Nelson 32:5873d0638277 119 if ((mdot_ret = mDotPtr->setFrequencySubBand(fsb)) != mDot::MDOT_OK) {
Shaun Nelson 32:5873d0638277 120 log_error(mDotPtr, "failed to set frequency sub band", mdot_ret);
Shaun Nelson 32:5873d0638277 121 ok = false;
Shaun Nelson 32:5873d0638277 122 }
Shaun Nelson 32:5873d0638277 123
Shaun Nelson 32:5873d0638277 124 printf("setting ADR\r\n");
Shaun Nelson 32:5873d0638277 125 if ((mdot_ret = mDotPtr->setAdr(adrOn)) != mDot::MDOT_OK) {
Shaun Nelson 32:5873d0638277 126 log_error(mDotPtr, "failed to set ADR", mdot_ret);
Shaun Nelson 32:5873d0638277 127 ok = false;
Shaun Nelson 32:5873d0638277 128 }
Shaun Nelson 32:5873d0638277 129
Shaun Nelson 32:5873d0638277 130 /*
Shaun Nelson 32:5873d0638277 131 * setNetworkName is used for private networks.
Shaun Nelson 32:5873d0638277 132 * Use setNetworkID(AppID) for public networks
Shaun Nelson 32:5873d0638277 133 */
Shaun Nelson 32:5873d0638277 134 printf("setting network name\r\n");
Shaun Nelson 32:5873d0638277 135 if ((mdot_ret = mDotPtr->setNetworkId(appEUI)) != mDot::MDOT_OK) {
Shaun Nelson 32:5873d0638277 136 log_error(mDotPtr, "failed to set network name", mdot_ret);
Shaun Nelson 32:5873d0638277 137 ok = false;
Shaun Nelson 32:5873d0638277 138 }
Shaun Nelson 32:5873d0638277 139
Shaun Nelson 32:5873d0638277 140 /*
Shaun Nelson 32:5873d0638277 141 * setNetworkPassphrase is used for private networks
Shaun Nelson 32:5873d0638277 142 * Use setNetworkKey for public networks
Shaun Nelson 32:5873d0638277 143 */
Shaun Nelson 32:5873d0638277 144 printf("setting network key\r\n");
Shaun Nelson 32:5873d0638277 145 if ((mdot_ret = mDotPtr->setNetworkKey(appKey)) != mDot::MDOT_OK) {
Shaun Nelson 32:5873d0638277 146 log_error(mDotPtr, "failed to set network password", mdot_ret);
Shaun Nelson 32:5873d0638277 147 ok = false;
Shaun Nelson 32:5873d0638277 148 }
Shaun Nelson 32:5873d0638277 149
Shaun Nelson 32:5873d0638277 150 } while(ok == false);
Shaun Nelson 32:5873d0638277 151
Shaun Nelson 32:5873d0638277 152 joinTicker.attach(joinLedToggle,1);
Shaun Nelson 32:5873d0638277 153
Shaun Nelson 32:5873d0638277 154 // attempt to join the network
Shaun Nelson 32:5873d0638277 155 printf("joining network\r\n");
Shaun Nelson 32:5873d0638277 156 while ((mdot_ret = mDotPtr->joinNetwork()) != mDot::MDOT_OK)
Shaun Nelson 32:5873d0638277 157 {
Shaun Nelson 32:5873d0638277 158 log_error(mDotPtr,"failed to join network:", mdot_ret);
Shaun Nelson 32:5873d0638277 159 uint32_t delay_s = (mDotPtr->getNextTxMs() / 1000) + 1;
Shaun Nelson 32:5873d0638277 160 wait(delay_s);
Shaun Nelson 32:5873d0638277 161 }
Shaun Nelson 32:5873d0638277 162
Shaun Nelson 32:5873d0638277 163 printf("network joined\r\n");
Shaun Nelson 32:5873d0638277 164
Shaun Nelson 32:5873d0638277 165 joinTicker.detach();
Shaun Nelson 32:5873d0638277 166 appLED=1;
Shaun Nelson 32:5873d0638277 167 }
Shaun Nelson 32:5873d0638277 168
Shaun Nelson 32:5873d0638277 169 void SendFrame()
Shaun Nelson 32:5873d0638277 170 {
Shaun Nelson 32:5873d0638277 171 std::vector<uint8_t> frame;
Shaun Nelson 32:5873d0638277 172 int32_t mdot_ret;
Shaun Nelson 32:5873d0638277 173 uint8_t buffer[20];
Shaun Nelson 32:5873d0638277 174 SensorPacket packet(buffer, sizeof(buffer));
Shaun Nelson 32:5873d0638277 175
Shaun Nelson 32:5873d0638277 176 // Sensor packet type serialized to the frame buffer
Shaun Nelson 32:5873d0638277 177 packet.setPrimarySensor(txOrientation.vertical ? VERTICAL_ORIENTATION_VALUE : HORIZONTAL_ORIENTATION_VALUE);
Shaun Nelson 32:5873d0638277 178 packet.setTemperature(sensorData.temperature);
Shaun Nelson 32:5873d0638277 179 packet.setPressure(sensorData.pressure);
Shaun Nelson 32:5873d0638277 180 packet.serialize();
Shaun Nelson 32:5873d0638277 181
Shaun Nelson 32:5873d0638277 182 frame.assign(packet.payload(), packet.payload() + packet.length());
Shaun Nelson 32:5873d0638277 183 if ((mdot_ret = mDotPtr->send(frame)) != mDot::MDOT_OK)
Shaun Nelson 32:5873d0638277 184 {
Shaun Nelson 32:5873d0638277 185 log_error(mDotPtr, "failed to send", mdot_ret);
Shaun Nelson 32:5873d0638277 186 }
Shaun Nelson 32:5873d0638277 187 else
Shaun Nelson 32:5873d0638277 188 {
Shaun Nelson 32:5873d0638277 189 printf("successfully sent data\r\n");
Shaun Nelson 32:5873d0638277 190 frame.clear();
Shaun Nelson 32:5873d0638277 191 if ((mdot_ret = mDotPtr->recv(frame)) == mDot::MDOT_OK)
Shaun Nelson 32:5873d0638277 192 {
Shaun Nelson 32:5873d0638277 193 printf("recv data: ");
Shaun Nelson 32:5873d0638277 194 for(uint32_t i = 0;i < frame.size();i++)
Shaun Nelson 32:5873d0638277 195 printf("%02X",frame[i]);
Shaun Nelson 32:5873d0638277 196 printf("\r\n");
Shaun Nelson 32:5873d0638277 197
Shaun Nelson 32:5873d0638277 198 ReceiveData(frame);
Shaun Nelson 32:5873d0638277 199 }
Shaun Nelson 32:5873d0638277 200 }
Shaun Nelson 32:5873d0638277 201 }
Shaun Nelson 32:5873d0638277 202
Shaun Nelson 32:5873d0638277 203 void ReceiveData(std::vector<uint8_t> frame)
Shaun Nelson 32:5873d0638277 204 {
Shaun Nelson 32:5873d0638277 205 BackendOrientation.vertical = (frame[0] == VERTICAL_ORIENTATION_VALUE);
Shaun Nelson 32:5873d0638277 206
Shaun Nelson 32:5873d0638277 207 if( BackendOrientation.vertical == txOrientation.vertical )
Shaun Nelson 32:5873d0638277 208 BackendSynchronized = true;
Shaun Nelson 32:5873d0638277 209 }
Shaun Nelson 32:5873d0638277 210
Shaun Nelson 32:5873d0638277 211 inline uint8_t getNextTxOrientation()
Shaun Nelson 32:5873d0638277 212 {
Shaun Nelson 32:5873d0638277 213 static uint8_t txOrientationType = Orientation_Max;
Shaun Nelson 32:5873d0638277 214
Shaun Nelson 32:5873d0638277 215 if (++txOrientationType >= Orientation_Max)
Shaun Nelson 32:5873d0638277 216 txOrientationType = 0;
Shaun Nelson 32:5873d0638277 217
Shaun Nelson 32:5873d0638277 218 for( ; txOrientationType < Orientation_Max; txOrientationType++)
Shaun Nelson 32:5873d0638277 219 {
Shaun Nelson 32:5873d0638277 220 if( orientationTxCount[txOrientationType] < orientationCount[txOrientationType] )
Shaun Nelson 32:5873d0638277 221 {
Shaun Nelson 32:5873d0638277 222 orientationTxCount[txOrientationType]++;
Shaun Nelson 32:5873d0638277 223 break;
Shaun Nelson 32:5873d0638277 224 }
Shaun Nelson 32:5873d0638277 225 }
Shaun Nelson 32:5873d0638277 226
Shaun Nelson 32:5873d0638277 227 return txOrientationType;
Shaun Nelson 32:5873d0638277 228 }
Shaun Nelson 32:5873d0638277 229
Shaun Nelson 32:5873d0638277 230 inline void incrementOrientation(BoardOrientation &orientation)
Shaun Nelson 32:5873d0638277 231 {
Shaun Nelson 32:5873d0638277 232 orientation.vertical ? orientationCount[Orientation_Vertical]++ : orientationCount[Orientation_Horizontal]++;
Shaun Nelson 32:5873d0638277 233 }
Shaun Nelson 32:5873d0638277 234
Shaun Nelson 32:5873d0638277 235
Shaun Nelson 32:5873d0638277 236 int main()
Shaun Nelson 32:5873d0638277 237 {
Shaun Nelson 32:5873d0638277 238 time_t lastTxT;
Shaun Nelson 32:5873d0638277 239 BoardOrientation lastOrientation;
Shaun Nelson 32:5873d0638277 240
Shaun Nelson 32:5873d0638277 241
Shaun Nelson 32:5873d0638277 242 memset(orientationCount, 0, sizeof(orientationCount));
Shaun Nelson 32:5873d0638277 243 memset(orientationTxCount, 0, sizeof(orientationTxCount));
Shaun Nelson 32:5873d0638277 244
Shaun Nelson 32:5873d0638277 245 // Initialize Board
Shaun Nelson 32:5873d0638277 246 BoardInit();
Shaun Nelson 32:5873d0638277 247
Shaun Nelson 32:5873d0638277 248 // Join Network
Shaun Nelson 32:5873d0638277 249 JoinNetwork();
Shaun Nelson 32:5873d0638277 250
Shaun Nelson 32:5873d0638277 251 // Start Board sensors
Shaun Nelson 32:5873d0638277 252 CBoard::Start();
Shaun Nelson 32:5873d0638277 253
Shaun Nelson 32:5873d0638277 254 // Send initial state
Shaun Nelson 32:5873d0638277 255 if( CBoard::ReadSensors(sensorData) == Board_Ok )
Shaun Nelson 32:5873d0638277 256 {
Shaun Nelson 32:5873d0638277 257 lastOrientation = sensorData.orientation;
Shaun Nelson 32:5873d0638277 258 incrementOrientation(lastOrientation);
Shaun Nelson 32:5873d0638277 259 }
Shaun Nelson 32:5873d0638277 260
Shaun Nelson 32:5873d0638277 261 // Start transmit timer
Shaun Nelson 32:5873d0638277 262 nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
Shaun Nelson 32:5873d0638277 263
Shaun Nelson 32:5873d0638277 264 while( true )
Shaun Nelson 32:5873d0638277 265 {
Shaun Nelson 32:5873d0638277 266 uint8_t nextTxOrientation;
Shaun Nelson 32:5873d0638277 267
Shaun Nelson 32:5873d0638277 268 // Read sensors
Shaun Nelson 32:5873d0638277 269 if( CBoard::ReadSensors(sensorData) == Board_Ok )
Shaun Nelson 32:5873d0638277 270 {
Shaun Nelson 32:5873d0638277 271 if( sensorData.orientation.vertical != lastOrientation.vertical )
Shaun Nelson 32:5873d0638277 272 {
Shaun Nelson 32:5873d0638277 273 lastOrientation = sensorData.orientation;
Shaun Nelson 32:5873d0638277 274 incrementOrientation(lastOrientation);
Shaun Nelson 32:5873d0638277 275 }
Shaun Nelson 32:5873d0638277 276 }
Shaun Nelson 32:5873d0638277 277
Shaun Nelson 32:5873d0638277 278 // Determine next orientation to transmit after backend sync of the last orientation is finished
Shaun Nelson 32:5873d0638277 279 if( ( BackendSynchronized == true ) && ( ( nextTxOrientation = getNextTxOrientation() ) < Orientation_Max ) )
Shaun Nelson 32:5873d0638277 280 {
Shaun Nelson 32:5873d0638277 281 BackendSynchronized = false;
Shaun Nelson 32:5873d0638277 282
Shaun Nelson 32:5873d0638277 283 switch(nextTxOrientation)
Shaun Nelson 32:5873d0638277 284 {
Shaun Nelson 32:5873d0638277 285 case Orientation_Horizontal:
Shaun Nelson 32:5873d0638277 286 txOrientation.vertical = false;
Shaun Nelson 32:5873d0638277 287 break;
Shaun Nelson 32:5873d0638277 288 case Orientation_Vertical:
Shaun Nelson 32:5873d0638277 289 txOrientation.vertical = true;
Shaun Nelson 32:5873d0638277 290 break;
Shaun Nelson 32:5873d0638277 291 default:
Shaun Nelson 32:5873d0638277 292 break;
Shaun Nelson 32:5873d0638277 293 }
Shaun Nelson 32:5873d0638277 294
Shaun Nelson 32:5873d0638277 295 // Get elapsed time since last transmit
Shaun Nelson 32:5873d0638277 296 time_t currT = time(NULL);
Shaun Nelson 32:5873d0638277 297 time_t elapsedT = ( currT - lastTxT ) * 1e3;
Shaun Nelson 32:5873d0638277 298
Shaun Nelson 32:5873d0638277 299 // Transmit now if elapsed time since last tx is greater than alarm mode dutycycle
Shaun Nelson 32:5873d0638277 300 if( elapsedT >= APP_TX_DUTY_CYCLE_ALARM )
Shaun Nelson 32:5873d0638277 301 {
Shaun Nelson 32:5873d0638277 302 nextTxTimer.detach();
Shaun Nelson 32:5873d0638277 303 NextTx = true;
Shaun Nelson 32:5873d0638277 304 }
Shaun Nelson 32:5873d0638277 305 // Otherwise wait until alarm time has elapased
Shaun Nelson 32:5873d0638277 306 else
Shaun Nelson 32:5873d0638277 307 {
Shaun Nelson 32:5873d0638277 308 nextTxTimer.detach();
Shaun Nelson 32:5873d0638277 309 nextTxTimer.attach_us(onNextTxTimerEvent, (APP_TX_DUTY_CYCLE_ALARM - elapsedT)* 1e3);
Shaun Nelson 32:5873d0638277 310 }
Shaun Nelson 32:5873d0638277 311
Shaun Nelson 32:5873d0638277 312 AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM;
Shaun Nelson 32:5873d0638277 313 }
Shaun Nelson 32:5873d0638277 314
Shaun Nelson 32:5873d0638277 315 if ( NextTx == true )
Shaun Nelson 32:5873d0638277 316 {
Shaun Nelson 32:5873d0638277 317 /* Backend synchronized flag set true when
Shaun Nelson 32:5873d0638277 318 * - Backend not enabled
Shaun Nelson 32:5873d0638277 319 * - Downlink received for current orientation
Shaun Nelson 32:5873d0638277 320 */
Shaun Nelson 32:5873d0638277 321 BackendSynchronized = !BackendEnabled;
Shaun Nelson 32:5873d0638277 322
Shaun Nelson 32:5873d0638277 323 // Transmit application frame
Shaun Nelson 32:5873d0638277 324 SendFrame();
Shaun Nelson 32:5873d0638277 325 lastTxT = time(NULL);
Shaun Nelson 32:5873d0638277 326
Shaun Nelson 32:5873d0638277 327 NextTx = false;
Shaun Nelson 32:5873d0638277 328
Shaun Nelson 32:5873d0638277 329 // Fast transmit rate while backend is out of sync with device state
Shaun Nelson 32:5873d0638277 330 if(BackendSynchronized == false)
Shaun Nelson 32:5873d0638277 331 {
Shaun Nelson 32:5873d0638277 332 if( ( AppTxDutyCycle != APP_TX_DUTY_CYCLE_ALARM ) )
Shaun Nelson 32:5873d0638277 333 {
Shaun Nelson 32:5873d0638277 334 AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM;
Shaun Nelson 32:5873d0638277 335 nextTxTimer.detach();
Shaun Nelson 32:5873d0638277 336 nextTxTimer.attach_us(onNextTxTimerEvent, APP_TX_DUTY_CYCLE_ALARM * 1e3);
Shaun Nelson 32:5873d0638277 337 }
Shaun Nelson 32:5873d0638277 338 }
Shaun Nelson 32:5873d0638277 339 else if( AppTxDutyCycle != APP_TX_DUTY_CYCLE_NORMAL )
Shaun Nelson 32:5873d0638277 340 {
Shaun Nelson 32:5873d0638277 341 AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL;
Shaun Nelson 32:5873d0638277 342 nextTxTimer.detach();
Shaun Nelson 32:5873d0638277 343 nextTxTimer.attach_us(onNextTxTimerEvent, APP_TX_DUTY_CYCLE_NORMAL * 1e3);
Shaun Nelson 32:5873d0638277 344 }
Shaun Nelson 32:5873d0638277 345 }
Shaun Nelson 32:5873d0638277 346
Shaun Nelson 32:5873d0638277 347 // Delay before next sensor poll
Shaun Nelson 32:5873d0638277 348 osDelay(2000);
Shaun Nelson 32:5873d0638277 349 }
Shaun Nelson 32:5873d0638277 350 }
Shaun Nelson 32:5873d0638277 351
Shaun Nelson 32:5873d0638277 352
Shaun Nelson 32:5873d0638277 353 /*
Shaun Nelson 32:5873d0638277 354 * prints of mDot error
Shaun Nelson 32:5873d0638277 355 */
Shaun Nelson 32:5873d0638277 356 void log_error(mDot* dot, const char* msg, int32_t retval)
Shaun Nelson 32:5873d0638277 357 {
Shaun Nelson 32:5873d0638277 358 printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
Shaun Nelson 32:5873d0638277 359 }
Shaun Nelson 32:5873d0638277 360
Shaun Nelson 32:5873d0638277 361 void joinLedToggle()
Shaun Nelson 32:5873d0638277 362 {
Shaun Nelson 32:5873d0638277 363 appLED= !appLED;
Shaun Nelson 32:5873d0638277 364 }
Shaun Nelson 32:5873d0638277 365
Shaun Nelson 32:5873d0638277 366 void onNextTxTimerEvent( void )
Shaun Nelson 32:5873d0638277 367 {
Shaun Nelson 32:5873d0638277 368 NextTx = true;
Shaun Nelson 32:5873d0638277 369 }
Shaun Nelson 32:5873d0638277 370