[FL-1542], [FL-1603] Generate random name (#602)

* Lib: add random name library
* Text_input: add default text clearing
* All: rework all applications with new text_input API
* Nfc: fix removing dev file on save_name scene enter
* Lib: move all free radicals to toolbox and update API usage.

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
gornekich
2021-07-25 14:34:54 +03:00
committed by GitHub
parent 0c7a8edf51
commit ebd158a823
39 changed files with 142 additions and 132 deletions
+2 -1
View File
@@ -297,7 +297,8 @@ static void archive_enter_text_input(ArchiveApp* archive) {
archive_text_input_callback,
archive,
archive->browser.text_input_buffer,
MAX_NAME_LEN);
MAX_NAME_LEN,
false);
view_dispatcher_switch_to_view(archive->view_dispatcher, ArchiveViewTextInput);
}
-1
View File
@@ -1,7 +1,6 @@
#include "cli_i.h"
#include "cli_commands.h"
#include <version.h>
#include <api-hal-version.h>
#include <loader/loader.h>
-1
View File
@@ -3,7 +3,6 @@
#include <gui/gui.h>
#include <gui/elements.h>
#include <api-hal.h>
#include <version.h>
#include <api-hal-version.h>
static char* Lockmenu_Items[3] = {"Lock", "Set PIN", "DUMB mode"};
+2 -1
View File
@@ -192,7 +192,8 @@ int32_t gui_test(void* param) {
text_input_callback,
gui_tester,
text_input_text,
text_input_text_len);
text_input_text_len,
false);
text_input_set_header_text(gui_tester->text_input, "Name the key");
const uint8_t byte_input_bytes_len = 16;
+18 -5
View File
@@ -16,6 +16,7 @@ typedef struct {
const char* header;
char* text_buffer;
size_t text_buffer_size;
bool clear_default_text;
TextInputCallback callback;
void* callback_context;
@@ -128,7 +129,7 @@ static const char char_to_uppercase(const char letter) {
}
static void text_input_backspace_cb(TextInputModel* model) {
uint8_t text_length = strlen(model->text_buffer);
uint8_t text_length = model->clear_default_text ? 1 : strlen(model->text_buffer);
if(text_length > 0) {
model->text_buffer[text_length - 1] = 0;
}
@@ -158,11 +159,16 @@ static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
text++;
}
if(model->clear_default_text) {
elements_slightly_rounded_box(
canvas, start_pos - 1, 14, canvas_string_width(canvas, text) + 2, 10);
canvas_set_color(canvas, ColorWhite);
} else {
canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 1, 22, "|");
canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 2, 22, "|");
}
canvas_draw_str(canvas, start_pos, 22, text);
canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 1, 22, "|");
canvas_draw_str(canvas, start_pos + canvas_string_width(canvas, text) + 2, 22, "|");
canvas_set_font(canvas, FontKeyboard);
for(uint8_t row = 0; row <= keyboard_row_count; row++) {
@@ -295,12 +301,16 @@ static void text_input_handle_ok(TextInput* text_input) {
} else if(selected == BACKSPACE_KEY) {
text_input_backspace_cb(model);
} else if(text_length < (model->text_buffer_size - 1)) {
if(model->clear_default_text) {
text_length = 0;
}
if(text_length == 0 && char_is_lowercase(selected)) {
selected = char_to_uppercase(selected);
}
model->text_buffer[text_length] = selected;
model->text_buffer[text_length + 1] = 0;
}
model->clear_default_text = false;
return true;
});
}
@@ -365,6 +375,7 @@ TextInput* text_input_alloc() {
model->header = "";
model->selected_row = 0;
model->selected_column = 0;
model->clear_default_text = false;
return true;
});
@@ -387,13 +398,15 @@ void text_input_set_result_callback(
TextInputCallback callback,
void* callback_context,
char* text_buffer,
size_t text_buffer_size) {
size_t text_buffer_size,
bool clear_default_text) {
with_view_model(
text_input->view, (TextInputModel * model) {
model->callback = callback;
model->callback_context = callback_context;
model->text_buffer = text_buffer;
model->text_buffer_size = text_buffer_size;
model->clear_default_text = clear_default_text;
return true;
});
}
+3 -1
View File
@@ -39,13 +39,15 @@ View* text_input_get_view(TextInput* text_input);
* @param callback_context - callback context
* @param text_buffer - pointer to YOUR text buffer, that we going to modify
* @param text_buffer_size - YOUR text buffer size in bytes. Max string length will be text_buffer_size - 1.
* @param clear_default_text - clear text from text_buffer on first OK event
*/
void text_input_set_result_callback(
TextInput* text_input,
TextInputCallback callback,
void* callback_context,
char* text_buffer,
size_t text_buffer_size);
size_t text_buffer_size,
bool clear_default_text);
/**
* @brief Set text input header text
+1 -36
View File
@@ -3,7 +3,7 @@
#include <callback-connector.h>
#include <m-string.h>
#include <file-worker-cpp.h>
#include <path.h>
#include <lib/toolbox/path.h>
const char* iButtonApp::app_folder = "/any/ibutton";
const char* iButtonApp::app_extension = ".ibtn";
@@ -40,9 +40,6 @@ iButtonApp::iButtonApp()
: notification{"notification"} {
api_hal_power_insomnia_enter();
key_worker = new KeyWorker(&ibutton_gpio);
// we need random
srand(DWT->CYCCNT);
}
iButtonApp::~iButtonApp() {
@@ -186,38 +183,6 @@ uint8_t iButtonApp::get_text_store_size() {
return text_store_size;
}
void iButtonApp::generate_random_name(char* name, uint8_t max_name_size) {
const uint8_t prefix_size = 9;
const char* prefix[prefix_size] = {
"ancient",
"hollow",
"strange",
"disappeared",
"unknown",
"unthinkable",
"unnamable",
"nameless",
"my",
};
const uint8_t suffix_size = 8;
const char* suffix[suffix_size] = {
"door",
"entrance",
"doorway",
"entry",
"portal",
"entree",
"opening",
"crack",
};
sniprintf(
name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
// to upper
name[0] = name[0] - 0x20;
}
// file managment
bool iButtonApp::save_key(const char* key_name) {
// Create ibutton directory if necessary
-2
View File
@@ -90,8 +90,6 @@ public:
char* get_file_name();
uint8_t get_file_name_size();
void generate_random_name(char* name, uint8_t max_name_size);
bool save_key(const char* key_name);
bool load_key();
bool load_key(const char* key_name);
+1 -1
View File
@@ -2,7 +2,7 @@
#include <api-hal.h>
#include <stdarg.h>
#include <cli/cli.h>
#include <args.h>
#include <lib/toolbox/args.h>
#include "helpers/key-info.h"
#include "helpers/key-worker.h"
@@ -4,6 +4,7 @@
#include "../ibutton-event.h"
#include "../ibutton-key.h"
#include <callback-connector.h>
#include <lib/toolbox/random_name.h>
void iButtonSceneSaveName::on_enter(iButtonApp* app) {
iButtonAppViewManager* view_manager = app->get_view_manager();
@@ -12,16 +13,17 @@ void iButtonSceneSaveName::on_enter(iButtonApp* app) {
iButtonKey* key = app->get_key();
const char* key_name = key->get_name();
bool key_name_empty = !strcmp(key_name, "");
if(strcmp(key_name, "") == 0) {
app->generate_random_name(app->get_text_store(), app->get_text_store_size());
if(key_name_empty) {
set_random_name(app->get_text_store(), app->get_text_store_size());
} else {
app->set_text_store("%s", key_name);
}
text_input_set_header_text(text_input, "Name the key");
text_input_set_result_callback(
text_input, callback, app, app->get_text_store(), IBUTTON_KEY_NAME_SIZE);
text_input, callback, app, app->get_text_store(), IBUTTON_KEY_NAME_SIZE, key_name_empty);
view_manager->switch_to(iButtonAppViewManager::Type::iButtonAppViewTextInput);
}
@@ -47,7 +49,7 @@ bool iButtonSceneSaveName::on_event(iButtonApp* app, iButtonEvent* event) {
void iButtonSceneSaveName::on_exit(iButtonApp* app) {
TextInput* text_input = app->get_view_manager()->get_text_input();
text_input_set_header_text(text_input, "");
text_input_set_result_callback(text_input, NULL, NULL, NULL, 0);
text_input_set_result_callback(text_input, NULL, NULL, NULL, 0, false);
}
void iButtonSceneSaveName::text_input_callback(void* context) {
@@ -20,7 +20,8 @@ void IrdaAppSceneEditRename::on_enter(IrdaApp* app) {
IrdaApp::text_input_callback,
app,
app->get_text_store(0),
app->get_text_store_size());
app->get_text_store_size(),
false);
view_manager->switch_to(IrdaAppViewManager::ViewType::TextInput);
}
@@ -26,7 +26,8 @@ void IrdaAppSceneLearnEnterName::on_enter(IrdaApp* app) {
IrdaApp::text_input_callback,
app,
app->get_text_store(0),
app->get_text_store_size());
app->get_text_store_size(),
false);
view_manager->switch_to(IrdaAppViewManager::ViewType::TextInput);
}
@@ -1,35 +0,0 @@
#include "rfid-name-generator.h"
#include <stdio.h>
#include <stdlib.h>
void rfid_generate_random_name(char* name, uint8_t max_name_size) {
const uint8_t prefix_size = 9;
const char* prefix[prefix_size] = {
"good",
"nice",
"best",
"some",
"strange",
"working",
"that",
"forgettable",
"easy",
};
const uint8_t suffix_size = 7;
const char* suffix[suffix_size] = {
"pass",
"card",
"key",
"fob",
"permit",
"pass",
"one",
};
sniprintf(
name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
// to upper
name[0] = name[0] - ('a' - 'A');
}
@@ -1,4 +0,0 @@
#pragma once
#include "stdint.h"
void rfid_generate_random_name(char* name, uint8_t max_name_size);
+1 -4
View File
@@ -17,7 +17,7 @@
#include "scene/lfrfid-app-scene-delete-success.h"
#include <file-worker-cpp.h>
#include <path.h>
#include <lib/toolbox/path.h>
const char* LfRfidApp::app_folder = "/any/lfrfid";
const char* LfRfidApp::app_extension = ".rfid";
@@ -27,9 +27,6 @@ LfRfidApp::LfRfidApp()
, notification{"notification"}
, text_store(40) {
api_hal_power_insomnia_enter();
// we need random
srand(DWT->CYCCNT);
}
LfRfidApp::~LfRfidApp() {
+1 -1
View File
@@ -2,7 +2,7 @@
#include <api-hal.h>
#include <stdarg.h>
#include <cli/cli.h>
#include <args.h>
#include <lib/toolbox/args.h>
#include "helpers/rfid-reader.h"
#include "helpers/rfid-timer-emulator.h"
+9 -4
View File
@@ -1,11 +1,12 @@
#include "lfrfid-app-scene-save-name.h"
#include "../helpers/rfid-name-generator.h"
#include <lib/toolbox/random_name.h>
void LfRfidAppSceneSaveName::on_enter(LfRfidApp* app, bool need_restore) {
const char* key_name = app->worker.key.get_name();
if(strcmp(key_name, "") == 0) {
rfid_generate_random_name(app->text_store.text, app->text_store.text_size);
bool key_name_empty = !strcmp(key_name, "");
if(key_name_empty) {
set_random_name(app->text_store.text, app->text_store.text_size);
} else {
app->text_store.set("%s", key_name);
}
@@ -14,7 +15,11 @@ void LfRfidAppSceneSaveName::on_enter(LfRfidApp* app, bool need_restore) {
text_input->set_header_text("Name the card");
text_input->set_result_callback(
save_callback, app, app->text_store.text, app->worker.key.get_name_length());
save_callback,
app,
app->text_store.text,
app->worker.key.get_name_length(),
key_name_empty);
app->view_controller.switch_to<TextInputVM>();
}
+2 -2
View File
@@ -1,8 +1,8 @@
#include "nfc_device_i.h"
#include <file-worker.h>
#include <path.h>
#include <hex.h>
#include <lib/toolbox/path.h>
#include <lib/toolbox/hex.h>
#define NFC_DEVICE_MAX_DATA_LEN 14
+13 -4
View File
@@ -1,4 +1,5 @@
#include "../nfc_i.h"
#include <lib/toolbox/random_name.h>
#define SCENE_SAVE_NAME_CUSTOM_EVENT (0UL)
@@ -13,17 +14,21 @@ const void nfc_scene_save_name_on_enter(void* context) {
// Setup view
TextInput* text_input = nfc->text_input;
if(nfc->dev.dev_name) {
nfc_device_delete(&nfc->dev);
bool dev_name_empty = false;
if(!strcmp(nfc->dev.dev_name, "")) {
set_random_name(nfc->text_store, sizeof(nfc->text_store));
dev_name_empty = true;
} else {
nfc_text_store_set(nfc, nfc->dev.dev_name);
}
nfc_text_store_set(nfc, nfc->dev.dev_name);
text_input_set_header_text(text_input, "Name the card");
text_input_set_result_callback(
text_input,
nfc_scene_save_name_text_input_callback,
nfc,
nfc->text_store,
sizeof(nfc->text_store));
NFC_DEV_NAME_MAX_LEN,
dev_name_empty);
view_dispatcher_switch_to_view(nfc->view_dispatcher, NfcViewTextInput);
}
@@ -32,6 +37,9 @@ const bool nfc_scene_save_name_on_event(void* context, SceneManagerEvent event)
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SCENE_SAVE_NAME_CUSTOM_EVENT) {
if(nfc->dev.dev_name) {
nfc_device_delete(&nfc->dev);
}
memcpy(&nfc->dev.dev_name, nfc->text_store, strlen(nfc->text_store));
if(nfc_device_save(&nfc->dev, nfc->text_store)) {
scene_manager_next_scene(nfc->scene_manager, NfcSceneSaveSuccess);
@@ -49,4 +57,5 @@ const void nfc_scene_save_name_on_exit(void* context) {
// Clear view
text_input_set_header_text(nfc->text_input, NULL);
text_input_set_result_callback(nfc->text_input, NULL, NULL, NULL, 0, false);
}
+1 -1
View File
@@ -1,6 +1,6 @@
#include <furi.h>
#include <cli/cli.h>
#include <args.h>
#include <lib/toolbox/args.h>
#include <storage/storage.h>
#include <storage/storage-sd-api.h>
#include <api-hal-version.h>