source: roaraudio/roard/light.c @ 1965:3a0678019008

Last change on this file since 1965:3a0678019008 was 1965:3a0678019008, checked in by phi, 15 years ago

added input support for roardmx code

File size: 3.9 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 g_light_state.channels = channels;
39
40 return light_reset();
41}
42
43int light_free  (void) {
44 if ( g_light_state.state != NULL ) {
45  free(g_light_state.state);
46 }
47
48 g_light_state.channels = 0;
49
50 return 0;
51}
52
53int light_reset (void) {
54 if ( g_light_state.channels == 0 )
55  return 0;
56
57 if ( g_light_state.state == NULL )
58  return -1;
59
60 memset(g_light_state.state, 0, g_light_state.channels);
61
62 return 0;
63}
64
65int light_update(void) {
66 return 0;
67}
68
69int light_check_stream  (int id) {
70 struct roar_stream        *   s;
71 struct roar_stream_server *  ss;
72 struct roar_roardmx_message  mes;
73 char buf[512];
74 int i, c;
75 uint16_t      channel;
76 unsigned char value;
77
78 if ( g_streams[id] == NULL )
79  return -1;
80
81 ROAR_DBG("light_check_stream(id=%i) = ?", id);
82
83 s = ROAR_STREAM(ss = g_streams[id]);
84
85 switch (s->info.codec) {
86  case ROAR_CODEC_DMX512:
87    if ( stream_vio_s_read(ss, buf, 512) != 512 ) {
88     streams_delete(id);
89     return -1;
90    }
91
92    memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512);
93
94    for (i = 0; i < ROAR_STREAMS_MAX; i++) {
95     if ( g_streams[i] != NULL && ROAR_STREAM(g_streams[i])->pos_rel_id == id ) {
96      if ( ROAR_STREAM(g_streams[i])->dir == ROAR_DIR_THRU ) {
97       if ( stream_vio_write(i, buf, 512) != 512 ) {
98        streams_delete(i);
99       }
100      }
101     }
102    }
103
104    return 0;
105   break;
106  case ROAR_CODEC_ROARDMX:
107    if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) {
108     streams_delete(id); // because we don't know at the moment...
109     return -1;
110    }
111
112    // we ignore errors here at the moment as 0 not < -1
113    c = roar_roardmx_message_numchannels(&mes);
114
115    for (i = 0; i < c; i++) {
116     if ( roar_roardmx_message_get_chanval(&mes, &channel, &value, i) == -1 )
117      return -1;
118
119     if ( g_light_state.channels < channel ) {
120      ROAR_WARN("light_check_stream(id=%i): Writing on non extisting DMX channel %u", id, channel);
121      continue;
122     } else {
123      g_light_state.state[channel] = value;
124     }
125    }
126   break;
127  default:
128    streams_delete(id);
129    return -1;
130 }
131
132 return 0;
133}
134
135int light_send_stream   (int id) {
136 int chans;
137 struct roar_stream        *   s;
138 struct roar_stream_server *  ss;
139 unsigned char buf[512];
140 register unsigned char * bufptr;
141
142 if ( g_streams[id] == NULL )
143  return -1;
144
145 ROAR_DBG("light_send_stream(id=%i) = ?", id);
146
147 s = ROAR_STREAM(ss = g_streams[id]);
148
149 switch (s->info.codec) {
150  case ROAR_CODEC_DMX512:
151    chans = g_light_state.channels;
152
153    if ( chans > 512 )
154     chans = 512;
155
156    if ( chans == 512 ) {
157     bufptr = g_light_state.state;
158    } else {
159     memset(buf, 0, 512);
160     memcpy(buf, g_light_state.state, chans);
161     bufptr = buf;
162    }
163
164    if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) {
165     streams_delete(id);
166     return -1;
167    }
168
169    return 0;
170   break;
171  default:
172    streams_delete(id);
173    return -1;
174 }
175
176 return 0;
177}
178
179//ll
Note: See TracBrowser for help on using the repository browser.