Bela
Real-time, ultra-low-latency audio and sensor processing system for BeagleBone Black
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
Gui.h
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 #include <functional>
6 #include <WSServer.h>
7 #include <JSON.h>
8 #include <typeinfo> // for types in templates
9 #include <DataBuffer.h>
10 
11 // forward declarations
12 class WSServer;
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, unsigned int size);
26  void ws_onData(const char* data, unsigned 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(JSONObject&, void*)> customOnControlData;
36  std::function<bool(const char*, unsigned 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 
91  void setControlDataCallback(std::function<bool(JSONObject&, void*)> callback, void* customControlData=nullptr){
92  customOnControlData = callback;
93  userControlData = customControlData;
94  };
95 
110  void setBinaryDataCallback(std::function<bool(const char*, unsigned int, void*)> callback, void* customBinaryData=nullptr){
111  customOnData = callback;
112  userBinaryData = customBinaryData;
113  };
117  int sendControl(JSONValue* root);
118 
124  template<typename T, typename A>
125  int sendBuffer(unsigned int bufferId, std::vector<T,A> & buffer);
131  template <typename T, size_t N>
132  int sendBuffer(unsigned int bufferId, T (&buffer)[N]);
140  template <typename T>
141  int sendBuffer(unsigned int bufferId, T* ptr, size_t count);
147  template <typename T>
148  int sendBuffer(unsigned int bufferId, T value);
149 };
150 
151 template<typename T, typename A>
152 int Gui::sendBuffer(unsigned int bufferId, std::vector<T,A> & buffer)
153 {
154  const char* type = typeid(T).name();
155  return doSendBuffer(type, bufferId, (const void*)buffer.data(), (buffer.size()*sizeof(T)));
156 }
157 
158 template <typename T, size_t N>
159 int Gui::sendBuffer(unsigned int bufferId, T (&buffer)[N])
160 {
161  const char* type = typeid(T).name();
162  return doSendBuffer(type, bufferId, (const void*)buffer, N*sizeof(T));
163 }
164 
165 template <typename T>
166 int Gui::sendBuffer(unsigned int bufferId, T* ptr, size_t count){
167  const char* type = typeid(T).name();
168  return doSendBuffer(type, bufferId, ptr, count * sizeof(T));
169 }
170 
171 template <typename T>
172 int Gui::sendBuffer(unsigned int bufferId, T value)
173 {
174  const char* type = typeid(T).name();
175  return doSendBuffer(type, bufferId, (const void*)&value, sizeof(T));
176 }
void setBinaryDataCallback(std::function< bool(const char *, unsigned int, void *)> callback, void *customBinaryData=nullptr)
Definition: Gui.h:110
int sendBuffer(unsigned int bufferId, std::vector< T, A > &buffer)
Definition: Gui.h:152
DataBuffer & getDataBuffer(unsigned int bufferId)
Definition: Gui.cpp:162
Definition: Gui.h:14
Definition: WSServer.h:17
Definition: JSONValue.h:37
Definition: DataBuffer.h:11
void setControlDataCallback(std::function< bool(JSONObject &, void *)> callback, void *customControlData=nullptr)
Definition: Gui.h:91
int sendControl(JSONValue *root)
Definition: Gui.cpp:178
unsigned int setBuffer(char bufferType, unsigned int size)
Definition: Gui.cpp:155