source: roaraudio/roard/light.c @ 3215:13678c1774be

Last change on this file since 3215:13678c1774be was 3215:13678c1774be, checked in by phi, 14 years ago

reset mixer stream in case a mixer comes up

File size: 5.9 KB
Line 
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
29int light_init  (unsigned int channels) {
30 struct roar_stream_server * ss;
31 int i;
32
33 g_light_state.channels = 0;
34
35 if ( channels == 0 || channels > (512*512) ) /* unrealstic values */
36  return -1;
37
38 if ( (g_light_state.state = malloc(channels)) == NULL ) {
39  return -1;
40 }
41
42 if ( (g_light_state.changes = malloc(channels)) == NULL ) {
43  free(g_light_state.state);
44  return -1;
45 }
46
47 g_light_state.channels = channels;
48
49 if ( (g_light_mixer.stream = add_mixer(ROAR_SUBSYS_LIGHT, _MIXER_NAME("Light Control"), &ss)) == -1 ) {
50  free(g_light_state.state);
51  return -1;
52 }
53
54 ROAR_STREAM(ss)->info.codec = ROAR_CODEC_DMX512;
55 ROAR_STREAM(ss)->info.bits  = ROAR_LIGHT_BITS;
56 ROAR_STREAM(ss)->info.rate  = ROAR_OUTPUT_CFREQ;
57
58 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
59  if ( g_streams[i] != NULL ) {
60   if ( streams_get_subsys(i) == ROAR_SUBSYS_LIGHT ) {
61    streams_set_mixer_stream(i, g_light_mixer.stream);
62   }
63  }
64 }
65
66 return light_reset();
67}
68
69int light_free  (void) {
70 if ( g_light_state.state != NULL ) {
71  free(g_light_state.state);
72 }
73
74 if ( g_light_state.changes != NULL ) {
75  free(g_light_state.changes);
76 }
77
78 g_light_state.channels = 0;
79
80 return 0;
81}
82
83int light_reset (void) {
84 if ( g_light_state.channels == 0 )
85  return 0;
86
87 if ( g_light_state.state == NULL )
88  return -1;
89
90 if ( g_light_state.changes == NULL )
91  return -1;
92
93 memset(g_light_state.state,   0, g_light_state.channels);
94 memset(g_light_state.changes, 0, g_light_state.channels);
95
96 return 0;
97}
98
99int light_reinit(void) {
100 if ( g_light_state.changes == NULL )
101  return -1;
102
103 memset(g_light_state.changes, 0, g_light_state.channels);
104
105 return 0;
106}
107
108int light_update(void) {
109 return 0;
110}
111
112int light_check_stream  (int id) {
113 struct roar_stream        *   s;
114 struct roar_stream_server *  ss;
115 struct roar_roardmx_message  mes;
116 char buf[512];
117 int i, c;
118 uint16_t      channel;
119 unsigned char value;
120
121 if ( g_streams[id] == NULL )
122  return -1;
123
124 ROAR_DBG("light_check_stream(id=%i) = ?", id);
125
126 s = ROAR_STREAM(ss = g_streams[id]);
127
128 switch (s->info.codec) {
129  case ROAR_CODEC_DMX512:
130    if ( stream_vio_s_read(ss, buf, 512) != 512 ) {
131     streams_delete(id);
132     return -1;
133    }
134
135    for (i = 0; i < (g_light_state.channels < 512 ? g_light_state.channels : 512); i++) {
136     g_light_state.changes[i] |= g_light_state.state[i] ^ buf[i];
137     g_light_state.state[i]    =                          buf[i];
138    }
139//    memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512);
140
141    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
142
143    return 0;
144   break;
145  case ROAR_CODEC_ROARDMX:
146    ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id);
147    if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) {
148     streams_delete(id); // because we don't know at the moment...
149     return -1;
150    }
151
152    // we ignore errors here at the moment as 0 not < -1
153    c = roar_roardmx_message_numchannels(&mes);
154    ROAR_DBG("light_check_stream(id=%i): Number of subframes: %u", id, c);
155
156    for (i = 0; i < c; i++) {
157     if ( roar_roardmx_message_get_chanval(&mes, &channel, &value, i) == -1 )
158      return -1;
159
160     if ( g_light_state.channels < channel ) {
161      ROAR_WARN("light_check_stream(id=%i): Writing on non extisting DMX channel %u", id, channel);
162      continue;
163     } else {
164      g_light_state.state[channel]   = value;
165      g_light_state.changes[channel] = 0xFF; // the channel changed
166     }
167    }
168   break;
169  default:
170    streams_delete(id);
171    return -1;
172 }
173
174 return 0;
175}
176
177int light_send_stream   (int id) {
178 int chans;
179 struct roar_stream        *   s;
180 struct roar_stream_server *  ss;
181 unsigned char buf[512];
182 register unsigned char * bufptr;
183 struct roar_roardmx_message  mes;
184 int i;
185 int have_message = 0;
186
187 if ( g_streams[id] == NULL )
188  return -1;
189
190 ROAR_DBG("light_send_stream(id=%i) = ?", id);
191
192 s = ROAR_STREAM(ss = g_streams[id]);
193
194 switch (s->info.codec) {
195  case ROAR_CODEC_DMX512:
196    chans = g_light_state.channels;
197
198    if ( chans > 512 )
199     chans = 512;
200
201    if ( chans == 512 ) {
202     bufptr = g_light_state.state;
203    } else {
204     memset(buf, 0, 512);
205     memcpy(buf, g_light_state.state, chans);
206     bufptr = buf;
207    }
208
209    if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) {
210     streams_delete(id);
211     return -1;
212    }
213
214    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
215
216    return 0;
217   break;
218  case ROAR_CODEC_ROARDMX:
219    for (i = 0; i < g_light_state.channels; i++) {
220     if ( g_light_state.changes[i] ) {
221      if ( !have_message )
222       if ( roar_roardmx_message_new_sset(&mes) == -1 )
223        return -1;
224
225      have_message = 2;
226
227      if ( roar_roardmx_message_add_chanval(&mes, i, g_light_state.state[i]) == -1 ) {
228       if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 )
229        return -1;
230
231       if ( roar_roardmx_message_new_sset(&mes) == -1 )
232        return -1;
233
234       have_message = 1;
235      }
236     }
237    }
238
239    if ( have_message == 2 ) {
240     if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 )
241      return -1;
242    }
243    return 0;
244   break;
245  default:
246    streams_delete(id);
247    return -1;
248 }
249
250 return 0;
251}
252
253#endif
254
255//ll
Note: See TracBrowser for help on using the repository browser.