![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Fork to see if I can get working
Dependencies: BufferedSerial OneWire WinbondSPIFlash libxDot-dev-mbed5-deprecated
Fork of xDotBridge_update_test20180823 by
xDotBridge/src/main.cpp@25:fea776239709, 2017-01-03 (annotated)
- Committer:
- Matt Briggs
- Date:
- Tue Jan 03 16:07:16 2017 -0700
- Revision:
- 25:fea776239709
- Parent:
- 24:fdf87e4b72e5
- Child:
- 26:9411b26a5084
Prelim version for RX
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Matt Briggs | 22:9453658b8d4b | 1 | #include "config.h" |
Mike Fiore |
11:d2e31743433a | 2 | #include "dot_util.h" |
Mike Fiore |
14:19fae4509473 | 3 | #include "RadioEvent.h" |
Mike Fiore |
11:d2e31743433a | 4 | |
Matt Briggs | 22:9453658b8d4b | 5 | |
mfiore | 17:d4f82e16de5f | 6 | ///////////////////////////////////////////////////////////////////////////// |
mfiore | 17:d4f82e16de5f | 7 | // -------------------- DOT LIBRARY REQUIRED ------------------------------// |
mfiore | 17:d4f82e16de5f | 8 | // * Because these example programs can be used for both mDot and xDot // |
mfiore | 17:d4f82e16de5f | 9 | // devices, the LoRa stack is not included. The libmDot library should // |
mfiore | 17:d4f82e16de5f | 10 | // be imported if building for mDot devices. The libxDot library // |
mfiore | 17:d4f82e16de5f | 11 | // should be imported if building for xDot devices. // |
mfiore | 17:d4f82e16de5f | 12 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-dev-mbed5/ // |
mfiore | 17:d4f82e16de5f | 13 | // * https://developer.mbed.org/teams/MultiTech/code/libmDot-mbed5/ // |
mfiore | 17:d4f82e16de5f | 14 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-dev-mbed5/ // |
mfiore | 17:d4f82e16de5f | 15 | // * https://developer.mbed.org/teams/MultiTech/code/libxDot-mbed5/ // |
mfiore | 17:d4f82e16de5f | 16 | ///////////////////////////////////////////////////////////////////////////// |
mfiore | 17:d4f82e16de5f | 17 | |
Mike Fiore |
11:d2e31743433a | 18 | ///////////////////////////////////////////////////////////// |
Mike Fiore |
11:d2e31743433a | 19 | // * these options must match between the two devices in // |
Mike Fiore |
11:d2e31743433a | 20 | // order for communication to be successful |
Mike Fiore |
11:d2e31743433a | 21 | ///////////////////////////////////////////////////////////// |
Mike Fiore |
11:d2e31743433a | 22 | static uint8_t network_address[] = { 0x01, 0x02, 0x03, 0x04 }; |
Mike Fiore |
11:d2e31743433a | 23 | static uint8_t network_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
mbriggs_vortex | 19:75b28b4117cc | 24 | static uint8_t data_session_key[] = { 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04, 0x01, 0x02, 0x03, 0x04 }; |
Mike Fiore |
11:d2e31743433a | 25 | |
Matt Briggs | 25:fea776239709 | 26 | DigitalOut led1(LED1); |
Mike Fiore |
11:d2e31743433a | 27 | mDot* dot = NULL; |
Mike Fiore |
11:d2e31743433a | 28 | |
Mike Fiore |
11:d2e31743433a | 29 | Serial pc(USBTX, USBRX); |
Mike Fiore |
11:d2e31743433a | 30 | |
Mike Fiore |
11:d2e31743433a | 31 | int main() { |
Mike Fiore |
14:19fae4509473 | 32 | // Custom event handler for automatically displaying RX data |
Mike Fiore |
11:d2e31743433a | 33 | RadioEvent events; |
Mike Fiore |
11:d2e31743433a | 34 | uint32_t tx_frequency; |
Mike Fiore |
11:d2e31743433a | 35 | uint8_t tx_datarate; |
Mike Fiore |
11:d2e31743433a | 36 | uint8_t tx_power; |
Mike Fiore |
11:d2e31743433a | 37 | uint8_t frequency_band; |
Mike Fiore |
11:d2e31743433a | 38 | |
Mike Fiore |
11:d2e31743433a | 39 | pc.baud(115200); |
Mike Fiore |
11:d2e31743433a | 40 | |
Mike Fiore |
11:d2e31743433a | 41 | mts::MTSLog::setLogLevel(mts::MTSLog::TRACE_LEVEL); |
Mike Fiore |
11:d2e31743433a | 42 | |
Mike Fiore |
11:d2e31743433a | 43 | dot = mDot::getInstance(); |
Mike Fiore |
11:d2e31743433a | 44 | |
Mike Fiore |
16:a3832552dfe1 | 45 | logInfo("mbed-os library version: %d", MBED_LIBRARY_VERSION); |
Mike Fiore |
16:a3832552dfe1 | 46 | |
Mike Fiore |
12:ec9768677cea | 47 | // start from a well-known state |
Mike Fiore |
12:ec9768677cea | 48 | logInfo("defaulting Dot configuration"); |
Mike Fiore |
12:ec9768677cea | 49 | dot->resetConfig(); |
Mike Fiore |
12:ec9768677cea | 50 | |
Mike Fiore |
11:d2e31743433a | 51 | // make sure library logging is turned on |
Mike Fiore |
11:d2e31743433a | 52 | dot->setLogLevel(mts::MTSLog::INFO_LEVEL); |
Mike Fiore |
11:d2e31743433a | 53 | |
Mike Fiore |
11:d2e31743433a | 54 | // attach the custom events handler |
Mike Fiore |
11:d2e31743433a | 55 | dot->setEvents(&events); |
Mike Fiore |
11:d2e31743433a | 56 | |
Mike Fiore |
11:d2e31743433a | 57 | // update configuration if necessary |
Mike Fiore |
11:d2e31743433a | 58 | if (dot->getJoinMode() != mDot::PEER_TO_PEER) { |
Mike Fiore |
11:d2e31743433a | 59 | logInfo("changing network join mode to PEER_TO_PEER"); |
Mike Fiore |
11:d2e31743433a | 60 | if (dot->setJoinMode(mDot::PEER_TO_PEER) != mDot::MDOT_OK) { |
Mike Fiore |
11:d2e31743433a | 61 | logError("failed to set network join mode to PEER_TO_PEER"); |
Mike Fiore |
11:d2e31743433a | 62 | } |
Mike Fiore |
11:d2e31743433a | 63 | } |
Mike Fiore |
11:d2e31743433a | 64 | frequency_band = dot->getFrequencyBand(); |
Mike Fiore |
11:d2e31743433a | 65 | switch (frequency_band) { |
Mike Fiore |
11:d2e31743433a | 66 | case mDot::FB_EU868: |
Mike Fiore |
11:d2e31743433a | 67 | // 250kHz channels achieve higher throughput |
Mike Fiore |
11:d2e31743433a | 68 | // DR6 : SF7 @ 250kHz |
Mike Fiore |
11:d2e31743433a | 69 | // DR0 - DR5 (125kHz channels) available but much slower |
Mike Fiore |
11:d2e31743433a | 70 | tx_frequency = 869850000; |
Mike Fiore |
11:d2e31743433a | 71 | tx_datarate = mDot::DR6; |
Mike Fiore |
11:d2e31743433a | 72 | // the 869850000 frequency is 100% duty cycle if the total power is under 7 dBm - tx power 4 + antenna gain 3 = 7 |
Mike Fiore |
11:d2e31743433a | 73 | tx_power = 4; |
Mike Fiore |
11:d2e31743433a | 74 | break; |
Mike Fiore |
11:d2e31743433a | 75 | case mDot::FB_US915: |
Mike Fiore |
11:d2e31743433a | 76 | case mDot::FB_AU915: |
Mike Fiore |
11:d2e31743433a | 77 | default: |
Mike Fiore |
11:d2e31743433a | 78 | // 500kHz channels achieve highest throughput |
Mike Fiore |
11:d2e31743433a | 79 | // DR8 : SF12 @ 500kHz |
Mike Fiore |
11:d2e31743433a | 80 | // DR9 : SF11 @ 500kHz |
Mike Fiore |
11:d2e31743433a | 81 | // DR10 : SF10 @ 500kHz |
Mike Fiore |
11:d2e31743433a | 82 | // DR11 : SF9 @ 500kHz |
Mike Fiore |
11:d2e31743433a | 83 | // DR12 : SF8 @ 500kHz |
Mike Fiore |
11:d2e31743433a | 84 | // DR13 : SF7 @ 500kHz |
Mike Fiore |
11:d2e31743433a | 85 | // DR0 - DR3 (125kHz channels) available but much slower |
Mike Fiore |
11:d2e31743433a | 86 | tx_frequency = 915500000; |
Mike Fiore |
11:d2e31743433a | 87 | tx_datarate = mDot::DR13; |
Mike Fiore |
11:d2e31743433a | 88 | // 915 bands have no duty cycle restrictions, set tx power to max |
Mike Fiore |
11:d2e31743433a | 89 | tx_power = 20; |
Mike Fiore |
11:d2e31743433a | 90 | break; |
Mike Fiore |
11:d2e31743433a | 91 | } |
Mike Fiore |
11:d2e31743433a | 92 | // in PEER_TO_PEER mode there is no join request/response transaction |
Mike Fiore |
11:d2e31743433a | 93 | // as long as both Dots are configured correctly, they should be able to communicate |
Mike Fiore |
11:d2e31743433a | 94 | update_peer_to_peer_config(network_address, network_session_key, data_session_key, tx_frequency, tx_datarate, tx_power); |
Mike Fiore |
11:d2e31743433a | 95 | |
Matt Briggs | 24:fdf87e4b72e5 | 96 | |
Matt Briggs | 24:fdf87e4b72e5 | 97 | unsigned int wakeMode; |
Matt Briggs | 24:fdf87e4b72e5 | 98 | /////////////////////////////// |
Matt Briggs | 24:fdf87e4b72e5 | 99 | // Transmitter Configuration // |
Matt Briggs | 24:fdf87e4b72e5 | 100 | /////////////////////////////// |
Matt Briggs | 24:fdf87e4b72e5 | 101 | #if BRIDGE_TX |
Matt Briggs | 24:fdf87e4b72e5 | 102 | wakeMode = mDot::INTERRUPT; |
Matt Briggs | 24:fdf87e4b72e5 | 103 | #endif |
Matt Briggs | 24:fdf87e4b72e5 | 104 | |
Matt Briggs | 24:fdf87e4b72e5 | 105 | //////////////////////////// |
Matt Briggs | 24:fdf87e4b72e5 | 106 | // Receiver Configuration // |
Matt Briggs | 24:fdf87e4b72e5 | 107 | //////////////////////////// |
Matt Briggs | 24:fdf87e4b72e5 | 108 | #if BRIDGE_RX |
Matt Briggs | 24:fdf87e4b72e5 | 109 | wakeMode = mDot::RTC_ALARM_OR_INTERRUPT; |
Matt Briggs | 24:fdf87e4b72e5 | 110 | #endif |
Matt Briggs | 24:fdf87e4b72e5 | 111 | |
Matt Briggs | 24:fdf87e4b72e5 | 112 | // Common Configuration |
Matt Briggs | 25:fea776239709 | 113 | dot->setWakePin(WAKE); // Use the wake pin as sleep interrupt |
Matt Briggs | 25:fea776239709 | 114 | dot->setClass("C"); // Set class C |
Matt Briggs | 25:fea776239709 | 115 | dot->setAck(0); // Disable Ack |
Matt Briggs | 24:fdf87e4b72e5 | 116 | |
Mike Fiore |
11:d2e31743433a | 117 | // save changes to configuration |
Mike Fiore |
11:d2e31743433a | 118 | logInfo("saving configuration"); |
Mike Fiore |
11:d2e31743433a | 119 | if (!dot->saveConfig()) { |
Mike Fiore |
11:d2e31743433a | 120 | logError("failed to save configuration"); |
Mike Fiore |
11:d2e31743433a | 121 | } |
Mike Fiore |
11:d2e31743433a | 122 | |
Mike Fiore |
11:d2e31743433a | 123 | // display configuration |
Mike Fiore |
11:d2e31743433a | 124 | display_config(); |
Mike Fiore |
11:d2e31743433a | 125 | |
mbriggs_vortex | 23:4ed894108882 | 126 | uint16_t seqNum=0; |
Matt Briggs | 25:fea776239709 | 127 | uint32_t cDwnLink; |
Mike Fiore |
11:d2e31743433a | 128 | while (true) { |
Matt Briggs | 25:fea776239709 | 129 | std::vector<uint8_t> data; |
Matt Briggs | 25:fea776239709 | 130 | led1=0; |
Matt Briggs | 24:fdf87e4b72e5 | 131 | /////////////////////////// |
Matt Briggs | 24:fdf87e4b72e5 | 132 | // Transmitter main loop // |
Matt Briggs | 24:fdf87e4b72e5 | 133 | /////////////////////////// |
Matt Briggs | 24:fdf87e4b72e5 | 134 | #if BRIDGE_TX |
Mike Fiore |
11:d2e31743433a | 135 | |
Mike Fiore |
11:d2e31743433a | 136 | // join network if not joined |
Mike Fiore |
11:d2e31743433a | 137 | if (!dot->getNetworkJoinStatus()) { |
Mike Fiore |
11:d2e31743433a | 138 | join_network(); |
Mike Fiore |
11:d2e31743433a | 139 | } |
Mike Fiore |
11:d2e31743433a | 140 | |
Matt Briggs | 25:fea776239709 | 141 | data.push_back((seqNum >> 8) & 0xFF); |
Matt Briggs | 25:fea776239709 | 142 | data.push_back(seqNum & 0xFF); |
mbriggs_vortex | 23:4ed894108882 | 143 | logInfo("seqNum: %lu", seqNum); |
Matt Briggs | 25:fea776239709 | 144 | send_data(data); |
mbriggs_vortex | 23:4ed894108882 | 145 | seqNum++; |
Matt Briggs | 25:fea776239709 | 146 | led1=1; |
Matt Briggs | 25:fea776239709 | 147 | wait(0.5); |
Matt Briggs | 25:fea776239709 | 148 | led1=0; |
mbriggs_vortex | 23:4ed894108882 | 149 | |
Matt Briggs | 24:fdf87e4b72e5 | 150 | dot->sleep(0, wakeMode, false); // Go to sleep until wake button |
Matt Briggs | 24:fdf87e4b72e5 | 151 | #endif |
Matt Briggs | 25:fea776239709 | 152 | #if BRIDGE_RX |
Matt Briggs | 25:fea776239709 | 153 | cDwnLink = dot->getDownLinkCounter(); |
Matt Briggs | 25:fea776239709 | 154 | logInfo("Waiting for new message current DLC: %d", cDwnLink); |
Matt Briggs | 25:fea776239709 | 155 | while (cDwnLink >= dot->getDownLinkCounter()) { |
Matt Briggs | 25:fea776239709 | 156 | wait(0.1); |
Matt Briggs | 25:fea776239709 | 157 | } |
Matt Briggs | 25:fea776239709 | 158 | led1 = 1; |
Matt Briggs | 25:fea776239709 | 159 | dot->recv(data); |
Matt Briggs | 25:fea776239709 | 160 | std::string dataStr(data.begin(), data.end()); |
Matt Briggs | 25:fea776239709 | 161 | logInfo("Got msg num: %d, payload: %s", seqNum, dataStr.c_str()); |
Matt Briggs | 25:fea776239709 | 162 | seqNum++; |
Matt Briggs | 25:fea776239709 | 163 | wait(0.5); |
Matt Briggs | 25:fea776239709 | 164 | led1=0; |
Matt Briggs | 25:fea776239709 | 165 | |
Matt Briggs | 25:fea776239709 | 166 | #endif |
Mike Fiore |
11:d2e31743433a | 167 | } |
Mike Fiore |
11:d2e31743433a | 168 | |
Mike Fiore |
11:d2e31743433a | 169 | return 0; |
Mike Fiore |
11:d2e31743433a | 170 | } |
Mike Fiore |
11:d2e31743433a | 171 |