source: roaraudio/roard/light.c @ 5917:316fad0b1a8e

Last change on this file since 5917:316fad0b1a8e was 5917:316fad0b1a8e, checked in by phi, 11 years ago

some more cleanup: moved RoarDMX support in a diffrent file and clean up some random functions

File size: 5.3 KB
Line 
1//light.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013
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
107 return 0;
108}
109
110int light_reinit(void) {
111 if ( g_light_state.changes == NULL )
112  return -1;
113
114 memset(g_light_state.changes, 0, g_light_state.channels);
115
116 return 0;
117}
118
119int light_update(void) {
120 return 0;
121}
122
123int light_check_stream  (int id) {
124 struct roar_stream        *   s;
125 struct roar_stream_server *  ss;
126 char buf[512];
127 int i;
128
129 if ( g_streams[id] == NULL )
130  return -1;
131
132 ROAR_DBG("light_check_stream(id=%i) = ?", id);
133
134 s = ROAR_STREAM(ss = g_streams[id]);
135
136 switch (s->info.codec) {
137  case ROAR_CODEC_DMX512:
138    if ( stream_vio_s_read(ss, buf, 512) != 512 ) {
139     streams_delete(id);
140     return -1;
141    }
142
143    for (i = 0; i < (g_light_state.channels < 512 ? g_light_state.channels : 512); i++) {
144     __set_channel(i, buf[i]);
145    }
146//    memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512);
147
148    s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
149
150    return 0;
151   break;
152  case ROAR_CODEC_ROARDMX:
153    ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id);
154    if ( cf_light_roardmx_read(id, ss) == -1 ) {
155     streams_delete(id); // simply drop the client on error.
156     return -1;
157    }
158   break;
159  default:
160    streams_delete(id);
161    return -1;
162 }
163
164 return 0;
165}
166
167static inline int light_send_stream_dmx512  (int id, struct roar_stream * s, struct roar_stream_server * ss) {
168 int chans = g_light_state.channels;
169 uint8_t buf[512];
170 register uint8_t * bufptr;
171
172 if ( chans > 512 )
173  chans = 512;
174
175 if ( chans == 512 ) {
176  bufptr = g_light_state.state;
177 } else {
178  memset(buf, 0, 512);
179  memcpy(buf, g_light_state.state, chans);
180  bufptr = buf;
181 }
182
183 if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) {
184  streams_delete(id);
185  return -1;
186 }
187
188 s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1);
189
190 return 0;
191}
192
193int light_send_stream   (int id) {
194 struct roar_stream        *   s;
195 struct roar_stream_server *  ss;
196
197 if ( g_streams[id] == NULL )
198  return -1;
199
200 ROAR_DBG("light_send_stream(id=%i) = ?", id);
201
202 s = ROAR_STREAM(ss = g_streams[id]);
203
204 switch (s->info.codec) {
205  case ROAR_CODEC_DMX512:
206    return light_send_stream_dmx512(id, s, ss);
207   break;
208  case ROAR_CODEC_ROARDMX:
209    return cf_light_roardmx_write(id, ss);
210   break;
211  default:
212    streams_delete(id);
213    return -1;
214 }
215
216 return 0;
217}
218
219int light_dmxchannel_get(size_t index) {
220 if ( (size_t)g_light_state.channels <= index ) {
221  roar_err_set(ROAR_ERROR_NOENT);
222  return -1;
223 }
224
225 return (int)(unsigned int)(uint8_t)g_light_state.state[index];
226}
227
228int light_dmxchannel_set(size_t index, uint8_t val) {
229 if ( (size_t)g_light_state.channels <= index ) {
230  roar_err_set(ROAR_ERROR_NOENT);
231  return -1;
232 }
233
234 __set_channel(index, val);
235 return 0;
236}
237
238#endif
239
240//ll
Note: See TracBrowser for help on using the repository browser.