Elmo Terminal provides functionality to test Lora radio and access SX1272 chip registers delivered with Elmo board. Also contains example ping-pong application.

Dependencies:   SX1272lib mbed-src

Settings/Variable.cpp

Committer:
WGorniak
Date:
2015-10-01
Revision:
2:8d8295a51f68
Child:
3:bb58d4e78e68

File content as of revision 2:8d8295a51f68:

#include <sstream>
#include <limits>
#include "Variable.h"

Variable::ValueDescription Variable::none[] = {{0,0}};

int32_t Variable::nonSetValue()
{
    return std::numeric_limits<int32_t>::min();
}

Variable::Variable()
    : name_("")
    , description_("")
    , pValueDescriptions_(none)
    , value_(nonSetValue())
{
}

bool Variable::endGuard() const
{
    return value_ == nonSetValue();
}

Variable::Variable(const char* name, const char* description, int32_t value, const ValueDescription* const pValueDescriptions)
    : name_(name)
    , description_(description)
    , pValueDescriptions_(pValueDescriptions)
    , value_(value)
{
}

std::string Variable::name() const
{
    return name_;
}

std::string Variable::help() const
{
    std::ostringstream temp;

    temp << description_ << "\r\n";

    for (const ValueDescription* p = pValueDescriptions_; !p->isEnd(); ++p)
    {
        if (p->isEndRange())
        {
            continue;
        }

        temp << "    ";

        if ((p+1)->isEndRange())
        {
            temp << p->value << "..." << (p+1)->value << " " << p->description;
        }
        else
        {
            temp << p->value << ": " << p->description << " ";
        }

        temp << "\r\n";
    }

    std::string s = temp.str();
    return s;
}

std::string Variable::describe(int32_t value) const
{
    for (const ValueDescription* p = pValueDescriptions_; !p->isEnd(); ++p)
    {
        if (p->value > value || p->isEndRange())
        {
            continue;
        }

        if ((p+1)->isEndRange())
        {
            if ((p->value <= value) && (value <= (p+1)->value))
            {
                std::ostringstream temp;
                temp << value << " " << p->description;
                return temp.str();
            }
        } else
        if (p->value == value)
        {
            std::ostringstream temp;
            temp << value << " - " << p->description;
            return temp.str();
        }
    }
    return "";
}

bool Variable::set(int32_t value)
{
    if (describe(value).size() == 0)
    {
        return false;
    }
    value_ = value;
    return true;
}

int32_t Variable::get() const
{
    return value_;
}

std::string Variable::describe() const
{
    return  describe(get());
}