source: roaraudio/roard/light.c @ 1967:801f10400cda

Last change on this file since 1967:801f10400cda was 1967:801f10400cda, checked in by phi, 15 years ago

detect changes

File size: 4.7 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
169 if ( g_streams[id] == NULL )
170  return -1;
171
172 ROAR_DBG("light_send_stream(id=%i) = ?", id);
173
174 s = ROAR_STREAM(ss = g_streams[id]);
175
176 switch (s->info.codec) {
177  case ROAR_CODEC_DMX512:
178    chans = g_light_state.channels;
179
180    if ( chans > 512 )
181     chans = 512;
182
183    if ( chans == 512 ) {
184     bufptr = g_light_state.state;
185    } else {
186     memset(buf, 0, 512);
187     memcpy(buf, g_light_state.state, chans);
188     bufptr = buf;
189    }
190
191    if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) {
192     streams_delete(id);
193     return -1;
194    }
195
196    return 0;
197   break;
198  default:
199    streams_delete(id);
200    return -1;
201 }
202
203 return 0;
204}
205
206//ll
Note: See TracBrowser for help on using the repository browser.