source: roaraudio/roard/light.c @ 4708:c9d40761088a

Last change on this file since 4708:c9d40761088a was 4708:c9d40761088a, checked in by phi, 13 years ago

updated copyright statements

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