source: roaraudio/roard/light.c @ 2944:82dd35b2fa40

Last change on this file since 2944:82dd35b2fa40 was 2944:82dd35b2fa40, checked in by phi, 15 years ago

added light subsystem mixer

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