source: roaraudio/roard/light.c @ 5961:06e7fd9e4c25

Last change on this file since 5961:06e7fd9e4c25 was 5961:06e7fd9e4c25, checked in by phi, 6 years ago

Updates of copyright and license headers

File size: 6.8 KB
Line 
1//light.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014
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, 51 Franklin Street, Fifth Floor,
22 *  Boston, MA 02110-1301, USA.
23 *
24 */
25
26#include "roard.h"
27
28#ifndef ROAR_WITHOUT_DCOMP_LIGHT
29
30// declared 'extern'
31struct light_state g_light_state;
32struct light_mixer g_light_mixer;
33// //
34
35static inline void __set_channel(size_t index, uint8_t val) {
36 g_light_state.changes[index] |= g_light_state.state[index] ^ val;
37 g_light_state.state[index]    =                              val;
38}
39
40int light_init  (uint32_t channels) {
41 struct roar_stream_server * ss;
42 int i;
43
44 g_light_state.channels = 0;
45
46 if ( channels == 0 || channels > ((uint32_t)512UL*(uint32_t)512UL) ) /* unrealstic values */
47  return -1;
48
49 if ( (g_light_state.state = roar_mm_malloc(channels)) == NULL ) {
50  return -1;
51 }
52
53 if ( (g_light_state.changes = roar_mm_malloc(channels)) == NULL ) {
54  roar_mm_free(g_light_state.state);
55  return -1;
56 }
57
58 g_light_state.channels = channels;
59
60 if ( (g_light_mixer.stream = add_mixer(ROAR_SUBSYS_LIGHT, _MIXER_NAME("Light Control"), &ss)) == -1 ) {
61  roar_mm_free(g_light_state.state);
62  return -1;
63 }
64
65 ROAR_STREAM(ss)->info.codec = ROAR_CODEC_DMX512;
66 ROAR_STREAM(ss)->info.bits  = ROAR_LIGHT_BITS;
67 ROAR_STREAM(ss)->info.rate  = ROAR_OUTPUT_CFREQ;
68
69 for (i = 0; i < ROAR_STREAMS_MAX; i++) {
70  if ( g_streams[i] != NULL ) {
71   if ( streams_get_subsys(i) == ROAR_SUBSYS_LIGHT ) {
72    streams_set_mixer_stream(i, g_light_mixer.stream);
73   }
74  }
75 }
76
77 return light_reset();
78}
79
80int light_free  (void) {
81 if ( g_light_state.state != NULL ) {
82  roar_mm_free(g_light_state.state);
83 }
84
85 if ( g_light_state.changes != NULL ) {
86  roar_mm_free(g_light_state.changes);
87 }
88
89 g_light_state.channels = 0;
90
91 return 0;
92}
93
94int light_reset (void) {
95 if ( g_light_state.channels == 0 )
96  return 0;
97
98 if ( g_light_state.state == NULL )
99  return -1;
100
101 if ( g_light_state.changes == NULL )
102  return -1;
103
104 memset(g_light_state.state,   0, g_light_state.channels);
105 memset(g_light_state.changes, 0, g_light_state.channels);
106 memset(g_light_state.events,  0, sizeof(g_light_state.events));
107
108 g_light_state.eventsqueuelen = 0;
109
110 return 0;
111}
112
113int light_reinit(void) {
114 if ( g_light_state.changes == NULL )
115  return -1;
116
117 memset(g_light_state.changes, 0, g_light_state.channels);
118 memset(g_light_state.events,  0, sizeof(g_light_state.events));
119
120 g_light_state.eventsqueuelen = 0;
121
122 return 0;
123}
124
125int light_update(void) {
126 return 0;
127}
128
129int light_check_stream  (int id) {
130 struct roar_stream        *   s;
131 struct roar_stream_server *  ss;
132 char buf[512];
133 int i;
134
135 if ( g_streams[id] == NULL )
136  return -1;
137
138 ROAR_DBG("light_check_stream(id=%i) = ?", id);
139
140 s = ROAR_STREAM(ss = g_streams[id]);
141
142 switch (s->info.codec) {
143  case ROAR_CODEC_DMX512:
144    if ( stream_vio_s_read(ss, buf, 512) != 512 ) {
145     streams_delete(id);
146     return -1;
147    }
148
149    for (i = 0; i < (g_light_state.channels < 512 ? g_light_state.channels : 512); i++) {
150     __set_channel(i, buf[i]);
151    }
152//    memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512);
153
154    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
155
156    return 0;
157   break;
158  case ROAR_CODEC_ROARDMX:
159    ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id);
160    if ( cf_light_roardmx_read(id, ss) == -1 ) {
161     streams_delete(id); // simply drop the client on error.
162     return -1;
163    }
164   break;
165  default:
166    streams_delete(id);
167    return -1;
168 }
169
170 return 0;
171}
172
173static inline int light_send_stream_dmx512  (int id, struct roar_stream * s, struct roar_stream_server * ss) {
174 size_t chans = g_light_state.channels;
175 uint8_t buf[512];
176 register uint8_t * bufptr;
177
178 if ( chans > 512 )
179  chans = 512;
180
181 if ( chans == 512 ) {
182  bufptr = g_light_state.state;
183 } else {
184  memset(buf, 0, 512);
185  memcpy(buf, g_light_state.state, chans);
186  bufptr = buf;
187 }
188
189 if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) {
190  streams_delete(id);
191  return -1;
192 }
193
194 s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
195
196 return 0;
197}
198
199int light_send_stream   (int id) {
200 struct roar_stream        *   s;
201 struct roar_stream_server *  ss;
202
203 if ( g_streams[id] == NULL )
204  return -1;
205
206 ROAR_DBG("light_send_stream(id=%i) = ?", id);
207
208 s = ROAR_STREAM(ss = g_streams[id]);
209
210 switch (s->info.codec) {
211  case ROAR_CODEC_DMX512:
212    return light_send_stream_dmx512(id, s, ss);
213   break;
214  case ROAR_CODEC_ROARDMX:
215    return cf_light_roardmx_write(id, ss);
216   break;
217  default:
218    streams_delete(id);
219    return -1;
220 }
221
222 return 0;
223}
224
225int light_dmxchannel_get(size_t index) {
226 if ( (size_t)g_light_state.channels <= index ) {
227  roar_err_set(ROAR_ERROR_NOENT);
228  return -1;
229 }
230
231 return (int)(unsigned int)(uint8_t)g_light_state.state[index];
232}
233
234int light_dmxchannel_set(size_t index, uint8_t val) {
235 if ( (size_t)g_light_state.channels <= index ) {
236  roar_err_set(ROAR_ERROR_NOENT);
237  return -1;
238 }
239
240 __set_channel(index, val);
241 return 0;
242}
243
244ssize_t light_dmxchannel_num(void) {
245 if ( g_light_state.state == NULL ) {
246  roar_err_set(ROAR_ERROR_BADSTATE);
247  return -1;
248 }
249
250 return g_light_state.channels;
251}
252
253int     light_dmxchannel_swap_universe(uint8_t * universe, size_t len) {
254 uint8_t c;
255 size_t i;
256
257 if ( g_light_state.state == NULL ) {
258  roar_err_set(ROAR_ERROR_BADSTATE);
259  return -1;
260 }
261
262 if ( g_light_state.channels != len ) {
263  roar_err_set(ROAR_ERROR_FAULT);
264  return -1;
265 }
266
267 for (i = 0; i < len; i++) {
268  c = g_light_state.state[i];
269  __set_channel(i, universe[i]);
270  universe[i] = c;
271 }
272
273 return 0;
274}
275
276int light_dmxevent_add(const uint8_t * events, size_t len) {
277 size_t i;
278
279 if ( events == NULL ) {
280  roar_err_set(ROAR_ERROR_FAULT);
281  return -1;
282 }
283
284 if ( len > (sizeof(g_light_state.events) - g_light_state.eventsqueuelen) ) {
285  roar_err_set(ROAR_ERROR_NOSPC);
286  return -1;
287 }
288
289 for (i = 0; i < len; i++) {
290  g_light_state.events[g_light_state.eventsqueuelen++] = events[i];
291  roar_notify_core_emit_simple(ROAR_DATA_DMX512_EVENT, -1, g_light_mixer.stream, ROAR_OT_STREAM, events[i], -1, NULL, -1);
292 }
293
294 return 0;
295}
296
297int light_dmxevent_read(const uint8_t ** events, size_t * len) {
298 if ( events == NULL || len == NULL ) {
299  roar_err_set(ROAR_ERROR_FAULT);
300  return -1;
301 }
302
303 *events = g_light_state.events;
304 *len    = g_light_state.eventsqueuelen;
305
306 return 0;
307}
308
309#endif
310
311//ll
Note: See TracBrowser for help on using the repository browser.