-
Notifications
You must be signed in to change notification settings - Fork 13.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
libtunes: Various bugfixes and improvements #9117
Changes from 11 commits
b727cbf
fb0c00d
7b01302
61bd2b9
7cfa866
bda5da2
f711747
18e66bd
a17e4ce
d61cf60
0c09ff8
37cd67a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,16 +43,27 @@ | |
|
||
#define TUNE_MAX_UPDATE_INTERVAL_US 100000 | ||
|
||
#define TUNE_DEFAULT_TEMPO 120 | ||
#define TUNE_DEFAULT_OCTAVE 4 | ||
#define TUNE_DEFAULT_NOTE_LENGTH 4 | ||
#define TUNE_MAX_STRENGTH 100 | ||
|
||
|
||
/** | ||
* Library for parsing tunes from melody-strings or dedicated tune messages. | ||
* Needs to be instantiated as it keeps track of which tune is to be played | ||
* next. Also handles repeated tunes. | ||
*/ | ||
class Tunes | ||
{ | ||
public: | ||
enum class NoteMode {NORMAL, LEGATO, STACCATO}; | ||
|
||
/** | ||
* Constructor with the default parameter set to: | ||
* default_tempo: 120 | ||
* default_octave: 4 | ||
* default_note_length: 4 | ||
* default_tempo: TUNE_DEFAULT_TEMPO | ||
* default_octave: TUNE_DEFAULT_OCTAVE | ||
* default_note_length: TUNE_DEFAULT_NOTE_LENGTH | ||
* default_mode: NORMAL | ||
*/ | ||
Tunes(); | ||
|
@@ -62,42 +73,66 @@ class Tunes | |
*/ | ||
Tunes(unsigned default_tempo, unsigned default_octave, unsigned default_note_length, NoteMode default_mode); | ||
|
||
/** | ||
* Default destructor | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment is a bit redundant There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah I figured that the documentation looks more complete if there is a comment above every function. But you are right, it is redundant. |
||
*/ | ||
~Tunes() = default; | ||
|
||
/** | ||
* Set tune to be played. | ||
* Set tune to be played using the message. If a tune is already being played | ||
* the call to this function will be ignored, unless the override flag is set | ||
* or the tune being already played is a repeated tune. | ||
* @param tune_control struct containig the uORB message | ||
* @return return -EINVAL if the default tune does not exist. | ||
*/ | ||
int set_control(const tune_control_s &tune_control); | ||
|
||
/** | ||
* Parse a tune string, formatted with the syntax of the Microsoft GWBasic/QBasic. | ||
* This has to be kept in memory for the whole duration of the melody. | ||
* Set tune to be played using a string. | ||
* Parses a tune string, formatted with the syntax of the Microsoft GWBasic/QBasic. | ||
* Ownership of the string is NOT transferred. The string has to be kept in | ||
* memory for the whole duration of the melody. | ||
* | ||
* @param string tune input string | ||
*/ | ||
void set_string(const char *string); | ||
void set_string(const char *const string, uint8_t strength); | ||
|
||
/** | ||
* Get next note in the setted string in set_control or play_string | ||
* Get next note in the current tune, which has been provided by either | ||
* set_control or play_string | ||
* @param frequency return frequency value (Hz) | ||
* @param duration return duration of the tone (us) | ||
* @param silence return silence duration (us) | ||
* @return -1 for error, 0 for play one tone and 1 for continue a sequence | ||
*/ | ||
int get_next_tune(unsigned &frequency, unsigned &duration, unsigned &silence); | ||
|
||
unsigned int get_default_tunes_size() {return _default_tunes_size;} | ||
/** | ||
* Get next note in the current tune, which has been provided by either | ||
* set_control or play_string | ||
* @param frequency return frequency value (Hz) | ||
* @param duration return duration of the tone (us) | ||
* @param silence return silence duration (us) | ||
* @param strength return the strength of the note (between 0-100) | ||
* @return -1 for error, 0 for play one tone and 1 for continue a sequence | ||
*/ | ||
int get_next_tune(unsigned &frequency, unsigned &duration, unsigned &silence, | ||
uint8_t &strength); | ||
|
||
/** | ||
* Get the number of default tunes. This is useful for when a tune is | ||
* requested via its tune ID. | ||
* @return Number of default tunes accessible via tune ID | ||
*/ | ||
unsigned int get_default_tunes_size() const {return _default_tunes_size;} | ||
|
||
unsigned int get_maximum_update_interval() {return (unsigned int)TUNE_MAX_UPDATE_INTERVAL_US;} | ||
|
||
private: | ||
static const char *_default_tunes[]; | ||
static const uint8_t _note_tab[]; | ||
static const unsigned int _default_tunes_size; | ||
bool _repeat; ///< if true, tune restarts at end | ||
|
||
bool _repeat = false; ///< if true, tune restarts at end | ||
const char *_tune = nullptr; ///< current tune string | ||
const char *_next = nullptr; ///< next note in the string | ||
const char *_tune_start_ptr = nullptr; ///< pointer to repeat tune | ||
|
@@ -107,14 +142,15 @@ class Tunes | |
NoteMode _note_mode; | ||
unsigned _octave; | ||
|
||
unsigned _default_tempo = 120; | ||
unsigned _default_note_length = 4; | ||
NoteMode _default_mode = NoteMode::NORMAL; | ||
unsigned _default_octave = 4; | ||
unsigned _default_tempo; | ||
unsigned _default_note_length; | ||
NoteMode _default_mode; | ||
unsigned _default_octave; | ||
|
||
unsigned _frequency; | ||
unsigned _duration; | ||
unsigned _silence; | ||
uint8_t _strength; | ||
bool _using_custom_msg = false; | ||
|
||
/** | ||
|
@@ -123,7 +159,7 @@ class Tunes | |
* @param note unsigned value of the semitone from C | ||
* @return frequency (Hz) | ||
*/ | ||
uint32_t note_to_frequency(unsigned note); | ||
uint32_t note_to_frequency(unsigned note) const; | ||
|
||
/** | ||
* Calculate the duration in microseconds of play and silence for a | ||
|
@@ -135,7 +171,7 @@ class Tunes | |
* @param dots extention of the note length | ||
* @return duration of the note (us) | ||
*/ | ||
unsigned note_duration(unsigned &silence, unsigned note_length, unsigned dots); | ||
unsigned note_duration(unsigned &silence, unsigned note_length, unsigned dots) const; | ||
|
||
/** | ||
* Calculate the duration in microseconds of a rest corresponding to | ||
|
@@ -145,7 +181,7 @@ class Tunes | |
* @param dots number of extension dots | ||
* @return rest duration (us) | ||
*/ | ||
unsigned rest_duration(unsigned rest_length, unsigned dots); | ||
unsigned rest_duration(unsigned rest_length, unsigned dots) const; | ||
|
||
/** | ||
* Find the next character in the string, discard any whitespace. | ||
|
@@ -169,8 +205,10 @@ class Tunes | |
unsigned next_dots(); | ||
|
||
/** | ||
* if repeat false set the tune parameters to default else point to the beginning of the tune | ||
* Reset the tune parameters. This is necessary when for example a tune moved | ||
* one or more octaves up or down. reset() should always be called before | ||
* (re)-starting a tune. | ||
*/ | ||
void config_tone(bool repeat); | ||
void reset(bool repeat_flag); | ||
|
||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd remove the printf and just return the EINVAL. Reduces the amount of strings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good thinking