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@3:bb58d4e78e68, 2015-10-01 (annotated)
- Committer:
- WGorniak
- Date:
- Thu Oct 01 14:42:59 2015 +0200
- Revision:
- 3:bb58d4e78e68
- Parent:
- 2:8d8295a51f68
removed sstream (arm microlib compatibility)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WGorniak | 3:bb58d4e78e68 | 1 | #include <stdio.h> |
WGorniak | 2:8d8295a51f68 | 2 | #include <limits> |
WGorniak | 2:8d8295a51f68 | 3 | #include "Variable.h" |
WGorniak | 2:8d8295a51f68 | 4 | |
WGorniak | 3:bb58d4e78e68 | 5 | std::string to_string(const int32_t val) |
WGorniak | 3:bb58d4e78e68 | 6 | { |
WGorniak | 3:bb58d4e78e68 | 7 | char buffer[10]; |
WGorniak | 3:bb58d4e78e68 | 8 | sprintf(buffer,"%ld",val); |
WGorniak | 3:bb58d4e78e68 | 9 | return std::string(buffer); |
WGorniak | 3:bb58d4e78e68 | 10 | } |
WGorniak | 3:bb58d4e78e68 | 11 | |
WGorniak | 2:8d8295a51f68 | 12 | Variable::ValueDescription Variable::none[] = {{0,0}}; |
WGorniak | 2:8d8295a51f68 | 13 | |
WGorniak | 2:8d8295a51f68 | 14 | int32_t Variable::nonSetValue() |
WGorniak | 2:8d8295a51f68 | 15 | { |
WGorniak | 2:8d8295a51f68 | 16 | return std::numeric_limits<int32_t>::min(); |
WGorniak | 2:8d8295a51f68 | 17 | } |
WGorniak | 2:8d8295a51f68 | 18 | |
WGorniak | 2:8d8295a51f68 | 19 | Variable::Variable() |
WGorniak | 2:8d8295a51f68 | 20 | : name_("") |
WGorniak | 2:8d8295a51f68 | 21 | , description_("") |
WGorniak | 2:8d8295a51f68 | 22 | , pValueDescriptions_(none) |
WGorniak | 2:8d8295a51f68 | 23 | , value_(nonSetValue()) |
WGorniak | 2:8d8295a51f68 | 24 | { |
WGorniak | 2:8d8295a51f68 | 25 | } |
WGorniak | 2:8d8295a51f68 | 26 | |
WGorniak | 2:8d8295a51f68 | 27 | bool Variable::endGuard() const |
WGorniak | 2:8d8295a51f68 | 28 | { |
WGorniak | 2:8d8295a51f68 | 29 | return value_ == nonSetValue(); |
WGorniak | 2:8d8295a51f68 | 30 | } |
WGorniak | 2:8d8295a51f68 | 31 | |
WGorniak | 2:8d8295a51f68 | 32 | Variable::Variable(const char* name, const char* description, int32_t value, const ValueDescription* const pValueDescriptions) |
WGorniak | 2:8d8295a51f68 | 33 | : name_(name) |
WGorniak | 2:8d8295a51f68 | 34 | , description_(description) |
WGorniak | 2:8d8295a51f68 | 35 | , pValueDescriptions_(pValueDescriptions) |
WGorniak | 2:8d8295a51f68 | 36 | , value_(value) |
WGorniak | 2:8d8295a51f68 | 37 | { |
WGorniak | 2:8d8295a51f68 | 38 | } |
WGorniak | 2:8d8295a51f68 | 39 | |
WGorniak | 2:8d8295a51f68 | 40 | std::string Variable::name() const |
WGorniak | 2:8d8295a51f68 | 41 | { |
WGorniak | 2:8d8295a51f68 | 42 | return name_; |
WGorniak | 2:8d8295a51f68 | 43 | } |
WGorniak | 2:8d8295a51f68 | 44 | |
WGorniak | 2:8d8295a51f68 | 45 | std::string Variable::help() const |
WGorniak | 2:8d8295a51f68 | 46 | { |
WGorniak | 3:bb58d4e78e68 | 47 | // std::ostringstream temp; |
WGorniak | 3:bb58d4e78e68 | 48 | std::string temp(description_); |
WGorniak | 3:bb58d4e78e68 | 49 | temp.append("\r\n"); |
WGorniak | 2:8d8295a51f68 | 50 | |
WGorniak | 2:8d8295a51f68 | 51 | for (const ValueDescription* p = pValueDescriptions_; !p->isEnd(); ++p) |
WGorniak | 2:8d8295a51f68 | 52 | { |
WGorniak | 2:8d8295a51f68 | 53 | if (p->isEndRange()) |
WGorniak | 2:8d8295a51f68 | 54 | { |
WGorniak | 2:8d8295a51f68 | 55 | continue; |
WGorniak | 2:8d8295a51f68 | 56 | } |
WGorniak | 2:8d8295a51f68 | 57 | |
WGorniak | 3:bb58d4e78e68 | 58 | temp.append(" "); |
WGorniak | 2:8d8295a51f68 | 59 | |
WGorniak | 2:8d8295a51f68 | 60 | if ((p+1)->isEndRange()) |
WGorniak | 2:8d8295a51f68 | 61 | { |
WGorniak | 3:bb58d4e78e68 | 62 | temp.append(to_string(p->value)); |
WGorniak | 3:bb58d4e78e68 | 63 | temp.append("..."); |
WGorniak | 3:bb58d4e78e68 | 64 | temp.append(to_string((p+1)->value)); |
WGorniak | 3:bb58d4e78e68 | 65 | temp.append(" "); |
WGorniak | 3:bb58d4e78e68 | 66 | temp.append(p->description); |
WGorniak | 2:8d8295a51f68 | 67 | } |
WGorniak | 2:8d8295a51f68 | 68 | else |
WGorniak | 2:8d8295a51f68 | 69 | { |
WGorniak | 3:bb58d4e78e68 | 70 | temp.append(to_string(p->value)); |
WGorniak | 3:bb58d4e78e68 | 71 | temp.append(": "); |
WGorniak | 3:bb58d4e78e68 | 72 | temp.append(p->description); |
WGorniak | 3:bb58d4e78e68 | 73 | temp.append(" "); |
WGorniak | 2:8d8295a51f68 | 74 | } |
WGorniak | 3:bb58d4e78e68 | 75 | temp.append("\r\n"); |
WGorniak | 2:8d8295a51f68 | 76 | } |
WGorniak | 3:bb58d4e78e68 | 77 | return temp; |
WGorniak | 2:8d8295a51f68 | 78 | } |
WGorniak | 2:8d8295a51f68 | 79 | |
WGorniak | 2:8d8295a51f68 | 80 | std::string Variable::describe(int32_t value) const |
WGorniak | 2:8d8295a51f68 | 81 | { |
WGorniak | 2:8d8295a51f68 | 82 | for (const ValueDescription* p = pValueDescriptions_; !p->isEnd(); ++p) |
WGorniak | 2:8d8295a51f68 | 83 | { |
WGorniak | 2:8d8295a51f68 | 84 | if (p->value > value || p->isEndRange()) |
WGorniak | 2:8d8295a51f68 | 85 | { |
WGorniak | 2:8d8295a51f68 | 86 | continue; |
WGorniak | 2:8d8295a51f68 | 87 | } |
WGorniak | 2:8d8295a51f68 | 88 | |
WGorniak | 2:8d8295a51f68 | 89 | if ((p+1)->isEndRange()) |
WGorniak | 2:8d8295a51f68 | 90 | { |
WGorniak | 2:8d8295a51f68 | 91 | if ((p->value <= value) && (value <= (p+1)->value)) |
WGorniak | 2:8d8295a51f68 | 92 | { |
WGorniak | 3:bb58d4e78e68 | 93 | std::string temp(to_string(value)); |
WGorniak | 3:bb58d4e78e68 | 94 | temp.append(p->description); |
WGorniak | 3:bb58d4e78e68 | 95 | return temp; |
WGorniak | 2:8d8295a51f68 | 96 | } |
WGorniak | 2:8d8295a51f68 | 97 | } else |
WGorniak | 2:8d8295a51f68 | 98 | if (p->value == value) |
WGorniak | 2:8d8295a51f68 | 99 | { |
WGorniak | 3:bb58d4e78e68 | 100 | std::string temp(to_string(value)); |
WGorniak | 3:bb58d4e78e68 | 101 | temp.append(" - "); |
WGorniak | 3:bb58d4e78e68 | 102 | temp.append(p->description); |
WGorniak | 3:bb58d4e78e68 | 103 | return temp; |
WGorniak | 2:8d8295a51f68 | 104 | } |
WGorniak | 2:8d8295a51f68 | 105 | } |
WGorniak | 2:8d8295a51f68 | 106 | return ""; |
WGorniak | 2:8d8295a51f68 | 107 | } |
WGorniak | 2:8d8295a51f68 | 108 | |
WGorniak | 2:8d8295a51f68 | 109 | bool Variable::set(int32_t value) |
WGorniak | 2:8d8295a51f68 | 110 | { |
WGorniak | 2:8d8295a51f68 | 111 | if (describe(value).size() == 0) |
WGorniak | 2:8d8295a51f68 | 112 | { |
WGorniak | 2:8d8295a51f68 | 113 | return false; |
WGorniak | 2:8d8295a51f68 | 114 | } |
WGorniak | 2:8d8295a51f68 | 115 | value_ = value; |
WGorniak | 2:8d8295a51f68 | 116 | return true; |
WGorniak | 2:8d8295a51f68 | 117 | } |
WGorniak | 2:8d8295a51f68 | 118 | |
WGorniak | 2:8d8295a51f68 | 119 | int32_t Variable::get() const |
WGorniak | 2:8d8295a51f68 | 120 | { |
WGorniak | 2:8d8295a51f68 | 121 | return value_; |
WGorniak | 2:8d8295a51f68 | 122 | } |
WGorniak | 2:8d8295a51f68 | 123 | |
WGorniak | 2:8d8295a51f68 | 124 | std::string Variable::describe() const |
WGorniak | 2:8d8295a51f68 | 125 | { |
WGorniak | 2:8d8295a51f68 | 126 | return describe(get()); |
WGorniak | 2:8d8295a51f68 | 127 | } |