Line No. | Rev | Author | Line |
---|---|---|---|
1 | 6 | kaklik | /*! \file osc.c \brief Open Sound Control (OSC) client functions. */ |
2 | //***************************************************************************** |
||
3 | // |
||
4 | // File Name : 'osc.c' |
||
5 | // Title : Open Sound Control (OSC) client functions |
||
6 | // Author : Pascal Stang - Copyright (C) 2002 |
||
7 | // Created : 10/30/2002 |
||
8 | // Revised : 11/4/2002 |
||
9 | // Version : 0.1 |
||
10 | // Target MCU : Atmel AVR series |
||
11 | // Editor Tabs : 4 |
||
12 | // |
||
13 | // Description : This code implements a subset of the OSC protocol and |
||
14 | // messages. It is meant to be used with the OSC extension for the visual |
||
15 | // programming and data-processing package Pure-Data (or PD). Note that |
||
16 | // this code sends OSC messages over serial RS-232, not a network. You |
||
17 | // must use these functions with a suitable OSC SERIAL server (receiver) |
||
18 | // on a host machine. |
||
19 | // |
||
20 | // This code is distributed under the GNU Public License |
||
21 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
22 | // |
||
23 | //***************************************************************************** |
||
24 | |||
25 | #include <avr/io.h> |
||
26 | #include <avr/interrupt.h> |
||
27 | #include <avr/pgmspace.h> |
||
28 | |||
29 | #include "global.h" |
||
30 | #include "buffer.h" |
||
31 | #include "uart.h" |
||
32 | #include "osc.h" |
||
33 | |||
34 | // global variables |
||
35 | // ready flag from uart |
||
36 | extern volatile u08 uartReadyTx; |
||
37 | |||
38 | // functions |
||
39 | void oscInit(void) |
||
40 | { |
||
41 | // initialize uart (if not already done) |
||
42 | uartInit(); |
||
43 | // set the default communication rate |
||
44 | uartSetBaudRate(38400); |
||
45 | } |
||
46 | |||
47 | void oscSendMessage(char *address) |
||
48 | { |
||
49 | // write OSC packet header, argument length = 0bytes |
||
50 | oscWriteHeader(address, 0); |
||
51 | // write OSC address to packet |
||
52 | oscWriteString(address); |
||
53 | // apply checksum |
||
54 | oscWriteChecksum(); |
||
55 | // send buffer |
||
56 | uartSendTxBuffer(); |
||
57 | // wait for completion, transmitter to be ready |
||
58 | while(!uartReadyTx); |
||
59 | } |
||
60 | |||
61 | void oscSendMessageInt(char *address, u32 arg) |
||
62 | { |
||
63 | // write OSC packet header, argument length = 4bytes |
||
64 | oscWriteHeader(address, 4); |
||
65 | // write OSC address to packet |
||
66 | oscWriteString(address); |
||
67 | // copy arg to buffer |
||
68 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+3) ); |
||
69 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+2) ); |
||
70 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+1) ); |
||
71 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg))+0) ); |
||
72 | // apply checksum |
||
73 | oscWriteChecksum(); |
||
74 | // send buffer |
||
75 | uartSendTxBuffer(); |
||
76 | // wait for completion, transmitter to be ready |
||
77 | while(!uartReadyTx); |
||
78 | } |
||
79 | |||
80 | void oscSendMessageIntInt(char *address, u32 arg1, u32 arg2) |
||
81 | { |
||
82 | // write OSC packet header, argument length = 8bytes |
||
83 | oscWriteHeader(address, 8); |
||
84 | // write OSC address to packet |
||
85 | oscWriteString(address); |
||
86 | // copy arg1 to buffer |
||
87 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg1))+3) ); |
||
88 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg1))+2) ); |
||
89 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg1))+1) ); |
||
90 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg1))+0) ); |
||
91 | // copy arg2 to buffer |
||
92 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg2))+3) ); |
||
93 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg2))+2) ); |
||
94 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg2))+1) ); |
||
95 | bufferAddToEnd(uartGetTxBuffer(), *(((unsigned char*)(&arg2))+0) ); |
||
96 | // apply checksum |
||
97 | oscWriteChecksum(); |
||
98 | // send buffer |
||
99 | uartSendTxBuffer(); |
||
100 | // wait for completion, transmitter to be ready |
||
101 | while(!uartReadyTx); |
||
102 | } |
||
103 | |||
104 | void oscSendMessageString(char *address, char *arg) |
||
105 | { |
||
106 | u08 len; |
||
107 | |||
108 | // calculate length of argument string |
||
109 | for(len=0; PRG_RDB(arg+len); len++); |
||
110 | len++; // count a minumum of one null for termination |
||
111 | if(len&0x03) // are pad bytes necessary? |
||
112 | len += 4-(len&0x03); // add null pad bytes to reach multiple of four |
||
113 | |||
114 | // write OSC packet header, argument length = 8bytes |
||
115 | oscWriteHeader(address, len); |
||
116 | // write OSC address to packet |
||
117 | oscWriteString(address); |
||
118 | // write string buffer |
||
119 | oscWriteString(arg); |
||
120 | // apply checksum |
||
121 | oscWriteChecksum(); |
||
122 | // send buffer |
||
123 | uartSendTxBuffer(); |
||
124 | // wait for completion, transmitter to be ready |
||
125 | while(!uartReadyTx); |
||
126 | } |
||
127 | |||
128 | void oscWriteHeader(char *address, u08 arglen) |
||
129 | { |
||
130 | u08 len; |
||
131 | // write OSC packet header |
||
132 | bufferAddToEnd(uartGetTxBuffer(), OSC_HEADER); |
||
133 | // determine padded length of address |
||
134 | for(len=0; PRG_RDB(address+len); len++); |
||
135 | len++; // count a minumum of one null for termination |
||
136 | if(len&0x03) // are pad bytes necessary? |
||
137 | len += 4-(len&0x03); // add null pad bytes to reach multiple of four |
||
138 | // write length to packet header |
||
139 | bufferAddToEnd(uartGetTxBuffer(), len+arglen); |
||
140 | } |
||
141 | |||
142 | void oscWriteString(char *string) |
||
143 | { |
||
144 | u08 temp=1; |
||
145 | u08 len=0; |
||
146 | |||
147 | // write OSC string to packet |
||
148 | // copy string's null-termination intentionally |
||
149 | while(temp) |
||
150 | { |
||
151 | temp = PRG_RDB(string++); |
||
152 | bufferAddToEnd(uartGetTxBuffer(), temp); |
||
153 | len++; |
||
154 | } |
||
155 | |||
156 | // pad the string as necessary to reach a 4-byte multiple |
||
157 | // Note: (len&0x03) == (len%4) |
||
158 | //for(; (len&0x03); len++) |
||
159 | while(len&0x03) |
||
160 | { |
||
161 | bufferAddToEnd(uartGetTxBuffer(), 0); |
||
162 | len++; |
||
163 | } |
||
164 | } |
||
165 | |||
166 | void oscWriteChecksum(void) |
||
167 | { |
||
168 | u08 i; |
||
169 | u08 chksum = 0; |
||
170 | // calculate checksum |
||
171 | for(i=2; i<uartGetTxBuffer()->datalength; i++) |
||
172 | chksum += bufferGetAtIndex(uartGetTxBuffer(), i); |
||
173 | // write checksum to packet |
||
174 | bufferAddToEnd(uartGetTxBuffer(), chksum); |
||
175 | } |
Powered by WebSVN v2.8.3