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  * Mixer utility.
38  */
39 
40 #include <px4_platform_common/px4_config.h>
41 #include <px4_platform_common/module.h>
42 #include <px4_platform_common/posix.h>
43 #include <string.h>
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <fcntl.h>
48 #include <errno.h>
49 #include <ctype.h>
50 
51 #include <drivers/drv_mixer.h>
52 #include <lib/mixer/MixerGroup.hpp>
53 #include <lib/mixer/mixer_load.h>
55 
56 /**
57  * Mixer utility for loading mixer files to devices
58  *
59  * @ingroup apps
60  */
61 extern "C" __EXPORT int mixer_main(int argc, char *argv[]);
62 
63 static void usage(const char *reason);
64 static int load(const char *devname, const char *fname, bool append);
65 
66 int
67 mixer_main(int argc, char *argv[])
68 {
69  if (argc < 2) {
70  usage("missing command");
71  return 1;
72  }
73 
74  if (!strcmp(argv[1], "load")) {
75  if (argc < 4) {
76  usage("missing device or filename");
77  return 1;
78  }
79 
80  int ret = load(argv[2], argv[3], false);
81 
82  if (ret != 0) {
83  PX4_ERR("failed to load mixer");
84  return 1;
85  }
86 
87  } else if (!strcmp(argv[1], "append")) {
88  if (argc < 4) {
89  usage("missing device or filename");
90  return 1;
91  }
92 
93  int ret = load(argv[2], argv[3], true);
94 
95  if (ret != 0) {
96  PX4_ERR("failed to append mixer");
97  return 1;
98  }
99 
100  } else {
101  usage("Unknown command");
102  return 1;
103  }
104 
105  return 0;
106 }
107 
108 static void
109 usage(const char *reason)
110 {
111  if (reason && *reason) {
112  PX4_INFO("%s", reason);
113  }
114 
115  PRINT_MODULE_DESCRIPTION(
116  R"DESCR_STR(
117 ### Description
118 Load or append mixer files to the ESC driver.
119 
120 Note that the driver must support the used ioctl's, which is the case on NuttX, but for example not on RPi.
121 )DESCR_STR");
122 
123 
124  PRINT_MODULE_USAGE_NAME("mixer", "command");
125 
126  PRINT_MODULE_USAGE_COMMAND("load");
127  PRINT_MODULE_USAGE_ARG("<file:dev> <file>", "Output device (eg. /dev/pwm_output0) and mixer file", false);
128  PRINT_MODULE_USAGE_COMMAND("append");
129  PRINT_MODULE_USAGE_ARG("<file:dev> <file>", "Output device (eg. /dev/pwm_output0) and mixer file", false);
130 }
131 
132 static int
133 load(const char *devname, const char *fname, bool append)
134 {
135  // sleep a while to ensure device has been set up
136  px4_usleep(20000);
137 
138  int dev;
139 
140  /* open the device */
141  if ((dev = px4_open(devname, 0)) < 0) {
142  PX4_ERR("can't open %s\n", devname);
143  return 1;
144  }
145 
146  /* reset mixers on the device, but not if appending */
147  if (!append) {
148  if (px4_ioctl(dev, MIXERIOCRESET, 0)) {
149  PX4_ERR("can't reset mixers on %s", devname);
150  return 1;
151  }
152  }
153 
154  char buf[2048];
155 
156  if (load_mixer_file(fname, &buf[0], sizeof(buf)) < 0) {
157  PX4_ERR("can't load mixer file: %s", fname);
158  return 1;
159  }
160 
161  /* Pass the buffer to the device */
162  int ret = px4_ioctl(dev, MIXERIOCLOADBUF, (unsigned long)buf);
163 
164  if (ret < 0) {
165  PX4_ERR("failed to load mixers from %s", fname);
166  return 1;
167  }
168 
169  return 0;
170 }
#define MIXERIOCLOADBUF
Add mixer(s) from the buffer in (const char *)arg.
Definition: drv_mixer.h:79
__EXPORT int mixer_main(int argc, char *argv[])
Mixer utility for loading mixer files to devices.
Definition: mixer.cpp:67
Definition: I2C.hpp:51
Mixer ioctl interfaces.
int load_mixer_file(const char *fname, char *buf, unsigned maxlen)
#define MIXERIOCRESET
reset (clear) the mixer configuration
Definition: drv_mixer.h:71
int px4_open(const char *path, int flags,...)
static void usage(const char *reason)
Definition: mixer.cpp:109
static int load(const char *devname, const char *fname, bool append)
Definition: mixer.cpp:133
int px4_ioctl(int fd, int cmd, unsigned long arg)