source: roaraudio/roard/light.c @ 2495:61b6587c286a

Last change on this file since 2495:61b6587c286a was 2495:61b6587c286a, checked in by phi, 15 years ago

support to disable light subsystem

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