-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfloatoption.cpp
51 lines (38 loc) · 1.17 KB
/
floatoption.cpp
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
#include "floatoption.h"
#define SAVE(var) \
memcpy(ptr, &(var), sizeof((var))); \
ptr += sizeof((var))
#include <visstring.h>
#include <cstring>
namespace NaoControl {
FloatOption::FloatOption(const char* cname, float* val, float min, float max, float step,
std::function<void(float)> callb)
: Option(cname), value(val), callback(std::move(callb)) {
VisString optionName(cname);
length = sizeof(uint8_t) /* type */ + optionName.size() + sizeof(min) + sizeof(max) + sizeof(step) + sizeof(*val);
buffer = (uint8_t*)malloc(length);
uint8_t* ptr = buffer;
*ptr = (uint8_t)O_FLOAT;
ptr++;
ptr = optionName.save(ptr);
SAVE(min);
SAVE(max);
SAVE(step);
}
uint8_t* FloatOption::save(uint8_t* start) {
uint8_t* ptr = buffer;
ptr += length - sizeof(*value);
SAVE(*value);
return Option::save(start);
}
void FloatOption::setValue(const uint8_t* buffer, int len) {
float newValue;
if (len != sizeof(newValue))
return;
memcpy(&newValue, buffer, len);
*value = newValue;
if (callback != nullptr) {
callback(*value);
}
}
} // namespace NaoControl