2012-08-05 06:27:02 -03:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (C) 2012 PX4 Development Team. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
/**
|
2012-08-05 06:27:02 -03:00
|
|
|
* @file mixer.c
|
|
|
|
*
|
|
|
|
* Generic control value mixing library.
|
2012-08-04 19:12:36 -03:00
|
|
|
*
|
|
|
|
* This library implements a generic mixer function that can be used
|
|
|
|
* by any driver or subsytem that wants to combine several control signals
|
|
|
|
* into a single output.
|
|
|
|
*
|
|
|
|
* See mixer.h for more details.
|
|
|
|
*/
|
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
#include "mixer.h"
|
|
|
|
|
|
|
|
static int
|
2012-08-05 17:43:16 -03:00
|
|
|
scale_check(struct scaler_s *scale)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
2012-08-05 17:43:16 -03:00
|
|
|
if (scale->offset > 1.1f)
|
2012-08-05 06:09:11 -03:00
|
|
|
return 1;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
if (scale->offset < -1.1f)
|
2012-08-05 06:09:11 -03:00
|
|
|
return 2;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
|
|
|
if (scale->lower_limit > scale->upper_limit)
|
2012-08-05 06:09:11 -03:00
|
|
|
return 3;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
if (scale->lower_limit < -1.1f)
|
2012-08-05 06:09:11 -03:00
|
|
|
return 4;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
if (scale->upper_limit > 1.1f)
|
2012-08-05 06:09:11 -03:00
|
|
|
return 5;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
|
|
|
return 0;
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-05 17:43:16 -03:00
|
|
|
mixer_check(struct mixer_s *mixer, unsigned group_count, unsigned control_count)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
2012-08-05 06:09:11 -03:00
|
|
|
int ret;
|
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
/* sanity that presumes that a mixer includes a control no more than once */
|
|
|
|
if (mixer->control_count > (group_count * control_count))
|
2012-08-05 06:09:11 -03:00
|
|
|
return -2;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
/* validate the output scaler */
|
2012-08-05 06:09:11 -03:00
|
|
|
ret = scale_check(&mixer->output_scaler);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2012-08-04 19:12:36 -03:00
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
/* validate input scalers */
|
2012-08-04 19:12:36 -03:00
|
|
|
for (unsigned i = 0; i < mixer->control_count; i++) {
|
2012-08-05 17:43:16 -03:00
|
|
|
|
|
|
|
/* range-check input controls */
|
|
|
|
if (mixer->control_scaler[i].control_group >= group_count)
|
|
|
|
return -3;
|
|
|
|
|
|
|
|
if (mixer->control_scaler[i].control_index >= control_count)
|
2012-08-05 06:09:11 -03:00
|
|
|
return -3;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
/* validate the scaler */
|
2012-08-05 06:09:11 -03:00
|
|
|
ret = scale_check(&mixer->control_scaler[i]);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (ret != 0)
|
|
|
|
return (10 * i + ret);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
void
|
|
|
|
mixer_requires(struct mixer_s *mixer, uint32_t *groups)
|
|
|
|
{
|
|
|
|
for (unsigned i = 0; i < mixer->control_count; i++)
|
|
|
|
*groups |= 1 << mixer->control_scaler[i].control_group;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply a scaler to a value.
|
|
|
|
*
|
|
|
|
* @param scaler The applied scaler.
|
|
|
|
* @param input The value to scale.
|
|
|
|
* @output The scaled value.
|
|
|
|
*/
|
2012-08-04 19:12:36 -03:00
|
|
|
static float
|
2012-08-05 17:43:16 -03:00
|
|
|
scale(struct scaler_s *scaler, float input)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
float output;
|
|
|
|
|
|
|
|
if (input < 0.0f) {
|
|
|
|
output = (input * scaler->negative_scale) + scaler->offset;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else {
|
|
|
|
output = (input * scaler->positive_scale) + scaler->offset;
|
|
|
|
}
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
if (output > scaler->upper_limit) {
|
|
|
|
output = scaler->upper_limit;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-04 19:12:36 -03:00
|
|
|
} else if (output < scaler->lower_limit) {
|
|
|
|
output = scaler->lower_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
float
|
2012-08-05 17:43:16 -03:00
|
|
|
mixer_mix(struct mixer_s *mixer, float **controls)
|
2012-08-04 19:12:36 -03:00
|
|
|
{
|
|
|
|
float sum = 0.0f;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < mixer->control_count; i++) {
|
2012-08-05 17:43:16 -03:00
|
|
|
|
|
|
|
struct scaler_s *scaler = &mixer->control_scaler[i];
|
|
|
|
float *cg = controls[scaler->control_group];
|
|
|
|
|
|
|
|
sum += scale(scaler, cg[scaler->control_index]);
|
2012-08-04 19:12:36 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return scale(&mixer->output_scaler, sum);
|
|
|
|
}
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
/**
|
|
|
|
* Effectively fdgets()
|
|
|
|
*/
|
2012-08-05 00:05:47 -03:00
|
|
|
static int
|
|
|
|
mixer_getline(int fd, char *line, unsigned maxlen)
|
|
|
|
{
|
2012-08-05 20:30:28 -03:00
|
|
|
/* reduce line budget by 1 to account for terminal NUL */
|
|
|
|
maxlen--;
|
|
|
|
|
|
|
|
/* loop looking for a non-comment line */
|
|
|
|
for (;;) {
|
|
|
|
int ret;
|
|
|
|
char c;
|
|
|
|
char *p = line;
|
|
|
|
|
|
|
|
/* loop reading characters for this line */
|
|
|
|
for (;;) {
|
|
|
|
ret = read(fd, &c, 1);
|
|
|
|
|
|
|
|
/* on error or EOF, return same */
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
/* ignore carriage returns */
|
|
|
|
if (c == '\r')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* line termination */
|
|
|
|
if (c == '\n') {
|
|
|
|
/* ignore malformed lines */
|
|
|
|
if (line[1] != ':')
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* terminate line as string and return */
|
|
|
|
*p = '\0';
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if we have space, accumulate the byte and go on */
|
|
|
|
if ((p - line) < maxlen)
|
|
|
|
*p++ = c;
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-08-05 17:43:16 -03:00
|
|
|
mixer_load_scaler(const char *buf, struct scaler_s *scaler)
|
2012-08-05 00:05:47 -03:00
|
|
|
{
|
2012-08-05 17:43:16 -03:00
|
|
|
unsigned u[2];
|
|
|
|
int s[5];
|
|
|
|
|
|
|
|
if (sscanf(buf, "S: %u %u %d %d %d %d %d",
|
|
|
|
&u[0], &u[1], &s[0], &s[1], &s[2], &s[3], &s[4]) != 7)
|
2012-08-05 00:05:47 -03:00
|
|
|
return -1;
|
|
|
|
|
2012-08-05 17:43:16 -03:00
|
|
|
scaler->control_group = u[0];
|
|
|
|
scaler->control_index = u[1];
|
|
|
|
scaler->negative_scale = s[0] / 10000.0f;
|
|
|
|
scaler->positive_scale = s[1] / 10000.0f;
|
|
|
|
scaler->offset = s[2] / 10000.0f;
|
|
|
|
scaler->lower_limit = s[3] / 10000.0f;
|
|
|
|
scaler->upper_limit = s[4] / 10000.0f;
|
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-05 17:43:16 -03:00
|
|
|
mixer_load(int fd, struct mixer_s **mp)
|
2012-08-05 00:05:47 -03:00
|
|
|
{
|
|
|
|
int ret, result = -1;
|
2012-08-05 17:43:16 -03:00
|
|
|
struct mixer_s *mixer = NULL;
|
2012-08-05 20:30:28 -03:00
|
|
|
char buf[60];
|
2012-08-05 00:05:47 -03:00
|
|
|
unsigned scalers;
|
|
|
|
|
|
|
|
ret = mixer_getline(fd, buf, sizeof(buf));
|
|
|
|
|
|
|
|
/* end of file? */
|
|
|
|
if (ret == 0)
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
/* can't proceed */
|
|
|
|
if (ret < 1)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* get header */
|
|
|
|
if (sscanf(buf, "M: %u", &scalers) != 1)
|
|
|
|
goto out;
|
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
/* if there are scalers, load them */
|
|
|
|
if (scalers > 0) {
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
/* allocate mixer */
|
|
|
|
scalers--;
|
2012-08-05 17:43:16 -03:00
|
|
|
mixer = (struct mixer_s *)malloc(MIXER_SIZE(scalers));
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (mixer == NULL)
|
|
|
|
goto out;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
mixer->control_count = scalers;
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
ret = mixer_getline(fd, buf, sizeof(buf));
|
2012-08-05 00:05:47 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (ret < 1)
|
2012-08-05 00:05:47 -03:00
|
|
|
goto out;
|
2012-08-05 06:09:11 -03:00
|
|
|
|
|
|
|
if (mixer_load_scaler(buf, &mixer->output_scaler))
|
2012-08-05 00:05:47 -03:00
|
|
|
goto out;
|
2012-08-05 06:09:11 -03:00
|
|
|
|
|
|
|
for (unsigned i = 0; i < scalers; i++) {
|
|
|
|
ret = mixer_getline(fd, buf, sizeof(buf));
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (ret < 1)
|
|
|
|
goto out;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (mixer_load_scaler(buf, &mixer->control_scaler[i]))
|
|
|
|
goto out;
|
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
|
|
|
} else {
|
|
|
|
/* we return NULL for the mixer, which is interpreted elsewhere as "no mixer" */
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = 1;
|
|
|
|
|
|
|
|
out:
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
/* on error, discard allocated mixer */
|
|
|
|
if ((result <= 0) && (mixer != NULL))
|
|
|
|
free(mixer);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
*mp = mixer;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2012-08-05 17:43:16 -03:00
|
|
|
mixer_save_scaler(char *buf, struct scaler_s *scaler)
|
2012-08-05 00:05:47 -03:00
|
|
|
{
|
2012-08-05 17:43:16 -03:00
|
|
|
int s[5];
|
|
|
|
|
|
|
|
s[0] = 10000.0f * scaler->negative_scale;
|
|
|
|
s[1] = 10000.0f * scaler->positive_scale;
|
|
|
|
s[2] = 10000.0f * scaler->offset;
|
|
|
|
s[3] = 10000.0f * scaler->lower_limit;
|
|
|
|
s[4] = 10000.0f * scaler->upper_limit;
|
|
|
|
|
|
|
|
return sprintf(buf, "S: %u %u %d %d %d %d %d\n",
|
|
|
|
scaler->control_group, scaler->control_index,
|
|
|
|
s[0], s[1], s[2], s[3], s[4]);
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-05 17:43:16 -03:00
|
|
|
mixer_save(int fd, struct mixer_s *mixer)
|
2012-08-05 00:05:47 -03:00
|
|
|
{
|
|
|
|
char buf[100];
|
|
|
|
int len, ret;
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
/* write the mixer header */
|
2012-08-05 06:09:11 -03:00
|
|
|
len = sprintf(buf, "M: %u\n", (mixer != NULL) ? mixer->control_count : 0);
|
2012-08-05 00:05:47 -03:00
|
|
|
ret = write(fd, buf, len);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
if (ret != len)
|
|
|
|
return -1;
|
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (mixer != NULL) {
|
|
|
|
/* write the output scaler */
|
|
|
|
len = mixer_save_scaler(buf, &mixer->output_scaler);
|
2012-08-05 00:05:47 -03:00
|
|
|
write(fd, buf, len);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
if (ret != len)
|
|
|
|
return -1;
|
2012-08-05 06:09:11 -03:00
|
|
|
|
|
|
|
/* write the control scalers */
|
|
|
|
for (unsigned j = 0; j < mixer->control_count; j++) {
|
|
|
|
len = mixer_save_scaler(buf, &mixer->control_scaler[j]);
|
|
|
|
write(fd, buf, len);
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 06:09:11 -03:00
|
|
|
if (ret != len)
|
|
|
|
return -1;
|
|
|
|
}
|
2012-08-05 00:05:47 -03:00
|
|
|
}
|
2012-08-05 17:43:16 -03:00
|
|
|
|
2012-08-05 00:05:47 -03:00
|
|
|
return 0;
|
2012-08-05 06:09:11 -03:00
|
|
|
}
|