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: libxDot-mbed5 ISL29011
Fork of Dot-Examples by
examples/src/dot_util.cpp
- Committer:
- mfiore
- Date:
- 2016-10-05
- Revision:
- 0:a151a6350d7f
- Child:
- 1:c4915e00d2ce
File content as of revision 0:a151a6350d7f:
#include "dot_util.h"
void display_config() {
// display configuration and library version information
logInfo("version: %s", dot->getId().c_str());
logInfo("general configuration");
logInfo("---------------------");
logInfo("\tdevice ID/EUI: %s", mts::Text::bin2hexString(dot->getDeviceId()).c_str());
logInfo("\tfrequency band: %s", mDot::FrequencyBandStr(dot->getFrequencyBand()).c_str());
logInfo("\tfrequency sub band: %u", dot->getFrequencySubBand());
logInfo("\tpublic network: %s", dot->getPublicNetwork() == true ? "on" : "off");
logInfo("credentials configuration");
logInfo("-------------------------");
logInfo("\tnetwork name: %s", dot->getNetworkName().c_str());
logInfo("\tnetwork phrase: %s", dot->getNetworkPassphrase().c_str());
logInfo("\tnetwork join mode: %s", mDot::JoinModeStr(dot->getJoinMode()).c_str());
logInfo("communication parameters");
logInfo("------------------------");
logInfo("\tacks: %s, %u attempts", dot->getAck() > 0 ? "on" : "off", dot->getAck());
logInfo("\tTX datarate: %s", mDot::DataRateStr(dot->getTxDataRate()).c_str());
logInfo("\tTX power: %lu dBm", dot->getTxPower());
logInfo("\tatnenna gain: %u dBm", dot->getAntennaGain());
}
void update_ota_config(std::string network_name, std::string network_passphrase, uint8_t frequency_sub_band, bool public_network, uint8_t ack) {
std::string current_network_name = dot->getNetworkName();
std::string current_network_passphrase = dot->getNetworkPassphrase();
uint8_t current_frequency_sub_band = dot->getFrequencySubBand();
bool current_public_network = dot->getPublicNetwork();
uint8_t current_ack = dot->getAck();
if (current_network_name != network_name) {
logInfo("changing network name from \"%s\" to \"%s\"", current_network_name.c_str(), network_name.c_str());
if (dot->setNetworkName(network_name) != mDot::MDOT_OK) {
logError("failed to set network name to \"%s\"", network_name.c_str());
}
}
if (current_network_passphrase != network_passphrase) {
logInfo("changing network passphrase from \"%s\" to \"%s\"", current_network_passphrase.c_str(), network_passphrase.c_str());
if (dot->setNetworkPassphrase(network_passphrase) != mDot::MDOT_OK) {
logError("failed to set network passphrase to \"%s\"", network_passphrase.c_str());
}
}
if (current_frequency_sub_band != frequency_sub_band) {
logInfo("changing frequency sub band from %u to %u", current_frequency_sub_band, frequency_sub_band);
if (dot->setFrequencySubBand(frequency_sub_band) != mDot::MDOT_OK) {
logError("failed to set frequency sub band to %u", frequency_sub_band);
}
}
if (current_public_network != public_network) {
logInfo("changing public network from %s to %s", current_public_network ? "true" : "false", public_network ? "true" : "false");
if (dot->setPublicNetwork(public_network) != mDot::MDOT_OK) {
logError("failed to set public network to %s", public_network ? "true" : "false");
}
}
if (current_ack != ack) {
logInfo("changing acks from %u to %u", current_ack, ack);
if (dot->setAck(ack) != mDot::MDOT_OK) {
logError("failed to set acks to %u", ack);
}
}
}
void join_network() {
int32_t j_attempts = 0;
int32_t ret = mDot::MDOT_ERROR;
// attempt to join the network
while (ret != mDot::MDOT_OK) {
logInfo("attempt %d to join network", ++j_attempts);
ret = dot->joinNetwork();
if (ret != mDot::MDOT_OK) {
logError("failed to join network %d:%s", ret, mDot::getReturnCodeString(ret).c_str());
// in some frequency bands we need to wait until another channel is available before transmitting again
uint32_t delay_s = (dot->getNextTxMs() / 1000) + 1;
if (delay_s < 2) {
logInfo("waiting %lu s until next free channel", delay_s);
wait(delay_s);
} else {
logInfo("sleeping %lu s until next free channel", delay_s);
dot->sleep(delay_s, mDot::RTC_ALARM, false);
}
}
}
}
void sleep_wake_rtc_only(bool deepsleep) {
// in some frequency bands we need to wait until another channel is available before transmitting again
// wait at least 10s between transmissions
uint32_t delay_s = dot->getNextTxMs() / 1000;
if (delay_s < 10) {
delay_s = 10;
}
logInfo("%ssleeping %lus", deepsleep ? "deep" : "", delay_s);
logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume");
// go to sleep/deepsleep for delay_s seconds and wake using the RTC alarm
dot->sleep(delay_s, mDot::RTC_ALARM, deepsleep);
}
void sleep_wake_interrupt_only(bool deepsleep) {
#if defined (TARGET_XDOT_L151CC)
if (deepsleep) {
// for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep
// it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
} else {
// configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes
// other pins can be confgured instead: GPIO0-3 or UART_RX
dot->setWakePin(WAKE);
}
logInfo("%ssleeping until rising edge on %s pin", deepsleep ? "deep" : "", deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str());
#else
if (deepsleep) {
// for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep
// it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
} else {
// configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes
// other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN
dot->setWakePin(XBEE_DIO7);
}
logInfo("%ssleeping until rising edge on %s pin", deepsleep ? "deep" : "", deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str());
#endif
logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume");
// go to sleep/deepsleep and wake on rising edge of configured wake pin (only the WAKE pin in deepsleep)
// since we're not waking on the RTC alarm, the interval is ignored
dot->sleep(0, mDot::INTERRUPT, deepsleep);
}
void sleep_wake_rtc_or_interrupt(bool deepsleep) {
// in some frequency bands we need to wait until another channel is available before transmitting again
// wait at least 10s between transmissions
uint32_t delay_s = dot->getNextTxMs() / 1000;
if (delay_s < 10) {
delay_s = 10;
}
#if defined (TARGET_XDOT_L151CC)
if (deepsleep) {
// for xDot, WAKE pin (connected to S2 on xDot-DK) is the only pin that can wake the processor from deepsleep
// it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
} else {
// configure WAKE pin (connected to S2 on xDot-DK) as the pin that will wake the xDot from low power modes
// other pins can be confgured instead: GPIO0-3 or UART_RX
dot->setWakePin(WAKE);
}
logInfo("%ssleeping %lus or until rising edge on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "WAKE" : mDot::pinName2Str(dot->getWakePin()).c_str());
#else
if (deepsleep) {
// for mDot, XBEE_DIO7 pin is the only pin that can wake the processor from deepsleep
// it is automatically configured when INTERRUPT or RTC_ALARM_OR_INTERRUPT is the wakeup source and deepsleep is true in the mDot::sleep call
} else {
// configure XBEE_DIO7 pin as the pin that will wake the mDot from low power modes
// other pins can be confgured instead: XBEE_DIO2-6, XBEE_DI8, XBEE_DIN
dot->setWakePin(XBEE_DIO7);
}
logInfo("%ssleeping %lus or until rising edge on %s pin", deepsleep ? "deep" : "", delay_s, deepsleep ? "DIO7" : mDot::pinName2Str(dot->getWakePin()).c_str());
#endif
logInfo("application will %s after waking up", deepsleep ? "execute from beginning" : "resume");
// go to sleep/deepsleep and wake using the RTC alarm after delay_s seconds or rising edge of configured wake pin (only the WAKE pin in deepsleep)
// whichever comes first will wake the xDot
dot->sleep(delay_s, mDot::RTC_ALARM_OR_INTERRUPT, deepsleep);
}
void send_data(std::vector<uint8_t> data) {
uint32_t ret;
ret = dot->send(data);
if (ret != mDot::MDOT_OK) {
logError("failed to send light data to gateway [%d][%s]", ret, mDot::getReturnCodeString(ret).c_str());
} else {
logInfo("successfully sent light data to gateway");
}
}
