source: roaraudio/roard/light.c @ 1966:472ef4b721c7

Last change on this file since 1966:472ef4b721c7 was 1966:472ef4b721c7, checked in by phi, 15 years ago

added a array for to mark changes

File size: 4.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    memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512);
115
116    for (i = 0; i < ROAR_STREAMS_MAX; i++) {
117     if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
118      if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
119       if ( stream_vio_write(i, buf, 512) != 512 ) {
120        streams_delete(i);
121       }
122      }
123     }
124    }
125
126    return 0;
127   break;
128  case ROAR_CODEC_ROARDMX:
129    if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) {
130     streams_delete(id); // because we don't know at the moment...
131     return -1;
132    }
133
134    // we ignore errors here at the moment as 0 not < -1
135    c = roar_roardmx_message_numchannels(&mes);
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     }
147    }
148   break;
149  default:
150    streams_delete(id);
151    return -1;
152 }
153
154 return 0;
155}
156
157int light_send_stream   (int id) {
158 int chans;
159 struct roar_stream        *   s;
160 struct roar_stream_server *  ss;
161 unsigned char buf[512];
162 register unsigned char * bufptr;
163
164 if ( g_streams[id] == NULL )
165  return -1;
166
167 ROAR_DBG("light_send_stream(id=%i) = ?", id);
168
169 s = ROAR_STREAM(ss = g_streams[id]);
170
171 switch (s->info.codec) {
172  case ROAR_CODEC_DMX512:
173    chans = g_light_state.channels;
174
175    if ( chans > 512 )
176     chans = 512;
177
178    if ( chans == 512 ) {
179     bufptr = g_light_state.state;
180    } else {
181     memset(buf, 0, 512);
182     memcpy(buf, g_light_state.state, chans);
183     bufptr = buf;
184    }
185
186    if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) {
187     streams_delete(id);
188     return -1;
189    }
190
191    return 0;
192   break;
193  default:
194    streams_delete(id);
195    return -1;
196 }
197
198 return 0;
199}
200
201//ll
Note: See TracBrowser for help on using the repository browser.