mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2025-12-12 20:49:49 +04: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:
@@ -2,6 +2,7 @@
|
||||
#include "hex.h"
|
||||
#include "strint.h"
|
||||
#include "m-core.h"
|
||||
#include <errno.h>
|
||||
|
||||
size_t args_get_first_word_length(FuriString* args) {
|
||||
size_t ws = furi_string_search_char(args, ' ');
|
||||
@@ -34,6 +35,24 @@ bool args_read_int_and_trim(FuriString* args, int* value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool args_read_float_and_trim(FuriString* args, float* value) {
|
||||
size_t cmd_length = args_get_first_word_length(args);
|
||||
if(cmd_length == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
char* end_ptr;
|
||||
float temp = strtof(furi_string_get_cstr(args), &end_ptr);
|
||||
if(end_ptr == furi_string_get_cstr(args)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*value = temp;
|
||||
furi_string_right(args, cmd_length);
|
||||
furi_string_trim(args);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool args_read_string_and_trim(FuriString* args, FuriString* word) {
|
||||
size_t cmd_length = args_get_first_word_length(args);
|
||||
|
||||
@@ -97,3 +116,38 @@ bool args_read_hex_bytes(FuriString* args, uint8_t* bytes, size_t bytes_count) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool args_read_duration(FuriString* args, uint32_t* value, const char* default_unit) {
|
||||
const char* args_cstr = furi_string_get_cstr(args);
|
||||
|
||||
const char* unit;
|
||||
errno = 0;
|
||||
double duration_ms = strtod(args_cstr, (char**)&unit);
|
||||
if(errno) return false;
|
||||
if(duration_ms < 0) return false;
|
||||
if(unit == args_cstr) return false;
|
||||
|
||||
if(strcmp(unit, "") == 0) {
|
||||
unit = default_unit;
|
||||
if(!unit) unit = "ms";
|
||||
}
|
||||
|
||||
uint32_t multiplier;
|
||||
if(strcasecmp(unit, "ms") == 0) {
|
||||
multiplier = 1;
|
||||
} else if(strcasecmp(unit, "s") == 0) {
|
||||
multiplier = 1000;
|
||||
} else if(strcasecmp(unit, "m") == 0) {
|
||||
multiplier = 60 * 1000;
|
||||
} else if(strcasecmp(unit, "h") == 0) {
|
||||
multiplier = 60 * 60 * 1000;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
const uint32_t max_pre_multiplication = UINT32_MAX / multiplier;
|
||||
if(duration_ms > max_pre_multiplication) return false;
|
||||
|
||||
*value = round(duration_ms * multiplier);
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user