ESP  0.1
The Example-based Sensor Predictions (ESP) system tries to bring machine learning to the maker community.
ostream.h
Go to the documentation of this file.
1 
14 #pragma once
15 #ifndef OSTREAM_H_
16 #define OSTREAM_H_
17 
18 #include <stdarg.h>
19 #include <stdint.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "ofxNetwork.h"
25 #include "stream.h"
26 
27 const uint64_t kGracePeriod = 500; // 0.5 second
28 
29 // Forward declaration.
30 class ofxTCPClient;
31 
43 class OStream : public virtual Stream {
44  public:
45  virtual void onReceive(uint32_t label) = 0;
46 };
47 
60 class OStreamVector : public OStream {
61  public:
62  virtual void onReceive(vector<double>) = 0;
63 };
64 
76 class MacOSKeyboardOStream : public OStream {
77  public:
86  MacOSKeyboardOStream(std::map<uint32_t, char> key_mapping)
87  : key_mapping_(key_mapping) {
88  }
89 
99  MacOSKeyboardOStream(uint32_t count, ...) {
100  va_list args;
101  va_start(args, count);
102  for (uint32_t i = 1; i <= count; i++) {
103  key_mapping_[i] = va_arg(args, int);
104  }
105  va_end(args);
106  }
107 
108  virtual void onReceive(uint32_t label) {
109  if (has_started_) {
110  if (getChar(label) != '\0') {
111  sendKey(getChar(label));
112  }
113  }
114  }
115 
116  private:
117  void sendKey(char c);
118 
119  void sendString(const std::string& str);
120 
121  char getChar(uint32_t label) {
122  return key_mapping_[label];
123  }
124 
125  uint64_t elapsed_time_ = 0;
126  std::map<uint32_t, char> key_mapping_;
127 };
128 
141 class MacOSMouseOStream : public OStream {
142  public:
151  MacOSMouseOStream(std::map<uint32_t, pair<uint32_t, uint32_t> > mouse_mapping)
152  : mouse_mapping_(mouse_mapping) {
153  }
154 
166  MacOSMouseOStream(uint32_t count, ...) {
167  va_list args;
168  va_start(args, count);
169  for (uint32_t i = 1; i <= count; i++) {
170  mouse_mapping_[i] = make_pair(va_arg(args, uint32_t),
171  va_arg(args, uint32_t));
172 
173  }
174  va_end(args);
175  }
176 
177  virtual void onReceive(uint32_t label) {
178  if (has_started_) {
179  pair<uint32_t, uint32_t> mouse = getMousePosition(label);
180  if (mouse.first > 0 & mouse.second > 0) {
181  clickMouse(mouse);
182  }
183  }
184  }
185 
186 private:
187  void clickMouse(pair<uint32_t, uint32_t> mouse);
188  void doubleClick(pair<uint32_t, uint32_t> mouse, int clickCount = 2);
189 
190  pair<uint32_t, uint32_t> getMousePosition(uint32_t label) {
191  return mouse_mapping_[label];
192  }
193 
194  uint64_t elapsed_time_ = 0;
195  std::map<uint32_t, pair<uint32_t, uint32_t>> mouse_mapping_;
196 };
197 
209 class TcpOStream : public OStreamVector {
210  public:
219  TcpOStream(string server, int port)
220  : server_(server), port_(port),
221  use_tcp_stream_mapping_(false), is_in_retry_(false) {}
222 
234  TcpOStream(string server, int port,
235  std::map<uint32_t, string> tcp_stream_mapping)
236  : server_(server), port_(port),
237  use_tcp_stream_mapping_(true),
238  tcp_stream_mapping_(tcp_stream_mapping),
239  is_in_retry_(false) {
240  }
241 
254  TcpOStream(string server, int port, uint32_t count, ...)
255  : server_(server), port_(port), use_tcp_stream_mapping_(true),
256  is_in_retry_(false) {
257  va_list args;
258  va_start(args, count);
259  for (uint32_t i = 1; i <= count; i++) {
260  char* s = va_arg(args, char *);
261  tcp_stream_mapping_[i] = std::string(s);
262  }
263  va_end(args);
264  }
265 
266  virtual void onReceive(uint32_t label) {
267  if (has_started_) {
268  string to_send = getStreamString(label);
269  if (!to_send.empty()) {
270  sendString(to_send);
271  }
272  }
273  }
274 
275  virtual void onReceive(vector<double> data) {
276  string s;
277  for (int i = 0; i < data.size(); i++) {
278  if (i > 0) s += "\t";
279  s += to_string(data[i]);
280  }
281  s += "\n";
282  if (!s.empty()) sendString(s);
283  }
284 
285  bool start();
286 
287 private:
288  void sendString(const string& tosend);
289 
290  string getStreamString(uint32_t label) {
291  if (use_tcp_stream_mapping_) return tcp_stream_mapping_[label];
292  else return std::to_string(label) + "\n";
293  }
294 
295  string server_;
296  int port_;
297  ofxTCPClient *client_;
298 
299  uint64_t elapsed_time_ = 0;
300  std::map<uint32_t, string> tcp_stream_mapping_;
301  bool use_tcp_stream_mapping_;
302 
303  bool is_in_retry_;
304 };
305 
306 #endif
virtual void onReceive(uint32_t label)=0
Definition: stream.h:6
MacOSMouseOStream(uint32_t count,...)
Create a MacOSMouseOStream instance, specifying the location at which to double-click the mouse for e...
Definition: ostream.h:166
virtual void onReceive(uint32_t label)
Definition: ostream.h:108
virtual void onReceive(vector< double > data)
Definition: ostream.h:275
Emulate keyboard key presses corresponding to prediction results.
Definition: ostream.h:76
virtual void onReceive(uint32_t label)
Definition: ostream.h:177
Base class for output streams that forward ESP prediction results to other systems.
Definition: ostream.h:43
MacOSKeyboardOStream(uint32_t count,...)
Create a MacOSKeyboardOStream instance, specifying the key presses to emulate for each predicted clas...
Definition: ostream.h:99
std::atomic_bool has_started_
Definition: stream.h:24
MacOSKeyboardOStream(std::map< uint32_t, char > key_mapping)
Create a MacOSKeyboardOStream instance, specifying the key presses to emulate for each predicted clas...
Definition: ostream.h:86
virtual bool start()
Definition: stream.h:13
Emulate mouse double-clicks at locations corresponding to prediction results.
Definition: ostream.h:141
Base class for output streams that forward ESP pipeline output to other systems.
Definition: ostream.h:60
const uint64_t kGracePeriod
Definition: ostream.h:27
virtual void onReceive(uint32_t label)
Definition: ostream.h:266
MacOSMouseOStream(std::map< uint32_t, pair< uint32_t, uint32_t > > mouse_mapping)
Create a MacOSMouseOStream instance, specifying the locations at which to double-click the mouse for ...
Definition: ostream.h:151
TcpOStream(string server, int port)
Definition: ostream.h:219
TcpOStream(string server, int port, std::map< uint32_t, string > tcp_stream_mapping)
Definition: ostream.h:234
TcpOStream(string server, int port, uint32_t count,...)
Definition: ostream.h:254
Send strings over a TCP socket based on pipeline predictions.
Definition: ostream.h:209