source: roaraudio/roard/light.c @ 2509:c04450c1bbd8

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

update pos counter

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