Senet / Mbed OS MTDOT-UDKDemo

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

Fork of MTDOT-UDKDemo_Senet by canuck lehead

Committer:
shaunkrnelson
Date:
Fri Aug 25 13:44:11 2017 +0000
Branch:
develop
Revision:
29:055824db068a
Parent:
28:4fd8a894a403
Child:
32:5873d0638277
bzero not found, switched to memset;

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