[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); |
---|
| 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; |
---|
[1823] | 126 | char buf[512]; |
---|
[5917] | 127 | int i; |
---|
[1822] | 128 | |
---|
| 129 | if ( g_streams[id] == NULL ) |
---|
| 130 | return -1; |
---|
| 131 | |
---|
| 132 | ROAR_DBG("light_check_stream(id=%i) = ?", id); |
---|
| 133 | |
---|
| 134 | s = ROAR_STREAM(ss = g_streams[id]); |
---|
| 135 | |
---|
[1823] | 136 | switch (s->info.codec) { |
---|
| 137 | case ROAR_CODEC_DMX512: |
---|
| 138 | if ( stream_vio_s_read(ss, buf, 512) != 512 ) { |
---|
| 139 | streams_delete(id); |
---|
| 140 | return -1; |
---|
| 141 | } |
---|
| 142 | |
---|
[1967] | 143 | for (i = 0; i < (g_light_state.channels < 512 ? g_light_state.channels : 512); i++) { |
---|
[5706] | 144 | __set_channel(i, buf[i]); |
---|
[1967] | 145 | } |
---|
| 146 | // memcpy(g_light_state.state, buf, g_light_state.channels < 512 ? g_light_state.channels : 512); |
---|
[1823] | 147 | |
---|
[2509] | 148 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
| 149 | |
---|
[1823] | 150 | return 0; |
---|
| 151 | break; |
---|
[1965] | 152 | case ROAR_CODEC_ROARDMX: |
---|
[2055] | 153 | ROAR_DBG("light_check_stream(id=%i): Codec: RoarDMX", id); |
---|
[5917] | 154 | if ( cf_light_roardmx_read(id, ss) == -1 ) { |
---|
| 155 | streams_delete(id); // simply drop the client on error. |
---|
[1965] | 156 | return -1; |
---|
| 157 | } |
---|
| 158 | break; |
---|
[1823] | 159 | default: |
---|
| 160 | streams_delete(id); |
---|
| 161 | return -1; |
---|
| 162 | } |
---|
| 163 | |
---|
[1821] | 164 | return 0; |
---|
| 165 | } |
---|
| 166 | |
---|
[5917] | 167 | static inline int light_send_stream_dmx512 (int id, struct roar_stream * s, struct roar_stream_server * ss) { |
---|
| 168 | int chans = g_light_state.channels; |
---|
| 169 | uint8_t buf[512]; |
---|
| 170 | register uint8_t * bufptr; |
---|
| 171 | |
---|
| 172 | if ( chans > 512 ) |
---|
| 173 | chans = 512; |
---|
| 174 | |
---|
| 175 | if ( chans == 512 ) { |
---|
| 176 | bufptr = g_light_state.state; |
---|
| 177 | } else { |
---|
| 178 | memset(buf, 0, 512); |
---|
| 179 | memcpy(buf, g_light_state.state, chans); |
---|
| 180 | bufptr = buf; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | if ( stream_vio_s_write(ss, bufptr, 512) != 512 ) { |
---|
| 184 | streams_delete(id); |
---|
| 185 | return -1; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | s->pos = ROAR_MATH_OVERFLOW_ADD(s->pos, 1); |
---|
| 189 | |
---|
| 190 | return 0; |
---|
| 191 | } |
---|
| 192 | |
---|
[1821] | 193 | int light_send_stream (int id) { |
---|
[1822] | 194 | struct roar_stream * s; |
---|
| 195 | struct roar_stream_server * ss; |
---|
| 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: |
---|
[5917] | 206 | return light_send_stream_dmx512(id, s, ss); |
---|
[1822] | 207 | break; |
---|
[1968] | 208 | case ROAR_CODEC_ROARDMX: |
---|
[5917] | 209 | return cf_light_roardmx_write(id, ss); |
---|
[1968] | 210 | break; |
---|
[1822] | 211 | default: |
---|
| 212 | streams_delete(id); |
---|
| 213 | return -1; |
---|
| 214 | } |
---|
| 215 | |
---|
[1821] | 216 | return 0; |
---|
| 217 | } |
---|
| 218 | |
---|
[5706] | 219 | int light_dmxchannel_get(size_t index) { |
---|
| 220 | if ( (size_t)g_light_state.channels <= index ) { |
---|
| 221 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 222 | return -1; |
---|
| 223 | } |
---|
| 224 | |
---|
[5916] | 225 | return (int)(unsigned int)(uint8_t)g_light_state.state[index]; |
---|
[5706] | 226 | } |
---|
| 227 | |
---|
[5916] | 228 | int light_dmxchannel_set(size_t index, uint8_t val) { |
---|
[5706] | 229 | if ( (size_t)g_light_state.channels <= index ) { |
---|
| 230 | roar_err_set(ROAR_ERROR_NOENT); |
---|
| 231 | return -1; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | __set_channel(index, val); |
---|
| 235 | return 0; |
---|
| 236 | } |
---|
| 237 | |
---|
[2495] | 238 | #endif |
---|
| 239 | |
---|
[1816] | 240 | //ll |
---|