forked from unhuman-io/motorlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter_api.h
129 lines (114 loc) · 4.11 KB
/
parameter_api.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#ifndef UNHUMAN_MOTORLIB_PARAMETER_API_H_
#define UNHUMAN_MOTORLIB_PARAMETER_API_H_
#include <string>
#include <map>
#include <vector>
#include "util.h"
#include <algorithm>
#include "autocomplete.h"
class APIVariable {
public:
virtual std::string get() const = 0;
virtual void set(std::string) = 0;
};
template<class T>
class APIVariable2 : public APIVariable {
public:
APIVariable2(T *value) : value_(value) {};
APIVariable2(volatile T *value) : value_(value) {};
APIVariable2(const T* value) : value_(const_cast<T*>(value)) {}
virtual std::string get() const { return std::to_string(*value_); }
virtual void set(std::string) = 0;
protected:
volatile T *value_;
};
class APIFloat : public APIVariable2<float> {
public:
APIFloat(float *f) : APIVariable2(f) {}
APIFloat(volatile float *f) : APIVariable2(f) {}
APIFloat(const float *f) : APIVariable2(f) {}
void set(std::string);
};
template<class T>
class APIInt : public APIVariable2<T> {
public:
APIInt(T *u) : APIVariable2<T>(u) {}
APIInt(volatile T *u) : APIVariable2<T>(u) {}
APIInt(const T *u) : APIVariable2<T>(u) {}
void set(std::string s) {
*this->value_ = std::stoi(s);
}
};
typedef APIInt<uint32_t> APIUint32;
typedef APIInt<uint16_t> APIUint16;
typedef APIInt<uint8_t> APIUint8;
typedef APIInt<int32_t> APIInt32;
typedef APIInt<int16_t> APIInt16;
typedef APIInt<int8_t> APIInt8;
typedef APIInt<bool> APIBool;
template<class T>
class APIHex : public APIInt<T> {
public:
APIHex(T *u) : APIInt<T>(u) {}
APIHex(const T *u) : APIInt<T>(u) {}
void set(std::string s) {
*this->value_ = std::stoi(s, nullptr, 16);
}
virtual std::string get() const {
std::vector<char>bytes((char *) this->value_,(char *) this->value_+sizeof(T));
std::reverse(bytes.begin(),bytes.end());
return bytes_to_hex(bytes); }
};
#include <functional>
class APICallback : public APIVariable {
public:
APICallback(std::function<std::string()> getfun, std::function<void(std::string)> setfun) : getfun_(getfun), setfun_(setfun) {}
APICallback(std::function<std::string()> getfun) : getfun_(getfun) {}
void set(std::string s) { setfun_(s); }
std::string get() const {return getfun_(); }
private:
std::function<std::string()> getfun_;
std::function<void(std::string)> setfun_;
};
class APICallbackFloat : public APIVariable {
public:
APICallbackFloat(std::function<float()> getfun , std::function<void(float)> setfun) : getfun_(getfun), setfun_(setfun) {}
APICallbackFloat(std::function<float()> getfun) : getfun_(getfun) {}
void set(std::string s) { setfun_(stof(s)); }
std::string get() const { return std::to_string(getfun_()); };
private:
std::function<float()> getfun_;
std::function<void(float)> setfun_;
};
template<class T>
class APICallbackUint : public APIVariable {
public:
APICallbackUint(std::function<T()> getfun , std::function<void(T)> setfun) : getfun_(getfun), setfun_(setfun) {}
APICallbackUint(std::function<T()> getfun) : getfun_(getfun) {}
void set(std::string s) { setfun_(std::stoi(s)); }
std::string get() const { return std::to_string(getfun_()); }
private:
std::function<T()> getfun_;
std::function<void(T)> setfun_;
};
typedef APICallbackUint<uint32_t> APICallbackUint32;
typedef APICallbackUint<uint16_t> APICallbackUint16;
typedef APICallbackUint<uint8_t> APICallbackUint8;
// allows for setting variables through text commands
class ParameterAPI {
public:
// type is used by scanf to parse the string
void add_api_variable(std::string name, APIVariable *variable);
void add_api_variable(std::string name, const APIVariable *variable);
void set_api_variable(std::string name, std::string value);
std::string get_api_variable(std::string name);
std::string parse_string(std::string);
std::string get_all_api_variables() const;
uint16_t get_api_length() const;
std::string get_api_variable_name(uint16_t index) const;
private:
std::map<std::string, APIVariable *> variable_map_;
std::map<std::string, const APIVariable *> const_variable_map_;
AutoComplete auto_complete_;
};
#endif // UNHUMAN_MOTORLIB_PARAMETER_API_H_