Skip to content

Commit

Permalink
refactor: few renames and bugfixes
Browse files Browse the repository at this point in the history
  • Loading branch information
QtRoS committed Nov 26, 2022
1 parent 4c3a383 commit 1aebc09
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions hex_viewer.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
#define HEX_VIEWER_APP_PATH_FOLDER "/any"
#define HEX_VIEWER_APP_EXTENSION "*"

#define HEX_VIEWER_BYTES_PER_ROW 4
#define HEX_VIEWER_ROW_COUNT 4
#define HEX_VIEWER_BUF_SIZE (HEX_VIEWER_BYTES_PER_ROW * HEX_VIEWER_ROW_COUNT)
#define HEX_VIEWER_BYTES_PER_LINE 4u
#define HEX_VIEWER_LINES_ON_SCREEN 4u
#define HEX_VIEWER_BUF_SIZE (HEX_VIEWER_LINES_ON_SCREEN * HEX_VIEWER_BYTES_PER_LINE)

typedef struct {
uint8_t file_bytes[HEX_VIEWER_ROW_COUNT][HEX_VIEWER_ROW_COUNT];
uint8_t file_bytes[HEX_VIEWER_LINES_ON_SCREEN][HEX_VIEWER_BYTES_PER_LINE];
uint32_t file_offset;
uint32_t read_bytes;
uint32_t file_read_bytes;
uint32_t file_size;
Stream* stream;
bool mode; // Print address or content
Expand Down Expand Up @@ -54,28 +54,28 @@ static void render_callback(Canvas* canvas, void* ctx) {
int TOP_OFFSET = 10;
int LEFT_OFFSET = 3;

uint32_t line_count = hex_viewer->model->file_size / HEX_VIEWER_BYTES_PER_ROW;
if(hex_viewer->model->file_size % HEX_VIEWER_BYTES_PER_ROW != 0) line_count += 1;
uint32_t first_line_on_screen = hex_viewer->model->file_offset / HEX_VIEWER_BYTES_PER_ROW;
if(line_count > HEX_VIEWER_ROW_COUNT) {
uint32_t line_count = hex_viewer->model->file_size / HEX_VIEWER_BYTES_PER_LINE;
if(hex_viewer->model->file_size % HEX_VIEWER_BYTES_PER_LINE != 0) line_count += 1;
uint32_t first_line_on_screen = hex_viewer->model->file_offset / HEX_VIEWER_BYTES_PER_LINE;
if(line_count > HEX_VIEWER_LINES_ON_SCREEN) {
uint8_t width = canvas_width(canvas);
elements_scrollbar_pos(
canvas,
width,
0,
ROW_HEIGHT * HEX_VIEWER_ROW_COUNT,
ROW_HEIGHT * HEX_VIEWER_LINES_ON_SCREEN,
first_line_on_screen, // TODO
line_count - (HEX_VIEWER_ROW_COUNT - 1));
line_count - (HEX_VIEWER_LINES_ON_SCREEN - 1));
}

char temp_buf[32];
uint32_t row_iters = hex_viewer->model->read_bytes / HEX_VIEWER_BYTES_PER_ROW;
if(hex_viewer->model->read_bytes % HEX_VIEWER_BYTES_PER_ROW != 0) row_iters += 1;
uint32_t row_iters = hex_viewer->model->file_read_bytes / HEX_VIEWER_BYTES_PER_LINE;
if(hex_viewer->model->file_read_bytes % HEX_VIEWER_BYTES_PER_LINE != 0) row_iters += 1;

for(uint32_t i = 0; i < row_iters; ++i) {
uint32_t bytes_left_per_row = hex_viewer->model->read_bytes - i * HEX_VIEWER_BYTES_PER_ROW;
if(bytes_left_per_row > HEX_VIEWER_BYTES_PER_ROW)
bytes_left_per_row = HEX_VIEWER_BYTES_PER_ROW;
uint32_t bytes_left_per_row =
hex_viewer->model->file_read_bytes - i * HEX_VIEWER_BYTES_PER_LINE;
bytes_left_per_row = MIN(bytes_left_per_row, HEX_VIEWER_BYTES_PER_LINE);

if(hex_viewer->model->mode) {
memcpy(temp_buf, hex_viewer->model->file_bytes[i], bytes_left_per_row);
Expand All @@ -86,7 +86,7 @@ static void render_callback(Canvas* canvas, void* ctx) {
canvas_set_font(canvas, FontKeyboard);
canvas_draw_str(canvas, LEFT_OFFSET, TOP_OFFSET + i * ROW_HEIGHT, temp_buf);
} else {
uint32_t addr = hex_viewer->model->file_offset + i * HEX_VIEWER_BYTES_PER_ROW;
uint32_t addr = hex_viewer->model->file_offset + i * HEX_VIEWER_BYTES_PER_LINE;
snprintf(temp_buf, 32, "%04lX", addr);

canvas_set_font(canvas, FontKeyboard);
Expand Down Expand Up @@ -174,7 +174,7 @@ static bool hex_viewer_open_file(HexViewer* hex_viewer, const char* file_path) {
static bool hex_viewer_read_file(HexViewer* hex_viewer) {
furi_assert(hex_viewer);
furi_assert(hex_viewer->model->stream);
furi_assert(hex_viewer->model->file_offset % HEX_VIEWER_BYTES_PER_ROW == 0);
furi_assert(hex_viewer->model->file_offset % HEX_VIEWER_BYTES_PER_LINE == 0);

memset(hex_viewer->model->file_bytes, 0x0, HEX_VIEWER_BUF_SIZE);
bool isOk = true;
Expand All @@ -187,7 +187,7 @@ static bool hex_viewer_read_file(HexViewer* hex_viewer) {
break;
}

hex_viewer->model->read_bytes = stream_read(
hex_viewer->model->file_read_bytes = stream_read(
hex_viewer->model->stream,
(uint8_t*)hex_viewer->model->file_bytes,
HEX_VIEWER_BUF_SIZE);
Expand Down Expand Up @@ -236,17 +236,17 @@ int32_t hex_viewer_app(void* p) {
} else if(input.key == InputKeyUp) {
furi_check(furi_mutex_acquire(hex_viewer->mutex, FuriWaitForever) == FuriStatusOk);
if(hex_viewer->model->file_offset > 0) {
hex_viewer->model->file_offset -= HEX_VIEWER_BYTES_PER_ROW;
hex_viewer->model->file_offset -= HEX_VIEWER_BYTES_PER_LINE;
if(!hex_viewer_read_file(hex_viewer)) break;
}
furi_mutex_release(hex_viewer->mutex);
} else if(input.key == InputKeyDown) {
furi_check(furi_mutex_acquire(hex_viewer->mutex, FuriWaitForever) == FuriStatusOk);
uint32_t last_byte_on_screen =
hex_viewer->model->file_offset + hex_viewer->model->read_bytes;
hex_viewer->model->file_offset + hex_viewer->model->file_read_bytes;

if(hex_viewer->model->file_size > last_byte_on_screen) {
hex_viewer->model->file_offset += HEX_VIEWER_BYTES_PER_ROW;
hex_viewer->model->file_offset += HEX_VIEWER_BYTES_PER_LINE;
if(!hex_viewer_read_file(hex_viewer)) break;
}
furi_mutex_release(hex_viewer->mutex);
Expand Down

0 comments on commit 1aebc09

Please sign in to comment.