1 | //filter-slfi-channel2event.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2012-2014 |
---|
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 <roaraudio.h> |
---|
27 | #include <libroarlight/libroarlight.h> |
---|
28 | |
---|
29 | #define FLAG_BLACKOUT 0x0001 |
---|
30 | #define FLAG_FULLON 0x0002 |
---|
31 | |
---|
32 | struct slfi_channel2event { |
---|
33 | ssize_t channel; |
---|
34 | uint8_t lastvalue; |
---|
35 | uint8_t event; |
---|
36 | int flags; |
---|
37 | int emitting; |
---|
38 | }; |
---|
39 | |
---|
40 | static int __str2bool(const char * str) { |
---|
41 | if ( *str == 'y' || *str == 'j' || *str == '1' || *str == 't' ) |
---|
42 | return 1; |
---|
43 | return 0; |
---|
44 | } |
---|
45 | |
---|
46 | static int __init(struct roar_slfi_inst * inst, const struct roar_keyval * para, ssize_t paralen) { |
---|
47 | struct slfi_channel2event * self = roar_mm_malloc(sizeof(struct slfi_channel2event)); |
---|
48 | const struct roar_keyval * kv; |
---|
49 | ssize_t i; |
---|
50 | |
---|
51 | if ( self == NULL ) |
---|
52 | return -1; |
---|
53 | |
---|
54 | self->channel = -1; |
---|
55 | self->lastvalue = 0; |
---|
56 | self->event = ROAR_ROARDMX_EVENT_STEP; |
---|
57 | self->flags = FLAG_BLACKOUT|FLAG_FULLON; |
---|
58 | self->emitting = 0; |
---|
59 | inst->userdata = self; |
---|
60 | |
---|
61 | for (i = 0; i < paralen; i++) { |
---|
62 | kv = &(para[i]); |
---|
63 | if ( kv->key == NULL || kv->value == NULL ) |
---|
64 | continue; |
---|
65 | if ( !strcmp(kv->key, "channel") ) { |
---|
66 | self->channel = atoi(kv->value); |
---|
67 | if ( self->channel < 0 ) |
---|
68 | self->channel = 0; |
---|
69 | } else if ( !strcmp(kv->key, "event") ) { |
---|
70 | self->event = roar_roardmx_str2event(kv->value); |
---|
71 | } else if ( !strcmp(kv->key, "blackout") ) { |
---|
72 | self->flags |= FLAG_BLACKOUT; |
---|
73 | if ( !__str2bool(kv->value) ) |
---|
74 | self->flags -= FLAG_BLACKOUT; |
---|
75 | } else if ( !strcmp(kv->key, "fullon") ) { |
---|
76 | self->flags |= FLAG_BLACKOUT; |
---|
77 | if ( !__str2bool(kv->value) ) |
---|
78 | self->flags -= FLAG_BLACKOUT; |
---|
79 | } else { |
---|
80 | ROAR_WARN("__init(*): Unknown parameter: %s", kv->key); |
---|
81 | } |
---|
82 | } |
---|
83 | |
---|
84 | return 0; |
---|
85 | } |
---|
86 | |
---|
87 | static int __update(struct roar_slfi_inst * inst, uint8_t * universe, ssize_t size_of_universe, int32_t usecspassed, const uint8_t * event, size_t eventlen) { |
---|
88 | struct slfi_channel2event * self = inst->userdata; |
---|
89 | register uint8_t value; |
---|
90 | |
---|
91 | (void)usecspassed, (void)event, (void)eventlen; |
---|
92 | |
---|
93 | if ( self->channel >= size_of_universe ) { |
---|
94 | ROAR_WARN("__update(*): Universe too small for filter (source channel=%lu).", (unsigned long int)self->channel); |
---|
95 | roar_err_set(ROAR_ERROR_INVAL); |
---|
96 | return -1; |
---|
97 | } |
---|
98 | |
---|
99 | value = universe[self->channel]; |
---|
100 | if ( (self->flags & FLAG_BLACKOUT) && value == 0x00 ) { |
---|
101 | if ( self->emitting & FLAG_FULLON ) { |
---|
102 | roar_slfi_event_add(inst, ROAR_ROARDMX_EVENT_FULLON|ROAR_ROARDMX_ETYPE_OFF); |
---|
103 | self->emitting -= FLAG_FULLON; |
---|
104 | } |
---|
105 | if ( !(self->emitting & FLAG_BLACKOUT) ) { |
---|
106 | roar_slfi_event_add(inst, ROAR_ROARDMX_EVENT_BLACKOUT|ROAR_ROARDMX_ETYPE_ON); |
---|
107 | self->emitting |= FLAG_BLACKOUT; |
---|
108 | } |
---|
109 | } else if ( (self->flags & FLAG_FULLON) && value == 0xFF ) { |
---|
110 | if ( self->emitting & FLAG_BLACKOUT ) { |
---|
111 | roar_slfi_event_add(inst, ROAR_ROARDMX_EVENT_BLACKOUT|ROAR_ROARDMX_ETYPE_OFF); |
---|
112 | self->emitting -= FLAG_BLACKOUT; |
---|
113 | } |
---|
114 | if ( !(self->emitting & FLAG_FULLON) ) { |
---|
115 | roar_slfi_event_add(inst, ROAR_ROARDMX_EVENT_FULLON|ROAR_ROARDMX_ETYPE_ON); |
---|
116 | self->emitting |= FLAG_FULLON; |
---|
117 | } |
---|
118 | } else { |
---|
119 | if ( self->emitting & FLAG_BLACKOUT ) { |
---|
120 | roar_slfi_event_add(inst, ROAR_ROARDMX_EVENT_BLACKOUT|ROAR_ROARDMX_ETYPE_OFF); |
---|
121 | self->emitting -= FLAG_BLACKOUT; |
---|
122 | } |
---|
123 | |
---|
124 | if ( self->emitting & FLAG_FULLON ) { |
---|
125 | roar_slfi_event_add(inst, ROAR_ROARDMX_EVENT_FULLON|ROAR_ROARDMX_ETYPE_OFF); |
---|
126 | self->emitting -= FLAG_FULLON; |
---|
127 | } |
---|
128 | |
---|
129 | if ( value != self->lastvalue ) |
---|
130 | roar_slfi_event_add(inst, self->event); |
---|
131 | self->lastvalue = value; |
---|
132 | } |
---|
133 | |
---|
134 | return 0; |
---|
135 | } |
---|
136 | |
---|
137 | static const struct roar_slfi_filter filter[1] = { |
---|
138 | { |
---|
139 | .name = "channel2event", |
---|
140 | .description = "SLFI filter mapping channel data to events", |
---|
141 | .flags = ROAR_SLFI_FLAG_ON_UPDATE, |
---|
142 | .init = __init, |
---|
143 | .uninit = NULL, |
---|
144 | .update = __update, |
---|
145 | .ctl = NULL |
---|
146 | } |
---|
147 | }; |
---|
148 | |
---|
149 | ROAR_DL_PLUGIN_REG_SLFI(filter); |
---|
150 | |
---|
151 | // This is the plugin control block. |
---|
152 | ROAR_DL_PLUGIN_START(filter_slfi_channel2event) { |
---|
153 | // Here we set the name and vendor of our plugin. |
---|
154 | // If you have no Vendor ID you need to use ROAR_DL_PLUGIN_META_PRODUCT_NV(). |
---|
155 | ROAR_DL_PLUGIN_META_PRODUCT_NIV("filter-slfi-channel2event", ROAR_VID_ROARAUDIO, ROAR_VNAME_ROARAUDIO); |
---|
156 | |
---|
157 | // This sets the version of your plugin. |
---|
158 | ROAR_DL_PLUGIN_META_VERSION(ROAR_VERSION_STRING); |
---|
159 | |
---|
160 | // This sets the license of your plugin. |
---|
161 | // If there is no tag for the license you use you can just |
---|
162 | // use ROAR_DL_PLUGIN_META_LICENSE(). |
---|
163 | ROAR_DL_PLUGIN_META_LICENSE_TAG(GPLv3_0); |
---|
164 | |
---|
165 | // This sets the author and contact infos. |
---|
166 | // There are several other macros to do this with other parameters. |
---|
167 | // See ROAR_DL_PLUGIN_META_CONTACT*() in the header or documentation. |
---|
168 | ROAR_DL_PLUGIN_META_CONTACT_FLNE("Philipp", "Schafft", "ph3-der-loewe", "lion@lion.leolix.org"); |
---|
169 | |
---|
170 | // This sets the description for your plugin. |
---|
171 | ROAR_DL_PLUGIN_META_DESC("This plugin allows to map DMX channel data to RoarDMX events."); |
---|
172 | |
---|
173 | // Load filters. |
---|
174 | ROAR_DL_PLUGIN_REG_FNFUNC(ROAR_DL_FN_FILTER); |
---|
175 | |
---|
176 | // This is the end of the control block. |
---|
177 | } ROAR_DL_PLUGIN_END |
---|
178 | |
---|
179 | //ll |
---|