summaryrefslogtreecommitdiff
path: root/Libraries/OSC/OSCEncoder.cpp
blob: 79f0426ee946c350be9e11c04ef331a5e32721a6 (plain)
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
/*

 ArdOSC - OSC Library for Arduino.

 -------- Lisence -----------------------------------------------------------

 ArdOSC

 The MIT License

 Copyright (c) 2009 - 2010 recotana( http://recotana.com ) All right reserved

 */

#include <stdlib.h>
#include <string.h>

#include "OSCcommon.h"
#include "OSCEncoder.h"



int16_t OSCEncoder::encode( OSCMessage::OSCMessage *mes ,uint8_t *sendData ){


	uint8_t *packStartPtr=sendData;

	memset(sendData, 0, mes->getAllPackSize());

//=========== OSC Address(String) -> BIN Encode   ===========

	memcpy( sendData ,mes->oscAddress ,mes->oscAdrSize);
	packStartPtr += mes->oscAdrPacSize;
	mes->allPackSize = mes->oscAdrPacSize;

//=========== TypeTag(String) -> BIN Encode   ===========

	*packStartPtr=',';
	memcpy(packStartPtr+1 ,mes->typeTag ,mes->argsNum);
	packStartPtr += mes->typeTagPacSize;

	mes->allPackSize += mes->typeTagPacSize;


//=========== Auguments -> BIN Encode   ==================


	uint8_t *tmpPtr;

	uint32_t tmpInt;

#ifdef _USE_FLOAT_
	float	tmpFloat;
#endif

#ifdef _USE_STRING_
	uint16_t strSize;
#endif

	for (int i=0; i< mes->argsNum; i++) {

		switch ( mes->typeTag[i] ) {

			case 'i':

				tmpInt = mes->getInteger32(i);
				tmpPtr =(uint8_t*)(&tmpInt);

				*packStartPtr++=*(tmpPtr+3);
				*packStartPtr++=*(tmpPtr+2);
				*packStartPtr++=*(tmpPtr+1);
				*packStartPtr++=*tmpPtr;

				mes->allPackSize += 4;
				break;

#ifdef _USE_FLOAT_
			case 'f':

				tmpFloat = mes->getFloat(i);
				tmpPtr=(uint8_t*)(&tmpFloat);

				*packStartPtr++=*(tmpPtr+3);
				*packStartPtr++=*(tmpPtr+2);
				*packStartPtr++=*(tmpPtr+1);
				*packStartPtr++=*tmpPtr;

				mes->allPackSize += 4;
				break;
#endif

#ifdef _USE_STRING_
			case 's':
				strSize = strlen(mes->getString(i));

				if(strSize > kMaxStringCharactor){

					DBG_LOGLN("encode max str err");
					return 1;
				}

				memcpy( (char*)packStartPtr ,mes->getString(i) ,strSize );
				packStartPtr += mes->getPackSize(strSize);

				mes->allPackSize += mes->getPackSize(strSize);
				break;
#endif

		}
	}
	return 0;
}