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 | int 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 | |
---|
48 | int 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 | |
---|
62 | int 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 | |
---|
78 | int 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 | |
---|
87 | int light_update(void) { |
---|
88 | return 0; |
---|
89 | } |
---|
90 | |
---|
91 | int 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 | return 0; |
---|
121 | break; |
---|
122 | case ROAR_CODEC_ROARDMX: |
---|
123 | ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id); |
---|
124 | if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) { |
---|
125 | streams_delete(id); // because we don't know at the moment... |
---|
126 | return -1; |
---|
127 | } |
---|
128 | |
---|
129 | // we ignore errors here at the moment as 0 not < -1 |
---|
130 | c = roar_roardmx_message_numchannels(&mes); |
---|
131 | ROAR_DBG("light_check_stream(id=%i): Number of subframes: %u", id, c); |
---|
132 | |
---|
133 | for (i = 0; i < c; i++) { |
---|
134 | if ( roar_roardmx_message_get_chanval(&mes, &channel, &value, i) == -1 ) |
---|
135 | return -1; |
---|
136 | |
---|
137 | if ( g_light_state.channels < channel ) { |
---|
138 | ROAR_WARN("light_check_stream(id=%i): Writing on non extisting DMX channel %u", id, channel); |
---|
139 | continue; |
---|
140 | } else { |
---|
141 | g_light_state.state[channel] = value; |
---|
142 | g_light_state.changes[channel] = 0xFF; // the channel changed |
---|
143 | } |
---|
144 | } |
---|
145 | break; |
---|
146 | default: |
---|
147 | streams_delete(id); |
---|
148 | return -1; |
---|
149 | } |
---|
150 | |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | int light_send_stream (int id) { |
---|
155 | int chans; |
---|
156 | struct roar_stream * s; |
---|
157 | struct roar_stream_server * ss; |
---|
158 | unsigned char buf[512]; |
---|
159 | register unsigned char * bufptr; |
---|
160 | struct roar_roardmx_message mes; |
---|
161 | int i; |
---|
162 | int have_message = 0; |
---|
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 | case ROAR_CODEC_ROARDMX: |
---|
194 | for (i = 0; i < g_light_state.channels; i++) { |
---|
195 | if ( g_light_state.changes[i] ) { |
---|
196 | if ( !have_message ) |
---|
197 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
198 | return -1; |
---|
199 | |
---|
200 | have_message = 2; |
---|
201 | |
---|
202 | if ( roar_roardmx_message_add_chanval(&mes, i, g_light_state.state[i]) == -1 ) { |
---|
203 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
204 | return -1; |
---|
205 | |
---|
206 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
207 | return -1; |
---|
208 | |
---|
209 | have_message = 1; |
---|
210 | } |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | if ( have_message == 2 ) { |
---|
215 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
216 | return -1; |
---|
217 | } |
---|
218 | return 0; |
---|
219 | break; |
---|
220 | default: |
---|
221 | streams_delete(id); |
---|
222 | return -1; |
---|
223 | } |
---|
224 | |
---|
225 | return 0; |
---|
226 | } |
---|
227 | |
---|
228 | //ll |
---|