PX4 Firmware
PX4 Autopilot Software http://px4.io
Mixer.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.cpp
36  *
37  * Programmable multi-channel mixer library.
38  */
39 
40 #include "Mixer.hpp"
41 
42 #include <math.h>
43 #include <cstring>
44 #include <ctype.h>
45 
46 #define debug(fmt, args...) do { } while(0)
47 //#define debug(fmt, args...) do { printf("[mixer] " fmt "\n", ##args); } while(0)
48 
49 float
50 Mixer::get_control(uint8_t group, uint8_t index)
51 {
52  float value;
53 
54  _control_cb(_cb_handle, group, index, value);
55 
56  return value;
57 }
58 
59 const char *
60 Mixer::findtag(const char *buf, unsigned &buflen, char tag)
61 {
62  while (buflen >= 2) {
63  if ((buf[0] == tag) && (buf[1] == ':')) {
64  return buf;
65  }
66 
67  buf++;
68  buflen--;
69  }
70 
71  return nullptr;
72 }
73 
74 char
75 Mixer::findnexttag(const char *buf, unsigned buflen)
76 {
77  while (buflen >= 2) {
78  if (isupper(buf[0]) && buf[1] == ':') {
79  return buf[0];
80  }
81 
82  buf++;
83  buflen--;
84  }
85 
86  return 0;
87 }
88 
89 const char *
90 Mixer::skipline(const char *buf, unsigned &buflen)
91 {
92  const char *p;
93 
94  /* if we can find a CR or NL in the buffer, skip up to it */
95  if ((p = (const char *)memchr(buf, '\r', buflen)) || (p = (const char *)memchr(buf, '\n', buflen))) {
96  /* skip up to it AND one beyond - could be on the NUL symbol now */
97  buflen -= (p - buf) + 1;
98  return p + 1;
99  }
100 
101  return nullptr;
102 }
103 
104 bool
105 Mixer::string_well_formed(const char *buf, unsigned &buflen)
106 {
107  /* enforce that the mixer ends with a new line */
108  for (int i = buflen - 1; i >= 0; i--) {
109  if (buf[i] == '\0') {
110  continue;
111  }
112 
113  /* require a space or newline at the end of the buffer, fail on printable chars */
114  if (buf[i] == '\n' || buf[i] == '\r') {
115  /* found a line ending, so no split symbols / numbers. good. */
116  return true;
117  }
118 
119  }
120 
121  debug("pre-parser rejected: No newline in buf");
122 
123  return false;
124 }
static char findnexttag(const char *buf, unsigned buflen)
Find next tag and return it (0 is returned if no tag is found)
Definition: Mixer.cpp:75
static const char * skipline(const char *buf, unsigned &buflen)
Skip a line.
Definition: Mixer.cpp:90
ControlCallback _control_cb
client-supplied callback used when fetching control values
Definition: Mixer.hpp:235
Generic, programmable, procedural control signal mixers.
static bool string_well_formed(const char *buf, unsigned &buflen)
Check wether the string is well formed and suitable for parsing.
Definition: Mixer.cpp:105
uintptr_t _cb_handle
Definition: Mixer.hpp:236
float get_control(uint8_t group, uint8_t index)
Invoke the client callback to fetch a control value.
Definition: Mixer.cpp:50
static const char * findtag(const char *buf, unsigned &buflen, char tag)
Find a tag.
Definition: Mixer.cpp:60
#define debug(fmt, args...)
Definition: Mixer.cpp:46