mirror of
https://github.com/Next-Flip/Momentum-Firmware.git
synced 2026-05-22 05:14:46 -07:00
cli: Buzzer command (#4006)
* Add args_read_float_and_trim function * Add args_read_duration function * Add notes_frequency_from_name function * Add cli_sleep function and sleep CLI command * Update CLI top command to use cli_sleep * Add buzzer CLI command * toolbox: make args_read_duration less convoluted * notification: make notification_messages_notes_frequency_from_name less convoluted * unit_tests: better float checking * fix formatting and f18 --------- Co-authored-by: Anna Antonenko <portasynthinca3@gmail.com> Co-authored-by: hedger <hedger@nanode.su>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "notification.h"
|
||||
#include <toolbox/strint.h>
|
||||
|
||||
/*
|
||||
Python script for note messages generation
|
||||
@@ -571,3 +572,35 @@ const NotificationMessage message_note_b8 = {
|
||||
.data.sound.frequency = 7902.13f,
|
||||
.data.sound.volume = 1.0f,
|
||||
};
|
||||
|
||||
float notification_messages_notes_frequency_from_name(const char* note_name) {
|
||||
const float base_note = 16.3515979f; // C0
|
||||
|
||||
const char* note_names[] = {"c", "cs", "d", "ds", "e", "f", "fs", "g", "gs", "a", "as", "b"};
|
||||
const size_t notes_count = COUNT_OF(note_names);
|
||||
|
||||
char note_wo_octave[3] = {0};
|
||||
for(size_t i = 0; i < sizeof(note_wo_octave) - 1; i++) {
|
||||
char in = *note_name;
|
||||
if(!in) break;
|
||||
if(!isalpha(in)) break;
|
||||
note_wo_octave[i] = in;
|
||||
note_name++;
|
||||
}
|
||||
|
||||
int note_index = -1;
|
||||
for(size_t i = 0; i < notes_count; i++) {
|
||||
if(strcasecmp(note_wo_octave, note_names[i]) == 0) note_index = i;
|
||||
}
|
||||
if(note_index < 0) return 0.0;
|
||||
|
||||
uint16_t octave;
|
||||
StrintParseError error = strint_to_uint16(note_name, NULL, &octave, 10);
|
||||
if(error != StrintParseNoError) return 0.0;
|
||||
if(octave > 8) return 0.0;
|
||||
|
||||
int semitone_index = octave * notes_count + note_index;
|
||||
float frequency = base_note * powf(2.0f, semitone_index / 12.0f);
|
||||
|
||||
return roundf(frequency * 100) / 100.0f;
|
||||
}
|
||||
|
||||
@@ -115,6 +115,17 @@ extern const NotificationMessage message_note_a8;
|
||||
extern const NotificationMessage message_note_as8;
|
||||
extern const NotificationMessage message_note_b8;
|
||||
|
||||
/**
|
||||
* @brief Returns the frequency of the given note
|
||||
*
|
||||
* This function calculates and returns the frequency (in Hz) of the specified note.
|
||||
* If the input note name is invalid, the function returns 0.0.
|
||||
*
|
||||
* @param [in] note_name The name of the note (e.g., "A4", cs5")
|
||||
* @return The frequency of the note in Hz, or 0.0 if the note name is invalid
|
||||
*/
|
||||
extern float notification_messages_notes_frequency_from_name(const char* note_name);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user