1 | //light.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008 |
---|
5 | * |
---|
6 | * This file is part of roard a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * RoarAudio is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "roard.h" |
---|
26 | |
---|
27 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
28 | |
---|
29 | int light_init (unsigned int channels) { |
---|
30 | struct roar_stream_server * ss; |
---|
31 | |
---|
32 | g_light_state.channels = 0; |
---|
33 | |
---|
34 | if ( channels == 0 || channels > (512*512) ) /* unrealstic values */ |
---|
35 | return -1; |
---|
36 | |
---|
37 | if ( (g_light_state.state = malloc(channels)) == NULL ) { |
---|
38 | return -1; |
---|
39 | } |
---|
40 | |
---|
41 | if ( (g_light_state.changes = malloc(channels)) == NULL ) { |
---|
42 | free(g_light_state.state); |
---|
43 | return -1; |
---|
44 | } |
---|
45 | |
---|
46 | g_light_state.channels = channels; |
---|
47 | |
---|
48 | if ( (g_light_mixer.stream = add_mixer(ROAR_SUBSYS_LIGHT, _MIXER_NAME("Light Control"), &ss)) == -1 ) { |
---|
49 | free(g_light_state.state); |
---|
50 | return -1; |
---|
51 | } |
---|
52 | |
---|
53 | ROAR_STREAM(ss)->info.codec = ROAR_CODEC_DMX512; |
---|
54 | ROAR_STREAM(ss)->info.bits = ROAR_LIGHT_BITS; |
---|
55 | ROAR_STREAM(ss)->info.rate = ROAR_OUTPUT_CFREQ; |
---|
56 | |
---|
57 | return light_reset(); |
---|
58 | } |
---|
59 | |
---|
60 | int light_free (void) { |
---|
61 | if ( g_light_state.state != NULL ) { |
---|
62 | free(g_light_state.state); |
---|
63 | } |
---|
64 | |
---|
65 | if ( g_light_state.changes != NULL ) { |
---|
66 | free(g_light_state.changes); |
---|
67 | } |
---|
68 | |
---|
69 | g_light_state.channels = 0; |
---|
70 | |
---|
71 | return 0; |
---|
72 | } |
---|
73 | |
---|
74 | int light_reset (void) { |
---|
75 | if ( g_light_state.channels == 0 ) |
---|
76 | return 0; |
---|
77 | |
---|
78 | if ( g_light_state.state == NULL ) |
---|
79 | return -1; |
---|
80 | |
---|
81 | if ( g_light_state.changes == NULL ) |
---|
82 | return -1; |
---|
83 | |
---|
84 | memset(g_light_state.state, 0, g_light_state.channels); |
---|
85 | memset(g_light_state.changes, 0, g_light_state.channels); |
---|
86 | |
---|
87 | return 0; |
---|
88 | } |
---|
89 | |
---|
90 | int light_reinit(void) { |
---|
91 | if ( g_light_state.changes == NULL ) |
---|
92 | return -1; |
---|
93 | |
---|
94 | memset(g_light_state.changes, 0, g_light_state.channels); |
---|
95 | |
---|
96 | return 0; |
---|
97 | } |
---|
98 | |
---|
99 | int light_update(void) { |
---|
100 | return 0; |
---|
101 | } |
---|
102 | |
---|
103 | int light_check_stream (int id) { |
---|
104 | struct roar_stream * s; |
---|
105 | struct roar_stream_server * ss; |
---|
106 | struct roar_roardmx_message mes; |
---|
107 | char buf[512]; |
---|
108 | int i, c; |
---|
109 | uint16_t channel; |
---|
110 | unsigned char value; |
---|
111 | |
---|
112 | if ( g_streams[id] == NULL ) |
---|
113 | return -1; |
---|
114 | |
---|
115 | ROAR_DBG("light_check_stream(id=%i) = ?", id); |
---|
116 | |
---|
117 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
118 | |
---|
119 | switch (s->info.codec) { |
---|
120 | case ROAR_CODEC_DMX512: |
---|
121 | if ( stream_vio_s_read(ss, buf, 512) != 512 ) { |
---|
122 | streams_delete(id); |
---|
123 | return -1; |
---|
124 | } |
---|
125 | |
---|
126 | for (i = 0; i < (g_light_state.channels < 512 ? g_light_state.channels : 512); i++) { |
---|
127 | g_light_state.changes[i] |= g_light_state.state[i] ^ buf[i]; |
---|
128 | g_light_state.state[i] = buf[i]; |
---|
129 | } |
---|
130 | // memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512); |
---|
131 | |
---|
132 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
133 | |
---|
134 | return 0; |
---|
135 | break; |
---|
136 | case ROAR_CODEC_ROARDMX: |
---|
137 | ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id); |
---|
138 | if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) { |
---|
139 | streams_delete(id); // because we don't know at the moment... |
---|
140 | return -1; |
---|
141 | } |
---|
142 | |
---|
143 | // we ignore errors here at the moment as 0 not < -1 |
---|
144 | c = roar_roardmx_message_numchannels(&mes); |
---|
145 | ROAR_DBG("light_check_stream(id=%i): Number of subframes: %u", id, c); |
---|
146 | |
---|
147 | for (i = 0; i < c; i++) { |
---|
148 | if ( roar_roardmx_message_get_chanval(&mes, &channel, &value, i) == -1 ) |
---|
149 | return -1; |
---|
150 | |
---|
151 | if ( g_light_state.channels < channel ) { |
---|
152 | ROAR_WARN("light_check_stream(id=%i): Writing on non extisting DMX channel %u", id, channel); |
---|
153 | continue; |
---|
154 | } else { |
---|
155 | g_light_state.state[channel] = value; |
---|
156 | g_light_state.changes[channel] = 0xFF; // the channel changed |
---|
157 | } |
---|
158 | } |
---|
159 | break; |
---|
160 | default: |
---|
161 | streams_delete(id); |
---|
162 | return -1; |
---|
163 | } |
---|
164 | |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | int light_send_stream (int id) { |
---|
169 | int chans; |
---|
170 | struct roar_stream * s; |
---|
171 | struct roar_stream_server * ss; |
---|
172 | unsigned char buf[512]; |
---|
173 | register unsigned char * bufptr; |
---|
174 | struct roar_roardmx_message mes; |
---|
175 | int i; |
---|
176 | int have_message = 0; |
---|
177 | |
---|
178 | if ( g_streams[id] == NULL ) |
---|
179 | return -1; |
---|
180 | |
---|
181 | ROAR_DBG("light_send_stream(id=%i) = ?", id); |
---|
182 | |
---|
183 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
184 | |
---|
185 | switch (s->info.codec) { |
---|
186 | case ROAR_CODEC_DMX512: |
---|
187 | chans = g_light_state.channels; |
---|
188 | |
---|
189 | if ( chans > 512 ) |
---|
190 | chans = 512; |
---|
191 | |
---|
192 | if ( chans == 512 ) { |
---|
193 | bufptr = g_light_state.state; |
---|
194 | } else { |
---|
195 | memset(buf, 0, 512); |
---|
196 | memcpy(buf, g_light_state.state, chans); |
---|
197 | bufptr = buf; |
---|
198 | } |
---|
199 | |
---|
200 | if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) { |
---|
201 | streams_delete(id); |
---|
202 | return -1; |
---|
203 | } |
---|
204 | |
---|
205 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
206 | |
---|
207 | return 0; |
---|
208 | break; |
---|
209 | case ROAR_CODEC_ROARDMX: |
---|
210 | for (i = 0; i < g_light_state.channels; i++) { |
---|
211 | if ( g_light_state.changes[i] ) { |
---|
212 | if ( !have_message ) |
---|
213 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
214 | return -1; |
---|
215 | |
---|
216 | have_message = 2; |
---|
217 | |
---|
218 | if ( roar_roardmx_message_add_chanval(&mes, i, g_light_state.state[i]) == -1 ) { |
---|
219 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
220 | return -1; |
---|
221 | |
---|
222 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
223 | return -1; |
---|
224 | |
---|
225 | have_message = 1; |
---|
226 | } |
---|
227 | } |
---|
228 | } |
---|
229 | |
---|
230 | if ( have_message == 2 ) { |
---|
231 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
232 | return -1; |
---|
233 | } |
---|
234 | return 0; |
---|
235 | break; |
---|
236 | default: |
---|
237 | streams_delete(id); |
---|
238 | return -1; |
---|
239 | } |
---|
240 | |
---|
241 | return 0; |
---|
242 | } |
---|
243 | |
---|
244 | #endif |
---|
245 | |
---|
246 | //ll |
---|