source: roaraudio/roard/light.c @ 5381:430b1d26e12d

Last change on this file since 5381:430b1d26e12d was 5381:430b1d26e12d, checked in by phi, 12 years ago

updated copyright years

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