source: roaraudio/roard/light.c @ 5878:3b92b0d6ef9b

Last change on this file since 5878:3b92b0d6ef9b was 5823:f9f70dbaa376, checked in by phi, 11 years ago

updated copyright

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