source: roaraudio/roard/light.c @ 2055:d2ccdc7f304e

Last change on this file since 2055:d2ccdc7f304e was 2055:d2ccdc7f304e, checked in by phi, 15 years ago

added some debug lions

File size: 5.5 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    ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id);
134    if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) {
135     streams_delete(id); // because we don't know at the moment...
136     return -1;
137    }
138
139    // we ignore errors here at the moment as 0 not < -1
140    c = roar_roardmx_message_numchannels(&mes);
141    ROAR_DBG("light_check_stream(id=%i): Number of subframes: %u", id, c);
142
143    for (i = 0; i < c; i++) {
144     if ( roar_roardmx_message_get_chanval(&mes, &channel, &value, i) == -1 )
145      return -1;
146
147     if ( g_light_state.channels < channel ) {
148      ROAR_WARN("light_check_stream(id=%i): Writing on non extisting DMX channel %u", id, channel);
149      continue;
150     } else {
151      g_light_state.state[channel]   = value;
152      g_light_state.changes[channel] = 0xFF; // the channel changed
153     }
154    }
155   break;
156  default:
157    streams_delete(id);
158    return -1;
159 }
160
161 return 0;
162}
163
164int light_send_stream   (int id) {
165 int chans;
166 struct roar_stream        *   s;
167 struct roar_stream_server *  ss;
168 unsigned char buf[512];
169 register unsigned char * bufptr;
170 struct roar_roardmx_message  mes;
171 int i;
172 int have_message = 0;
173
174 if ( g_streams[id] == NULL )
175  return -1;
176
177 ROAR_DBG("light_send_stream(id=%i) = ?", id);
178
179 s = ROAR_STREAM(ss = g_streams[id]);
180
181 switch (s->info.codec) {
182  case ROAR_CODEC_DMX512:
183    chans = g_light_state.channels;
184
185    if ( chans > 512 )
186     chans = 512;
187
188    if ( chans == 512 ) {
189     bufptr = g_light_state.state;
190    } else {
191     memset(buf, 0, 512);
192     memcpy(buf, g_light_state.state, chans);
193     bufptr = buf;
194    }
195
196    if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) {
197     streams_delete(id);
198     return -1;
199    }
200
201    return 0;
202   break;
203  case ROAR_CODEC_ROARDMX:
204    for (i = 0; i < g_light_state.channels; i++) {
205     if ( g_light_state.changes[i] ) {
206      if ( !have_message )
207       if ( roar_roardmx_message_new_sset(&mes) == -1 )
208        return -1;
209
210      have_message = 2;
211
212      if ( roar_roardmx_message_add_chanval(&mes, i, g_light_state.state[i]) == -1 ) {
213       if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 )
214        return -1;
215
216       if ( roar_roardmx_message_new_sset(&mes) == -1 )
217        return -1;
218
219       have_message = 1;
220      }
221     }
222    }
223
224    if ( have_message == 2 ) {
225     if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 )
226      return -1;
227    }
228    return 0;
229   break;
230  default:
231    streams_delete(id);
232    return -1;
233 }
234
235 return 0;
236}
237
238//ll
Note: See TracBrowser for help on using the repository browser.