[1816] | 1 | //light.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5823] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2013 |
---|
[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 | |
---|
[5916] | 35 | static inline void __set_channel(size_t index, uint8_t val) { |
---|
[5706] | 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); |
---|
[5920] | 106 | memset(g_light_state.events, 0, sizeof(g_light_state.events)); |
---|
| 107 | |
---|
| 108 | g_light_state.eventsqueuelen = 0; |
---|
[1966] | 109 | |
---|
| 110 | return 0; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | int light_reinit(void) { |
---|
| 114 | if ( g_light_state.changes == NULL ) |
---|
| 115 | return -1; |
---|
| 116 | |
---|
| 117 | memset(g_light_state.changes, 0, g_light_state.channels); |
---|
[5920] | 118 | memset(g_light_state.events, 0, sizeof(g_light_state.events)); |
---|
| 119 | |
---|
| 120 | g_light_state.eventsqueuelen = 0; |
---|
[1818] | 121 | |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | int light_update(void) { |
---|
| 126 | return 0; |
---|
| 127 | } |
---|
| 128 | |
---|
[1821] | 129 | int light_check_stream (int id) { |
---|
[1822] | 130 | struct roar_stream * s; |
---|
| 131 | struct roar_stream_server * ss; |
---|
[1823] | 132 | char buf[512]; |
---|
[5917] | 133 | int i; |
---|
[1822] | 134 | |
---|
| 135 | if ( g_streams[id] == NULL ) |
---|
| 136 | return -1; |
---|
| 137 | |
---|
| 138 | ROAR_DBG("light_check_stream(id=%i) = ?", id); |
---|
| 139 | |
---|
| 140 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 141 | |
---|
[1823] | 142 | switch (s->info.codec) { |
---|
| 143 | case ROAR_CODEC_DMX512: |
---|
| 144 | if ( stream_vio_s_read(ss, buf, 512) != 512 ) { |
---|
| 145 | streams_delete(id); |
---|
| 146 | return -1; |
---|
| 147 | } |
---|
| 148 | |
---|
[1967] | 149 | for (i = 0; i < (g_light_state.channels < 512 ? g_light_state.channels : 512); i++) { |
---|
[5706] | 150 | __set_channel(i, buf[i]); |
---|
[1967] | 151 | } |
---|
| 152 | // memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512); |
---|
[1823] | 153 | |
---|
[2509] | 154 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
| 155 | |
---|
[1823] | 156 | return 0; |
---|
| 157 | break; |
---|
[1965] | 158 | case ROAR_CODEC_ROARDMX: |
---|
[2055] | 159 | ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id); |
---|
[5917] | 160 | if ( cf_light_roardmx_read(id, ss) == -1 ) { |
---|
| 161 | streams_delete(id); // simply drop the client on error. |
---|
[1965] | 162 | return -1; |
---|
| 163 | } |
---|
| 164 | break; |
---|
[1823] | 165 | default: |
---|
| 166 | streams_delete(id); |
---|
| 167 | return -1; |
---|
| 168 | } |
---|
| 169 | |
---|
[1821] | 170 | return 0; |
---|
| 171 | } |
---|
| 172 | |
---|
[5917] | 173 | static inline int light_send_stream_dmx512 (int id, struct roar_stream * s, struct roar_stream_server * ss) { |
---|
[5918] | 174 | size_t chans = g_light_state.channels; |
---|
[5917] | 175 | uint8_t buf[512]; |
---|
| 176 | register uint8_t * bufptr; |
---|
| 177 | |
---|
| 178 | if ( chans > 512 ) |
---|
| 179 | chans = 512; |
---|
| 180 | |
---|
| 181 | if ( chans == 512 ) { |
---|
| 182 | bufptr = g_light_state.state; |
---|
| 183 | } else { |
---|
| 184 | memset(buf, 0, 512); |
---|
| 185 | memcpy(buf, g_light_state.state, chans); |
---|
| 186 | bufptr = buf; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) { |
---|
| 190 | streams_delete(id); |
---|
| 191 | return -1; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
| 195 | |
---|
| 196 | return 0; |
---|
| 197 | } |
---|
| 198 | |
---|
[1821] | 199 | int light_send_stream (int id) { |
---|
[1822] | 200 | struct roar_stream * s; |
---|
| 201 | struct roar_stream_server * ss; |
---|
| 202 | |
---|
| 203 | if ( g_streams[id] == NULL ) |
---|
| 204 | return -1; |
---|
| 205 | |
---|
[1823] | 206 | ROAR_DBG("light_send_stream(id=%i) = ?", id); |
---|
[1822] | 207 | |
---|
| 208 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 209 | |
---|
| 210 | switch (s->info.codec) { |
---|
| 211 | case ROAR_CODEC_DMX512: |
---|
[5917] | 212 | return light_send_stream_dmx512(id, s, ss); |
---|
[1822] | 213 | break; |
---|
[1968] | 214 | case ROAR_CODEC_ROARDMX: |
---|
[5917] | 215 | return cf_light_roardmx_write(id, ss); |
---|
[1968] | 216 | break; |
---|
[1822] | 217 | default: |
---|
| 218 | streams_delete(id); |
---|
| 219 | return -1; |
---|
| 220 | } |
---|
| 221 | |
---|
[1821] | 222 | return 0; |
---|
| 223 | } |
---|
| 224 | |
---|
[5706] | 225 | int light_dmxchannel_get(size_t index) { |
---|
| 226 | if ( (size_t)g_light_state.channels <= index ) { |
---|
| 227 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 228 | return -1; |
---|
| 229 | } |
---|
| 230 | |
---|
[5916] | 231 | return (int)(unsigned int)(uint8_t)g_light_state.state[index]; |
---|
[5706] | 232 | } |
---|
| 233 | |
---|
[5916] | 234 | int light_dmxchannel_set(size_t index, uint8_t val) { |
---|
[5706] | 235 | if ( (size_t)g_light_state.channels <= index ) { |
---|
| 236 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 237 | return -1; |
---|
| 238 | } |
---|
| 239 | |
---|
| 240 | __set_channel(index, val); |
---|
| 241 | return 0; |
---|
| 242 | } |
---|
| 243 | |
---|
[5920] | 244 | int light_dmxevent_add(const uint8_t * events, size_t len) { |
---|
| 245 | size_t i; |
---|
| 246 | |
---|
| 247 | if ( events == NULL ) { |
---|
| 248 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 249 | return -1; |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | if ( len > (sizeof(g_light_state.events) - g_light_state.eventsqueuelen) ) { |
---|
| 253 | roar_err_set(ROAR_ERROR_NOSPC); |
---|
| 254 | return -1; |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | for (i = 0; i < len; i++) { |
---|
| 258 | g_light_state.events[g_light_state.eventsqueuelen++] = events[i]; |
---|
| 259 | roar_notify_core_emit_simple(ROAR_DATA_DMX512_EVENT, -1, g_light_mixer.stream, ROAR_OT_STREAM, events[i], -1, NULL, -1); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | return 0; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | int light_dmxevent_read(const uint8_t ** events, size_t * len) { |
---|
| 266 | if ( events == NULL || len == NULL ) { |
---|
| 267 | roar_err_set(ROAR_ERROR_FAULT); |
---|
| 268 | return -1; |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | *events = g_light_state.events; |
---|
| 272 | *len = g_light_state.eventsqueuelen; |
---|
| 273 | |
---|
| 274 | return 0; |
---|
| 275 | } |
---|
| 276 | |
---|
[2495] | 277 | #endif |
---|
| 278 | |
---|
[1816] | 279 | //ll |
---|