PX4 Firmware
PX4 Autopilot Software http://px4.io
pmw3901_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 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 "PMW3901.hpp"
35 
36 /*
37  * Driver 'main' command.
38  */
39 extern "C" __EXPORT int pmw3901_main(int argc, char *argv[]);
40 
41 /**
42  * Local functions in support of the shell command.
43  */
44 namespace pmw3901
45 {
46 
48 
49 void start(int spi_bus);
50 void stop();
51 void test();
52 void reset();
53 void info();
54 void usage();
55 
56 /**
57  * Start the driver.
58  */
59 void
60 start(int spi_bus)
61 {
62  if (g_dev != nullptr) {
63  errx(1, "already started");
64  }
65 
66  /* create the driver */
67  g_dev = new PMW3901(spi_bus, (enum Rotation)0);
68 
69  if (g_dev == nullptr) {
70  goto fail;
71  }
72 
73  if (OK != g_dev->init()) {
74  goto fail;
75  }
76 
77  exit(0);
78 
79 fail:
80 
81  if (g_dev != nullptr) {
82  delete g_dev;
83  g_dev = nullptr;
84  }
85 
86  errx(1, "driver start failed");
87 }
88 
89 /**
90  * Stop the driver
91  */
92 void stop()
93 {
94  if (g_dev != nullptr) {
95  delete g_dev;
96  g_dev = nullptr;
97 
98  } else {
99  errx(1, "driver not running");
100  }
101 
102  exit(0);
103 }
104 
105 /**
106  * Print a little info about the driver.
107  */
108 void
110 {
111  if (g_dev == nullptr) {
112  errx(1, "driver not running");
113  }
114 
115  printf("state @ %p\n", g_dev);
116  g_dev->print_info();
117 
118  exit(0);
119 }
120 
121 /**
122  * Print a little info about how to start/stop/use the driver
123  */
124 void usage()
125 {
126  PX4_INFO("usage: pmw3901 {start|test|reset|info'}");
127  PX4_INFO(" [-b SPI_BUS]");
128 }
129 
130 } // namespace pmw3901
131 
132 
133 int
134 pmw3901_main(int argc, char *argv[])
135 {
136  if (argc < 2) {
137  pmw3901::usage();
138  return PX4_ERROR;
139  }
140 
141  // don't exit from getopt loop to leave getopt global variables in consistent state,
142  // set error flag instead
143  bool err_flag = false;
144  int ch;
145  int myoptind = 1;
146  const char *myoptarg = nullptr;
147  int spi_bus = PMW3901_BUS;
148 
149  while ((ch = px4_getopt(argc, argv, "b:", &myoptind, &myoptarg)) != EOF) {
150  switch (ch) {
151  case 'b':
152  spi_bus = (uint8_t)atoi(myoptarg);
153 
154  break;
155 
156  default:
157  err_flag = true;
158  break;
159  }
160  }
161 
162  if (err_flag) {
163  pmw3901::usage();
164  return PX4_ERROR;
165  }
166 
167  /*
168  * Start/load the driver.
169  */
170  if (!strcmp(argv[myoptind], "start")) {
171  pmw3901::start(spi_bus);
172  }
173 
174  /*
175  * Stop the driver
176  */
177  if (!strcmp(argv[myoptind], "stop")) {
178  pmw3901::stop();
179  }
180 
181  /*
182  * Print driver information.
183  */
184  if (!strcmp(argv[myoptind], "info") || !strcmp(argv[myoptind], "status")) {
185  pmw3901::info();
186  }
187 
188  pmw3901::usage();
189  return PX4_ERROR;
190 }
void test()
Perform some basic functional tests on the driver; make sure we can collect data from the sensor in p...
Definition: mb12xx.cpp:554
void usage()
Print a little info about how to start/stop/use the driver.
void reset()
Reset the driver.
Definition: I2C.hpp:51
void print_info()
Diagnostics - print some basic information about the driver.
Definition: PMW3901.cpp:417
void info()
Print a little info about the driver.
PMW3901 * g_dev
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
void stop()
Stop the driver.
Local functions in support of the shell command.
__EXPORT int pmw3901_main(int argc, char *argv[])
#define errx(eval,...)
Definition: err.h:89
#define OK
Definition: uavcan_main.cpp:71
void start(int spi_bus)
Start the driver.
virtual int init()
Definition: PMW3901.cpp:211