Nol.A SDK Library 26.11.2
For IoT System Software Development
Loading...
Searching...
No Matches
ConfigNumber< T > Class Template Reference
Inheritance diagram for ConfigNumber< T >:
ConfigItem

Public Member Functions

 ConfigNumber (const char *name, Storage &s, uint64_t addr, T min, T max, void(*onSet)(T)=nullptr)
const char * getName () override
virtual bool handleCommand (const char *input, Print &p) override
virtual void print (Print &p) override
virtual T getValue ()
virtual error_t setValue (T newValue)
virtual bool isValid () override
virtual void printHelp (Print &p) override

Protected Member Functions

virtual void onValueSet () override

Protected Attributes

char * name
Storage & flash
const uint64_t addr
T min
T max
void(* onSet )(T)

Detailed Description

template<typename T>
class ConfigNumber< T >
Examples
plm150/PLS100W_RD-WSD-M-02/main.cpp.

Constructor & Destructor Documentation

◆ ConfigNumber()

template<typename T>
ConfigNumber< T >::ConfigNumber ( const char * name,
Storage & s,
uint64_t addr,
T min,
T max,
void(* onSet )(T) = nullptr )
inline
14 : flash(s), addr(addr), min(min), max(max), onSet(onSet) {
15 this->name = new char[strlen(name) + 1];
16 strcpy(this->name, name);
17 };
Definition ConfigNumber.hpp:10
#define min(x, y)
두 수 중 최소를 구합니다.
Definition nola-common.h:255
#define max(x, y)
두 수 중 최대를 구합니다.
Definition nola-common.h:267

Member Function Documentation

◆ getName()

template<typename T>
const char * ConfigNumber< T >::getName ( )
inlineoverridevirtual

Implements ConfigItem.

19 {
20 return this->name;
21 };

◆ getValue()

template<typename T>
virtual T ConfigNumber< T >::getValue ( )
inlinevirtual
73 {
74 T val;
75 this->flash.read(&val, this->addr, sizeof(T));
76 return val;
77 };

◆ handleCommand()

template<typename T>
virtual bool ConfigNumber< T >::handleCommand ( const char * input,
Print & p )
inlineoverridevirtual

Implements ConfigItem.

23 {
24 std::string command = "get ";
25 command += this->name;
26
27 if (command == input) {
28 p.printf("\"result\":\"OK\",\"%s\":", this->name);
29 this->print(p);
30 return true;
31 }
32
33 command = "set ";
34 command += this->name;
35 command += ' ';
36
38 if (inputCommand.find(command) == 0) {
39 const char *value = inputCommand.substr(command.length()).c_str();
40 char *pos = nullptr;
41
42 T v = strtol(value, &pos, 0);
43 if (*pos == '\0') {
44 error_t err = this->setValue(v);
45 if (err == ERROR_INVALID_ARGS && (v < this->min || v > this->max)) {
46 p.print("\"result\":\"out of range error\"");
47 return true;
48 } else if (err == ERROR_SUCCESS) {
49 p.printf("\"result\":\"OK\",\"%s\":", this->name);
50 this->print(p);
51 this->onValueSet();
52 } else {
53 p.printf("\"result\":\"error (%d)\"", err);
54 }
55 } else {
56 p.printf("\"result\":\"parsing error: %s -> %d\"", value, v);
57 }
58
59 return true;
60 }
61
62 return false;
63 };

◆ isValid()

template<typename T>
virtual bool ConfigNumber< T >::isValid ( )
inlineoverridevirtual

Implements ConfigItem.

87 {
88 T val = this->getValue();
89 return (val >= this->min && val <= this->max);
90 }

◆ onValueSet()

template<typename T>
virtual void ConfigNumber< T >::onValueSet ( )
inlineoverrideprotectedvirtual

Reimplemented from ConfigItem.

108 {
109 if (this->onSet) {
110 this->onSet(this->getValue());
111 }
112 };

◆ print()

template<typename T>
virtual void ConfigNumber< T >::print ( Print & p)
inlineoverridevirtual

Implements ConfigItem.

65 {
66 T val = this->getValue();
67 p.print(val);
68 if (val < this->min || val > this->max) {
69 p.print(" /* out of range */");
70 }
71 };

◆ printHelp()

template<typename T>
virtual void ConfigNumber< T >::printHelp ( Print & p)
inlineoverridevirtual

Implements ConfigItem.

92 {
93 p.printf("[\"get %s\"", this->getName());
94 p.printf(",\"set %s [", this->getName());
95 p.print(this->min);
96 p.print('~');
97 p.print(this->max);
98 p.print("]\"]");
99 }

◆ setValue()

template<typename T>
virtual error_t ConfigNumber< T >::setValue ( T newValue)
inlinevirtual
79 {
80 if (newValue < this->min || newValue > this->max) {
81 return ERROR_INVALID_ARGS;
82 }
83
84 return this->flash.write((const uint8_t *) &newValue, this->addr, sizeof(T));
85 };

The documentation for this class was generated from the following file: