[1816] | 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 | |
---|
[2495] | 27 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
| 28 | |
---|
[1818] | 29 | int light_init (unsigned int channels) { |
---|
[2959] | 30 | struct roar_stream_server * ss; |
---|
[3215] | 31 | int i; |
---|
[1818] | 32 | |
---|
[1819] | 33 | g_light_state.channels = 0; |
---|
| 34 | |
---|
[1922] | 35 | if ( channels == 0 || channels > (512*512) ) /* unrealstic values */ |
---|
| 36 | return -1; |
---|
| 37 | |
---|
[1818] | 38 | if ( (g_light_state.state = malloc(channels)) == NULL ) { |
---|
| 39 | return -1; |
---|
| 40 | } |
---|
| 41 | |
---|
[1966] | 42 | if ( (g_light_state.changes = malloc(channels)) == NULL ) { |
---|
| 43 | free(g_light_state.state); |
---|
| 44 | return -1; |
---|
| 45 | } |
---|
| 46 | |
---|
[1818] | 47 | g_light_state.channels = channels; |
---|
| 48 | |
---|
[2959] | 49 | if ( (g_light_mixer.stream = add_mixer(ROAR_SUBSYS_LIGHT, _MIXER_NAME("Light Control"), &ss)) == -1 ) { |
---|
[2944] | 50 | free(g_light_state.state); |
---|
| 51 | return -1; |
---|
| 52 | } |
---|
| 53 | |
---|
[2959] | 54 | ROAR_STREAM(ss)->info.codec = ROAR_CODEC_DMX512; |
---|
| 55 | ROAR_STREAM(ss)->info.bits = ROAR_LIGHT_BITS; |
---|
| 56 | ROAR_STREAM(ss)->info.rate = ROAR_OUTPUT_CFREQ; |
---|
| 57 | |
---|
[3215] | 58 | for (i = 0; i < ROAR_STREAMS_MAX; i++) { |
---|
| 59 | if ( g_streams[i] != NULL ) { |
---|
| 60 | if ( streams_get_subsys(i) == ROAR_SUBSYS_LIGHT ) { |
---|
| 61 | streams_set_mixer_stream(i, g_light_mixer.stream); |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
[1818] | 66 | return light_reset(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | int light_free (void) { |
---|
| 70 | if ( g_light_state.state != NULL ) { |
---|
| 71 | free(g_light_state.state); |
---|
| 72 | } |
---|
| 73 | |
---|
[1966] | 74 | if ( g_light_state.changes != NULL ) { |
---|
| 75 | free(g_light_state.changes); |
---|
| 76 | } |
---|
| 77 | |
---|
[1818] | 78 | g_light_state.channels = 0; |
---|
| 79 | |
---|
| 80 | return 0; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | int light_reset (void) { |
---|
| 84 | if ( g_light_state.channels == 0 ) |
---|
| 85 | return 0; |
---|
| 86 | |
---|
| 87 | if ( g_light_state.state == NULL ) |
---|
| 88 | return -1; |
---|
| 89 | |
---|
[1966] | 90 | if ( g_light_state.changes == NULL ) |
---|
| 91 | return -1; |
---|
| 92 | |
---|
| 93 | memset(g_light_state.state, 0, g_light_state.channels); |
---|
| 94 | memset(g_light_state.changes, 0, g_light_state.channels); |
---|
| 95 | |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | int light_reinit(void) { |
---|
| 100 | if ( g_light_state.changes == NULL ) |
---|
| 101 | return -1; |
---|
| 102 | |
---|
| 103 | memset(g_light_state.changes, 0, g_light_state.channels); |
---|
[1818] | 104 | |
---|
| 105 | return 0; |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | int light_update(void) { |
---|
| 109 | return 0; |
---|
| 110 | } |
---|
| 111 | |
---|
[1821] | 112 | int light_check_stream (int id) { |
---|
[1822] | 113 | struct roar_stream * s; |
---|
| 114 | struct roar_stream_server * ss; |
---|
[1965] | 115 | struct roar_roardmx_message mes; |
---|
[1823] | 116 | char buf[512]; |
---|
[1965] | 117 | int i, c; |
---|
| 118 | uint16_t channel; |
---|
| 119 | unsigned char value; |
---|
[1822] | 120 | |
---|
| 121 | if ( g_streams[id] == NULL ) |
---|
| 122 | return -1; |
---|
| 123 | |
---|
| 124 | ROAR_DBG("light_check_stream(id=%i) = ?", id); |
---|
| 125 | |
---|
| 126 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 127 | |
---|
[1823] | 128 | switch (s->info.codec) { |
---|
| 129 | case ROAR_CODEC_DMX512: |
---|
| 130 | if ( stream_vio_s_read(ss, buf, 512) != 512 ) { |
---|
| 131 | streams_delete(id); |
---|
| 132 | return -1; |
---|
| 133 | } |
---|
| 134 | |
---|
[1967] | 135 | for (i = 0; i < (g_light_state.channels < 512 ? g_light_state.channels : 512); i++) { |
---|
| 136 | g_light_state.changes[i] |= g_light_state.state[i] ^ buf[i]; |
---|
| 137 | g_light_state.state[i] = buf[i]; |
---|
| 138 | } |
---|
| 139 | // memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512); |
---|
[1823] | 140 | |
---|
[2509] | 141 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
| 142 | |
---|
[1823] | 143 | return 0; |
---|
| 144 | break; |
---|
[1965] | 145 | case ROAR_CODEC_ROARDMX: |
---|
[2055] | 146 | ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id); |
---|
[1965] | 147 | if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) { |
---|
| 148 | streams_delete(id); // because we don't know at the moment... |
---|
| 149 | return -1; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | // we ignore errors here at the moment as 0 not < -1 |
---|
| 153 | c = roar_roardmx_message_numchannels(&mes); |
---|
[2055] | 154 | ROAR_DBG("light_check_stream(id=%i): Number of subframes: %u", id, c); |
---|
[1965] | 155 | |
---|
| 156 | for (i = 0; i < c; i++) { |
---|
| 157 | if ( roar_roardmx_message_get_chanval(&mes, &channel, &value, i) == -1 ) |
---|
| 158 | return -1; |
---|
| 159 | |
---|
| 160 | if ( g_light_state.channels < channel ) { |
---|
| 161 | ROAR_WARN("light_check_stream(id=%i): Writing on non extisting DMX channel %u", id, channel); |
---|
| 162 | continue; |
---|
| 163 | } else { |
---|
[1967] | 164 | g_light_state.state[channel] = value; |
---|
| 165 | g_light_state.changes[channel] = 0xFF; // the channel changed |
---|
[1965] | 166 | } |
---|
| 167 | } |
---|
| 168 | break; |
---|
[1823] | 169 | default: |
---|
| 170 | streams_delete(id); |
---|
| 171 | return -1; |
---|
| 172 | } |
---|
| 173 | |
---|
[1821] | 174 | return 0; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | int light_send_stream (int id) { |
---|
[1822] | 178 | int chans; |
---|
| 179 | struct roar_stream * s; |
---|
| 180 | struct roar_stream_server * ss; |
---|
[1925] | 181 | unsigned char buf[512]; |
---|
| 182 | register unsigned char * bufptr; |
---|
[1968] | 183 | struct roar_roardmx_message mes; |
---|
| 184 | int i; |
---|
| 185 | int have_message = 0; |
---|
[1822] | 186 | |
---|
| 187 | if ( g_streams[id] == NULL ) |
---|
| 188 | return -1; |
---|
| 189 | |
---|
[1823] | 190 | ROAR_DBG("light_send_stream(id=%i) = ?", id); |
---|
[1822] | 191 | |
---|
| 192 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 193 | |
---|
| 194 | switch (s->info.codec) { |
---|
| 195 | case ROAR_CODEC_DMX512: |
---|
| 196 | chans = g_light_state.channels; |
---|
| 197 | |
---|
| 198 | if ( chans > 512 ) |
---|
| 199 | chans = 512; |
---|
| 200 | |
---|
[1844] | 201 | if ( chans == 512 ) { |
---|
| 202 | bufptr = g_light_state.state; |
---|
| 203 | } else { |
---|
| 204 | memset(buf, 0, 512); |
---|
| 205 | memcpy(buf, g_light_state.state, chans); |
---|
| 206 | bufptr = buf; |
---|
[1822] | 207 | } |
---|
| 208 | |
---|
[1844] | 209 | if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) { |
---|
| 210 | streams_delete(id); |
---|
| 211 | return -1; |
---|
[1822] | 212 | } |
---|
| 213 | |
---|
[2509] | 214 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
| 215 | |
---|
[1822] | 216 | return 0; |
---|
| 217 | break; |
---|
[1968] | 218 | case ROAR_CODEC_ROARDMX: |
---|
| 219 | for (i = 0; i < g_light_state.channels; i++) { |
---|
| 220 | if ( g_light_state.changes[i] ) { |
---|
| 221 | if ( !have_message ) |
---|
| 222 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
| 223 | return -1; |
---|
| 224 | |
---|
| 225 | have_message = 2; |
---|
| 226 | |
---|
| 227 | if ( roar_roardmx_message_add_chanval(&mes, i, g_light_state.state[i]) == -1 ) { |
---|
| 228 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
| 229 | return -1; |
---|
| 230 | |
---|
| 231 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
| 232 | return -1; |
---|
| 233 | |
---|
| 234 | have_message = 1; |
---|
| 235 | } |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | if ( have_message == 2 ) { |
---|
| 240 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
| 241 | return -1; |
---|
| 242 | } |
---|
| 243 | return 0; |
---|
| 244 | break; |
---|
[1822] | 245 | default: |
---|
| 246 | streams_delete(id); |
---|
| 247 | return -1; |
---|
| 248 | } |
---|
| 249 | |
---|
[1821] | 250 | return 0; |
---|
| 251 | } |
---|
| 252 | |
---|
[2495] | 253 | #endif |
---|
| 254 | |
---|
[1816] | 255 | //ll |
---|