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:
Wed Aug 30 12:35:33 2017 -0400
Revision:
43:55e7bb4d9b60
Parent:
39:022b327d6bf0
Simplified orientation change detection
Implement button press events
EVB Button1(left) for backend enabled state status (2 sblink = disabled, 3 = enabled)
EVB Button2(right) toggle bacend enabled status

LED Behavior
- LED solid when backend out of sync
- LED blinks on downlink

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 36:cd1077f40dbf 34
Shaun Nelson 36:cd1077f40dbf 35 /******************************************************************************
Shaun Nelson 36:cd1077f40dbf 36 * Application Configuration *
Shaun Nelson 36:cd1077f40dbf 37 ******************************************************************************/
Shaun Nelson 43:55e7bb4d9b60 38 #define APP_TX_DUTY_CYCLE_NORMAL 300000 // 5 min
Shaun Nelson 36:cd1077f40dbf 39 #define APP_TX_DUTY_CYCLE_ALARM 15000 // 15 s
Shaun Nelson 32:5873d0638277 40
Shaun Nelson 32:5873d0638277 41 // Backend configured state. Set true to enable alarm rate transmits until backend response
Shaun Nelson 32:5873d0638277 42 static bool BackendEnabled = false;
Shaun Nelson 36:cd1077f40dbf 43 /******************************************************************************/
Shaun Nelson 32:5873d0638277 44
Shaun Nelson 36:cd1077f40dbf 45 // Transmitted orientation values
Shaun Nelson 32:5873d0638277 46 #define HORIZONTAL_ORIENTATION_VALUE 1 // transmitted value when device is horizontal
Shaun Nelson 32:5873d0638277 47 #define VERTICAL_ORIENTATION_VALUE 2 // transmitted value when device is vertical
Shaun Nelson 32:5873d0638277 48
Shaun Nelson 32:5873d0638277 49
Shaun Nelson 43:55e7bb4d9b60 50 // Set to true when backend is synchronized
Shaun Nelson 43:55e7bb4d9b60 51 static bool BackendSynchronized = true;
Shaun Nelson 43:55e7bb4d9b60 52 // Set to true to force backend resync
Shaun Nelson 43:55e7bb4d9b60 53 static bool OrientationInit = true;
Shaun Nelson 43:55e7bb4d9b60 54 static bool DisplayBackendState = true;
Shaun Nelson 36:cd1077f40dbf 55 static BoardOrientation BackendOrientation;
Shaun Nelson 32:5873d0638277 56 static Ticker joinTicker;
Shaun Nelson 32:5873d0638277 57 static Ticker nextTxTimer;
Shaun Nelson 32:5873d0638277 58 static BoardSensorData sensorData;
Shaun Nelson 32:5873d0638277 59 static BoardOrientation txOrientation;
Shaun Nelson 43:55e7bb4d9b60 60 static BoardOrientation lastOrientation;
Shaun Nelson 36:cd1077f40dbf 61 static bool NextTx = true;
Shaun Nelson 36:cd1077f40dbf 62 static uint32_t AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL;
Shaun Nelson 43:55e7bb4d9b60 63 static uint32_t orientationChangeCount = 0;
Shaun Nelson 32:5873d0638277 64
Shaun Nelson 36:cd1077f40dbf 65 // Orientation comparison
Shaun Nelson 36:cd1077f40dbf 66 inline bool orientationIsEqual(BoardOrientation &a, BoardOrientation &b)
Shaun Nelson 36:cd1077f40dbf 67 {
Shaun Nelson 36:cd1077f40dbf 68 // For this application only vertical/horizontal state is tested
Shaun Nelson 36:cd1077f40dbf 69 return ( a.vertical == b.vertical );
Shaun Nelson 36:cd1077f40dbf 70 }
Shaun Nelson 36:cd1077f40dbf 71
Shaun Nelson 32:5873d0638277 72 static void log_error(mDot* dot, const char* msg, int32_t retval);
Shaun Nelson 32:5873d0638277 73 static void joinLedToggle();
Shaun Nelson 32:5873d0638277 74 static void onNextTxTimerEvent();
Shaun Nelson 32:5873d0638277 75 static void ReceiveData(std::vector<uint8_t> frame);
Shaun Nelson 32:5873d0638277 76
Shaun Nelson 36:cd1077f40dbf 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 39:022b327d6bf0 152 joinTicker.attach(joinLedToggle, 5);
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 39:022b327d6bf0 166 CBoard::SetLED(1, false);
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 43:55e7bb4d9b60 206 BackendSynchronized = !BackendEnabled || orientationIsEqual(BackendOrientation, txOrientation);
Shaun Nelson 32:5873d0638277 207
Shaun Nelson 43:55e7bb4d9b60 208 // Blink LED
Shaun Nelson 43:55e7bb4d9b60 209 bool ledState = false;
Shaun Nelson 43:55e7bb4d9b60 210 for(uint8_t i=0; i<3; i++)
Shaun Nelson 43:55e7bb4d9b60 211 {
Shaun Nelson 43:55e7bb4d9b60 212 CBoard::SetLED(1, ledState);
Shaun Nelson 43:55e7bb4d9b60 213 ledState = !ledState;
Shaun Nelson 43:55e7bb4d9b60 214 osDelay(500);
Shaun Nelson 43:55e7bb4d9b60 215 }
Shaun Nelson 43:55e7bb4d9b60 216 }
Shaun Nelson 43:55e7bb4d9b60 217
Shaun Nelson 43:55e7bb4d9b60 218 static void onButtonPress(uint8_t buttonNum)
Shaun Nelson 43:55e7bb4d9b60 219 {
Shaun Nelson 43:55e7bb4d9b60 220 DisplayBackendState = true;
Shaun Nelson 43:55e7bb4d9b60 221
Shaun Nelson 43:55e7bb4d9b60 222 if(buttonNum == 1)
Shaun Nelson 43:55e7bb4d9b60 223 {
Shaun Nelson 43:55e7bb4d9b60 224 OrientationInit = true;
Shaun Nelson 43:55e7bb4d9b60 225 BackendEnabled = !BackendEnabled;
Shaun Nelson 43:55e7bb4d9b60 226
Shaun Nelson 43:55e7bb4d9b60 227 // Start next transmit immediately
Shaun Nelson 43:55e7bb4d9b60 228 if( BackendEnabled && !NextTx )
Shaun Nelson 43:55e7bb4d9b60 229 {
Shaun Nelson 43:55e7bb4d9b60 230 nextTxTimer.detach();
Shaun Nelson 43:55e7bb4d9b60 231 nextTxTimer.attach_us(onNextTxTimerEvent, 1000);
Shaun Nelson 43:55e7bb4d9b60 232 }
Shaun Nelson 43:55e7bb4d9b60 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 43:55e7bb4d9b60 238 time_t lastTxT;
Shaun Nelson 32:5873d0638277 239
Shaun Nelson 32:5873d0638277 240 // Initialize Board
Shaun Nelson 32:5873d0638277 241 BoardInit();
Shaun Nelson 32:5873d0638277 242
Shaun Nelson 43:55e7bb4d9b60 243 // Register pushbutton handler
Shaun Nelson 43:55e7bb4d9b60 244 CBoard::SetButtonCallback(onButtonPress);
Shaun Nelson 43:55e7bb4d9b60 245
Shaun Nelson 32:5873d0638277 246 // Join Network
Shaun Nelson 32:5873d0638277 247 JoinNetwork();
Shaun Nelson 32:5873d0638277 248
Shaun Nelson 32:5873d0638277 249 // Start Board sensors
Shaun Nelson 32:5873d0638277 250 CBoard::Start();
Shaun Nelson 32:5873d0638277 251
Shaun Nelson 32:5873d0638277 252 while( true )
Shaun Nelson 32:5873d0638277 253 {
Shaun Nelson 32:5873d0638277 254 // Read sensors
Shaun Nelson 32:5873d0638277 255 if( CBoard::ReadSensors(sensorData) == Board_Ok )
Shaun Nelson 32:5873d0638277 256 {
Shaun Nelson 43:55e7bb4d9b60 257 if( !orientationIsEqual(sensorData.orientation, lastOrientation) || OrientationInit)
Shaun Nelson 32:5873d0638277 258 {
Shaun Nelson 43:55e7bb4d9b60 259 orientationChangeCount++;
Shaun Nelson 32:5873d0638277 260 lastOrientation = sensorData.orientation;
Shaun Nelson 32:5873d0638277 261 }
Shaun Nelson 32:5873d0638277 262 }
Shaun Nelson 32:5873d0638277 263
Shaun Nelson 43:55e7bb4d9b60 264 // Initialize orientation state
Shaun Nelson 43:55e7bb4d9b60 265 if( OrientationInit )
Shaun Nelson 43:55e7bb4d9b60 266 {
Shaun Nelson 43:55e7bb4d9b60 267 OrientationInit = false;
Shaun Nelson 43:55e7bb4d9b60 268 orientationChangeCount = 1;
Shaun Nelson 43:55e7bb4d9b60 269 // Set tx orientation to opposite of current as it will be toggled before transmit
Shaun Nelson 43:55e7bb4d9b60 270 txOrientation.vertical = !lastOrientation.vertical;
Shaun Nelson 43:55e7bb4d9b60 271 // Set backend to be out of sync
Shaun Nelson 43:55e7bb4d9b60 272 BackendOrientation.vertical = txOrientation.vertical;
Shaun Nelson 43:55e7bb4d9b60 273 }
Shaun Nelson 43:55e7bb4d9b60 274
Shaun Nelson 43:55e7bb4d9b60 275 // Display backend enabled state
Shaun Nelson 43:55e7bb4d9b60 276 if(DisplayBackendState)
Shaun Nelson 43:55e7bb4d9b60 277 {
Shaun Nelson 43:55e7bb4d9b60 278 DisplayBackendState = false;
Shaun Nelson 43:55e7bb4d9b60 279
Shaun Nelson 43:55e7bb4d9b60 280 uint8_t ledToggleCount = BackendEnabled ? 3 : 2;
Shaun Nelson 43:55e7bb4d9b60 281 CBoard::SetLED(1, false);
Shaun Nelson 43:55e7bb4d9b60 282 // blink LED to indicate backend state
Shaun Nelson 43:55e7bb4d9b60 283 for(uint32_t i=0; i < ledToggleCount*2; i++)
Shaun Nelson 43:55e7bb4d9b60 284 {
Shaun Nelson 43:55e7bb4d9b60 285 osDelay(1000);
Shaun Nelson 43:55e7bb4d9b60 286 CBoard::ToggleLED(1);
Shaun Nelson 43:55e7bb4d9b60 287 }
Shaun Nelson 43:55e7bb4d9b60 288
Shaun Nelson 43:55e7bb4d9b60 289 // Set LED to reflect Backend synchronized state
Shaun Nelson 43:55e7bb4d9b60 290 CBoard::SetLED(1, BackendOrientation.vertical != txOrientation.vertical);
Shaun Nelson 43:55e7bb4d9b60 291 }
Shaun Nelson 43:55e7bb4d9b60 292
Shaun Nelson 43:55e7bb4d9b60 293 // Get next orientation change to transmit after backend sync of the last transmitted orientation is finished
Shaun Nelson 43:55e7bb4d9b60 294 if( ( BackendSynchronized == true ) && ( orientationChangeCount != 0 ) )
Shaun Nelson 32:5873d0638277 295 {
Shaun Nelson 36:cd1077f40dbf 296 BackendSynchronized = false;
Shaun Nelson 43:55e7bb4d9b60 297
Shaun Nelson 43:55e7bb4d9b60 298 // Set transmit orientation
Shaun Nelson 43:55e7bb4d9b60 299 txOrientation.vertical = !txOrientation.vertical;
Shaun Nelson 43:55e7bb4d9b60 300 orientationChangeCount--;
Shaun Nelson 43:55e7bb4d9b60 301
Shaun Nelson 43:55e7bb4d9b60 302 // Turn on out-of-sync LED
Shaun Nelson 39:022b327d6bf0 303 CBoard::SetLED(1, true);
Shaun Nelson 32:5873d0638277 304
Shaun Nelson 32:5873d0638277 305 // Get elapsed time since last transmit
Shaun Nelson 32:5873d0638277 306 time_t currT = time(NULL);
Shaun Nelson 32:5873d0638277 307 time_t elapsedT = ( currT - lastTxT ) * 1e3;
Shaun Nelson 32:5873d0638277 308
Shaun Nelson 36:cd1077f40dbf 309 // Stop transmit timer
Shaun Nelson 36:cd1077f40dbf 310 AppTxDutyCycle = 0;
Shaun Nelson 36:cd1077f40dbf 311 nextTxTimer.detach();
Shaun Nelson 36:cd1077f40dbf 312
Shaun Nelson 36:cd1077f40dbf 313 // Wait to transmit until elapsed time is greater than alarm mode dutycycle
Shaun Nelson 43:55e7bb4d9b60 314 NextTx = ( elapsedT >= APP_TX_DUTY_CYCLE_ALARM );
Shaun Nelson 43:55e7bb4d9b60 315 if( !NextTx )
Shaun Nelson 32:5873d0638277 316 {
Shaun Nelson 36:cd1077f40dbf 317 AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM - elapsedT;
Shaun Nelson 36:cd1077f40dbf 318 nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
Shaun Nelson 32:5873d0638277 319 }
Shaun Nelson 32:5873d0638277 320 }
Shaun Nelson 32:5873d0638277 321
Shaun Nelson 32:5873d0638277 322 if ( NextTx == true )
Shaun Nelson 32:5873d0638277 323 {
Shaun Nelson 36:cd1077f40dbf 324 NextTx = false;
Shaun Nelson 36:cd1077f40dbf 325
Shaun Nelson 32:5873d0638277 326 /* Backend synchronized flag set true when
Shaun Nelson 36:cd1077f40dbf 327 * o Backend not enabled
Shaun Nelson 43:55e7bb4d9b60 328 * o Backend in sync
Shaun Nelson 32:5873d0638277 329 */
Shaun Nelson 36:cd1077f40dbf 330 BackendSynchronized = !BackendEnabled || orientationIsEqual(BackendOrientation, txOrientation);
Shaun Nelson 32:5873d0638277 331
Shaun Nelson 32:5873d0638277 332 // Transmit application frame
Shaun Nelson 32:5873d0638277 333 SendFrame();
Shaun Nelson 36:cd1077f40dbf 334
Shaun Nelson 36:cd1077f40dbf 335 // Update transmit timestamp
Shaun Nelson 32:5873d0638277 336 lastTxT = time(NULL);
Shaun Nelson 32:5873d0638277 337
Shaun Nelson 36:cd1077f40dbf 338 // Set next transmit time
Shaun Nelson 43:55e7bb4d9b60 339 if( BackendSynchronized == false )
Shaun Nelson 32:5873d0638277 340 {
Shaun Nelson 32:5873d0638277 341 if( ( AppTxDutyCycle != APP_TX_DUTY_CYCLE_ALARM ) )
Shaun Nelson 32:5873d0638277 342 {
Shaun Nelson 32:5873d0638277 343 AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM;
Shaun Nelson 32:5873d0638277 344 nextTxTimer.detach();
Shaun Nelson 36:cd1077f40dbf 345 nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
Shaun Nelson 32:5873d0638277 346 }
Shaun Nelson 32:5873d0638277 347 }
Shaun Nelson 32:5873d0638277 348 else if( AppTxDutyCycle != APP_TX_DUTY_CYCLE_NORMAL )
Shaun Nelson 32:5873d0638277 349 {
Shaun Nelson 32:5873d0638277 350 AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL;
Shaun Nelson 32:5873d0638277 351 nextTxTimer.detach();
Shaun Nelson 36:cd1077f40dbf 352 nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3);
Shaun Nelson 32:5873d0638277 353 }
Shaun Nelson 39:022b327d6bf0 354
Shaun Nelson 43:55e7bb4d9b60 355 // Set LED to reflect Backend synchronized state
Shaun Nelson 43:55e7bb4d9b60 356 CBoard::SetLED(1, BackendOrientation.vertical != txOrientation.vertical);
Shaun Nelson 32:5873d0638277 357 }
Shaun Nelson 32:5873d0638277 358
Shaun Nelson 32:5873d0638277 359 // Delay before next sensor poll
Shaun Nelson 32:5873d0638277 360 osDelay(2000);
Shaun Nelson 32:5873d0638277 361 }
Shaun Nelson 32:5873d0638277 362 }
Shaun Nelson 32:5873d0638277 363
Shaun Nelson 32:5873d0638277 364
Shaun Nelson 32:5873d0638277 365 /*
Shaun Nelson 32:5873d0638277 366 * prints of mDot error
Shaun Nelson 32:5873d0638277 367 */
Shaun Nelson 32:5873d0638277 368 void log_error(mDot* dot, const char* msg, int32_t retval)
Shaun Nelson 32:5873d0638277 369 {
Shaun Nelson 32:5873d0638277 370 printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str());
Shaun Nelson 32:5873d0638277 371 }
Shaun Nelson 32:5873d0638277 372
Shaun Nelson 39:022b327d6bf0 373 void joinLedToggle() { CBoard::ToggleLED(1); }
Shaun Nelson 39:022b327d6bf0 374
Shaun Nelson 36:cd1077f40dbf 375 void onNextTxTimerEvent( void ) { NextTx = true; }