Mike Fiore / mDot_test_rx

Dependencies:   libmDot mbed-rtos mbed-src

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "mDot.h"
00003 #include "MTSLog.h"
00004 #include "MTSText.h"
00005 #include <string>
00006 #include <vector>
00007 
00008 using namespace mts;
00009 
00010 static std::string config_network_name = "";
00011 static std::string config_network_pass = "";
00012 static uint8_t config_frequency_sub_band = 1;
00013 
00014 int main() {
00015     Serial debug(USBTX, USBRX);
00016     debug.baud(460800);
00017     
00018     int32_t ret;
00019     int32_t next_tx;
00020     int32_t wait_time = 2;
00021     mDot* dot;
00022     std::vector<uint8_t> send_data;
00023     std::vector<uint8_t> recv_data;
00024     uint8_t recv = 0;
00025     uint8_t recv_mismatch = 0;
00026     uint8_t send_failure = 0;
00027     uint8_t iterations = 50;
00028     
00029     send_data.push_back(0x00);
00030     send_data.push_back(0xFF);
00031     send_data.push_back(0xFF);
00032     send_data.push_back(0xFF);
00033     send_data.push_back(0xFF);
00034     send_data.push_back(0xFF);
00035     send_data.push_back(0xFF);
00036     send_data.push_back(0xFF);
00037 
00038     dot = mDot::getInstance();
00039 
00040     dot->resetConfig();
00041     
00042     dot->setLogLevel(MTSLog::TRACE_LEVEL);
00043 
00044     while ((ret = dot->setFrequencySubBand(config_frequency_sub_band)) != mDot::MDOT_OK) {
00045         logError("failed to set frequency sub band: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00046     }
00047     while ((ret = dot->setNetworkName(config_network_name)) != mDot::MDOT_OK) {
00048         logError("failed to set network name: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00049     }
00050     while ((ret = dot->setNetworkPassphrase(config_network_pass)) != mDot::MDOT_OK) {
00051         logError("failed to set network password: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00052     }
00053 
00054     logInfo("enabling activity LED");
00055     dot->setActivityLedEnable(true);
00056 
00057     logInfo("joining network");
00058     while ((ret = dot->joinNetwork()) != mDot::MDOT_OK) {
00059         logError("failed to join network: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00060         wait_ms(dot->getNextTxMs() + 1);
00061     }
00062     logInfo("joined");
00063 
00064     for (uint8_t i = 0; i < iterations; i++) {
00065         send_data[0] = i;
00066         if ((ret = dot->send(send_data)) != mDot::MDOT_OK) {
00067             logError("failed to send: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00068             send_failure++;
00069         } else {
00070             logInfo("send data: %s", Text::bin2hexString(send_data).c_str());
00071             if ((ret = dot->recv(recv_data)) != mDot::MDOT_OK) {
00072                 logError("failed to recv: [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
00073             } else {
00074                 logInfo("recv data: %s", Text::bin2hexString(recv_data).c_str());
00075                 if (recv_data == send_data) {
00076                     recv++;
00077                 } else {
00078                     recv_mismatch++;
00079                 }
00080             }
00081             recv_data.clear();
00082         }
00083         
00084         next_tx = dot->getNextTxMs() + 1;
00085         logInfo("waiting %ld ms to transmit again", next_tx);
00086         wait_ms(next_tx);
00087         logInfo("waiting another %d seconds", wait_time);
00088         wait(wait_time);
00089     }
00090     
00091     logInfo("Version: %s", dot->getId().c_str());
00092     logInfo("Recv: %d/%d", recv, iterations);
00093     logInfo("Recv Mismatch: %d/%d", recv_mismatch, iterations);
00094     logInfo("Send Failure: %d/%d", send_failure, iterations);
00095     logInfo("Dropped: %d/%d", iterations - (recv + recv_mismatch + send_failure), iterations);
00096 
00097     return 0;
00098 }