1
0
Fork 0
mirror of https://git.rwth-aachen.de/acs/public/villas/node/ synced 2025-03-09 00:00:00 +01:00
VILLASnode/lib/hooks/scale.c

103 lines
2.4 KiB
C
Raw Permalink Normal View History

2018-06-16 20:51:31 +02:00
/** Scale hook.
*
* @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
* @copyright 2018, Institute for Automation of Complex Power Systems, EONERC
* @license GNU General Public License (version 3)
*
* VILLASnode
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************************/
/** @addtogroup hooks Hook functions
* @{
*/
#include <string.h>
#include <villas/hook.h>
#include <villas/plugin.h>
#include <villas/sample.h>
struct scale {
double scale;
double offset;
};
static int scale_init(struct hook *h)
{
struct scale *p = (struct scale *) h->_vd;
p->scale = 1;
p->offset = 0;
return 0;
}
static int scale_parse(struct hook *h, json_t *cfg)
{
struct scale *p = (struct scale *) h->_vd;
int ret;
json_error_t err;
ret = json_unpack_ex(cfg, &err, 0, "{ s?: F, s?: F }",
"scale", &p->scale,
"offset", &p->offset
);
if (ret)
jerror(&err, "Failed to parse configuration of hook '%s'", plugin_name(h->_vt));
return 0;
}
static int scale_process(struct hook *h, struct sample *smps[], unsigned *cnt)
{
struct scale *p = (struct scale *) h->_vd;
for (int i = 0; i < *cnt; i++) {
for (int k = 0; k < smps[i]->length; k++) {
switch (sample_get_data_format(smps[i], k)) {
case SAMPLE_DATA_FORMAT_INT:
smps[i]->data[k].i = smps[i]->data[k].i * p->scale + p->offset;
break;
case SAMPLE_DATA_FORMAT_FLOAT:
smps[i]->data[k].f = smps[i]->data[k].f * p->scale + p->offset;
break;
}
}
}
return 0;
}
static struct plugin p = {
.name = "scale",
.description = "Scale all signals by and add offset",
.type = PLUGIN_TYPE_HOOK,
.hook = {
.flags = HOOK_PATH,
.priority = 99,
.init = scale_init,
.parse = scale_parse,
.process= scale_process,
.size = sizeof(struct scale)
}
};
REGISTER_PLUGIN(&p)
/** @} */