Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: libmDot-mbed5 DOGS102 ISL29011 MMA845x MPL3115A2 NCP5623B X_NUCLEO_IKS01A1 Senet_Packet
Fork of MTDOT-UDKDemo_Senet by
main.cpp@27:1753a44fa9ec, 2017-08-24 (annotated)
- Committer:
- Shaun Nelson
- Date:
- Thu Aug 24 17:56:53 2017 -0400
- Branch:
- develop
- Revision:
- 27:1753a44fa9ec
- Child:
- 28:4fd8a894a403
Reimplement for mbed 5
Move bsp to board files
UDK implemented
EVB in progress
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Shaun Nelson |
27:1753a44fa9ec | 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 | * |_____/ \___| |_| |_| \___| \__| |
| Shaun Nelson |
27:1753a44fa9ec | 8 | * (C) 2016 Senet, Inc |
| Shaun Nelson |
27:1753a44fa9ec | 9 | * |
| Shaun Nelson |
27:1753a44fa9ec | 10 | */ |
| Shaun Nelson |
27:1753a44fa9ec | 11 | |
| Shaun Nelson |
27:1753a44fa9ec | 12 | #include "board.h" |
| Shaun Nelson |
27:1753a44fa9ec | 13 | #include "senet_packet.h" |
| Shaun Nelson |
27:1753a44fa9ec | 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 |
27:1753a44fa9ec | 30 | static uint8_t fsb = 0; |
| Shaun Nelson |
27:1753a44fa9ec | 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 |
27:1753a44fa9ec | 51 | static BoardOrientation orientation; |
| 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 |
27:1753a44fa9ec | 58 | // Forward |
| Shaun Nelson |
27:1753a44fa9ec | 59 | static void log_error(mDot* dot, const char* msg, int32_t retval); |
| Shaun Nelson |
27:1753a44fa9ec | 60 | static void joinLedToggle(); |
| Shaun Nelson |
27:1753a44fa9ec | 61 | static void onNextTxTimerEvent(); |
| Shaun Nelson |
27:1753a44fa9ec | 62 | static void ReceiveData(std::vector<uint8_t> frame); |
| Shaun Nelson |
27:1753a44fa9ec | 63 | |
| Shaun Nelson |
27:1753a44fa9ec | 64 | void JoinNetwork() |
| Shaun Nelson |
27:1753a44fa9ec | 65 | { |
| Shaun Nelson |
27:1753a44fa9ec | 66 | bool ok; |
| Shaun Nelson |
27:1753a44fa9ec | 67 | int32_t mdot_ret; |
| Shaun Nelson |
27:1753a44fa9ec | 68 | |
| Shaun Nelson |
27:1753a44fa9ec | 69 | do{ |
| Shaun Nelson |
27:1753a44fa9ec | 70 | ok = true; |
| Shaun Nelson |
27:1753a44fa9ec | 71 | |
| Shaun Nelson |
27:1753a44fa9ec | 72 | // reset to default config so we know what state we're in |
| Shaun Nelson |
27:1753a44fa9ec | 73 | mDotPtr->resetConfig(); |
| Shaun Nelson |
27:1753a44fa9ec | 74 | mDotPtr->setLogLevel(6); |
| Shaun Nelson |
27:1753a44fa9ec | 75 | mDotPtr->setAntennaGain(-3); |
| Shaun Nelson |
27:1753a44fa9ec | 76 | |
| Shaun Nelson |
27:1753a44fa9ec | 77 | // Read node ID |
| Shaun Nelson |
27:1753a44fa9ec | 78 | std::vector<uint8_t> mdot_EUI; |
| Shaun Nelson |
27:1753a44fa9ec | 79 | mdot_EUI = mDotPtr->getDeviceId(); |
| Shaun Nelson |
27:1753a44fa9ec | 80 | printf("mDot EUI = "); |
| Shaun Nelson |
27:1753a44fa9ec | 81 | |
| Shaun Nelson |
27:1753a44fa9ec | 82 | for (uint8_t i=0; i<mdot_EUI.size(); i++) |
| Shaun Nelson |
27:1753a44fa9ec | 83 | printf("%02x ", mdot_EUI[i]); |
| Shaun Nelson |
27:1753a44fa9ec | 84 | printf("\n\r"); |
| Shaun Nelson |
27:1753a44fa9ec | 85 | |
| Shaun Nelson |
27:1753a44fa9ec | 86 | /* |
| Shaun Nelson |
27:1753a44fa9ec | 87 | * This call sets up private or public mode on the MTDOT. Set the function to true if |
| Shaun Nelson |
27:1753a44fa9ec | 88 | * connecting to a public network |
| Shaun Nelson |
27:1753a44fa9ec | 89 | */ |
| Shaun Nelson |
27:1753a44fa9ec | 90 | printf("setting Public Network Mode\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 91 | if ((mdot_ret = mDotPtr->setPublicNetwork(true)) != mDot::MDOT_OK) |
| Shaun Nelson |
27:1753a44fa9ec | 92 | log_error(mDotPtr, "failed to set Public Network Mode", mdot_ret); |
| Shaun Nelson |
27:1753a44fa9ec | 93 | |
| Shaun Nelson |
27:1753a44fa9ec | 94 | mDotPtr->setTxDataRate(DATARATE); |
| Shaun Nelson |
27:1753a44fa9ec | 95 | mDotPtr->setTxPower(TXPOWER); |
| Shaun Nelson |
27:1753a44fa9ec | 96 | mDotPtr->setJoinRetries(JOIN_RETRIES); |
| Shaun Nelson |
27:1753a44fa9ec | 97 | mDotPtr->setJoinMode(mDot::OTA); |
| Shaun Nelson |
27:1753a44fa9ec | 98 | |
| Shaun Nelson |
27:1753a44fa9ec | 99 | /* |
| Shaun Nelson |
27:1753a44fa9ec | 100 | * Frequency sub-band is valid for NAM only and for Private networks should be set to a value |
| Shaun Nelson |
27:1753a44fa9ec | 101 | * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only. |
| Shaun Nelson |
27:1753a44fa9ec | 102 | * This function can be commented out for EU networks |
| Shaun Nelson |
27:1753a44fa9ec | 103 | */ |
| Shaun Nelson |
27:1753a44fa9ec | 104 | printf("setting frequency sub band\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 105 | if ((mdot_ret = mDotPtr->setFrequencySubBand(fsb)) != mDot::MDOT_OK) { |
| Shaun Nelson |
27:1753a44fa9ec | 106 | log_error(mDotPtr, "failed to set frequency sub band", mdot_ret); |
| Shaun Nelson |
27:1753a44fa9ec | 107 | ok = false; |
| Shaun Nelson |
27:1753a44fa9ec | 108 | } |
| Shaun Nelson |
27:1753a44fa9ec | 109 | |
| Shaun Nelson |
27:1753a44fa9ec | 110 | printf("setting ADR\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 111 | if ((mdot_ret = mDotPtr->setAdr(adrOn)) != mDot::MDOT_OK) { |
| Shaun Nelson |
27:1753a44fa9ec | 112 | log_error(mDotPtr, "failed to set ADR", mdot_ret); |
| Shaun Nelson |
27:1753a44fa9ec | 113 | ok = false; |
| Shaun Nelson |
27:1753a44fa9ec | 114 | } |
| Shaun Nelson |
27:1753a44fa9ec | 115 | |
| Shaun Nelson |
27:1753a44fa9ec | 116 | /* |
| Shaun Nelson |
27:1753a44fa9ec | 117 | * setNetworkName is used for private networks. |
| Shaun Nelson |
27:1753a44fa9ec | 118 | * Use setNetworkID(AppID) for public networks |
| Shaun Nelson |
27:1753a44fa9ec | 119 | */ |
| Shaun Nelson |
27:1753a44fa9ec | 120 | printf("setting network name\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 121 | if ((mdot_ret = mDotPtr->setNetworkId(appEUI)) != mDot::MDOT_OK) { |
| Shaun Nelson |
27:1753a44fa9ec | 122 | log_error(mDotPtr, "failed to set network name", mdot_ret); |
| Shaun Nelson |
27:1753a44fa9ec | 123 | ok = false; |
| Shaun Nelson |
27:1753a44fa9ec | 124 | } |
| Shaun Nelson |
27:1753a44fa9ec | 125 | |
| Shaun Nelson |
27:1753a44fa9ec | 126 | /* |
| Shaun Nelson |
27:1753a44fa9ec | 127 | * setNetworkPassphrase is used for private networks |
| Shaun Nelson |
27:1753a44fa9ec | 128 | * Use setNetworkKey for public networks |
| Shaun Nelson |
27:1753a44fa9ec | 129 | */ |
| Shaun Nelson |
27:1753a44fa9ec | 130 | printf("setting network key\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 131 | if ((mdot_ret = mDotPtr->setNetworkKey(appKey)) != mDot::MDOT_OK) { |
| Shaun Nelson |
27:1753a44fa9ec | 132 | log_error(mDotPtr, "failed to set network password", mdot_ret); |
| Shaun Nelson |
27:1753a44fa9ec | 133 | ok = false; |
| Shaun Nelson |
27:1753a44fa9ec | 134 | } |
| Shaun Nelson |
27:1753a44fa9ec | 135 | |
| Shaun Nelson |
27:1753a44fa9ec | 136 | BoardCheckForExit(true); |
| Shaun Nelson |
27:1753a44fa9ec | 137 | |
| Shaun Nelson |
27:1753a44fa9ec | 138 | } while(ok == false); |
| Shaun Nelson |
27:1753a44fa9ec | 139 | |
| Shaun Nelson |
27:1753a44fa9ec | 140 | joinTicker.attach(joinLedToggle,1); |
| Shaun Nelson |
27:1753a44fa9ec | 141 | |
| Shaun Nelson |
27:1753a44fa9ec | 142 | // attempt to join the network |
| Shaun Nelson |
27:1753a44fa9ec | 143 | printf("joining network\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 144 | while ((mdot_ret = mDotPtr->joinNetwork()) != mDot::MDOT_OK) |
| Shaun Nelson |
27:1753a44fa9ec | 145 | { |
| Shaun Nelson |
27:1753a44fa9ec | 146 | BoardCheckForExit(true); |
| Shaun Nelson |
27:1753a44fa9ec | 147 | |
| Shaun Nelson |
27:1753a44fa9ec | 148 | log_error(mDotPtr,"failed to join network:", mdot_ret); |
| Shaun Nelson |
27:1753a44fa9ec | 149 | uint32_t delay_s = (mDotPtr->getNextTxMs() / 1000) + 1; |
| Shaun Nelson |
27:1753a44fa9ec | 150 | wait(delay_s); |
| Shaun Nelson |
27:1753a44fa9ec | 151 | } |
| Shaun Nelson |
27:1753a44fa9ec | 152 | |
| Shaun Nelson |
27:1753a44fa9ec | 153 | printf("network joined\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 154 | |
| Shaun Nelson |
27:1753a44fa9ec | 155 | joinTicker.detach(); |
| Shaun Nelson |
27:1753a44fa9ec | 156 | appLED=1; |
| Shaun Nelson |
27:1753a44fa9ec | 157 | } |
| Shaun Nelson |
27:1753a44fa9ec | 158 | |
| Shaun Nelson |
27:1753a44fa9ec | 159 | void SendFrame() |
| Shaun Nelson |
27:1753a44fa9ec | 160 | { |
| Shaun Nelson |
27:1753a44fa9ec | 161 | std::vector<uint8_t> frame; |
| Shaun Nelson |
27:1753a44fa9ec | 162 | int32_t mdot_ret; |
| Shaun Nelson |
27:1753a44fa9ec | 163 | uint8_t buffer[20]; |
| Shaun Nelson |
27:1753a44fa9ec | 164 | SensorPacket packet(buffer, sizeof(buffer)); |
| Shaun Nelson |
27:1753a44fa9ec | 165 | |
| Shaun Nelson |
27:1753a44fa9ec | 166 | // Sensor packet type serialized to the frame buffer |
| Shaun Nelson |
27:1753a44fa9ec | 167 | packet.setPrimarySensor(orientation.horizontal ? HORIZONTAL_ORIENTATION_VALUE : VERTICAL_ORIENTATION_VALUE); |
| Shaun Nelson |
27:1753a44fa9ec | 168 | packet.setTemperature(sensorData.temperature); |
| Shaun Nelson |
27:1753a44fa9ec | 169 | packet.setPressure(sensorData.pressure); |
| Shaun Nelson |
27:1753a44fa9ec | 170 | packet.serialize(); |
| Shaun Nelson |
27:1753a44fa9ec | 171 | |
| Shaun Nelson |
27:1753a44fa9ec | 172 | frame.assign(packet.payload(), packet.payload() + packet.length()); |
| Shaun Nelson |
27:1753a44fa9ec | 173 | if ((mdot_ret = mDotPtr->send(frame)) != mDot::MDOT_OK) |
| Shaun Nelson |
27:1753a44fa9ec | 174 | { |
| Shaun Nelson |
27:1753a44fa9ec | 175 | log_error(mDotPtr, "failed to send", mdot_ret); |
| Shaun Nelson |
27:1753a44fa9ec | 176 | } |
| Shaun Nelson |
27:1753a44fa9ec | 177 | else |
| Shaun Nelson |
27:1753a44fa9ec | 178 | { |
| Shaun Nelson |
27:1753a44fa9ec | 179 | printf("successfully sent data\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 180 | frame.clear(); |
| Shaun Nelson |
27:1753a44fa9ec | 181 | if ((mdot_ret = mDotPtr->recv(frame)) == mDot::MDOT_OK) |
| Shaun Nelson |
27:1753a44fa9ec | 182 | { |
| Shaun Nelson |
27:1753a44fa9ec | 183 | printf("recv data: "); |
| Shaun Nelson |
27:1753a44fa9ec | 184 | for(uint32_t i = 0;i < frame.size();i++) |
| Shaun Nelson |
27:1753a44fa9ec | 185 | printf("%02X",frame[i]); |
| Shaun Nelson |
27:1753a44fa9ec | 186 | printf("\r\n"); |
| Shaun Nelson |
27:1753a44fa9ec | 187 | |
| Shaun Nelson |
27:1753a44fa9ec | 188 | ReceiveData(frame); |
| Shaun Nelson |
27:1753a44fa9ec | 189 | } |
| Shaun Nelson |
27:1753a44fa9ec | 190 | } |
| Shaun Nelson |
27:1753a44fa9ec | 191 | } |
| Shaun Nelson |
27:1753a44fa9ec | 192 | |
| Shaun Nelson |
27:1753a44fa9ec | 193 | void ReceiveData(std::vector<uint8_t> frame) |
| Shaun Nelson |
27:1753a44fa9ec | 194 | { |
| Shaun Nelson |
27:1753a44fa9ec | 195 | BackendOrientation.horizontal = (frame[0] == HORIZONTAL_ORIENTATION_VALUE); |
| Shaun Nelson |
27:1753a44fa9ec | 196 | |
| Shaun Nelson |
27:1753a44fa9ec | 197 | if( BackendOrientation.horizontal == orientation.horizontal ) |
| Shaun Nelson |
27:1753a44fa9ec | 198 | BackendSynchronized = true; |
| Shaun Nelson |
27:1753a44fa9ec | 199 | } |
| Shaun Nelson |
27:1753a44fa9ec | 200 | |
| Shaun Nelson |
27:1753a44fa9ec | 201 | |
| Shaun Nelson |
27:1753a44fa9ec | 202 | int main() |
| Shaun Nelson |
27:1753a44fa9ec | 203 | { |
| Shaun Nelson |
27:1753a44fa9ec | 204 | time_t lastTxT; |
| Shaun Nelson |
27:1753a44fa9ec | 205 | |
| Shaun Nelson |
27:1753a44fa9ec | 206 | // Initialize Board |
| Shaun Nelson |
27:1753a44fa9ec | 207 | BoardSetState(Board_init); |
| Shaun Nelson |
27:1753a44fa9ec | 208 | |
| Shaun Nelson |
27:1753a44fa9ec | 209 | // Join Network |
| Shaun Nelson |
27:1753a44fa9ec | 210 | JoinNetwork(); |
| Shaun Nelson |
27:1753a44fa9ec | 211 | |
| Shaun Nelson |
27:1753a44fa9ec | 212 | // Start Board sensors |
| Shaun Nelson |
27:1753a44fa9ec | 213 | BoardSetState(Board_start); |
| Shaun Nelson |
27:1753a44fa9ec | 214 | |
| Shaun Nelson |
27:1753a44fa9ec | 215 | // Initialize board orientation |
| Shaun Nelson |
27:1753a44fa9ec | 216 | BoardReadSensors(sensorData); |
| Shaun Nelson |
27:1753a44fa9ec | 217 | orientation = sensorData.orientation; |
| Shaun Nelson |
27:1753a44fa9ec | 218 | |
| Shaun Nelson |
27:1753a44fa9ec | 219 | BackendSynchronized = false; |
| Shaun Nelson |
27:1753a44fa9ec | 220 | |
| Shaun Nelson |
27:1753a44fa9ec | 221 | // Start transmit timer |
| Shaun Nelson |
27:1753a44fa9ec | 222 | nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3); |
| Shaun Nelson |
27:1753a44fa9ec | 223 | |
| Shaun Nelson |
27:1753a44fa9ec | 224 | while( !BoardCheckForExit(false) ) |
| Shaun Nelson |
27:1753a44fa9ec | 225 | { |
| Shaun Nelson |
27:1753a44fa9ec | 226 | // Update device orientation |
| Shaun Nelson |
27:1753a44fa9ec | 227 | if( ( BackendSynchronized == true ) && ( sensorData.orientation.horizontal != orientation.horizontal ) ) |
| Shaun Nelson |
27:1753a44fa9ec | 228 | { |
| Shaun Nelson |
27:1753a44fa9ec | 229 | BackendSynchronized = false; |
| Shaun Nelson |
27:1753a44fa9ec | 230 | orientation = sensorData.orientation; |
| Shaun Nelson |
27:1753a44fa9ec | 231 | |
| Shaun Nelson |
27:1753a44fa9ec | 232 | // Get elapsed time since last transmit |
| Shaun Nelson |
27:1753a44fa9ec | 233 | time_t currT = time(NULL); |
| Shaun Nelson |
27:1753a44fa9ec | 234 | time_t elapsedT = ( currT - lastTxT ) * 1e3; |
| Shaun Nelson |
27:1753a44fa9ec | 235 | |
| Shaun Nelson |
27:1753a44fa9ec | 236 | // Transmit now if elapsed time since last tx is greater than alarm mode dutycycle |
| Shaun Nelson |
27:1753a44fa9ec | 237 | if( elapsedT >= APP_TX_DUTY_CYCLE_ALARM ) |
| Shaun Nelson |
27:1753a44fa9ec | 238 | { |
| Shaun Nelson |
27:1753a44fa9ec | 239 | nextTxTimer.detach(); |
| Shaun Nelson |
27:1753a44fa9ec | 240 | NextTx = true; |
| Shaun Nelson |
27:1753a44fa9ec | 241 | } |
| Shaun Nelson |
27:1753a44fa9ec | 242 | // Otherwise wait until alarm dutycycle time has elapased |
| Shaun Nelson |
27:1753a44fa9ec | 243 | else |
| Shaun Nelson |
27:1753a44fa9ec | 244 | { |
| Shaun Nelson |
27:1753a44fa9ec | 245 | nextTxTimer.detach(); |
| Shaun Nelson |
27:1753a44fa9ec | 246 | nextTxTimer.attach_us(onNextTxTimerEvent, (APP_TX_DUTY_CYCLE_ALARM - elapsedT)* 1e3); |
| Shaun Nelson |
27:1753a44fa9ec | 247 | } |
| Shaun Nelson |
27:1753a44fa9ec | 248 | |
| Shaun Nelson |
27:1753a44fa9ec | 249 | AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM; |
| Shaun Nelson |
27:1753a44fa9ec | 250 | } |
| Shaun Nelson |
27:1753a44fa9ec | 251 | |
| Shaun Nelson |
27:1753a44fa9ec | 252 | if ( NextTx == true ) |
| Shaun Nelson |
27:1753a44fa9ec | 253 | { |
| Shaun Nelson |
27:1753a44fa9ec | 254 | /* Backend synchronized flag set true when |
| Shaun Nelson |
27:1753a44fa9ec | 255 | * - backend not enabled |
| Shaun Nelson |
27:1753a44fa9ec | 256 | * - Downlink received containing last known orientation |
| Shaun Nelson |
27:1753a44fa9ec | 257 | */ |
| Shaun Nelson |
27:1753a44fa9ec | 258 | BackendSynchronized = !BackendEnabled; |
| Shaun Nelson |
27:1753a44fa9ec | 259 | |
| Shaun Nelson |
27:1753a44fa9ec | 260 | // Transmit application frame |
| Shaun Nelson |
27:1753a44fa9ec | 261 | SendFrame(); |
| Shaun Nelson |
27:1753a44fa9ec | 262 | lastTxT = time(NULL); |
| Shaun Nelson |
27:1753a44fa9ec | 263 | |
| Shaun Nelson |
27:1753a44fa9ec | 264 | NextTx = false; |
| Shaun Nelson |
27:1753a44fa9ec | 265 | |
| Shaun Nelson |
27:1753a44fa9ec | 266 | // Fast transmit rate while backend is out of sync with device state |
| Shaun Nelson |
27:1753a44fa9ec | 267 | if(BackendSynchronized == false) |
| Shaun Nelson |
27:1753a44fa9ec | 268 | { |
| Shaun Nelson |
27:1753a44fa9ec | 269 | if( ( AppTxDutyCycle != APP_TX_DUTY_CYCLE_ALARM ) ) |
| Shaun Nelson |
27:1753a44fa9ec | 270 | { |
| Shaun Nelson |
27:1753a44fa9ec | 271 | AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM; |
| Shaun Nelson |
27:1753a44fa9ec | 272 | nextTxTimer.detach(); |
| Shaun Nelson |
27:1753a44fa9ec | 273 | nextTxTimer.attach_us(onNextTxTimerEvent, APP_TX_DUTY_CYCLE_ALARM * 1e3); |
| Shaun Nelson |
27:1753a44fa9ec | 274 | } |
| Shaun Nelson |
27:1753a44fa9ec | 275 | } |
| Shaun Nelson |
27:1753a44fa9ec | 276 | else if( AppTxDutyCycle != APP_TX_DUTY_CYCLE_NORMAL ) |
| Shaun Nelson |
27:1753a44fa9ec | 277 | { |
| Shaun Nelson |
27:1753a44fa9ec | 278 | AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL; |
| Shaun Nelson |
27:1753a44fa9ec | 279 | nextTxTimer.detach(); |
| Shaun Nelson |
27:1753a44fa9ec | 280 | nextTxTimer.attach_us(onNextTxTimerEvent, APP_TX_DUTY_CYCLE_NORMAL * 1e3); |
| Shaun Nelson |
27:1753a44fa9ec | 281 | } |
| Shaun Nelson |
27:1753a44fa9ec | 282 | } |
| Shaun Nelson |
27:1753a44fa9ec | 283 | |
| Shaun Nelson |
27:1753a44fa9ec | 284 | // Delay before next sensor poll |
| Shaun Nelson |
27:1753a44fa9ec | 285 | osDelay(2000); |
| Shaun Nelson |
27:1753a44fa9ec | 286 | |
| Shaun Nelson |
27:1753a44fa9ec | 287 | // Read sensors |
| Shaun Nelson |
27:1753a44fa9ec | 288 | BoardReadSensors(sensorData); |
| Shaun Nelson |
27:1753a44fa9ec | 289 | } |
| Shaun Nelson |
27:1753a44fa9ec | 290 | |
| Shaun Nelson |
27:1753a44fa9ec | 291 | BoardSetState(Board_stop); |
| Shaun Nelson |
27:1753a44fa9ec | 292 | |
| Shaun Nelson |
27:1753a44fa9ec | 293 | return 0; |
| Shaun Nelson |
27:1753a44fa9ec | 294 | } |
| Shaun Nelson |
27:1753a44fa9ec | 295 | |
| Shaun Nelson |
27:1753a44fa9ec | 296 | |
| Shaun Nelson |
27:1753a44fa9ec | 297 | /* |
| Shaun Nelson |
27:1753a44fa9ec | 298 | * prints of mDot error |
| Shaun Nelson |
27:1753a44fa9ec | 299 | */ |
| Shaun Nelson |
27:1753a44fa9ec | 300 | void log_error(mDot* dot, const char* msg, int32_t retval) |
| Shaun Nelson |
27:1753a44fa9ec | 301 | { |
| Shaun Nelson |
27:1753a44fa9ec | 302 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
| Shaun Nelson |
27:1753a44fa9ec | 303 | } |
| Shaun Nelson |
27:1753a44fa9ec | 304 | |
| Shaun Nelson |
27:1753a44fa9ec | 305 | void joinLedToggle() |
| Shaun Nelson |
27:1753a44fa9ec | 306 | { |
| Shaun Nelson |
27:1753a44fa9ec | 307 | appLED= !appLED; |
| Shaun Nelson |
27:1753a44fa9ec | 308 | } |
| Shaun Nelson |
27:1753a44fa9ec | 309 | |
| Shaun Nelson |
27:1753a44fa9ec | 310 | void onNextTxTimerEvent( void ) |
| Shaun Nelson |
27:1753a44fa9ec | 311 | { |
| Shaun Nelson |
27:1753a44fa9ec | 312 | NextTx = true; |
| Shaun Nelson |
27:1753a44fa9ec | 313 | } |
| Shaun Nelson |
27:1753a44fa9ec | 314 |
