PX4 Firmware
PX4 Autopilot Software http://px4.io
ToneAlarm.h
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 /**
35  * @file ToneAlarm.h
36  *
37  * Low Level Driver for the PX4 audio alarm port. Subscribes to
38  * tune_control and plays notes on this architecture specific timer HW.
39  */
40 
41 #pragma once
42 
44 #include <drivers/device/device.h>
45 #include <drivers/drv_tone_alarm.h>
46 #include <lib/tunes/tunes.h>
47 #include <px4_platform_common/defines.h>
48 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
49 #include <uORB/Subscription.hpp>
51 
52 #include <string.h>
53 
54 #if !defined(UNUSED)
55 # define UNUSED(a) ((void)(a))
56 #endif
57 
58 class ToneAlarm : public cdev::CDev, public px4::ScheduledWorkItem
59 {
60 public:
61  ToneAlarm();
62  ~ToneAlarm();
63 
64  /**
65  * @brief Initializes the character device and hardware registers.
66  */
67  int init() override;
68 
69  /**
70  * @brief Prints the driver status to the console.
71  */
72  void status();
73 
74 protected:
75 
76  /**
77  * @brief Parses the next note out of the string and plays it.
78  */
79  void next_note();
80 
81  /**
82  * @brief Trampoline for the work queue.
83  */
84  void Run() override;
85 
86  /**
87  * @brief Updates the uORB topics for local subscribers.
88  */
89  void orb_update();
90 
91  /**
92  * @brief Starts playing the note.
93  */
94  void start_note(unsigned frequency);
95 
96  /**
97  * @brief Stops playing the current note and makes the player 'safe'.
98  */
99  void stop_note();
100 
101  volatile bool _running{false}; ///< Flag to indicate the current driver status.
102 
103  int _cbrk{CBRK_UNINIT}; ///< If true, no audio output.
104 
105 private:
106 
107  volatile bool _should_run{true};
108 
109  bool _play_tone{false};
110 
111  unsigned int _silence_length{0}; ///< If nonzero, silence before next note.
112 
114 
116 
118 };
int init() override
Initializes the character device and hardware registers.
Definition: ToneAlarm.cpp:59
void status()
Prints the driver status to the console.
Definition: ToneAlarm.cpp:139
tune_control_s _tune
Definition: ToneAlarm.h:115
volatile bool _should_run
Definition: ToneAlarm.h:107
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
Library for parsing tunes from melody-strings or dedicated tune messages.
Definition: tunes.h:56
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
Definition: uORB.h:87
Abstract class for any character device.
Definition: CDev.hpp:58
uORB::Subscription _tune_control_sub
Definition: ToneAlarm.h:113
bool _play_tone
Definition: ToneAlarm.h:109
void start_note(unsigned frequency)
Starts playing the note.
Definition: ToneAlarm.cpp:149
Driver for the PX4 audio alarm port, /dev/tone_alarm.
void Run() override
Trampoline for the work queue.
Definition: ToneAlarm.cpp:122
Tunes _tunes
Definition: ToneAlarm.h:117
#define CBRK_UNINIT
int _cbrk
If true, no audio output.
Definition: ToneAlarm.h:103
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
static tune_control_s tune_control
unsigned int _silence_length
If nonzero, silence before next note.
Definition: ToneAlarm.h:111