PX4 Firmware
PX4 Autopilot Software http://px4.io
ToneAlarm.cpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (C) 2013-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 /**
35  * @file ToneAlarm.cpp
36  */
37 
38 #include "ToneAlarm.h"
39 
40 #include <px4_platform_common/time.h>
41 
44  ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::hp_default)
45 {
46 }
47 
49 {
50  _should_run = false;
51  int counter = 0;
52 
53  while (_running && counter < 10) {
54  px4_usleep(100000);
55  counter++;
56  }
57 }
58 
60 {
61  if (CDev::init() != OK) {
62  return PX4_ERROR;
63  }
64 
65  // NOTE: Implement hardware specific detail in the ToneAlarmInterface class implementation.
67 
68  _running = true;
69 
70  ScheduleNow();
71 
72  return OK;
73 }
74 
76 {
77  if (!_should_run) {
78  _running = false;
79  return;
80  }
81 
82  // Check for updates
83  orb_update();
84 
85  unsigned int frequency = 0;
86  unsigned int duration = 0;
87 
88  // Does an inter-note silence occur?
89  if (_silence_length > 0) {
90  stop_note();
91  duration = _silence_length;
92  _silence_length = 0;
93 
94  } else if (_play_tone) {
95  int parse_ret_val = _tunes.get_next_note(frequency, duration, _silence_length);
96 
97  if (parse_ret_val > 0) {
98  // Continue playing.
99  _play_tone = true;
100 
101  // A frequency of 0 corresponds to stop_note();
102  if (frequency > 0) {
103  // Start playing the note.
104  start_note(frequency);
105  }
106 
107  } else {
108  _play_tone = false;
109  stop_note();
110  }
111 
112  } else {
113  // Schedule a callback with the tunes max interval.
114  duration = _tunes.get_maximum_update_interval();
115  stop_note();
116  }
117 
118  // Schedule a callback when the note should stop.
119  ScheduleDelayed(duration);
120 }
121 
123 {
124  next_note();
125 }
126 
128 {
129  // Check for updates
130  if (_tune_control_sub.updated()) {
132 
133  if (_tune.timestamp > 0) {
135  }
136  }
137 }
138 
140 {
141  if (_running) {
142  PX4_INFO("running");
143 
144  } else {
145  PX4_INFO("stopped");
146  }
147 }
148 
149 void ToneAlarm::start_note(unsigned frequency)
150 {
151  // Check if circuit breaker is enabled.
152  if (_cbrk == CBRK_UNINIT) {
154  }
155 
156  if (_cbrk != CBRK_OFF) {
157  return;
158  }
159 
160  // NOTE: Implement hardware specific detail in the ToneAlarmInterface class implementation.
162 }
163 
165 {
166  // NOTE: Implement hardware specific detail in the ToneAlarmInterface class implementation.
168 }
169 
170 /**
171  * Local functions in support of the shell command.
172  */
173 namespace
174 {
175 
177 
178 } // namespace
179 
180 /**
181  * Tone alarm Driver 'main' command.
182  * Entry point for the tone_alarm driver module.
183  */
184 extern "C" __EXPORT int tone_alarm_main(int argc, char *argv[])
185 {
186  if (argc > 1) {
187  const char *argv1 = argv[1];
188 
189  if (!strcmp(argv1, "start")) {
190  if (g_dev == nullptr) {
191  g_dev = new ToneAlarm();
192 
193  if (g_dev == nullptr) {
194  PX4_ERR("could not allocate the driver.");
195  }
196 
197  if (g_dev->init() != OK) {
198  delete g_dev;
199  g_dev = nullptr;
200  PX4_ERR("driver init failed.");
201  }
202 
203  } else {
204  PX4_INFO("already started");
205  }
206 
207  return 0;
208  }
209 
210  if (!strcmp(argv1, "stop")) {
211  delete g_dev;
212  g_dev = nullptr;
213  return 0;
214  }
215 
216  if (!strcmp(argv1, "status")) {
217  if (g_dev != nullptr) {
218  g_dev->status();
219 
220  } else {
221  PX4_INFO("driver stopped");
222  }
223 
224  return 0;
225  }
226 
227  } else {
228  PX4_INFO("missing command, try 'start', status, 'stop'");
229  }
230 
231  return 0;
232 };
int init() override
Initializes the character device and hardware registers.
Definition: ToneAlarm.cpp:59
int get_next_note(unsigned &frequency, unsigned &duration, unsigned &silence)
Get next note in the current tune, which has been provided by either set_control or play_string...
Definition: tunes.cpp:172
virtual int init()
Definition: airspeed.cpp:92
void status()
Prints the driver status to the console.
Definition: ToneAlarm.cpp:139
bool circuit_breaker_enabled(const char *breaker, int32_t magic)
tune_control_s _tune
Definition: ToneAlarm.h:115
void stop_note()
Stops playing the current note and makes the player &#39;safe&#39;.
Definition: I2C.hpp:51
volatile bool _should_run
Definition: ToneAlarm.h:107
#define CBRK_OFF
uint64_t timestamp
Definition: tune_control.h:57
volatile bool _running
Flag to indicate the current driver status.
Definition: ToneAlarm.h:101
void next_note()
Parses the next note out of the string and plays it.
Definition: ToneAlarm.cpp:75
Low Level Driver for the PX4 audio alarm port.
ETSAirspeed * g_dev
void init()
Activates/configures the hardware registers.
void start_note(unsigned frequency)
Starts playing the note.
uORB::Subscription _tune_control_sub
Definition: ToneAlarm.h:113
int set_control(const tune_control_s &tune_control)
Set tune to be played using the message.
Definition: tunes.cpp:85
bool _play_tone
Definition: ToneAlarm.h:109
static unsigned counter
Definition: safety.c:56
#define TONE_ALARM0_DEVICE_PATH
void start_note(unsigned frequency)
Starts playing the note.
Definition: ToneAlarm.cpp:149
bool updated()
Check if there is a new update.
void Run() override
Trampoline for the work queue.
Definition: ToneAlarm.cpp:122
Tunes _tunes
Definition: ToneAlarm.h:117
__EXPORT int tone_alarm_main(int argc, char *argv[])
Tone alarm Driver &#39;main&#39; command.
Definition: ToneAlarm.cpp:184
#define CBRK_UNINIT
Definition: bst.cpp:62
int _cbrk
If true, no audio output.
Definition: ToneAlarm.h:103
#define OK
Definition: uavcan_main.cpp:71
void stop_note()
Stops playing the current note and makes the player &#39;safe&#39;.
Definition: ToneAlarm.cpp:164
void orb_update()
Updates the uORB topics for local subscribers.
Definition: ToneAlarm.cpp:127
unsigned int _silence_length
If nonzero, silence before next note.
Definition: ToneAlarm.h:111
bool copy(void *dst)
Copy the struct.
#define CBRK_BUZZER_KEY
unsigned int get_maximum_update_interval()
Definition: tunes.h:123