-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosc.cpp
executable file
·210 lines (185 loc) · 3.98 KB
/
osc.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
// Basic handling of an OSC packet
#include <energia.h>
#include <stdio.h>
#include <string.h>
#include "osc.h"
#include "common.h"
////////////////////////////////////////////////////////////////////////////////////
// Pre-allocate and fills the OSC structures
// adds the address and the type tags like "/address/subaddress ,iiiii"
// and finds the total packet size and begining of the data for further simplified
// update. This saves a lot of time by NOT re computing the OSC structure
// and padding, and limits data updates to RAM moves.
void PrepareOSC(OscBuffer *TheBuffer, char *OscAddress, char TypeTag, uint8_t Slots)
{
int i;
int size = 0;
char *pBuf;
pBuf = TheBuffer->buf;
size = strlen(OscAddress);
strcpy(pBuf, OscAddress);
pBuf += size;
// We can't stop on an aligned %4, as we need at least one zero terminator in the address
if(!(size%4))
{
*pBuf = '\0';
size++;
pBuf++;
}
// 4 byte padding / stuffing
while(size%4)
{
*pBuf = '\0';
size++;
pBuf++;
}
// adds the comma char (separator for typetags)
*pBuf = ',';
pBuf++;
size++;
// adds the type tags
for(i = 0 ; i < Slots ; i++)
{
*pBuf = TypeTag;
pBuf++;
}
size += Slots;
// We can't stop on an aligned %4, as we need at least one zero terminator in the address
if(!(size%4))
{
*pBuf = '\0';
size++;
pBuf++;
}
// 4 byte padding / stuffing
while(size%4)
{
*pBuf = '\0';
pBuf++;
size++;
}
// Stores where the actual data start
TheBuffer->pData = pBuf;
// Adds size of data
size += Slots*4;
pBuf += Slots*4;
// Dumb check as at that point it's supposed to be 4-byte aligned
while(size%4)
{
*pBuf = '\0';
size++;
pBuf++;
}
TheBuffer->PacketSize = size;
*(pBuf+1) = '\0';
//Serial.print("OSC packet size = ");
//Serial.println(size);
}
void StringToOsc(OscBuffer *TheBuffer, char *OscAddress, char *StringMessage)
{
int i;
int size = 0;
char *pBuf;
pBuf = TheBuffer->buf;
size = strlen(OscAddress);
strcpy(pBuf, OscAddress);
pBuf += size;
// We can't stop on an aligned %4, as we need at least one zero terminator in the address
if(!(size%4))
{
*pBuf = '\0';
size++;
pBuf++;
}
// 4 byte padding / stuffing
while(size%4)
{
*pBuf = '\0';
size++;
pBuf++;
}
// adds the comma char (separator for typetags)
*pBuf = ',';
pBuf++;
size++;
// adds the string type tags
*pBuf = 's';
pBuf++;
size++;
// We can't stop on an aligned %4, as we need at least one zero terminator in the address
if(!(size%4))
{
*pBuf = '\0';
size++;
pBuf++;
}
// 4 byte padding / stuffing
while(size%4)
{
*pBuf = '\0';
pBuf++;
size++;
}
// Stores where the actual data start
TheBuffer->pData = pBuf;
// Adds size of data (string length)
strcpy(pBuf, StringMessage);
size += strlen(StringMessage);
pBuf += strlen(StringMessage);
// There is a mandatory string terminator that isn't inserted by the
// string copy above (as it stops without including it)
*pBuf = '\0';
pBuf++;
size++;
// Eventually pads the string to %4
while(size%4)
{
*pBuf = '\0';
size++;
pBuf++;
}
TheBuffer->PacketSize = size;
*pBuf = '\0';
//Serial.print("OSC packet size = ");
//Serial.println(size);
}
void ShortToBigEndian(char *Dest, short int Val)
{
// Sign padding
if(Val < 0)
{
Dest[0] = 0xFF;
Dest[1] = 0xFF;
}
else
{
Dest[0] = 0;
Dest[1] = 0;
}
Dest[2] = (unsigned char)(Val >> 8);
Dest[3] = (unsigned char)(Val & 0x00FF);
}
void WordToBigEndian(char *Dest, Word TheWord)
{
// Sign padding / extension
if(TheWord.Value < 0)
{
Dest[0] = 0xFF;
Dest[1] = 0xFF;
}
else
{
Dest[0] = 0;
Dest[1] = 0;
}
Dest[2] = TheWord.Val[1];
Dest[3] = TheWord.Val[0];
}
void FloatToBigEndian(char *Dest, float *TheFloat)
{
char *pF = (char*)TheFloat;
Dest[0] = pF[3];
Dest[1] = pF[2];
Dest[2] = pF[1];
Dest[3] = pF[0];
}