ESP  0.1
The Example-based Sensor Predictions (ESP) system tries to bring machine learning to the maker community.
tuneable.h
Go to the documentation of this file.
1 
13 #pragma once
14 
15 #include <string>
16 
17 #include "ofxDatGui.h"
18 
19 using std::string;
20 
21 class Tuneable {
22  public:
23  // Set is not implemented, yet.
25 
26  // Range tuneable (int)
27  Tuneable(int* value, int min, int max, const string& title,
28  const string& description,
29  std::function<void(int)> cb)
30  : value_ptr_(value), ui_ptr_(NULL),
31  type_(INT_RANGE), title_(title), description_(description),
32  min_(min), max_(max),
33  int_cb_(cb), double_cb_(nullptr), bool_cb_(nullptr) {
34  }
35 
36  // Range tuneable (double)
37  Tuneable(double* value, double min, double max,
38  const string& title, const string& description,
39  std::function<void(double)> cb)
40  : value_ptr_(value), ui_ptr_(NULL),
41  type_(DOUBLE_RANGE), title_(title), description_(description),
42  min_(min), max_(max),
43  int_cb_(nullptr), double_cb_(cb), bool_cb_(nullptr) {
44  }
45 
46  // Boolean tuneable
47  Tuneable(bool* value, const string& title, const string& description,
48  std::function<void(bool)> cb)
49  : value_ptr_(value), ui_ptr_(NULL),
50  type_(BOOL), title_(title), description_(description),
51  int_cb_(nullptr), double_cb_(nullptr), bool_cb_(cb) {
52  }
53 
54  void addToGUI(ofxDatGui& gui) {
55  switch (type_) {
56  case INT_RANGE: {
57  int* value = static_cast<int*>(value_ptr_);
58  ofxDatGuiSlider *slider = gui.addSlider(title_, min_, max_);
59  slider->setValue(*value);
60  ui_ptr_ = static_cast<void*>(slider);
61  gui.onSliderEvent(this, &Tuneable::onSliderEvent);
62  gui.addTextBlock(description_);
63  break;
64  }
65 
66  case DOUBLE_RANGE: {
67  double* value = static_cast<double*>(value_ptr_);
68  ofxDatGuiSlider *slider = gui.addSlider(title_, min_, max_);
69  slider->setValue(*value);
70  ui_ptr_ = static_cast<void*>(slider);
71  gui.onSliderEvent(this, &Tuneable::onSliderEvent);
72  gui.addTextBlock(description_);
73  break;
74  }
75 
76  case BOOL: {
77  bool* value = static_cast<bool*>(value_ptr_);
78  ofxDatGuiToggle *toggle = gui.addToggle(title_, *value);
79  ui_ptr_ = static_cast<void*>(toggle);
80  gui.onButtonEvent(this, &Tuneable::onToggleEvent);
81  gui.addTextBlock(description_);
82  break;
83  }
84 
85  default: break;
86  }
87  }
88 
89  std::string toString() {
90  switch (type_) {
91  case INT_RANGE: {
92  int* value = static_cast<int*>(value_ptr_);
93  return "INT " + std::to_string(*value);
94  }
95  case DOUBLE_RANGE: {
96  double* value = static_cast<double*>(value_ptr_);
97  return "DOUBLE " + std::to_string(*value);
98  }
99  case BOOL: {
100  bool* value = static_cast<bool*>(value_ptr_);
101  return std::string("BOOL ") + (*value ? "true" : "false");
102  }
103  default: {
104  ofLog(OF_LOG_ERROR) << "Unknown type";
105  break;
106  }
107  }
108  return "";
109  }
110 
111  // Return value indicates success or not.
112  bool fromString(std::string str) {
113  std::istringstream iss(str);
114  std::string word;
115 
116  iss >> word;
117  if (word == "INT") {
118  int* p = static_cast<int*>(value_ptr_);
119  iss >> *p;
120  ofxDatGuiSlider *slider = static_cast<ofxDatGuiSlider*>(ui_ptr_);
121  slider->setValue(*p);
122  } else if (word == "DOUBLE") {
123  double* p = static_cast<double*>(value_ptr_);
124  iss >> *p;
125  ofxDatGuiSlider *slider = static_cast<ofxDatGuiSlider*>(ui_ptr_);
126  slider->setValue(*p);
127  } else if (word == "BOOL") {
128  bool* p = static_cast<bool*>(value_ptr_);
129  iss >> word;
130  *p = (word == "true") ? true : false;
131  ofxDatGuiToggle *toggle = static_cast<ofxDatGuiToggle*>(ui_ptr_);
132  toggle->setEnabled(*p);
133  }
134 
135  return false;
136  }
137 
138  void* getUIAddress() const {
139  return ui_ptr_;
140  }
141 
142  void* getDataAddress() const {
143  return value_ptr_;
144  }
145 
146  Type getType() const {
147  return type_;
148  }
149 
150  private:
151  void onSliderEvent(ofxDatGuiSliderEvent e);
152  void onToggleEvent(ofxDatGuiButtonEvent e);
153 
154  void* value_ptr_;
155  void* ui_ptr_;
156  Type type_;
157 
158  string title_;
159  string description_;
160  double min_;
161  double max_;
162 
163  std::function<void(int)> int_cb_;
164  std::function<void(double)> double_cb_;
165  std::function<void(bool)> bool_cb_;
166 };
167 
187 void registerTuneable(int& value, int min, int max,
188  const string& name, const string& description,
189  std::function<void(int)> cb = nullptr);
190 
210 void registerTuneable(double& value, double min, double max,
211  const string& name, const string& description,
212  std::function<void(double)> cb = nullptr);
213 
214 
231 void registerTuneable(bool& value, const string& name, const string& description,
232  std::function<void(bool)> cb = nullptr);
Definition: tuneable.h:24
void title(const std::string &titlestr)
Definition: matplotlibcpp.h:292
Tuneable(double *value, double min, double max, const string &title, const string &description, std::function< void(double)> cb)
Definition: tuneable.h:37
Tuneable(int *value, int min, int max, const string &title, const string &description, std::function< void(int)> cb)
Definition: tuneable.h:27
Definition: tuneable.h:24
Definition: tuneable.h:21
Type getType() const
Definition: tuneable.h:146
void addToGUI(ofxDatGui &gui)
Definition: tuneable.h:54
void registerTuneable(int &value, int min, int max, const string &name, const string &description, std::function< void(int)> cb=nullptr)
Definition: tuneable.cpp:63
Tuneable(bool *value, const string &title, const string &description, std::function< void(bool)> cb)
Definition: tuneable.h:47
Definition: tuneable.h:24
Type
Definition: tuneable.h:24
void * getDataAddress() const
Definition: tuneable.h:142
void * getUIAddress() const
Definition: tuneable.h:138
bool fromString(std::string str)
Definition: tuneable.h:112
Definition: tuneable.h:24
std::string toString()
Definition: tuneable.h:89