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
main.cpp@39:022b327d6bf0, 2017-08-29 (annotated)
- Committer:
- Shaun Nelson
- Date:
- Tue Aug 29 18:05:34 2017 -0400
- Branch:
- develop
- Revision:
- 39:022b327d6bf0
- Parent:
- 36:cd1077f40dbf
- Child:
- 43:55e7bb4d9b60
- Add Set/Toggle LED to board API
- Turn LED on/off based on backend state (on = out-of-sync, off = in sync)
Who changed what in which revision?
| User | Revision | Line number | New 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 |
36:cd1077f40dbf | 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 |
36:cd1077f40dbf | 50 | static bool BackendSynchronized = true; |
| Shaun Nelson |
36:cd1077f40dbf | 51 | static BoardOrientation BackendOrientation; |
| Shaun Nelson |
32:5873d0638277 | 52 | static Ticker joinTicker; |
| Shaun Nelson |
32:5873d0638277 | 53 | static Ticker nextTxTimer; |
| Shaun Nelson |
32:5873d0638277 | 54 | static BoardSensorData sensorData; |
| Shaun Nelson |
32:5873d0638277 | 55 | static BoardOrientation txOrientation; |
| Shaun Nelson |
36:cd1077f40dbf | 56 | static bool NextTx = true; |
| Shaun Nelson |
36:cd1077f40dbf | 57 | static uint32_t AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL; |
| 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 | //orientation change count |
| Shaun Nelson |
32:5873d0638277 | 67 | uint32_t orientationCount[Orientation_Max]; |
| Shaun Nelson |
32:5873d0638277 | 68 | // orientation transmit count |
| Shaun Nelson |
32:5873d0638277 | 69 | uint32_t orientationTxCount[Orientation_Max]; |
| Shaun Nelson |
32:5873d0638277 | 70 | |
| Shaun Nelson |
36:cd1077f40dbf | 71 | // Orientation comparison |
| Shaun Nelson |
36:cd1077f40dbf | 72 | inline bool orientationIsEqual(BoardOrientation &a, BoardOrientation &b) |
| Shaun Nelson |
36:cd1077f40dbf | 73 | { |
| Shaun Nelson |
36:cd1077f40dbf | 74 | // For this application only vertical/horizontal state is tested |
| Shaun Nelson |
36:cd1077f40dbf | 75 | return ( a.vertical == b.vertical ); |
| Shaun Nelson |
36:cd1077f40dbf | 76 | } |
| Shaun Nelson |
36:cd1077f40dbf | 77 | |
| Shaun Nelson |
36:cd1077f40dbf | 78 | // Next orientation change to transmit |
| Shaun Nelson |
36:cd1077f40dbf | 79 | inline uint8_t getNextTxOrientation(BoardOrientation &orientation) |
| Shaun Nelson |
36:cd1077f40dbf | 80 | { |
| Shaun Nelson |
36:cd1077f40dbf | 81 | static uint8_t txOrientationType = Orientation_Max; |
| Shaun Nelson |
36:cd1077f40dbf | 82 | |
| Shaun Nelson |
36:cd1077f40dbf | 83 | if (++txOrientationType >= Orientation_Max) |
| Shaun Nelson |
36:cd1077f40dbf | 84 | txOrientationType = 0; |
| Shaun Nelson |
36:cd1077f40dbf | 85 | |
| Shaun Nelson |
36:cd1077f40dbf | 86 | for( ; txOrientationType < Orientation_Max; txOrientationType++) |
| Shaun Nelson |
36:cd1077f40dbf | 87 | { |
| Shaun Nelson |
36:cd1077f40dbf | 88 | if( orientationTxCount[txOrientationType] < orientationCount[txOrientationType] ) |
| Shaun Nelson |
36:cd1077f40dbf | 89 | { |
| Shaun Nelson |
36:cd1077f40dbf | 90 | orientationTxCount[txOrientationType]++; |
| Shaun Nelson |
36:cd1077f40dbf | 91 | switch(txOrientationType) |
| Shaun Nelson |
36:cd1077f40dbf | 92 | { |
| Shaun Nelson |
36:cd1077f40dbf | 93 | case Orientation_Horizontal: |
| Shaun Nelson |
36:cd1077f40dbf | 94 | orientation.vertical = false; |
| Shaun Nelson |
36:cd1077f40dbf | 95 | break; |
| Shaun Nelson |
36:cd1077f40dbf | 96 | case Orientation_Vertical: |
| Shaun Nelson |
36:cd1077f40dbf | 97 | orientation.vertical = true; |
| Shaun Nelson |
36:cd1077f40dbf | 98 | break; |
| Shaun Nelson |
36:cd1077f40dbf | 99 | default: |
| Shaun Nelson |
36:cd1077f40dbf | 100 | break; |
| Shaun Nelson |
36:cd1077f40dbf | 101 | } |
| Shaun Nelson |
36:cd1077f40dbf | 102 | break; |
| Shaun Nelson |
36:cd1077f40dbf | 103 | } |
| Shaun Nelson |
36:cd1077f40dbf | 104 | } |
| Shaun Nelson |
36:cd1077f40dbf | 105 | |
| Shaun Nelson |
36:cd1077f40dbf | 106 | return txOrientationType; |
| Shaun Nelson |
36:cd1077f40dbf | 107 | } |
| Shaun Nelson |
36:cd1077f40dbf | 108 | |
| Shaun Nelson |
36:cd1077f40dbf | 109 | inline void incrementOrientation(BoardOrientation &orientation) |
| Shaun Nelson |
36:cd1077f40dbf | 110 | { |
| Shaun Nelson |
36:cd1077f40dbf | 111 | orientation.vertical ? orientationCount[Orientation_Vertical]++ : orientationCount[Orientation_Horizontal]++; |
| Shaun Nelson |
36:cd1077f40dbf | 112 | } |
| Shaun Nelson |
36:cd1077f40dbf | 113 | |
| Shaun Nelson |
32:5873d0638277 | 114 | static void log_error(mDot* dot, const char* msg, int32_t retval); |
| Shaun Nelson |
32:5873d0638277 | 115 | static void joinLedToggle(); |
| Shaun Nelson |
32:5873d0638277 | 116 | static void onNextTxTimerEvent(); |
| Shaun Nelson |
32:5873d0638277 | 117 | static void ReceiveData(std::vector<uint8_t> frame); |
| Shaun Nelson |
32:5873d0638277 | 118 | |
| Shaun Nelson |
36:cd1077f40dbf | 119 | |
| Shaun Nelson |
32:5873d0638277 | 120 | void JoinNetwork() |
| Shaun Nelson |
32:5873d0638277 | 121 | { |
| Shaun Nelson |
32:5873d0638277 | 122 | bool ok; |
| Shaun Nelson |
32:5873d0638277 | 123 | int32_t mdot_ret; |
| Shaun Nelson |
32:5873d0638277 | 124 | |
| Shaun Nelson |
32:5873d0638277 | 125 | do{ |
| Shaun Nelson |
32:5873d0638277 | 126 | ok = true; |
| Shaun Nelson |
32:5873d0638277 | 127 | |
| Shaun Nelson |
32:5873d0638277 | 128 | // reset to default config so we know what state we're in |
| Shaun Nelson |
32:5873d0638277 | 129 | mDotPtr->resetConfig(); |
| Shaun Nelson |
32:5873d0638277 | 130 | mDotPtr->setLogLevel(6); |
| Shaun Nelson |
32:5873d0638277 | 131 | mDotPtr->setAntennaGain(-3); |
| Shaun Nelson |
32:5873d0638277 | 132 | |
| Shaun Nelson |
32:5873d0638277 | 133 | // Read node ID |
| Shaun Nelson |
32:5873d0638277 | 134 | std::vector<uint8_t> mdot_EUI; |
| Shaun Nelson |
32:5873d0638277 | 135 | mdot_EUI = mDotPtr->getDeviceId(); |
| Shaun Nelson |
32:5873d0638277 | 136 | printf("mDot EUI = "); |
| Shaun Nelson |
32:5873d0638277 | 137 | |
| Shaun Nelson |
32:5873d0638277 | 138 | for (uint8_t i=0; i<mdot_EUI.size(); i++) |
| Shaun Nelson |
32:5873d0638277 | 139 | printf("%02x ", mdot_EUI[i]); |
| Shaun Nelson |
32:5873d0638277 | 140 | printf("\n\r"); |
| Shaun Nelson |
32:5873d0638277 | 141 | |
| Shaun Nelson |
32:5873d0638277 | 142 | /* |
| Shaun Nelson |
32:5873d0638277 | 143 | * This call sets up private or public mode on the MTDOT. Set the function to true if |
| Shaun Nelson |
32:5873d0638277 | 144 | * connecting to a public network |
| Shaun Nelson |
32:5873d0638277 | 145 | */ |
| Shaun Nelson |
32:5873d0638277 | 146 | printf("setting Public Network Mode\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 147 | if ((mdot_ret = mDotPtr->setPublicNetwork(true)) != mDot::MDOT_OK) |
| Shaun Nelson |
32:5873d0638277 | 148 | log_error(mDotPtr, "failed to set Public Network Mode", mdot_ret); |
| Shaun Nelson |
32:5873d0638277 | 149 | |
| Shaun Nelson |
32:5873d0638277 | 150 | mDotPtr->setTxDataRate(DATARATE); |
| Shaun Nelson |
32:5873d0638277 | 151 | mDotPtr->setTxPower(TXPOWER); |
| Shaun Nelson |
32:5873d0638277 | 152 | mDotPtr->setJoinRetries(JOIN_RETRIES); |
| Shaun Nelson |
32:5873d0638277 | 153 | mDotPtr->setJoinMode(mDot::OTA); |
| Shaun Nelson |
32:5873d0638277 | 154 | |
| Shaun Nelson |
32:5873d0638277 | 155 | /* |
| Shaun Nelson |
32:5873d0638277 | 156 | * Frequency sub-band is valid for NAM only and for Private networks should be set to a value |
| Shaun Nelson |
32:5873d0638277 | 157 | * between 1-8 that matches the the LoRa gateway setting. Public networks use sub-band 0 only. |
| Shaun Nelson |
32:5873d0638277 | 158 | * This function can be commented out for EU networks |
| Shaun Nelson |
32:5873d0638277 | 159 | */ |
| Shaun Nelson |
32:5873d0638277 | 160 | printf("setting frequency sub band\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 161 | if ((mdot_ret = mDotPtr->setFrequencySubBand(fsb)) != mDot::MDOT_OK) { |
| Shaun Nelson |
32:5873d0638277 | 162 | log_error(mDotPtr, "failed to set frequency sub band", mdot_ret); |
| Shaun Nelson |
32:5873d0638277 | 163 | ok = false; |
| Shaun Nelson |
32:5873d0638277 | 164 | } |
| Shaun Nelson |
32:5873d0638277 | 165 | |
| Shaun Nelson |
32:5873d0638277 | 166 | printf("setting ADR\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 167 | if ((mdot_ret = mDotPtr->setAdr(adrOn)) != mDot::MDOT_OK) { |
| Shaun Nelson |
32:5873d0638277 | 168 | log_error(mDotPtr, "failed to set ADR", mdot_ret); |
| Shaun Nelson |
32:5873d0638277 | 169 | ok = false; |
| Shaun Nelson |
32:5873d0638277 | 170 | } |
| Shaun Nelson |
32:5873d0638277 | 171 | |
| Shaun Nelson |
32:5873d0638277 | 172 | /* |
| Shaun Nelson |
32:5873d0638277 | 173 | * setNetworkName is used for private networks. |
| Shaun Nelson |
32:5873d0638277 | 174 | * Use setNetworkID(AppID) for public networks |
| Shaun Nelson |
32:5873d0638277 | 175 | */ |
| Shaun Nelson |
32:5873d0638277 | 176 | printf("setting network name\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 177 | if ((mdot_ret = mDotPtr->setNetworkId(appEUI)) != mDot::MDOT_OK) { |
| Shaun Nelson |
32:5873d0638277 | 178 | log_error(mDotPtr, "failed to set network name", mdot_ret); |
| Shaun Nelson |
32:5873d0638277 | 179 | ok = false; |
| Shaun Nelson |
32:5873d0638277 | 180 | } |
| Shaun Nelson |
32:5873d0638277 | 181 | |
| Shaun Nelson |
32:5873d0638277 | 182 | /* |
| Shaun Nelson |
32:5873d0638277 | 183 | * setNetworkPassphrase is used for private networks |
| Shaun Nelson |
32:5873d0638277 | 184 | * Use setNetworkKey for public networks |
| Shaun Nelson |
32:5873d0638277 | 185 | */ |
| Shaun Nelson |
32:5873d0638277 | 186 | printf("setting network key\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 187 | if ((mdot_ret = mDotPtr->setNetworkKey(appKey)) != mDot::MDOT_OK) { |
| Shaun Nelson |
32:5873d0638277 | 188 | log_error(mDotPtr, "failed to set network password", mdot_ret); |
| Shaun Nelson |
32:5873d0638277 | 189 | ok = false; |
| Shaun Nelson |
32:5873d0638277 | 190 | } |
| Shaun Nelson |
32:5873d0638277 | 191 | |
| Shaun Nelson |
32:5873d0638277 | 192 | } while(ok == false); |
| Shaun Nelson |
32:5873d0638277 | 193 | |
| Shaun Nelson |
39:022b327d6bf0 | 194 | joinTicker.attach(joinLedToggle, 5); |
| Shaun Nelson |
32:5873d0638277 | 195 | |
| Shaun Nelson |
32:5873d0638277 | 196 | // attempt to join the network |
| Shaun Nelson |
32:5873d0638277 | 197 | printf("joining network\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 198 | while ((mdot_ret = mDotPtr->joinNetwork()) != mDot::MDOT_OK) |
| Shaun Nelson |
32:5873d0638277 | 199 | { |
| Shaun Nelson |
32:5873d0638277 | 200 | log_error(mDotPtr,"failed to join network:", mdot_ret); |
| Shaun Nelson |
32:5873d0638277 | 201 | uint32_t delay_s = (mDotPtr->getNextTxMs() / 1000) + 1; |
| Shaun Nelson |
32:5873d0638277 | 202 | wait(delay_s); |
| Shaun Nelson |
32:5873d0638277 | 203 | } |
| Shaun Nelson |
32:5873d0638277 | 204 | |
| Shaun Nelson |
32:5873d0638277 | 205 | printf("network joined\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 206 | |
| Shaun Nelson |
32:5873d0638277 | 207 | joinTicker.detach(); |
| Shaun Nelson |
39:022b327d6bf0 | 208 | CBoard::SetLED(1, false); |
| Shaun Nelson |
32:5873d0638277 | 209 | } |
| Shaun Nelson |
32:5873d0638277 | 210 | |
| Shaun Nelson |
32:5873d0638277 | 211 | void SendFrame() |
| Shaun Nelson |
32:5873d0638277 | 212 | { |
| Shaun Nelson |
32:5873d0638277 | 213 | std::vector<uint8_t> frame; |
| Shaun Nelson |
32:5873d0638277 | 214 | int32_t mdot_ret; |
| Shaun Nelson |
32:5873d0638277 | 215 | uint8_t buffer[20]; |
| Shaun Nelson |
32:5873d0638277 | 216 | SensorPacket packet(buffer, sizeof(buffer)); |
| Shaun Nelson |
32:5873d0638277 | 217 | |
| Shaun Nelson |
32:5873d0638277 | 218 | // Sensor packet type serialized to the frame buffer |
| Shaun Nelson |
32:5873d0638277 | 219 | packet.setPrimarySensor(txOrientation.vertical ? VERTICAL_ORIENTATION_VALUE : HORIZONTAL_ORIENTATION_VALUE); |
| Shaun Nelson |
32:5873d0638277 | 220 | packet.setTemperature(sensorData.temperature); |
| Shaun Nelson |
32:5873d0638277 | 221 | packet.setPressure(sensorData.pressure); |
| Shaun Nelson |
32:5873d0638277 | 222 | packet.serialize(); |
| Shaun Nelson |
32:5873d0638277 | 223 | |
| Shaun Nelson |
32:5873d0638277 | 224 | frame.assign(packet.payload(), packet.payload() + packet.length()); |
| Shaun Nelson |
32:5873d0638277 | 225 | if ((mdot_ret = mDotPtr->send(frame)) != mDot::MDOT_OK) |
| Shaun Nelson |
32:5873d0638277 | 226 | { |
| Shaun Nelson |
32:5873d0638277 | 227 | log_error(mDotPtr, "failed to send", mdot_ret); |
| Shaun Nelson |
32:5873d0638277 | 228 | } |
| Shaun Nelson |
32:5873d0638277 | 229 | else |
| Shaun Nelson |
32:5873d0638277 | 230 | { |
| Shaun Nelson |
32:5873d0638277 | 231 | printf("successfully sent data\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 232 | frame.clear(); |
| Shaun Nelson |
32:5873d0638277 | 233 | if ((mdot_ret = mDotPtr->recv(frame)) == mDot::MDOT_OK) |
| Shaun Nelson |
32:5873d0638277 | 234 | { |
| Shaun Nelson |
32:5873d0638277 | 235 | printf("recv data: "); |
| Shaun Nelson |
32:5873d0638277 | 236 | for(uint32_t i = 0;i < frame.size();i++) |
| Shaun Nelson |
32:5873d0638277 | 237 | printf("%02X",frame[i]); |
| Shaun Nelson |
32:5873d0638277 | 238 | printf("\r\n"); |
| Shaun Nelson |
32:5873d0638277 | 239 | |
| Shaun Nelson |
32:5873d0638277 | 240 | ReceiveData(frame); |
| Shaun Nelson |
32:5873d0638277 | 241 | } |
| Shaun Nelson |
32:5873d0638277 | 242 | } |
| Shaun Nelson |
32:5873d0638277 | 243 | } |
| Shaun Nelson |
32:5873d0638277 | 244 | |
| Shaun Nelson |
32:5873d0638277 | 245 | void ReceiveData(std::vector<uint8_t> frame) |
| Shaun Nelson |
32:5873d0638277 | 246 | { |
| Shaun Nelson |
32:5873d0638277 | 247 | BackendOrientation.vertical = (frame[0] == VERTICAL_ORIENTATION_VALUE); |
| Shaun Nelson |
32:5873d0638277 | 248 | |
| Shaun Nelson |
36:cd1077f40dbf | 249 | BackendSynchronized = orientationIsEqual(BackendOrientation, txOrientation); |
| Shaun Nelson |
32:5873d0638277 | 250 | } |
| Shaun Nelson |
32:5873d0638277 | 251 | |
| Shaun Nelson |
32:5873d0638277 | 252 | int main() |
| Shaun Nelson |
32:5873d0638277 | 253 | { |
| Shaun Nelson |
32:5873d0638277 | 254 | time_t lastTxT; |
| Shaun Nelson |
32:5873d0638277 | 255 | BoardOrientation lastOrientation; |
| Shaun Nelson |
32:5873d0638277 | 256 | |
| Shaun Nelson |
32:5873d0638277 | 257 | memset(orientationCount, 0, sizeof(orientationCount)); |
| Shaun Nelson |
32:5873d0638277 | 258 | memset(orientationTxCount, 0, sizeof(orientationTxCount)); |
| Shaun Nelson |
32:5873d0638277 | 259 | |
| Shaun Nelson |
32:5873d0638277 | 260 | // Initialize Board |
| Shaun Nelson |
32:5873d0638277 | 261 | BoardInit(); |
| Shaun Nelson |
32:5873d0638277 | 262 | |
| Shaun Nelson |
32:5873d0638277 | 263 | // Join Network |
| Shaun Nelson |
32:5873d0638277 | 264 | JoinNetwork(); |
| Shaun Nelson |
32:5873d0638277 | 265 | |
| Shaun Nelson |
32:5873d0638277 | 266 | // Start Board sensors |
| Shaun Nelson |
32:5873d0638277 | 267 | CBoard::Start(); |
| Shaun Nelson |
32:5873d0638277 | 268 | |
| Shaun Nelson |
32:5873d0638277 | 269 | // Send initial state |
| Shaun Nelson |
32:5873d0638277 | 270 | if( CBoard::ReadSensors(sensorData) == Board_Ok ) |
| Shaun Nelson |
32:5873d0638277 | 271 | { |
| Shaun Nelson |
32:5873d0638277 | 272 | lastOrientation = sensorData.orientation; |
| Shaun Nelson |
32:5873d0638277 | 273 | incrementOrientation(lastOrientation); |
| Shaun Nelson |
36:cd1077f40dbf | 274 | |
| Shaun Nelson |
36:cd1077f40dbf | 275 | //Initialize backend state to opposite of current to trigger |
| Shaun Nelson |
36:cd1077f40dbf | 276 | // backend synchronization if enabled |
| Shaun Nelson |
36:cd1077f40dbf | 277 | BackendOrientation.vertical = !lastOrientation.vertical; |
| Shaun Nelson |
32:5873d0638277 | 278 | } |
| Shaun Nelson |
32:5873d0638277 | 279 | |
| Shaun Nelson |
32:5873d0638277 | 280 | while( true ) |
| Shaun Nelson |
32:5873d0638277 | 281 | { |
| Shaun Nelson |
32:5873d0638277 | 282 | // Read sensors |
| Shaun Nelson |
32:5873d0638277 | 283 | if( CBoard::ReadSensors(sensorData) == Board_Ok ) |
| Shaun Nelson |
32:5873d0638277 | 284 | { |
| Shaun Nelson |
36:cd1077f40dbf | 285 | if( !orientationIsEqual(sensorData.orientation, lastOrientation) ) |
| Shaun Nelson |
32:5873d0638277 | 286 | { |
| Shaun Nelson |
32:5873d0638277 | 287 | lastOrientation = sensorData.orientation; |
| Shaun Nelson |
32:5873d0638277 | 288 | incrementOrientation(lastOrientation); |
| Shaun Nelson |
32:5873d0638277 | 289 | } |
| Shaun Nelson |
32:5873d0638277 | 290 | } |
| Shaun Nelson |
32:5873d0638277 | 291 | |
| Shaun Nelson |
36:cd1077f40dbf | 292 | // Determine next orientation to transmit after backend sync of the last transmitted orientation is finished |
| Shaun Nelson |
36:cd1077f40dbf | 293 | if( ( BackendSynchronized == true ) && ( getNextTxOrientation(txOrientation) < Orientation_Max ) ) |
| Shaun Nelson |
32:5873d0638277 | 294 | { |
| Shaun Nelson |
36:cd1077f40dbf | 295 | BackendSynchronized = false; |
| Shaun Nelson |
39:022b327d6bf0 | 296 | CBoard::SetLED(1, true); |
| Shaun Nelson |
32:5873d0638277 | 297 | |
| Shaun Nelson |
32:5873d0638277 | 298 | // Get elapsed time since last transmit |
| Shaun Nelson |
32:5873d0638277 | 299 | time_t currT = time(NULL); |
| Shaun Nelson |
32:5873d0638277 | 300 | time_t elapsedT = ( currT - lastTxT ) * 1e3; |
| Shaun Nelson |
32:5873d0638277 | 301 | |
| Shaun Nelson |
36:cd1077f40dbf | 302 | // Stop transmit timer |
| Shaun Nelson |
36:cd1077f40dbf | 303 | AppTxDutyCycle = 0; |
| Shaun Nelson |
36:cd1077f40dbf | 304 | nextTxTimer.detach(); |
| Shaun Nelson |
36:cd1077f40dbf | 305 | |
| Shaun Nelson |
36:cd1077f40dbf | 306 | // Wait to transmit until elapsed time is greater than alarm mode dutycycle |
| Shaun Nelson |
36:cd1077f40dbf | 307 | if( !( NextTx = ( elapsedT >= APP_TX_DUTY_CYCLE_ALARM ) ) ) |
| Shaun Nelson |
32:5873d0638277 | 308 | { |
| Shaun Nelson |
36:cd1077f40dbf | 309 | AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM - elapsedT; |
| Shaun Nelson |
36:cd1077f40dbf | 310 | nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3); |
| Shaun Nelson |
32:5873d0638277 | 311 | } |
| Shaun Nelson |
32:5873d0638277 | 312 | } |
| Shaun Nelson |
32:5873d0638277 | 313 | |
| Shaun Nelson |
32:5873d0638277 | 314 | if ( NextTx == true ) |
| Shaun Nelson |
32:5873d0638277 | 315 | { |
| Shaun Nelson |
36:cd1077f40dbf | 316 | NextTx = false; |
| Shaun Nelson |
36:cd1077f40dbf | 317 | |
| Shaun Nelson |
32:5873d0638277 | 318 | /* Backend synchronized flag set true when |
| Shaun Nelson |
36:cd1077f40dbf | 319 | * o Backend not enabled |
| Shaun Nelson |
36:cd1077f40dbf | 320 | * o Backend state == current orientation |
| Shaun Nelson |
32:5873d0638277 | 321 | */ |
| Shaun Nelson |
36:cd1077f40dbf | 322 | BackendSynchronized = !BackendEnabled || orientationIsEqual(BackendOrientation, txOrientation); |
| Shaun Nelson |
32:5873d0638277 | 323 | |
| Shaun Nelson |
32:5873d0638277 | 324 | // Transmit application frame |
| Shaun Nelson |
32:5873d0638277 | 325 | SendFrame(); |
| Shaun Nelson |
36:cd1077f40dbf | 326 | |
| Shaun Nelson |
36:cd1077f40dbf | 327 | // Update transmit timestamp |
| Shaun Nelson |
32:5873d0638277 | 328 | lastTxT = time(NULL); |
| Shaun Nelson |
32:5873d0638277 | 329 | |
| Shaun Nelson |
36:cd1077f40dbf | 330 | // Set next transmit time |
| Shaun Nelson |
32:5873d0638277 | 331 | if(BackendSynchronized == false) |
| Shaun Nelson |
32:5873d0638277 | 332 | { |
| Shaun Nelson |
32:5873d0638277 | 333 | if( ( AppTxDutyCycle != APP_TX_DUTY_CYCLE_ALARM ) ) |
| Shaun Nelson |
32:5873d0638277 | 334 | { |
| Shaun Nelson |
32:5873d0638277 | 335 | AppTxDutyCycle = APP_TX_DUTY_CYCLE_ALARM; |
| Shaun Nelson |
32:5873d0638277 | 336 | nextTxTimer.detach(); |
| Shaun Nelson |
36:cd1077f40dbf | 337 | nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3); |
| Shaun Nelson |
32:5873d0638277 | 338 | } |
| Shaun Nelson |
32:5873d0638277 | 339 | } |
| Shaun Nelson |
32:5873d0638277 | 340 | else if( AppTxDutyCycle != APP_TX_DUTY_CYCLE_NORMAL ) |
| Shaun Nelson |
32:5873d0638277 | 341 | { |
| Shaun Nelson |
32:5873d0638277 | 342 | AppTxDutyCycle = APP_TX_DUTY_CYCLE_NORMAL; |
| Shaun Nelson |
32:5873d0638277 | 343 | nextTxTimer.detach(); |
| Shaun Nelson |
36:cd1077f40dbf | 344 | nextTxTimer.attach_us(onNextTxTimerEvent, AppTxDutyCycle * 1e3); |
| Shaun Nelson |
32:5873d0638277 | 345 | } |
| Shaun Nelson |
39:022b327d6bf0 | 346 | |
| Shaun Nelson |
39:022b327d6bf0 | 347 | if(BackendSynchronized == true) |
| Shaun Nelson |
39:022b327d6bf0 | 348 | CBoard::SetLED(1, false); |
| Shaun Nelson |
32:5873d0638277 | 349 | } |
| Shaun Nelson |
32:5873d0638277 | 350 | |
| Shaun Nelson |
32:5873d0638277 | 351 | // Delay before next sensor poll |
| Shaun Nelson |
32:5873d0638277 | 352 | osDelay(2000); |
| Shaun Nelson |
32:5873d0638277 | 353 | } |
| Shaun Nelson |
32:5873d0638277 | 354 | } |
| Shaun Nelson |
32:5873d0638277 | 355 | |
| Shaun Nelson |
32:5873d0638277 | 356 | |
| Shaun Nelson |
32:5873d0638277 | 357 | /* |
| Shaun Nelson |
32:5873d0638277 | 358 | * prints of mDot error |
| Shaun Nelson |
32:5873d0638277 | 359 | */ |
| Shaun Nelson |
32:5873d0638277 | 360 | void log_error(mDot* dot, const char* msg, int32_t retval) |
| Shaun Nelson |
32:5873d0638277 | 361 | { |
| Shaun Nelson |
32:5873d0638277 | 362 | printf("%s - %ld:%s, %s\r\n", msg, retval, mDot::getReturnCodeString(retval).c_str(), dot->getLastError().c_str()); |
| Shaun Nelson |
32:5873d0638277 | 363 | } |
| Shaun Nelson |
32:5873d0638277 | 364 | |
| Shaun Nelson |
39:022b327d6bf0 | 365 | void joinLedToggle() { CBoard::ToggleLED(1); } |
| Shaun Nelson |
39:022b327d6bf0 | 366 | |
| Shaun Nelson |
36:cd1077f40dbf | 367 | void onNextTxTimerEvent( void ) { NextTx = true; } |
