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