PX4 Firmware
PX4 Autopilot Software http://px4.io
paw3902_main.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 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 "PAW3902.hpp"
35 
36 /**
37  * Local functions in support of the shell command.
38  */
39 namespace pmw3902
40 {
41 
43 
44 void start(int spi_bus);
45 void stop();
46 void test();
47 void reset();
48 void info();
49 void usage();
50 
51 
52 /**
53  * Start the driver.
54  */
55 void
56 start(int spi_bus)
57 {
58  if (g_dev != nullptr) {
59  errx(1, "already started");
60  }
61 
62  /* create the driver */
63  g_dev = new PAW3902(spi_bus, (enum Rotation)0);
64 
65  if (g_dev == nullptr) {
66  goto fail;
67  }
68 
69  if (OK != g_dev->init()) {
70  goto fail;
71  }
72 
73  exit(0);
74 
75 fail:
76 
77  if (g_dev != nullptr) {
78  delete g_dev;
79  g_dev = nullptr;
80  }
81 
82  errx(1, "driver start failed");
83 }
84 
85 /**
86  * Stop the driver
87  */
88 void stop()
89 {
90  if (g_dev != nullptr) {
91  delete g_dev;
92  g_dev = nullptr;
93 
94  } else {
95  errx(1, "driver not running");
96  }
97 
98  exit(0);
99 }
100 
101 /**
102  * Print a little info about the driver.
103  */
104 void
106 {
107  if (g_dev == nullptr) {
108  errx(1, "driver not running");
109  }
110 
111  printf("state @ %p\n", g_dev);
112  g_dev->print_info();
113 
114  exit(0);
115 }
116 
117 /**
118  * Print a little info about how to start/stop/use the driver
119  */
120 void usage()
121 {
122  PX4_INFO("usage: pmw3902 {start|test|reset|info'}");
123  PX4_INFO(" [-b SPI_BUS]");
124 }
125 
126 } // namespace pmw3902
127 
128 /*
129  * Driver 'main' command.
130  */
131 extern "C" __EXPORT int paw3902_main(int argc, char *argv[]);
132 
133 int
134 paw3902_main(int argc, char *argv[])
135 {
136  if (argc < 2) {
137  pmw3902::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 = PAW3902_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  pmw3902::usage();
164  return PX4_ERROR;
165  }
166 
167  /*
168  * Start/load the driver.
169  */
170  if (!strcmp(argv[myoptind], "start")) {
171  pmw3902::start(spi_bus);
172  }
173 
174  /*
175  * Stop the driver
176  */
177  if (!strcmp(argv[myoptind], "stop")) {
178  pmw3902::stop();
179  }
180 
181  /*
182  * Print driver information.
183  */
184  if (!strcmp(argv[myoptind], "status")) {
185  pmw3902::info();
186  }
187 
188  pmw3902::usage();
189  return PX4_ERROR;
190 }
void info()
Print a little info about the driver.
void print_info()
Definition: PAW3902.cpp:695
Definition: I2C.hpp:51
void reset()
Reset the driver.
__EXPORT int paw3902_main(int argc, char *argv[])
void stop()
Stop the driver.
void usage()
Print a little info about how to start/stop/use the driver.
Rotation
Enum for board and external compass rotations.
Definition: rotation.h:51
PAW3902 * g_dev
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
#define errx(eval,...)
Definition: err.h:89
void start(int spi_bus)
Start the driver.
#define OK
Definition: uavcan_main.cpp:71
virtual int init()
Definition: PAW3902.cpp:58
Local functions in support of the shell command.