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