Firmware
tunes.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2017 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 
38 #pragma once
39 
40 #include <uORB/uORB.h>
41 #include <uORB/topics/tune_control.h>
42 #include "tune_definition.h"
43 
44 #define TUNE_DEFAULT_NOTE_LENGTH 4
45 #define TUNE_DEFAULT_OCTAVE 4
46 #define TUNE_DEFAULT_TEMPO 120
47 
48 #define TUNE_MAX_UPDATE_INTERVAL_US 100000
49 
50 
56 class Tunes
57 {
58 public:
59  enum class NoteMode {NORMAL, LEGATO, STACCATO};
60 
68  Tunes(unsigned default_note_length = TUNE_DEFAULT_NOTE_LENGTH,
69  NoteMode default_note_mode = NoteMode::NORMAL,
70  unsigned default_octave = TUNE_DEFAULT_OCTAVE,
71  unsigned default_tempo = TUNE_DEFAULT_TEMPO);
72 
73  ~Tunes() = default;
74 
82  int set_control(const tune_control_s &tune_control);
83 
92  void set_string(const char *const string, uint8_t volume);
93 
102  int get_next_note(unsigned &frequency, unsigned &duration, unsigned &silence);
103 
113  int get_next_note(unsigned &frequency, unsigned &duration,
114  unsigned &silence, uint8_t &volume);
115 
121  unsigned int get_default_tunes_size() const {return _default_tunes_size;}
122 
123  unsigned int get_maximum_update_interval() {return (unsigned int)TUNE_MAX_UPDATE_INTERVAL_US;}
124 
125 private:
126 
133  uint32_t note_to_frequency(unsigned note) const;
134 
145  unsigned note_duration(unsigned &silence, unsigned note_length, unsigned dots) const;
146 
155  unsigned rest_duration(unsigned rest_length, unsigned dots) const;
156 
162  int next_char();
163 
169  unsigned next_number();
170 
176  unsigned next_dots();
177 
183  void reset(bool repeat_flag);
184 
185  int tune_end();
186 
187  int tune_error();
188 
189  static const char *const _default_tunes[];
190  static const bool _default_tunes_interruptable[];
191  static const unsigned int _default_tunes_size;
192  static const uint8_t _note_tab[];
193 
194  const char *_next_tune = nullptr;
195  const char *_tune = nullptr;
196  const char *_tune_start_ptr = nullptr;
197 
198  int _current_tune_id = static_cast<int>(TuneID::NONE);
199 
200  bool _repeat = false;
201 
202  unsigned int _default_note_length = TUNE_DEFAULT_NOTE_LENGTH;
203  NoteMode _default_note_mode = NoteMode::NORMAL;
204  unsigned int _default_octave = TUNE_DEFAULT_OCTAVE;
205  unsigned int _default_tempo = TUNE_DEFAULT_TEMPO;
206 
207  unsigned int _note_length = TUNE_DEFAULT_NOTE_LENGTH;
208  NoteMode _note_mode = NoteMode::NORMAL;
209  unsigned int _octave = TUNE_DEFAULT_OCTAVE;
210  unsigned int _tempo = TUNE_DEFAULT_TEMPO;
211 
212  unsigned int _duration = 0;
213  unsigned int _frequency = 0;
214  unsigned int _silence = 0;
215  uint8_t _volume = 0;
216 
217  bool _using_custom_msg = false;
218 };
int set_control(const tune_control_s &tune_control)
Set tune to be played using the message.
Definition: tunes.cpp:85
void set_string(const char *const string, uint8_t volume)
Set tune to be played using a string.
Definition: tunes.cpp:138
API for the uORB lightweight object broker.
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
unsigned int get_default_tunes_size() const
Get the number of default tunes.
Definition: tunes.h:121
Library for parsing tunes from melody-strings or dedicated tune messages.
Definition: tunes.h:56
Tunes(unsigned default_note_length=TUNE_DEFAULT_NOTE_LENGTH, NoteMode default_note_mode=NoteMode::NORMAL, unsigned default_octave=TUNE_DEFAULT_OCTAVE, unsigned default_tempo=TUNE_DEFAULT_TEMPO)
Constructor with the default parameters set to: default_tempo: TUNE_DEFAULT_TEMPO default_octave: TUN...
Definition: tunes.cpp:56