UI: New way to input bytes in byte_input (#2890)

* New byte input UI option
* Gui: reformat and cleanup byte_input docs, make PVS happy

Co-authored-by: hedger <hedger@users.noreply.github.com>
Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
MMX 2023-08-01 12:09:30 +03:00 committed by GitHub
parent 68eb1ecebb
commit aadb72af53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,6 +4,7 @@
#include <furi.h> #include <furi.h>
#include <assets_icons.h> #include <assets_icons.h>
/** ByteInput type */
struct ByteInput { struct ByteInput {
View* view; View* view;
}; };
@ -25,7 +26,7 @@ typedef struct {
bool selected_high_nibble; bool selected_high_nibble;
uint8_t selected_byte; uint8_t selected_byte;
int8_t selected_row; // row -1 - input, row 0 & 1 - keyboard int8_t selected_row; // row -2 - mini_editor, -1 - input, row 0 & 1 - keyboard
uint8_t selected_column; uint8_t selected_column;
uint8_t first_visible_byte; uint8_t first_visible_byte;
} ByteInputModel; } ByteInputModel;
@ -61,11 +62,11 @@ static const ByteInputKey keyboard_keys_row_2[] = {
{enter_symbol, 95, 17}, {enter_symbol, 95, 17},
}; };
/** /** Get row size
* @brief Get row size *
* * @param row_index Index of row
* @param row_index Index of row *
* @return uint8_t Row size * @return uint8_t Row size
*/ */
static uint8_t byte_input_get_row_size(uint8_t row_index) { static uint8_t byte_input_get_row_size(uint8_t row_index) {
uint8_t row_size = 0; uint8_t row_size = 0;
@ -84,11 +85,11 @@ static uint8_t byte_input_get_row_size(uint8_t row_index) {
return row_size; return row_size;
} }
/** /** Get row pointer
* @brief Get row pointer *
* * @param row_index Index of row
* @param row_index Index of row *
* @return const ByteInputKey* Row pointer * @return const ByteInputKey* Row pointer
*/ */
static const ByteInputKey* byte_input_get_row(uint8_t row_index) { static const ByteInputKey* byte_input_get_row(uint8_t row_index) {
const ByteInputKey* row = NULL; const ByteInputKey* row = NULL;
@ -107,12 +108,12 @@ static const ByteInputKey* byte_input_get_row(uint8_t row_index) {
return row; return row;
} }
/** /** Get text from nibble
* @brief Get text from nibble *
* * @param byte byte value
* @param byte byte value * @param high_nibble Get from high nibble, otherwise low nibble
* @param high_nibble Get from high nibble, otherwise low nibble *
* @return char nibble text * @return char nibble text
*/ */
static char byte_input_get_nibble_text(uint8_t byte, bool high_nibble) { static char byte_input_get_nibble_text(uint8_t byte, bool high_nibble) {
if(high_nibble) { if(high_nibble) {
@ -149,15 +150,20 @@ static char byte_input_get_nibble_text(uint8_t byte, bool high_nibble) {
return byte; return byte;
} }
/** const char num_to_char[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
* @brief Draw input box (common view)
* /** Draw input box (common view)
* @param canvas *
* @param model * @param canvas The canvas
* @param model The model
*/ */
static void byte_input_draw_input(Canvas* canvas, ByteInputModel* model) { static void byte_input_draw_input(Canvas* canvas, ByteInputModel* model) {
const uint8_t text_x = 8; const uint8_t text_x = 8;
const uint8_t text_y = 25; const uint8_t text_y = 25;
const uint8_t text_y2 = 40;
const bool draw_index_line =
(model->selected_row == -2) &&
(model->first_visible_byte + MIN(model->bytes_count, max_drawable_bytes + 1) <= 100);
elements_slightly_rounded_frame(canvas, 6, 14, 116, 15); elements_slightly_rounded_frame(canvas, 6, 14, 116, 15);
@ -225,6 +231,27 @@ static void byte_input_draw_input(Canvas* canvas, ByteInputModel* model) {
text_y, text_y,
byte_input_get_nibble_text(model->bytes[i], false)); byte_input_get_nibble_text(model->bytes[i], false));
} }
if(draw_index_line) {
canvas_draw_glyph(
canvas, text_x + 2 + byte_position * 14, text_y2, num_to_char[(i + 1) / 10]);
canvas_draw_glyph(
canvas, text_x + 8 + byte_position * 14, text_y2, num_to_char[(i + 1) % 10]);
}
}
if((model->selected_row == -2) &&
(model->first_visible_byte + MIN(model->bytes_count, max_drawable_bytes + 1) > 100)) {
char str[20];
canvas_set_font(canvas, FontSecondary);
snprintf(str, 20, "Selected index");
canvas_draw_str(canvas, text_x, text_y2, str);
canvas_set_font(canvas, FontPrimary);
snprintf(str, 20, "%u", (model->selected_byte + 1));
canvas_draw_str(canvas, text_x + 75, text_y2, str);
} }
if(model->bytes_count - model->first_visible_byte > max_drawable_bytes) { if(model->bytes_count - model->first_visible_byte > max_drawable_bytes) {
@ -236,11 +263,10 @@ static void byte_input_draw_input(Canvas* canvas, ByteInputModel* model) {
} }
} }
/** /** Draw input box (selected view)
* @brief Draw input box (selected view) *
* * @param canvas The canvas
* @param canvas * @param model The model
* @param model
*/ */
static void byte_input_draw_input_selected(Canvas* canvas, ByteInputModel* model) { static void byte_input_draw_input_selected(Canvas* canvas, ByteInputModel* model) {
const uint8_t text_x = 7; const uint8_t text_x = 7;
@ -297,13 +323,12 @@ static void byte_input_draw_input_selected(Canvas* canvas, ByteInputModel* model
canvas_invert_color(canvas); canvas_invert_color(canvas);
} }
/** /** Set nibble at position
* @brief Set nibble at position *
* * @param data where to set nibble
* @param data where to set nibble * @param position byte position
* @param position byte position * @param value char value
* @param value char value * @param high_nibble set high nibble
* @param high_nibble set high nibble
*/ */
static void byte_input_set_nibble(uint8_t* data, uint8_t position, char value, bool high_nibble) { static void byte_input_set_nibble(uint8_t* data, uint8_t position, char value, bool high_nibble) {
switch(value) { switch(value) {
@ -341,29 +366,28 @@ static void byte_input_set_nibble(uint8_t* data, uint8_t position, char value, b
} }
} }
/** /** What currently selected
* @brief What currently selected *
* * @param model The model
* @return true - keyboard selected, false - input selected *
* @return true - keyboard selected, false - input selected
*/ */
static bool byte_input_keyboard_selected(ByteInputModel* model) { static bool byte_input_keyboard_selected(ByteInputModel* model) {
return model->selected_row >= 0; return model->selected_row >= 0;
} }
/** /** Do transition from keyboard
* @brief Do transition from keyboard *
* * @param model The model
* @param model
*/ */
static void byte_input_transition_from_keyboard(ByteInputModel* model) { static void byte_input_transition_from_keyboard(ByteInputModel* model) {
model->selected_row += 1; model->selected_row += 1;
model->selected_high_nibble = true; model->selected_high_nibble = true;
} }
/** /** Increase selected byte position
* @brief Increase selected byte position *
* * @param model The model
* @param model
*/ */
static void byte_input_inc_selected_byte(ByteInputModel* model) { static void byte_input_inc_selected_byte(ByteInputModel* model) {
if(model->selected_byte < model->bytes_count - 1) { if(model->selected_byte < model->bytes_count - 1) {
@ -379,10 +403,20 @@ static void byte_input_inc_selected_byte(ByteInputModel* model) {
} }
} }
/** static void byte_input_inc_selected_byte_mini(ByteInputModel* model) {
* @brief Decrease selected byte position if((model->selected_byte < model->bytes_count - 1) || model->selected_high_nibble) {
* if(!model->selected_high_nibble) {
* @param model model->selected_high_nibble = !model->selected_high_nibble; //-V547
byte_input_inc_selected_byte(model);
} else {
model->selected_high_nibble = !model->selected_high_nibble; //-V547
}
}
}
/** Decrease selected byte position
*
* @param model The model
*/ */
static void byte_input_dec_selected_byte(ByteInputModel* model) { static void byte_input_dec_selected_byte(ByteInputModel* model) {
if(model->selected_byte > 0) { if(model->selected_byte > 0) {
@ -397,10 +431,20 @@ static void byte_input_dec_selected_byte(ByteInputModel* model) {
} }
} }
/** static void byte_input_dec_selected_byte_mini(ByteInputModel* model) {
* @brief Call input callback if(model->selected_byte > 0 || !model->selected_high_nibble) {
* if(model->selected_high_nibble) {
* @param model model->selected_high_nibble = !model->selected_high_nibble; //-V547
byte_input_dec_selected_byte(model);
} else {
model->selected_high_nibble = !model->selected_high_nibble; //-V547
}
}
}
/** Call input callback
*
* @param model The model
*/ */
static void byte_input_call_input_callback(ByteInputModel* model) { static void byte_input_call_input_callback(ByteInputModel* model) {
if(model->input_callback != NULL) { if(model->input_callback != NULL) {
@ -408,10 +452,9 @@ static void byte_input_call_input_callback(ByteInputModel* model) {
} }
} }
/** /** Call changed callback
* @brief Call changed callback *
* * @param model The model
* @param model
*/ */
static void byte_input_call_changed_callback(ByteInputModel* model) { static void byte_input_call_changed_callback(ByteInputModel* model) {
if(model->changed_callback != NULL) { if(model->changed_callback != NULL) {
@ -419,8 +462,9 @@ static void byte_input_call_changed_callback(ByteInputModel* model) {
} }
} }
/** /** Clear selected byte
* @brief Clear selected byte *
* @param model The model
*/ */
static void byte_input_clear_selected_byte(ByteInputModel* model) { static void byte_input_clear_selected_byte(ByteInputModel* model) {
@ -430,36 +474,55 @@ static void byte_input_clear_selected_byte(ByteInputModel* model) {
byte_input_call_changed_callback(model); byte_input_call_changed_callback(model);
} }
/** /** Handle up button
* @brief Handle up button *
* * @param model The model
* @param model
*/ */
static void byte_input_handle_up(ByteInputModel* model) { static void byte_input_handle_up(ByteInputModel* model) {
if(model->selected_row > -1) { if(model->selected_row > -2) {
model->selected_row -= 1; model->selected_row -= 1;
} else if(model->selected_row == -2) {
if(!model->selected_high_nibble) {
model->bytes[model->selected_byte] = (model->bytes[model->selected_byte] & 0xF0) |
((model->bytes[model->selected_byte] + 1) & 0x0F);
} else {
model->bytes[model->selected_byte] =
((model->bytes[model->selected_byte] + 0x10) & 0xF0) |
(model->bytes[model->selected_byte] & 0x0F);
}
byte_input_call_changed_callback(model);
} }
} }
/** /** Handle down button
* @brief Handle down button *
* * @param model The model
* @param model
*/ */
static void byte_input_handle_down(ByteInputModel* model) { static void byte_input_handle_down(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) { if(model->selected_row != -2) {
if(model->selected_row < keyboard_row_count - 1) { if(byte_input_keyboard_selected(model)) {
model->selected_row += 1; if(model->selected_row < keyboard_row_count - 1) {
model->selected_row += 1;
}
} else {
byte_input_transition_from_keyboard(model);
} }
} else { } else {
byte_input_transition_from_keyboard(model); if(!model->selected_high_nibble) {
model->bytes[model->selected_byte] = (model->bytes[model->selected_byte] & 0xF0) |
((model->bytes[model->selected_byte] - 1) & 0x0F);
} else {
model->bytes[model->selected_byte] =
((model->bytes[model->selected_byte] - 0x10) & 0xF0) |
(model->bytes[model->selected_byte] & 0x0F);
}
byte_input_call_changed_callback(model);
} }
} }
/** /** Handle left button
* @brief Handle left button *
* * @param model The model
* @param model
*/ */
static void byte_input_handle_left(ByteInputModel* model) { static void byte_input_handle_left(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) { if(byte_input_keyboard_selected(model)) {
@ -469,14 +532,17 @@ static void byte_input_handle_left(ByteInputModel* model) {
model->selected_column = byte_input_get_row_size(model->selected_row) - 1; model->selected_column = byte_input_get_row_size(model->selected_row) - 1;
} }
} else { } else {
byte_input_dec_selected_byte(model); if(model->selected_row != -2) {
byte_input_dec_selected_byte(model);
} else {
byte_input_dec_selected_byte_mini(model);
}
} }
} }
/** /** Handle right button
* @brief Handle right button *
* * @param model The model
* @param model
*/ */
static void byte_input_handle_right(ByteInputModel* model) { static void byte_input_handle_right(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) { if(byte_input_keyboard_selected(model)) {
@ -486,14 +552,17 @@ static void byte_input_handle_right(ByteInputModel* model) {
model->selected_column = 0; model->selected_column = 0;
} }
} else { } else {
byte_input_inc_selected_byte(model); if(model->selected_row != -2) {
byte_input_inc_selected_byte(model);
} else {
byte_input_inc_selected_byte_mini(model);
}
} }
} }
/** /** Handle OK button
* @brief Handle OK button *
* * @param model The model
* @param model
*/ */
static void byte_input_handle_ok(ByteInputModel* model) { static void byte_input_handle_ok(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) { if(byte_input_keyboard_selected(model)) {
@ -514,16 +583,17 @@ static void byte_input_handle_ok(ByteInputModel* model) {
} }
byte_input_call_changed_callback(model); byte_input_call_changed_callback(model);
} }
} else if(model->selected_row == -2) {
byte_input_call_input_callback(model);
} else { } else {
byte_input_transition_from_keyboard(model); byte_input_transition_from_keyboard(model);
} }
} }
/** /** Draw callback
* @brief Draw callback *
* * @param canvas The canvas
* @param canvas * @param _model The model
* @param _model
*/ */
static void byte_input_view_draw_callback(Canvas* canvas, void* _model) { static void byte_input_view_draw_callback(Canvas* canvas, void* _model) {
ByteInputModel* model = _model; ByteInputModel* model = _model;
@ -541,80 +611,89 @@ static void byte_input_view_draw_callback(Canvas* canvas, void* _model) {
byte_input_draw_input(canvas, model); byte_input_draw_input(canvas, model);
} }
for(uint8_t row = 0; row < keyboard_row_count; row++) { if(model->selected_row == -2) {
const uint8_t column_count = byte_input_get_row_size(row); canvas_set_font(canvas, FontSecondary);
const ByteInputKey* keys = byte_input_get_row(row); canvas_draw_icon(canvas, 3, 52, &I_Pin_back_arrow_10x8);
canvas_draw_str_aligned(canvas, 16, 60, AlignLeft, AlignBottom, "back to keyboard");
} else {
// Draw keyboard
for(uint8_t row = 0; row < keyboard_row_count; row++) {
const uint8_t column_count = byte_input_get_row_size(row);
const ByteInputKey* keys = byte_input_get_row(row);
for(size_t column = 0; column < column_count; column++) { for(size_t column = 0; column < column_count; column++) {
if(keys[column].value == enter_symbol) { if(keys[column].value == enter_symbol) {
canvas_set_color(canvas, ColorBlack);
if(model->selected_row == row && model->selected_column == column) {
canvas_draw_icon(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
&I_KeySaveSelected_24x11);
} else {
canvas_draw_icon(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
&I_KeySave_24x11);
}
} else if(keys[column].value == backspace_symbol) {
canvas_set_color(canvas, ColorBlack);
if(model->selected_row == row && model->selected_column == column) {
canvas_draw_icon(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
&I_KeyBackspaceSelected_16x9);
} else {
canvas_draw_icon(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
&I_KeyBackspace_16x9);
}
} else {
if(model->selected_row == row && model->selected_column == column) {
canvas_set_color(canvas, ColorBlack); canvas_set_color(canvas, ColorBlack);
canvas_draw_box( if(model->selected_row == row && model->selected_column == column) {
canvas, canvas_draw_icon(
keyboard_origin_x + keys[column].x - 3, canvas,
keyboard_origin_y + keys[column].y - 10, keyboard_origin_x + keys[column].x,
11, keyboard_origin_y + keys[column].y,
13); &I_KeySaveSelected_24x11);
canvas_set_color(canvas, ColorWhite); } else {
} else if(model->selected_row == -1 && row == 0 && model->selected_column == column) { canvas_draw_icon(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
&I_KeySave_24x11);
}
} else if(keys[column].value == backspace_symbol) {
canvas_set_color(canvas, ColorBlack); canvas_set_color(canvas, ColorBlack);
canvas_draw_frame( if(model->selected_row == row && model->selected_column == column) {
canvas, canvas_draw_icon(
keyboard_origin_x + keys[column].x - 3, canvas,
keyboard_origin_y + keys[column].y - 10, keyboard_origin_x + keys[column].x,
11, keyboard_origin_y + keys[column].y,
13); &I_KeyBackspaceSelected_16x9);
} else {
canvas_draw_icon(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
&I_KeyBackspace_16x9);
}
} else { } else {
canvas_set_color(canvas, ColorBlack); if(model->selected_row == row && model->selected_column == column) {
} canvas_set_color(canvas, ColorBlack);
canvas_draw_box(
canvas,
keyboard_origin_x + keys[column].x - 3,
keyboard_origin_y + keys[column].y - 10,
11,
13);
canvas_set_color(canvas, ColorWhite);
} else if(
model->selected_row == -1 && row == 0 &&
model->selected_column == column) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_frame(
canvas,
keyboard_origin_x + keys[column].x - 3,
keyboard_origin_y + keys[column].y - 10,
11,
13);
} else {
canvas_set_color(canvas, ColorBlack);
}
canvas_draw_glyph( canvas_draw_glyph(
canvas, canvas,
keyboard_origin_x + keys[column].x, keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y, keyboard_origin_y + keys[column].y,
keys[column].value); keys[column].value);
}
} }
} }
} }
} }
/** /** Input callback
* @brief Input callback *
* * @param event The event
* @param event * @param context The context
* @param context *
* @return true * @return true
* @return false * @return false
*/ */
static bool byte_input_view_input_callback(InputEvent* event, void* context) { static bool byte_input_view_input_callback(InputEvent* event, void* context) {
ByteInput* byte_input = context; ByteInput* byte_input = context;
@ -656,6 +735,20 @@ static bool byte_input_view_input_callback(InputEvent* event, void* context) {
} }
} }
if(event->type == InputTypeShort && event->key == InputKeyBack) {
// Back to keyboard
with_view_model(
byte_input->view,
ByteInputModel * model,
{
if(model->selected_row == -2) {
model->selected_row += 1;
consumed = true;
};
},
true);
}
if((event->type == InputTypeLong || event->type == InputTypeRepeat) && if((event->type == InputTypeLong || event->type == InputTypeRepeat) &&
event->key == InputKeyBack) { event->key == InputKeyBack) {
with_view_model( with_view_model(
@ -669,10 +762,9 @@ static bool byte_input_view_input_callback(InputEvent* event, void* context) {
return consumed; return consumed;
} }
/** /** Reset all input-related data in model
* @brief Reset all input-related data in model *
* * @param model The model
* @param model ByteInputModel
*/ */
static void byte_input_reset_model_input_data(ByteInputModel* model) { static void byte_input_reset_model_input_data(ByteInputModel* model) {
model->bytes = NULL; model->bytes = NULL;
@ -684,11 +776,6 @@ static void byte_input_reset_model_input_data(ByteInputModel* model) {
model->first_visible_byte = 0; model->first_visible_byte = 0;
} }
/**
* @brief Allocate and initialize byte input. This byte input is used to enter bytes.
*
* @return ByteInput instance pointer
*/
ByteInput* byte_input_alloc() { ByteInput* byte_input_alloc() {
ByteInput* byte_input = malloc(sizeof(ByteInput)); ByteInput* byte_input = malloc(sizeof(ByteInput));
byte_input->view = view_alloc(); byte_input->view = view_alloc();
@ -712,38 +799,17 @@ ByteInput* byte_input_alloc() {
return byte_input; return byte_input;
} }
/**
* @brief Deinitialize and free byte input
*
* @param byte_input Byte input instance
*/
void byte_input_free(ByteInput* byte_input) { void byte_input_free(ByteInput* byte_input) {
furi_assert(byte_input); furi_assert(byte_input);
view_free(byte_input->view); view_free(byte_input->view);
free(byte_input); free(byte_input);
} }
/**
* @brief Get byte input view
*
* @param byte_input byte input instance
* @return View instance that can be used for embedding
*/
View* byte_input_get_view(ByteInput* byte_input) { View* byte_input_get_view(ByteInput* byte_input) {
furi_assert(byte_input); furi_assert(byte_input);
return byte_input->view; return byte_input->view;
} }
/**
* @brief Deinitialize and free byte input
*
* @param byte_input byte input instance
* @param input_callback input callback fn
* @param changed_callback changed callback fn
* @param callback_context callback context
* @param bytes buffer to use
* @param bytes_count buffer length
*/
void byte_input_set_result_callback( void byte_input_set_result_callback(
ByteInput* byte_input, ByteInput* byte_input,
ByteInputCallback input_callback, ByteInputCallback input_callback,
@ -765,12 +831,6 @@ void byte_input_set_result_callback(
true); true);
} }
/**
* @brief Set byte input header text
*
* @param byte_input byte input instance
* @param text text to be shown
*/
void byte_input_set_header_text(ByteInput* byte_input, const char* text) { void byte_input_set_header_text(ByteInput* byte_input, const char* text) {
with_view_model( with_view_model(
byte_input->view, ByteInputModel * model, { model->header = text; }, true); byte_input->view, ByteInputModel * model, { model->header = text; }, true);