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