Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Files Functions Variables Typedefs Macros Groups
Gui.h
1 #include <vector>
2 #include <string>
3 #include <WSServer.h>
4 #include <JSON.h>
5 #include <typeinfo> // for types in templates
6 #include <DataBuffer.h>
7 
8 
9 // forward declarations
10 class WSServer;
11 class JSONValue;
12 class AuxTaskRT;
13 
14 class Gui
15 {
16  private:
17 
18  std::vector<DataBuffer> _buffers;
19  std::unique_ptr<WSServer> ws_server;
20 
21  bool wsIsConnected = false;
22 
23  void ws_connect();
24  void ws_disconnect();
25  void ws_onControlData(const char* data, int size);
26  void ws_onData(const char* data, int size);
27  int doSendBuffer(const char* type, unsigned int bufferId, const void* data, size_t size);
28 
29  unsigned int _port;
30  std::string _addressControl;
31  std::string _addressData;
32  std::wstring _projectName;
33 
34  // User defined functions
35  std::function<bool(const char*, int, void*)> customOnControlData;
36  std::function<bool(const char*, int, void*)> customOnData;
37 
38  void* userControlData = nullptr;
39  void* userBinaryData = nullptr;
40 
41  public:
42  Gui();
43  Gui(unsigned int port, std::string address);
44  ~Gui();
45 
46  int setup(unsigned int port = 5555, std::string address = "gui");
55  int setup(std::string projectName, unsigned int port = 5555, std::string address = "gui");
56  void cleanup();
57 
58  bool isConnected(){ return wsIsConnected; };
59 
60  // BUFFERS
68  unsigned int setBuffer(char bufferType, unsigned int size);
74  DataBuffer& getDataBuffer(unsigned int bufferId);
75 
85  void setControlDataCallback(std::function<bool(const char*, int, void*)> callback, void* customControlData=nullptr){
86  customOnControlData = callback;
87  userControlData = customControlData;
88  };
99  void setBinaryDataCallback(std::function<bool(const char*, int, void*)> callback, void* customBinaryData=nullptr){
100  customOnData = callback;
101  userBinaryData = customBinaryData;
102  };
103 
109  template<typename T, typename A>
110  int sendBuffer(unsigned int bufferId, std::vector<T,A> & buffer);
116  template <typename T, size_t N>
117  int sendBuffer(unsigned int bufferId, T (&buffer)[N]);
123  template <typename T>
124  int sendBuffer(unsigned int bufferId, T value);
125 };
126 
127 template<typename T, typename A>
128 int Gui::sendBuffer(unsigned int bufferId, std::vector<T,A> & buffer)
129 {
130  const char* type = typeid(T).name();
131  return doSendBuffer(type, bufferId, (const void*)buffer.data(), (buffer.size()*sizeof(T)));
132 }
133 
134 template <typename T, size_t N>
135 int Gui::sendBuffer(unsigned int bufferId, T (&buffer)[N])
136 {
137  const char* type = typeid(T).name();
138  return doSendBuffer(type, bufferId, (const void*)buffer, N*sizeof(T));
139 }
140 
141 template <typename T>
142 int Gui::sendBuffer(unsigned int bufferId, T value)
143 {
144  const char* type = typeid(T).name();
145  return doSendBuffer(type, bufferId, (const void*)&value, sizeof(T));
146 }
int sendBuffer(unsigned int bufferId, std::vector< T, A > &buffer)
Definition: Gui.h:128
DataBuffer & getDataBuffer(unsigned int bufferId)
Definition: Gui.cpp:167
void setBinaryDataCallback(std::function< bool(const char *, int, void *)> callback, void *customBinaryData=nullptr)
Definition: Gui.h:99
Definition: Gui.h:14
Definition: AuxTaskRT.h:24
Definition: WSServer.h:18
Definition: JSONValue.h:37
Definition: DataBuffer.h:11
unsigned int setBuffer(char bufferType, unsigned int size)
Definition: Gui.cpp:160
void setControlDataCallback(std::function< bool(const char *, int, void *)> callback, void *customControlData=nullptr)
Definition: Gui.h:85