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 mbed mbed-rtos
main.cpp@17:cbd4487522da, 2019-02-09 (annotated)
- Committer:
- fangchen2
- Date:
- Sat Feb 09 09:05:40 2019 +0000
- Revision:
- 17:cbd4487522da
- Parent:
- 16:3dd14aac7233
IOT_UTS_1;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mfiore | 0:09250cd371d2 | 1 | #include "mbed.h" |
| mfiore | 0:09250cd371d2 | 2 | #include "mDot.h" |
| mfiore | 4:36e214ebfa56 | 3 | #include "MTSLog.h" |
| mfiore | 0:09250cd371d2 | 4 | #include <string> |
| mfiore | 0:09250cd371d2 | 5 | #include <vector> |
| mfiore | 4:36e214ebfa56 | 6 | #include <algorithm> |
| fangchen2 | 17:cbd4487522da | 7 | #include "MTSText.h" |
| fangchen2 | 16:3dd14aac7233 | 8 | |
| fangchen2 | 16:3dd14aac7233 | 9 | |
| fangchen2 | 16:3dd14aac7233 | 10 | |
| fangchen2 | 17:cbd4487522da | 11 | using namespace mts; |
| fangchen2 | 16:3dd14aac7233 | 12 | |
| fangchen2 | 14:3ce2765dd92f | 13 | static std::string config_network_name = "iotlab01"; |
| fangchen2 | 16:3dd14aac7233 | 14 | static std::string config_network_pass = "11223344"; |
| fangchen2 | 17:cbd4487522da | 15 | static uint8_t config_frequency_sub_band = 7; |
| wang1tao | 13:c966b64b227f | 16 | |
| mfiore | 0:09250cd371d2 | 17 | int main() { |
| fangchen2 | 17:cbd4487522da | 18 | Serial debug(USBTX, USBRX); |
| fangchen2 | 17:cbd4487522da | 19 | debug.baud(9600); |
| fangchen2 | 17:cbd4487522da | 20 | |
| fangchen2 | 17:cbd4487522da | 21 | int32_t ret; |
| fangchen2 | 17:cbd4487522da | 22 | int32_t next_tx; |
| fangchen2 | 17:cbd4487522da | 23 | int32_t wait_time = 3; |
| mfiore | 0:09250cd371d2 | 24 | mDot* dot; |
| fangchen2 | 17:cbd4487522da | 25 | std::vector<uint8_t> send_data; |
| fangchen2 | 17:cbd4487522da | 26 | std::vector<uint8_t> recv_data; |
| fangchen2 | 17:cbd4487522da | 27 | uint8_t recv = 0; |
| fangchen2 | 17:cbd4487522da | 28 | uint8_t recv_mismatch = 0; |
| fangchen2 | 17:cbd4487522da | 29 | uint8_t send_failure = 0; |
| fangchen2 | 17:cbd4487522da | 30 | uint8_t iterations = 50; |
| fangchen2 | 17:cbd4487522da | 31 | |
| fangchen2 | 17:cbd4487522da | 32 | send_data.push_back(0x00); |
| fangchen2 | 17:cbd4487522da | 33 | send_data.push_back(0xFF); |
| fangchen2 | 17:cbd4487522da | 34 | send_data.push_back(0xFF); |
| fangchen2 | 17:cbd4487522da | 35 | send_data.push_back(0xFF); |
| fangchen2 | 17:cbd4487522da | 36 | send_data.push_back(0xFF); |
| fangchen2 | 17:cbd4487522da | 37 | send_data.push_back(0xFF); |
| fangchen2 | 17:cbd4487522da | 38 | send_data.push_back(0xFF); |
| fangchen2 | 17:cbd4487522da | 39 | send_data.push_back(0xFF); |
| fangchen2 | 17:cbd4487522da | 40 | |
| mfiore | 0:09250cd371d2 | 41 | dot = mDot::getInstance(); |
| JCheng | 5:6c7d2f4d7377 | 42 | |
| mfiore | 0:09250cd371d2 | 43 | dot->resetConfig(); |
| fangchen2 | 17:cbd4487522da | 44 | |
| fangchen2 | 17:cbd4487522da | 45 | dot->setLogLevel(MTSLog::TRACE_LEVEL); |
| JCheng | 5:6c7d2f4d7377 | 46 | |
| fangchen2 | 17:cbd4487522da | 47 | while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) { |
| fangchen2 | 17:cbd4487522da | 48 | logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 2:6e2c378339d9 | 49 | } |
| fangchen2 | 17:cbd4487522da | 50 | while ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) { |
| fangchen2 | 17:cbd4487522da | 51 | logError("failed to set network name: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:09250cd371d2 | 52 | } |
| fangchen2 | 17:cbd4487522da | 53 | while ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) { |
| fangchen2 | 17:cbd4487522da | 54 | logError("failed to set network password: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| mfiore | 0:09250cd371d2 | 55 | } |
| JCheng | 5:6c7d2f4d7377 | 56 | |
| fangchen2 | 17:cbd4487522da | 57 | logInfo("enabling activity LED"); |
| fangchen2 | 17:cbd4487522da | 58 | dot->setActivityLedEnable(true); |
| fangchen2 | 17:cbd4487522da | 59 | |
| fangchen2 | 17:cbd4487522da | 60 | logInfo("joining network"); |
| fangchen2 | 17:cbd4487522da | 61 | while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) { |
| fangchen2 | 17:cbd4487522da | 62 | logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| fangchen2 | 17:cbd4487522da | 63 | wait_ms(dot->getNextTxMs() + 1000); |
| mfiore | 4:36e214ebfa56 | 64 | } |
| fangchen2 | 17:cbd4487522da | 65 | logInfo("joined"); |
| JCheng | 5:6c7d2f4d7377 | 66 | |
| fangchen2 | 17:cbd4487522da | 67 | for (uint8_t i = 0; i < iterations; i++) { |
| fangchen2 | 17:cbd4487522da | 68 | send_data[0] = i; |
| fangchen2 | 17:cbd4487522da | 69 | if ((ret = dot->send(send_data)) != mDot::MDOT_OK) { |
| fangchen2 | 17:cbd4487522da | 70 | logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| fangchen2 | 17:cbd4487522da | 71 | send_failure++; |
| fangchen2 | 17:cbd4487522da | 72 | } else { |
| fangchen2 | 17:cbd4487522da | 73 | logInfo("send data: %s", Text::bin2hexString(send_data).c_str()); |
| fangchen2 | 17:cbd4487522da | 74 | if ((ret = dot->recv(recv_data)) != mDot::MDOT_OK) { |
| fangchen2 | 17:cbd4487522da | 75 | logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str()); |
| fangchen2 | 17:cbd4487522da | 76 | } else { |
| fangchen2 | 17:cbd4487522da | 77 | logInfo("recv data: %s", Text::bin2hexString(recv_data).c_str()); |
| fangchen2 | 17:cbd4487522da | 78 | if (recv_data == send_data) { |
| fangchen2 | 17:cbd4487522da | 79 | recv++; |
| fangchen2 | 17:cbd4487522da | 80 | } else { |
| fangchen2 | 17:cbd4487522da | 81 | recv_mismatch++; |
| fangchen2 | 17:cbd4487522da | 82 | } |
| fangchen2 | 17:cbd4487522da | 83 | } |
| fangchen2 | 17:cbd4487522da | 84 | recv_data.clear(); |
| fangchen2 | 17:cbd4487522da | 85 | } |
| fangchen2 | 17:cbd4487522da | 86 | |
| fangchen2 | 17:cbd4487522da | 87 | next_tx = dot->getNextTxMs() + 1000; |
| fangchen2 | 17:cbd4487522da | 88 | logInfo("waiting %ld ms to transmit again", next_tx); |
| fangchen2 | 17:cbd4487522da | 89 | wait_ms(next_tx); |
| fangchen2 | 17:cbd4487522da | 90 | logInfo("waiting another %d seconds", wait_time); |
| fangchen2 | 17:cbd4487522da | 91 | wait(wait_time); |
| mfiore | 0:09250cd371d2 | 92 | } |
| wang1tao | 13:c966b64b227f | 93 | |
| fangchen2 | 17:cbd4487522da | 94 | logInfo("Version: %s", dot->getId().c_str()); |
| fangchen2 | 17:cbd4487522da | 95 | logInfo("Recv: %d/%d", recv, iterations); |
| fangchen2 | 17:cbd4487522da | 96 | logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations); |
| fangchen2 | 17:cbd4487522da | 97 | logInfo("Send Failure: %d/%d", send_failure, iterations); |
| fangchen2 | 17:cbd4487522da | 98 | logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations); |
| mfiore | 0:09250cd371d2 | 99 | |
| fangchen2 | 17:cbd4487522da | 100 | return 0; |
| fangchen2 | 17:cbd4487522da | 101 | } |