PX4 Firmware
PX4 Autopilot Software http://px4.io
load_mixer_file.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2012 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file mixer_load.c
36  *
37  * Programmable multi-channel mixer library.
38  */
39 
40 #include <string.h>
41 #include <stdio.h>
42 #include <ctype.h>
43 
44 #include "mixer_load.h"
45 
46 int load_mixer_file(const char *fname, char *buf, unsigned maxlen)
47 {
48  FILE *fp;
49  char line[120];
50 
51  /* open the mixer definition file */
52  fp = fopen(fname, "r");
53 
54  if (fp == nullptr) {
55  printf("file not found\n");
56  return -1;
57  }
58 
59  /* read valid lines from the file into a buffer */
60  buf[0] = '\0';
61 
62  for (;;) {
63 
64  /* get a line, bail on error/EOF */
65  line[0] = '\0';
66 
67  if (fgets(line, sizeof(line), fp) == nullptr) {
68  break;
69  }
70 
71  /* if the line doesn't look like a mixer definition line, skip it */
72  if ((strlen(line) < 2) || !isupper(line[0]) || (line[1] != ':')) {
73  continue;
74  }
75 
76  /* compact whitespace in the buffer */
77  char *t, *f;
78 
79  for (f = line; *f != '\0'; f++) {
80  /* scan for space characters */
81  if (*f == ' ') {
82  /* look for additional spaces */
83  t = f + 1;
84 
85  while (*t == ' ') {
86  t++;
87  }
88 
89  if (*t == '\0') {
90  /* strip trailing whitespace */
91  *f = '\0';
92 
93  } else if (t > (f + 1)) {
94  memmove(f + 1, t, strlen(t) + 1);
95  }
96  }
97  }
98 
99  /* if the line is too long to fit in the buffer, bail */
100  if ((strlen(line) + strlen(buf) + 1) >= maxlen) {
101  printf("line too long\n");
102  fclose(fp);
103  return -1;
104  }
105 
106  /* add the line to the buffer */
107  strcat(buf, line);
108  }
109 
110  fclose(fp);
111  return 0;
112 }
Vector< float, 6 > f(float t, const Matrix< float, 6, 1 > &, const Matrix< float, 3, 1 > &)
Definition: integration.cpp:8
int load_mixer_file(const char *fname, char *buf, unsigned maxlen)