PX4 Firmware
PX4 Autopilot Software http://px4.io
cm8jl65_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018-2019 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 #include <px4_platform_common/cli.h>
35 #include <px4_platform_common/getopt.h>
36 
37 #include "CM8JL65.hpp"
38 
39 /**
40  * Local functions in support of the shell command.
41  */
42 namespace cm8jl65
43 {
44 
46 
47 int reset(const char *port);
48 int start(const char *port, const uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING);
49 int status();
50 int stop();
51 int usage();
52 
53 /**
54  * Reset the driver.
55  */
56 int
57 reset(const char *port)
58 {
59  if (stop() == PX4_OK) {
60  return start(port);
61  }
62 
63  return PX4_ERROR;
64 }
65 
66 /**
67  * Start the driver.
68  */
69 int
70 start(const char *port, const uint8_t rotation)
71 {
72  if (port == nullptr) {
73  PX4_ERR("invalid port");
74  return PX4_ERROR;
75  }
76 
77  if (g_dev != nullptr) {
78  PX4_INFO("already started");
79  return PX4_OK;
80  }
81 
82  // Instantiate the driver.
83  g_dev = new CM8JL65(port, rotation);
84 
85  if (g_dev == nullptr) {
86  PX4_ERR("object instantiate failed");
87  return PX4_ERROR;
88  }
89 
90  if (g_dev->init() != PX4_OK) {
91  PX4_ERR("driver start failed");
92  delete g_dev;
93  g_dev = nullptr;
94  return PX4_ERROR;
95  }
96 
97  return PX4_OK;
98 }
99 
100 /**
101  * Print the driver status.
102  */
103 int
105 {
106  if (g_dev == nullptr) {
107  PX4_ERR("driver not running");
108  return PX4_ERROR;
109  }
110 
111  g_dev->print_info();
112 
113  return PX4_OK;
114 }
115 
116 /**
117  * Stop the driver
118  */
119 int stop()
120 {
121  if (g_dev != nullptr) {
122  delete g_dev;
123  g_dev = nullptr;
124  }
125 
126  return PX4_ERROR;
127 }
128 
129 int
131 {
132  PX4_INFO("usage: cm8jl65 command [options]");
133  PX4_INFO("command:");
134  PX4_INFO("\treset|start|status|stop");
135  PX4_INFO("options:");
136  PX4_INFO("\t-R --rotation (%d)", distance_sensor_s::ROTATION_DOWNWARD_FACING);
137  PX4_INFO("\t-d --device_path");
138  return PX4_OK;
139 }
140 
141 } // namespace cm8jl65
142 
143 
144 /**
145  * Driver 'main' command.
146  */
147 extern "C" __EXPORT int cm8jl65_main(int argc, char *argv[])
148 {
149  uint8_t rotation = distance_sensor_s::ROTATION_DOWNWARD_FACING;
150  const char *device_path = nullptr;
151  int ch;
152  int myoptind = 1;
153  const char *myoptarg = nullptr;
154 
155  while ((ch = px4_getopt(argc, argv, "R:d:", &myoptind, &myoptarg)) != EOF) {
156  switch (ch) {
157  case 'R': {
158  int rot = -1;
159 
160  if (px4_get_parameter_value(myoptarg, rot) != 0) {
161  PX4_ERR("rotation parsing failed");
162  return -1;
163  }
164 
165  rotation = (uint8_t)rot;
166  break;
167  }
168 
169  case 'd':
170  device_path = myoptarg;
171  break;
172 
173  default:
174  PX4_WARN("Unknown option!");
175  return cm8jl65::usage();
176  }
177  }
178 
179  if (myoptind >= argc) {
180  return cm8jl65::usage();
181  }
182 
183  // Reset the driver.
184  if (!strcmp(argv[myoptind], "reset")) {
185  return cm8jl65::reset(device_path);
186  }
187 
188  // Start/load the driver.
189  if (!strcmp(argv[myoptind], "start")) {
190  return cm8jl65::start(device_path, rotation);
191  }
192 
193  // Print driver information.
194  if (!strcmp(argv[myoptind], "status")) {
195  return cm8jl65::status();
196  }
197 
198  // Stop the driver
199  if (!strcmp(argv[myoptind], "stop")) {
200  return cm8jl65::stop();
201  }
202 
203  return cm8jl65::usage();
204 }
int init()
Method : init() This method initializes the general driver for a range finder sensor.
Definition: CM8JL65.cpp:254
Definition: I2C.hpp:51
CM8JL65 * g_dev
int status()
Print the driver status.
__EXPORT int cm8jl65_main(int argc, char *argv[])
Driver &#39;main&#39; command.
void print_info()
Diagnostics - print some basic information about the driver.
Definition: CM8JL65.cpp:324
int reset(const char *port)
Reset the driver.
int stop()
Stop the driver.
int usage()
Prints info about the driver argument usage.
Local functions in support of the shell command.
int start(const char *port, const uint8_t rotation=distance_sensor_s::ROTATION_DOWNWARD_FACING)
Start the driver.