source: roaraudio/roard/light.c @ 1974:3cfe906b214f

Last change on this file since 1974:3cfe906b214f was 1968:ca3b6d6306d8, checked in by phi, 15 years ago

added RoarDMX support for outputs

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