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