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 09:29:01 2017 -0400
Branch:
develop
Revision:
28:4fd8a894a403
Parent:
27:1753a44fa9ec
Child:
29:055824db068a
Create board api class and implement for EVB and UDK mDot boards.
UDK platform is working
Do not try EVB, not sure it even compiles

Who changed what in which revision?

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