[5917] | 1 | //codecfilter_roardmx.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
[5961] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2014 |
---|
[5917] | 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, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
| 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "roard.h" |
---|
| 27 | |
---|
| 28 | #ifndef ROAR_WITHOUT_DCOMP_LIGHT |
---|
| 29 | |
---|
[5927] | 30 | static inline int __read_sset(int id, struct roar_stream_server * ss, struct roar_roardmx_message * mes) { |
---|
[5917] | 31 | uint16_t channel; |
---|
| 32 | uint8_t value; |
---|
[5927] | 33 | int i, c; |
---|
[5917] | 34 | |
---|
| 35 | // we ignore errors here at the moment as 0 not < -1 |
---|
[5927] | 36 | c = roar_roardmx_message_numchannels(mes); |
---|
[5917] | 37 | ROAR_DBG("light_check_stream(id=%i): Number of subframes: %u", id, c); |
---|
| 38 | |
---|
| 39 | for (i = 0; i < c; i++) { |
---|
[5927] | 40 | if ( roar_roardmx_message_get_chanval(mes, &channel, &value, i) == -1 ) |
---|
[5917] | 41 | return -1; |
---|
| 42 | |
---|
| 43 | if ( g_light_state.channels < channel ) { |
---|
| 44 | ROAR_WARN("light_check_stream(id=%i): Writing on non extisting DMX channel %u", id, channel); |
---|
| 45 | continue; |
---|
| 46 | } else { |
---|
| 47 | g_light_state.state[channel] = value; |
---|
| 48 | g_light_state.changes[channel] = 0xFF; // the channel changed |
---|
| 49 | } |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | return 0; |
---|
| 53 | } |
---|
| 54 | |
---|
[5977] | 55 | static inline int __read_rangeset(int id, struct roar_stream_server * ss, struct roar_roardmx_message * mes) { |
---|
| 56 | uint16_t start, end; |
---|
| 57 | uint8_t value; |
---|
| 58 | size_t p; |
---|
| 59 | int i, c; |
---|
| 60 | |
---|
| 61 | // we ignore errors here at the moment as 0 not < -1 |
---|
| 62 | c = roar_roardmx_message_numchannels(mes); |
---|
| 63 | ROAR_DBG("__read_rangeset(id=%i, ...): Number of subframes: %u", id, c); |
---|
| 64 | |
---|
| 65 | for (i = 0; i < c; i++) { |
---|
| 66 | ROAR_DBG("__read_rangeset(id=%i, ...): i=%i", id, i); |
---|
| 67 | |
---|
| 68 | if ( roar_roardmx_message_get_rangeval(mes, &start, &end, &value, i) == -1 ) { |
---|
| 69 | ROAR_WARN("__read_rangeset(id=%i, ...): Can not read subframe: %i: %s", id, i, roar_errorstring); |
---|
| 70 | return -1; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | ROAR_DBG("__read_rangeset(id=%i, ...): i=%i, start=%u, end=%u, value=%u", id, i, start, end, value); |
---|
| 74 | |
---|
| 75 | if ( start > end ) { |
---|
| 76 | ROAR_WARN("__read_rangeset(id=%i, ...): Bad RoarDMX Rangeset frame: start > end.", id); |
---|
| 77 | continue; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | if ( g_light_state.channels < end ) { |
---|
| 81 | ROAR_WARN("__read_rangeset(id=%i, ...): Range out of DMX universe: %u-%u", id, start, end); |
---|
| 82 | continue; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | for (p = start; p <= end; p++) { |
---|
| 86 | g_light_state.state[p] = value; |
---|
| 87 | g_light_state.changes[p] = 0xFF; // the channel changed |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return 0; |
---|
| 92 | } |
---|
| 93 | |
---|
[5927] | 94 | static inline int __read_events(int id, struct roar_stream_server * ss, struct roar_roardmx_message * mes) { |
---|
| 95 | const uint8_t * events; |
---|
| 96 | size_t len; |
---|
| 97 | |
---|
| 98 | (void)id, (void)ss; |
---|
| 99 | |
---|
| 100 | if ( roar_roardmx_message_get_events(mes, &events, &len) == -1 ) |
---|
| 101 | return -1; |
---|
| 102 | |
---|
| 103 | return light_dmxevent_add(events, len); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | int cf_light_roardmx_read(int id, struct roar_stream_server * ss) { |
---|
| 107 | struct roar_roardmx_message mes; |
---|
| 108 | unsigned char type; |
---|
| 109 | |
---|
| 110 | if ( roar_roardmx_message_recv(&mes, &(ss->vio)) == -1 ) |
---|
| 111 | return -1; |
---|
| 112 | |
---|
| 113 | if ( roar_roardmx_message_get_type(&mes, &type) == -1 ) |
---|
| 114 | return -1; |
---|
| 115 | |
---|
| 116 | switch (type) { |
---|
| 117 | case ROAR_ROARDMX_TYPE_SSET: return __read_sset(id, ss, &mes); break; |
---|
[5977] | 118 | case ROAR_ROARDMX_TYPE_RANGESET: return __read_rangeset(id, ss, &mes); break; |
---|
[5927] | 119 | case ROAR_ROARDMX_TYPE_EVENT: return __read_events(id, ss, &mes); break; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | roar_err_set(ROAR_ERROR_NSTYPE); |
---|
| 123 | return -1; |
---|
| 124 | } |
---|
| 125 | |
---|
| 126 | static inline int __write_events(int id, struct roar_stream_server * ss) { |
---|
| 127 | struct roar_roardmx_message mes; |
---|
| 128 | const uint8_t * events; |
---|
| 129 | size_t len; |
---|
| 130 | |
---|
| 131 | if ( light_dmxevent_read(&events, &len) == -1 ) |
---|
| 132 | return -1; |
---|
| 133 | |
---|
| 134 | // check if we need to send events at all: |
---|
| 135 | if ( len == 0 ) |
---|
| 136 | return 0; |
---|
| 137 | |
---|
| 138 | // ok, now let's build the message: |
---|
| 139 | if ( roar_roardmx_message_new_event(&mes) == -1 ) |
---|
| 140 | return -1; |
---|
| 141 | |
---|
| 142 | if ( roar_roardmx_message_add_events(&mes, events, len) == -1 ) |
---|
| 143 | return -1; |
---|
| 144 | |
---|
| 145 | // and send it: |
---|
| 146 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
| 147 | return -1; |
---|
| 148 | |
---|
| 149 | return 0; |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | static inline int __write_channels(int id, struct roar_stream_server * ss) { |
---|
[5917] | 153 | struct roar_roardmx_message mes; |
---|
| 154 | int have_message = 0; |
---|
| 155 | int i; |
---|
| 156 | |
---|
| 157 | for (i = 0; i < g_light_state.channels; i++) { |
---|
[5980] | 158 | if ( g_light_state.outputchanges[i] ) { |
---|
[5917] | 159 | if ( !have_message ) |
---|
| 160 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
| 161 | return -1; |
---|
| 162 | |
---|
| 163 | have_message = 2; |
---|
| 164 | |
---|
[5980] | 165 | if ( roar_roardmx_message_add_chanval(&mes, i, g_light_state.output[i]) == -1 ) { |
---|
[5917] | 166 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
| 167 | return -1; |
---|
| 168 | |
---|
| 169 | if ( roar_roardmx_message_new_sset(&mes) == -1 ) |
---|
| 170 | return -1; |
---|
| 171 | |
---|
| 172 | have_message = 1; |
---|
| 173 | } |
---|
| 174 | } |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | if ( have_message == 2 ) |
---|
| 178 | if ( roar_roardmx_message_send(&mes, &(ss->vio)) == -1 ) |
---|
| 179 | return -1; |
---|
| 180 | |
---|
| 181 | return 0; |
---|
| 182 | } |
---|
| 183 | |
---|
[5927] | 184 | int cf_light_roardmx_write(int id, struct roar_stream_server * ss) { |
---|
| 185 | if ( __write_events(id, ss) == -1 ) |
---|
| 186 | return -1; |
---|
| 187 | |
---|
| 188 | if ( __write_channels(id, ss) == -1 ) |
---|
| 189 | return -1; |
---|
| 190 | |
---|
| 191 | return 0; |
---|
| 192 | } |
---|
| 193 | |
---|
[5917] | 194 | #endif |
---|
| 195 | |
---|
| 196 | //ll |
---|