-
Notifications
You must be signed in to change notification settings - Fork 0
/
EosOscManager.cpp
215 lines (153 loc) · 3.87 KB
/
EosOscManager.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include <EosOscManager.h>
#include <BufferStore.h>
//define static vars
EosOscManager EosOscManager::manager;
bool EosOscManager::madeManager = false;
EosOscManager::EosOscManager()
{
//setUser(255);
}
void EosOscManager::setClient(WiFiClient &theClient)
{
client = theClient;
}
void EosOscManager::registerHandler(EosOscHandler *theHandler)
//void EosOscManager::registerHandler(EosOscCommand * theHandler)
{
if (handlerCount>= MAX_HANDLERS)
return; //should throw an error or return -1?
handlers[handlerCount] = theHandler;
handlerCount++;
}
void EosOscManager::setUser(byte theUser)
{
user=theUser;
if (handlerCount>0)
{
for (byte i=0; i<handlerCount; i++)
handlers[i]->userChanged();
}
}
byte EosOscManager::getUser()
{
return user;
}
//route the incoming OSC
void EosOscManager::routeOSC(OSCMessage &theMessage)
{
//lcd.clear();
if (!theMessage.hasError()) //if the OSC is OK then act on it
{
if (handlerCount>0)
{
for (byte i=0; i<handlerCount; i++)
handlers[i]->routeOsc(theMessage);
}
} else
{
Serial.println("error");
}
}
void EosOscManager::sendOSCMessage(OSCMessage &message)
{
if (client.connected())
{
//send out via TCP
BufferStore(buf); //declare a buffer to send to
//MsgLength mLen;
message.send(buf); //send to the buffer
buf.sendOut(client); //now transmit it en masse
}
message.empty(); // free space occupied by message
}
bool EosOscManager::getScreenNeedsUpdate()
{
return screenNeedsUpdate;
}
void EosOscManager::setScreenNeedsUpdate(bool doesIt)
{
screenNeedsUpdate = doesIt;
}
void EosOscManager::checkForIncomingTCP()
{
if (!client.connected())
{
delay(500);
return;
}
int rdS = 0;
// read in the OSC From the client if we have it
if ((rdS = client.available()) > 12) //must be more than 12 bytes available or we can't do anything
{
//Serial.println("Got stuff");
MsgLength len; //a struct for the length
len.value = 0; //preset it to 0
byte f4 = 4; //first 4 bytes are the size, then we have the actual message..
//get the size
while (f4--)
{
len.bytes[f4] = client.read();
rdS--;
}
OSCMessage(rMsg); //initialize a new OSC message to put this in
if (client.available() < len.value)
{
client.flush();
return; //not enough data - should we wait??
}
while (len.value--)
//str +=(char)client.read();
rMsg.fill(client.read());
// Serial.println(wheelC);
if (!rMsg.hasError())
{ //is valid OSC
//send out via SLIP
delay(10); //this appears necessary!!
yield();
this->routeOSC(rMsg);
//delay(1);
} else
{
Serial.print("error");
}
rMsg.empty();
yield();
} else {
client.flush();//*/
}
}
void EosOscManager::resetConnection()
{
if (!client.connected())
return; //don't try and connect if we aren't connected!
//first reset the connection
OSCMessage msg("/eos/reset");
sendOSCMessage(msg);
msg.empty();
//now register which user we are
if (user!=255)
{
//return; //don't need to register if we don't have a user
char cmd[14];
strcpy(cmd, "/eos/user/");
char userNum[3];
sprintf(userNum, "%i", user);
strcat(cmd, userNum);
OSCMessage msg2(cmd);
this->sendOSCMessage(msg2);
}
yield();
//TODO should we transmit the current user so we remember what we are listening for?
}
//call this each time we have any OSC that might affect the output
void EosOscManager::routeScreenNeedsUpdate(OSCMessage &msg, int addrOffSet)
{
//todo need to implement a singleton object for oscManager
EosOscManager::getInstance()->setScreenNeedsUpdate(true);
}
EosOscManager* EosOscManager::getInstance() {
if (!madeManager) EosOscManager(manager);
madeManager = true;
return &manager;
//return NULL;
};