new iButton app (#328)

* rename old ibutton app to ibutton-test

* more renames

* updated onewire library compilation condition

* add submenu_clean subroutine

* add index for submenu callback

* c++ guard for gui modules

* add released ibutton app

* fix the position of the submenu window if there are too few items

* iButton app basis

* negative icon position info

* fix submenu_clean subroutine

* add ibutton app to applications makefile

* add onewire key read routine to read mode

* rename mode to scene

* rename files and folder (mode to scene)

* rename ibutton view to view manager

* rename get_view to get_view_manager

* cpp guards

* key read, store and notify features

* syntax fix

* make iButtonScene functions pure virtual

* fix syntax

* add text store, add new scene (crc error)

* not a key scene

* syntax fix

* read success scene

* app, switching to the previous scene with the number of scenes to be skipped

* scene whith menu when key is readed

* fix font height calculation, fix offsets

* add key write scene

* view_dispatcher_remove_view subroutine

* generic pause/resume os methods

* fix furi_assert usage

* key store, worker

* fix pointer comparsion

* saved keys, saved key action scenes

* key delete/confirm delete scenes and routines

* use last input subsystem changes

* fix syntax

* fix new model usage in submenu

* fix includes

* use vibro pin

* use stored key name if valid

* emulate scene

* random name generator

* name and save readed key scenes, new icon

* fix icon position

* fix text scene exit

* fix naming, fix text placement, new info scene

* state-driven cyfral decoder

* better cyfral decoder

* better cyfral decoder

* one wire: search command set

* metakom decoder

* more key types

* add next scene to error scenes

* universal key reader

* use new key reader

* syntax fix

* warning fix

* byte input module template

* new thread and insomnia api usage

* New element: slightly rounded frame

* Use elements_slightly_rounded_frame in text input

* Gui test app: byte input usage

* Byte input module: data drawing and selection

* Byte input: comment currently unused fns

* remove volatile qualifier

* base byte input realisation

* App gui test: remove internal fns visibility

* Byne input, final version

* test install gcc-arm-none-eabi-10-2020-q4-major

* test install gcc-arm-none-eabi-10-2020-q4-major

* App iButton: byte input view managment

* App iButton: add key manually scenes

* App iButton: rename scenes, add popup timeout

* App iButton: use new scenes, new fn for rollback to specific prevous scene.

* App iButton: remove byte input view on app exit

* App iButton: edit key scene

* Module byte input: reduce swintch value to uint8_t

* Module byte input: switch from switch-case to if, unfortunately we need compile-time constants to use with switch

* Icons: new small arrows

* Module byte input: new arrangement of elements

* OneWire slave lib: fix deattach sequence

* App iButton: pulse sequencer

* App iButton: add more keys to store

* App iButton: split key worker to separate read/write/emulate entitys

* App iButton: use new read/emulate entities

* fix callback pointer saving

* App iButton: use KeyReader error enum instead of KeyWorker error list handling

* App iButton: do not use insomnia fns in pulse sequencer

* App iButton: use KeyReader error enum in read scene

* OneWire slave lib: more READ ROM command variants, call callback only if positive result

* GPIO resources: add external gpio

* App SD/NFC: removed application

* App iButton-test: update to new light api

* App iButton: update to new light-api

* Outdated apps: add api-light-usage

* Gpio: update SD card CS pin settings

* API-power: added fns to disable/enable external 3v3 dc-dc

* API-gpio: separated SD card detect routines

* Resources: removed sd cs pin

* SD card: low level init now resets card power supply

* App SD-filesystem: use new card detect fns

* SD card: fix low level init headers

* SD card: more realilable low level init, power reset, exit from command read cycle conditionally

* App SD-filesystem: led notifiers, init cycling

* SD card: backport to F4

* Api PWM: add c++ guards

* App iButton: yellow blink in emulate scene, vibro on

* App iButton: one wire keys command set

* App iButton: successful write scene

* App iButton: key writer

* App iButton: syntax fix

* App iButton: notify write success

* App iButton: fix double scene change

* SD card: handle eject in init sequence

* SD card: api to set level on detect gpio

* SPI: api to set state on bus pins

* SD card: set low state on bus pins while power reset

* File select: init

* File select: fix input consuming

* SD Card: fixed dir open api error

* SD-card: replace strncpy by strlcpy. Fix buffer overflow error.

* API HAL OS: replace CMP based ticks with ARR based one, hard reset lptimer on reconfiguration.

* GUI: More stack size for (temporary, wee need to implement sd card api in separate thread)

* GUI: File select module.

* App iButton-test: remove obsolete app

Co-authored-by: rusdacent <rusdacentx0x08@gmail.com>
Co-authored-by: coreglitch <mail@s3f.ru>
Co-authored-by: Aleksandr Kutuzov <alleteam@gmail.com>
This commit is contained in:
SG
2021-03-12 15:45:18 +03:00
committed by GitHub
parent fc12f91a64
commit 1daef3d025
99 changed files with 5341 additions and 653 deletions

View File

@@ -0,0 +1,765 @@
#include "byte_input.h"
#include <gui/elements.h>
#include <furi.h>
struct ByteInput {
View* view;
};
typedef struct {
const uint8_t value;
const uint8_t x;
const uint8_t y;
} ByteInputKey;
typedef struct {
const char* header;
uint8_t* bytes;
uint8_t bytes_count;
ByteInputCallback input_callback;
ByteChangedCallback changed_callback;
void* callback_context;
bool selected_high_nibble;
uint8_t selected_byte;
int8_t selected_row; // row -1 - input, row 0 & 1 - keyboard
uint8_t selected_column;
uint8_t first_visible_byte;
} ByteInputModel;
#ifndef MAX
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#endif
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
static const uint8_t keyboard_origin_x = 8;
static const uint8_t keyboard_origin_y = 32;
static const uint8_t keyboard_row_count = 2;
static const uint8_t enter_symbol = '\r';
static const uint8_t backspace_symbol = '\b';
static const uint8_t max_drawable_bytes = 8;
static const ByteInputKey keyboard_keys_row_1[] = {
{'0', 0, 12},
{'1', 11, 12},
{'2', 22, 12},
{'3', 33, 12},
{'4', 44, 12},
{'5', 55, 12},
{'6', 66, 12},
{'7', 77, 12},
{backspace_symbol, 101, 4},
};
static const ByteInputKey keyboard_keys_row_2[] = {
{'8', 0, 26},
{'9', 11, 26},
{'A', 22, 26},
{'B', 33, 26},
{'C', 44, 26},
{'D', 55, 26},
{'E', 66, 26},
{'F', 77, 26},
{enter_symbol, 93, 17},
};
/**
* @brief Get row size
*
* @param row_index Index of row
* @return uint8_t Row size
*/
static uint8_t byte_input_get_row_size(uint8_t row_index) {
uint8_t row_size = 0;
switch(row_index + 1) {
case 1:
row_size = sizeof(keyboard_keys_row_1) / sizeof(ByteInputKey);
break;
case 2:
row_size = sizeof(keyboard_keys_row_2) / sizeof(ByteInputKey);
break;
}
return row_size;
}
/**
* @brief Get row pointer
*
* @param row_index Index of row
* @return const ByteInputKey* Row pointer
*/
static const ByteInputKey* byte_input_get_row(uint8_t row_index) {
const ByteInputKey* row = NULL;
switch(row_index + 1) {
case 1:
row = keyboard_keys_row_1;
break;
case 2:
row = keyboard_keys_row_2;
break;
}
return row;
}
/**
* @brief Get text from nibble
*
* @param byte byte value
* @param high_nibble Get from high nibble, otherwise low nibble
* @return char nibble text
*/
static char byte_input_get_nibble_text(uint8_t byte, bool high_nibble) {
if(high_nibble) {
byte = byte >> 4;
}
byte = byte & 0x0F;
switch(byte & 0x0F) {
case 0x0:
case 0x1:
case 0x2:
case 0x3:
case 0x4:
case 0x5:
case 0x6:
case 0x7:
case 0x8:
case 0x9:
byte = byte + '0';
break;
case 0xA:
case 0xB:
case 0xC:
case 0xD:
case 0xE:
case 0xF:
byte = byte - 0xA + 'A';
break;
default:
byte = '!';
break;
}
return byte;
}
/**
* @brief Draw input box (common view)
*
* @param canvas
* @param model
*/
static void byte_input_draw_input(Canvas* canvas, ByteInputModel* model) {
const uint8_t text_x = 7;
const uint8_t text_y = 27;
elements_slightly_rounded_frame(canvas, 5, 16, 117, 15);
for(uint8_t i = model->first_visible_byte;
i < model->first_visible_byte + MIN(model->bytes_count, max_drawable_bytes);
i++) {
uint8_t byte_position = i - model->first_visible_byte;
if(i == model->selected_byte) {
canvas_draw_frame(canvas, text_x + byte_position * 14, text_y - 9, 15, 11);
if(model->selected_high_nibble) {
canvas_draw_glyph(
canvas,
text_x + 8 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], false));
canvas_draw_box(canvas, text_x + 1 + byte_position * 14, text_y - 8, 7, 9);
canvas_invert_color(canvas);
canvas_draw_line(
canvas,
text_x + 14 + byte_position * 14,
text_y - 6,
text_x + 14 + byte_position * 14,
text_y - 2);
canvas_draw_glyph(
canvas,
text_x + 2 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], true));
canvas_invert_color(canvas);
} else {
canvas_draw_box(canvas, text_x + 7 + byte_position * 14, text_y - 8, 7, 9);
canvas_draw_glyph(
canvas,
text_x + 2 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], true));
canvas_invert_color(canvas);
canvas_draw_line(
canvas,
text_x + byte_position * 14,
text_y - 6,
text_x + byte_position * 14,
text_y - 2);
canvas_draw_glyph(
canvas,
text_x + 8 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], false));
canvas_invert_color(canvas);
}
} else {
canvas_draw_glyph(
canvas,
text_x + 2 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], true));
canvas_draw_glyph(
canvas,
text_x + 8 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], false));
}
}
if(model->bytes_count - model->first_visible_byte > max_drawable_bytes) {
canvas_draw_icon_name(canvas, 123, 21, I_ButtonRightSmall_3x5);
}
if(model->first_visible_byte > 0) {
canvas_draw_icon_name(canvas, 1, 21, I_ButtonLeftSmall_3x5);
}
}
/**
* @brief Draw input box (selected view)
*
* @param canvas
* @param model
*/
static void byte_input_draw_input_selected(Canvas* canvas, ByteInputModel* model) {
const uint8_t text_x = 7;
const uint8_t text_y = 27;
canvas_draw_box(canvas, 0, 14, 128, 19);
canvas_invert_color(canvas);
elements_slightly_rounded_frame(canvas, 5, 16, 117, 15);
for(uint8_t i = model->first_visible_byte;
i < model->first_visible_byte + MIN(model->bytes_count, max_drawable_bytes);
i++) {
uint8_t byte_position = i - model->first_visible_byte;
if(i == model->selected_byte) {
canvas_draw_box(canvas, text_x + byte_position * 14, text_y - 9, 15, 11);
canvas_invert_color(canvas);
canvas_draw_glyph(
canvas,
text_x + 2 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], true));
canvas_draw_glyph(
canvas,
text_x + 8 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], false));
canvas_invert_color(canvas);
} else {
canvas_draw_glyph(
canvas,
text_x + 2 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], true));
canvas_draw_glyph(
canvas,
text_x + 8 + byte_position * 14,
text_y,
byte_input_get_nibble_text(model->bytes[i], false));
}
}
if(model->bytes_count - model->first_visible_byte > max_drawable_bytes) {
canvas_draw_icon_name(canvas, 123, 21, I_ButtonRightSmall_3x5);
}
if(model->first_visible_byte > 0) {
canvas_draw_icon_name(canvas, 1, 21, I_ButtonLeftSmall_3x5);
}
canvas_invert_color(canvas);
}
/**
* @brief Set nibble at position
*
* @param data where to set nibble
* @param position byte position
* @param value char value
* @param high_nibble set high nibble
*/
static void byte_input_set_nibble(uint8_t* data, uint8_t position, char value, bool high_nibble) {
switch(value) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
value = value - '0';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
value = value - 'A' + 10;
break;
default:
value = 0;
break;
}
if(high_nibble) {
data[position] &= 0x0F;
data[position] |= value << 4;
} else {
data[position] &= 0xF0;
data[position] |= value;
}
}
/**
* @brief What currently selected
*
* @return true - keyboard selected, false - input selected
*/
static bool byte_input_keyboard_selected(ByteInputModel* model) {
return model->selected_row >= 0;
}
/**
* @brief Do transition from keyboard
*
* @param model
*/
static void byte_input_transition_from_keyboard(ByteInputModel* model) {
model->selected_row += 1;
model->selected_high_nibble = true;
}
/**
* @brief Increase selected byte position
*
* @param model
*/
static void byte_input_inc_selected_byte(ByteInputModel* model) {
if(model->selected_byte < model->bytes_count - 1) {
model->selected_byte += 1;
if(model->bytes_count > max_drawable_bytes) {
if(model->selected_byte - model->first_visible_byte > (max_drawable_bytes - 2)) {
if(model->first_visible_byte < model->bytes_count - max_drawable_bytes) {
model->first_visible_byte++;
}
}
}
}
}
/**
* @brief Decrease selected byte position
*
* @param model
*/
static void byte_input_dec_selected_byte(ByteInputModel* model) {
if(model->selected_byte > 0) {
model->selected_byte -= 1;
if(model->selected_byte - model->first_visible_byte < 1) {
if(model->first_visible_byte > 0) {
model->first_visible_byte--;
}
}
}
}
/**
* @brief Call input callback
*
* @param model
*/
static void byte_input_call_input_callback(ByteInputModel* model) {
if(model->input_callback != NULL) {
model->input_callback(model->callback_context, model->bytes, model->bytes_count);
}
}
/**
* @brief Call changed callback
*
* @param model
*/
static void byte_input_call_changed_callback(ByteInputModel* model) {
if(model->changed_callback != NULL) {
model->changed_callback(model->callback_context, model->bytes, model->bytes_count);
}
}
/**
* @brief Handle up button
*
* @param model
*/
static void byte_input_handle_up(ByteInputModel* model) {
if(model->selected_row > -1) {
model->selected_row -= 1;
}
}
/**
* @brief Handle down button
*
* @param model
*/
static void byte_input_handle_down(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) {
if(model->selected_row < keyboard_row_count - 1) {
model->selected_row += 1;
}
} else {
byte_input_transition_from_keyboard(model);
}
}
/**
* @brief Handle left button
*
* @param model
*/
static void byte_input_handle_left(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) {
if(model->selected_column > 0) {
model->selected_column -= 1;
} else {
model->selected_column = byte_input_get_row_size(model->selected_row) - 1;
}
} else {
byte_input_dec_selected_byte(model);
}
}
/**
* @brief Handle right button
*
* @param model
*/
static void byte_input_handle_right(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) {
if(model->selected_column < byte_input_get_row_size(model->selected_row) - 1) {
model->selected_column += 1;
} else {
model->selected_column = 0;
}
} else {
byte_input_inc_selected_byte(model);
}
}
/**
* @brief Handle OK button
*
* @param model
*/
static void byte_input_handle_ok(ByteInputModel* model) {
if(byte_input_keyboard_selected(model)) {
uint8_t value = byte_input_get_row(model->selected_row)[model->selected_column].value;
if(value == enter_symbol) {
byte_input_call_input_callback(model);
} else if(value == backspace_symbol) {
model->bytes[model->selected_byte] = 0;
model->selected_high_nibble = true;
byte_input_dec_selected_byte(model);
byte_input_call_changed_callback(model);
} else {
byte_input_set_nibble(
model->bytes, model->selected_byte, value, model->selected_high_nibble);
if(model->selected_high_nibble == true) {
model->selected_high_nibble = false;
} else {
byte_input_inc_selected_byte(model);
model->selected_high_nibble = true;
}
byte_input_call_changed_callback(model);
}
} else {
byte_input_transition_from_keyboard(model);
}
}
/**
* @brief Draw callback
*
* @param canvas
* @param _model
*/
static void byte_input_view_draw_callback(Canvas* canvas, void* _model) {
ByteInputModel* model = _model;
canvas_clear(canvas);
canvas_set_color(canvas, ColorBlack);
canvas_draw_str(canvas, 5, 10, model->header);
canvas_set_font(canvas, FontKeyboard);
if(model->selected_row == -1) {
byte_input_draw_input_selected(canvas, model);
} else {
byte_input_draw_input(canvas, model);
}
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++) {
if(keys[column].value == enter_symbol) {
canvas_set_color(canvas, ColorBlack);
if(model->selected_row == row && model->selected_column == column) {
canvas_draw_icon_name(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
I_KeySaveSelected_24x11);
} else {
canvas_draw_icon_name(
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_name(
canvas,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
I_KeyBackspaceSelected_16x9);
} else {
canvas_draw_icon_name(
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_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,
keyboard_origin_x + keys[column].x,
keyboard_origin_y + keys[column].y,
keys[column].value);
}
}
}
}
/**
* @brief Input callback
*
* @param event
* @param context
* @return true
* @return false
*/
static bool byte_input_view_input_callback(InputEvent* event, void* context) {
ByteInput* byte_input = context;
furi_assert(byte_input);
bool consumed = false;
if(event->type == InputTypeShort) {
switch(event->key) {
case InputKeyLeft:
with_view_model(
byte_input->view, (ByteInputModel * model) {
byte_input_handle_left(model);
return true;
});
consumed = true;
break;
case InputKeyRight:
with_view_model(
byte_input->view, (ByteInputModel * model) {
byte_input_handle_right(model);
return true;
});
consumed = true;
break;
case InputKeyUp:
with_view_model(
byte_input->view, (ByteInputModel * model) {
byte_input_handle_up(model);
return true;
});
consumed = true;
break;
case InputKeyDown:
with_view_model(
byte_input->view, (ByteInputModel * model) {
byte_input_handle_down(model);
return true;
});
consumed = true;
break;
case InputKeyOk:
with_view_model(
byte_input->view, (ByteInputModel * model) {
byte_input_handle_ok(model);
return true;
});
consumed = true;
break;
default:
break;
}
}
return consumed;
}
/**
* @brief Reset all input-related data in model
*
* @param model ByteInputModel
*/
static void byte_input_reset_model_input_data(ByteInputModel* model) {
model->bytes = NULL;
model->bytes_count = 0;
model->selected_high_nibble = true;
model->selected_byte = 0;
model->selected_row = 0;
model->selected_column = 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 = furi_alloc(sizeof(ByteInput));
byte_input->view = view_alloc();
view_set_context(byte_input->view, byte_input);
view_allocate_model(byte_input->view, ViewModelTypeLocking, sizeof(ByteInputModel));
view_set_draw_callback(byte_input->view, byte_input_view_draw_callback);
view_set_input_callback(byte_input->view, byte_input_view_input_callback);
with_view_model(
byte_input->view, (ByteInputModel * model) {
model->header = "";
model->input_callback = NULL;
model->changed_callback = NULL;
model->callback_context = NULL;
byte_input_reset_model_input_data(model);
return true;
});
return byte_input;
}
/**
* @brief Deinitialize and free byte input
*
* @param byte_input Byte input instance
*/
void byte_input_free(ByteInput* byte_input) {
furi_assert(byte_input);
view_free(byte_input->view);
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) {
furi_assert(byte_input);
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(
ByteInput* byte_input,
ByteInputCallback input_callback,
ByteChangedCallback changed_callback,
void* callback_context,
uint8_t* bytes,
uint8_t bytes_count) {
with_view_model(
byte_input->view, (ByteInputModel * model) {
byte_input_reset_model_input_data(model);
model->input_callback = input_callback;
model->changed_callback = changed_callback;
model->callback_context = callback_context;
model->bytes = bytes;
model->bytes_count = bytes_count;
return 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) {
with_view_model(
byte_input->view, (ByteInputModel * model) {
model->header = text;
return true;
});
}

View File

@@ -0,0 +1,76 @@
#pragma once
#include <gui/view.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Byte input anonymous structure
*
*/
typedef struct ByteInput ByteInput;
/**
* @brief callback that is executed on save button press
*
*/
typedef void (*ByteInputCallback)(void* context, uint8_t* bytes, uint8_t bytes_count);
/**
* @brief callback that is executed when byte buffer is changed
*
*/
typedef void (*ByteChangedCallback)(void* context, uint8_t* bytes, uint8_t bytes_count);
/**
* @brief Allocate and initialize byte input. This byte input is used to enter bytes.
*
* @return ByteInput instance pointer
*/
ByteInput* byte_input_alloc();
/**
* @brief Deinitialize and free byte input
*
* @param byte_input Byte input instance
*/
void byte_input_free(ByteInput* 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);
/**
* @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(
ByteInput* byte_input,
ByteInputCallback input_callback,
ByteChangedCallback changed_callback,
void* callback_context,
uint8_t* bytes,
uint8_t bytes_count);
/**
* @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);
#ifdef __cplusplus
}
#endif

View File

@@ -1,7 +1,10 @@
#pragma once
#include <gui/view.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Dialog anonymous structure */
typedef struct Dialog Dialog;
@@ -67,3 +70,7 @@ void dialog_set_left_button_text(Dialog* dialog, const char* text);
* @param text - text to be shown
*/
void dialog_set_right_button_text(Dialog* dialog, const char* text);
#ifdef __cplusplus
}
#endif

View File

@@ -1,7 +1,10 @@
#pragma once
#include <gui/view.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Dialog anonymous structure */
typedef struct DialogEx DialogEx;
@@ -103,3 +106,7 @@ void dialog_ex_set_center_button_text(DialogEx* dialog_ex, const char* text);
* @param text - text to be shown
*/
void dialog_ex_set_right_button_text(DialogEx* dialog_ex, const char* text);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,358 @@
#include "file_select.h"
#include <gui/elements.h>
#include <m-string.h>
#define FILENAME_COUNT 4
struct FileSelect {
// public
View* view;
FS_Api* fs_api;
char* path;
char* extension;
bool init_completed;
FileSelectCallback callback;
void* context;
};
typedef struct {
string_t filename[FILENAME_COUNT];
uint8_t position;
uint16_t first_file_index;
uint16_t file_count;
} FileSelectModel;
bool file_select_fill_strings(FileSelect* file_select);
bool file_select_fill_count(FileSelect* file_select);
static bool file_select_init(FileSelect* file_select);
static void file_select_draw_callback(Canvas* canvas, void* _model) {
FileSelectModel* model = _model;
const uint8_t item_height = 16;
const uint8_t item_width = 123;
canvas_clear(canvas);
canvas_set_font(canvas, FontSecondary);
for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
if(i == model->position) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_box(canvas, 0, (i * item_height) + 1, item_width, item_height - 2);
canvas_set_color(canvas, ColorWhite);
canvas_draw_dot(canvas, 0, (i * item_height) + 1);
canvas_draw_dot(canvas, 0, (i * item_height) + item_height - 2);
canvas_draw_dot(canvas, item_width - 1, (i * item_height) + 1);
canvas_draw_dot(canvas, item_width - 1, (i * item_height) + item_height - 2);
} else {
canvas_set_color(canvas, ColorBlack);
}
canvas_draw_str(
canvas, 6, (i * item_height) + item_height - 4, string_get_cstr(model->filename[i]));
}
elements_scrollbar(canvas, model->first_file_index + model->position, model->file_count);
}
static bool file_select_input_callback(InputEvent* event, void* context) {
FileSelect* file_select = (FileSelect*)context;
bool consumed = false;
if(event->type == InputTypeShort) {
if(!file_select->init_completed) {
if(!file_select_init(file_select)) {
file_select->callback(NULL, file_select->context);
}
} else if(event->key == InputKeyUp) {
with_view_model(
file_select->view, (FileSelectModel * model) {
if(model->position == 0) {
if(model->first_file_index == 0) {
// wrap
uint16_t max_first_file_index = model->file_count - FILENAME_COUNT;
model->position = FILENAME_COUNT - 1;
model->first_file_index = max_first_file_index;
} else {
model->first_file_index--;
}
} else if(model->position == 1) {
if(model->first_file_index == 0) {
model->position--;
} else {
model->first_file_index--;
}
} else {
model->position--;
}
return true;
});
consumed = true;
} else if(event->key == InputKeyDown) {
with_view_model(
file_select->view, (FileSelectModel * model) {
uint16_t max_first_file_index = model->file_count - FILENAME_COUNT;
if(model->position >= (FILENAME_COUNT - 1)) {
if(model->first_file_index >= max_first_file_index) {
// wrap
model->position = 0;
model->first_file_index = 0;
} else {
model->first_file_index++;
}
} else if(model->position >= (FILENAME_COUNT - 2)) {
if(model->first_file_index >= max_first_file_index) {
model->position++;
} else {
model->first_file_index++;
}
} else {
model->position++;
}
return true;
});
consumed = true;
} else if(event->key == InputKeyOk) {
if(file_select->callback != NULL) {
const char* result;
with_view_model(
file_select->view, (FileSelectModel * model) {
result = string_get_cstr(model->filename[model->position]);
return false;
});
file_select->callback(result, file_select->context);
}
consumed = true;
}
if(!file_select_fill_strings(file_select)) {
file_select->callback(NULL, file_select->context);
}
}
return consumed;
}
static bool file_select_init(FileSelect* file_select) {
bool result = false;
if(file_select->path && file_select->extension && file_select->fs_api) {
if(file_select_fill_count(file_select)) {
if(file_select_fill_strings(file_select)) {
file_select->init_completed = true;
result = true;
}
}
}
return result;
}
FileSelect* file_select_alloc() {
FileSelect* file_select = furi_alloc(sizeof(FileSelect));
file_select->view = view_alloc();
view_set_context(file_select->view, file_select);
view_allocate_model(file_select->view, ViewModelTypeLockFree, sizeof(FileSelectModel));
view_set_draw_callback(file_select->view, file_select_draw_callback);
view_set_input_callback(file_select->view, file_select_input_callback);
file_select->fs_api = NULL;
file_select->path = NULL;
file_select->extension = NULL;
file_select->init_completed = false;
file_select->callback = NULL;
file_select->context = NULL;
with_view_model(
file_select->view, (FileSelectModel * model) {
for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
string_init(model->filename[i]);
}
model->first_file_index = 0;
model->file_count = 0;
return false;
});
return file_select;
}
void file_select_free(FileSelect* file_select) {
furi_assert(file_select);
with_view_model(
file_select->view, (FileSelectModel * model) {
for(uint8_t i = 0; i < FILENAME_COUNT; i++) {
string_clear(model->filename[i]);
}
return false;
});
view_free(file_select->view);
free(file_select);
}
View* file_select_get_view(FileSelect* file_select) {
furi_assert(file_select);
return file_select->view;
}
void file_select_set_api(FileSelect* file_select, FS_Api* fs_api) {
furi_assert(file_select);
file_select->fs_api = fs_api;
}
void file_select_set_callback(FileSelect* file_select, FileSelectCallback callback, void* context) {
}
void file_select_set_filter(FileSelect* file_select, char* path, char* extension) {
furi_assert(file_select);
file_select->path = path;
file_select->extension = extension;
}
static bool filter_file(FileSelect* file_select, FileInfo* file_info, char* name) {
bool result = false;
if(!(file_info->flags & FSF_DIRECTORY)) {
if(strcmp(file_select->extension, "*") == 0) {
result = true;
} else if(strstr(name, file_select->extension) != NULL) {
result = true;
}
}
return result;
}
bool file_select_fill_strings(FileSelect* file_select) {
furi_assert(file_select);
furi_assert(file_select->fs_api);
furi_assert(file_select->path);
furi_assert(file_select->extension);
FileInfo file_info;
File directory;
bool result;
FS_Dir_Api* dir_api = &file_select->fs_api->dir;
uint8_t string_counter = 0;
uint16_t file_counter = 0;
const uint8_t name_length = 100;
char* name = calloc(name_length, sizeof(char));
uint16_t first_file_index = 0;
with_view_model(
file_select->view, (FileSelectModel * model) {
first_file_index = model->first_file_index;
return false;
});
if(name == NULL) {
return false;
}
result = dir_api->open(&directory, file_select->path);
if(!result) {
dir_api->close(&directory);
free(name);
return false;
}
while(1) {
result = dir_api->read(&directory, &file_info, name, name_length);
if(directory.error_id == FSE_NOT_EXIST || name[0] == 0) {
break;
}
if(result) {
if(directory.error_id == FSE_OK) {
if(filter_file(file_select, &file_info, name)) {
if(file_counter >= first_file_index) {
with_view_model(
file_select->view, (FileSelectModel * model) {
string_set_str(model->filename[string_counter], name);
return true;
});
string_counter++;
if(string_counter >= FILENAME_COUNT) {
break;
}
}
file_counter++;
}
} else {
dir_api->close(&directory);
free(name);
return false;
}
}
}
dir_api->close(&directory);
free(name);
return true;
}
bool file_select_fill_count(FileSelect* file_select) {
furi_assert(file_select);
furi_assert(file_select->fs_api);
furi_assert(file_select->path);
furi_assert(file_select->extension);
FileInfo file_info;
File directory;
bool result;
FS_Dir_Api* dir_api = &file_select->fs_api->dir;
uint16_t file_counter = 0;
const uint8_t name_length = 100;
char* name = calloc(name_length, sizeof(char));
if(name == NULL) {
return false;
}
result = dir_api->open(&directory, file_select->path);
if(!result) {
dir_api->close(&directory);
free(name);
return false;
}
while(1) {
result = dir_api->read(&directory, &file_info, name, name_length);
if(directory.error_id == FSE_NOT_EXIST || name[0] == 0) {
break;
}
if(result) {
if(directory.error_id == FSE_OK) {
if(filter_file(file_select, &file_info, name)) {
file_counter++;
}
} else {
dir_api->close(&directory);
free(name);
return false;
}
}
}
with_view_model(
file_select->view, (FileSelectModel * model) {
model->file_count = file_counter;
return false;
});
dir_api->close(&directory);
free(name);
return true;
}

View File

@@ -0,0 +1,24 @@
#pragma once
#include <gui/view.h>
#include <filesystem-api.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct FileSelect FileSelect;
typedef void (*FileSelectCallback)(const char* result, void* context);
FileSelect* file_select_alloc();
void file_select_free(FileSelect* file_select);
View* file_select_get_view(FileSelect* file_select);
void file_select_set_api(FileSelect* file_select, FS_Api* fs_api);
void file_select_set_callback(FileSelect* file_select, FileSelectCallback callback, void* context);
void file_select_set_filter(FileSelect* file_select, char* path, char* extension);
#ifdef __cplusplus
}
#endif

View File

@@ -1,7 +1,10 @@
#pragma once
#include <gui/view.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Popup anonymous structure */
typedef struct Popup Popup;
@@ -69,6 +72,7 @@ void popup_set_text(
Align vertical);
/* Set popup icon
* If icon position is negative, popup icon will not be rendered
* @param popup - Popup instance
* @param x, y - icon position
* @param name - icon to be shown
@@ -89,4 +93,8 @@ void popup_enable_timeout(Popup* popup);
/* Disable popup timeout
* @param popup - Popup instance
*/
void popup_disable_timeout(Popup* popup);
void popup_disable_timeout(Popup* popup);
#ifdef __cplusplus
}
#endif

View File

@@ -1,4 +1,5 @@
#include "text_input.h"
#include <gui/elements.h>
#include <furi.h>
struct TextInput {
@@ -136,10 +137,7 @@ static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
canvas_set_color(canvas, ColorBlack);
canvas_draw_str(canvas, 2, 8, model->header);
canvas_draw_line(canvas, 2, 12, canvas_width(canvas) - 7, 12);
canvas_draw_line(canvas, 1, 13, 1, 25);
canvas_draw_line(canvas, canvas_width(canvas) - 6, 13, canvas_width(canvas) - 6, 25);
canvas_draw_line(canvas, 2, 26, canvas_width(canvas) - 7, 26);
elements_slightly_rounded_frame(canvas, 1, 12, 122, 15);
while(text != 0 && canvas_string_width(canvas, text) > needed_string_width) {
text++;
@@ -151,7 +149,7 @@ static void text_input_view_draw_callback(Canvas* canvas, void* _model) {
canvas_set_font(canvas, FontKeyboard);
for(uint8_t row = 0; row <= keyboard_row_count; row++) {
uint8_t volatile column_count = get_row_size(row);
const uint8_t column_count = get_row_size(row);
const TextInputKey* keys = get_row(row);
for(size_t column = 0; column < column_count; column++) {

View File

@@ -1,6 +1,10 @@
#pragma once
#include <gui/view.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Text input anonymous structure */
typedef struct TextInput TextInput;
typedef void (*TextInputCallback)(void* context, char* text);
@@ -39,4 +43,8 @@ void text_input_set_result_callback(
* @param text input - Text input instance
* @param text - text to be shown
*/
void text_input_set_header_text(TextInput* text_input, const char* text);
void text_input_set_header_text(TextInput* text_input, const char* text);
#ifdef __cplusplus
}
#endif