Skip to content

Commit

Permalink
feat: Maybe working?..
Browse files Browse the repository at this point in the history
  • Loading branch information
QtRoS committed Sep 30, 2023
1 parent 2f6b16a commit 142973c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
3 changes: 2 additions & 1 deletion hex_viewer.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ typedef struct {
uint32_t file_offset;
uint32_t file_read_bytes;
uint32_t file_size;
Stream* stream;
bool mode; // Print address or content

Stream* stream;
} HexViewerModel;


Expand Down
2 changes: 1 addition & 1 deletion scenes/hex_viewer_scene_startscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bool hex_viewer_scene_startscreen_on_event(void* context, SceneManagerEvent even
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case HexViewerCustomEventStartscreenLeft:
app->model->mode = !hex_viewer->model->mode;
app->model->mode = !app->model->mode;
consumed = true;
break;
case HexViewerCustomEventStartscreenRight:
Expand Down
69 changes: 46 additions & 23 deletions views/hex_viewer_startscreen.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ struct HexViewerStartscreen {


typedef struct {
int some_value;
uint8_t file_bytes[HEX_VIEWER_LINES_ON_SCREEN][HEX_VIEWER_BYTES_PER_LINE];
uint32_t file_offset;
uint32_t file_read_bytes;
uint32_t file_size;
bool mode; // Print address or content
} HexViewerStartscreenModel;

void hex_viewer_startscreen_set_callback(
Expand All @@ -29,7 +33,7 @@ void hex_viewer_startscreen_draw(Canvas* canvas, HexViewerStartscreenModel* mode
UNUSED(model);
canvas_clear(canvas);

if (!app->model->file_size) {
if (!model->file_size) {
canvas_set_color(canvas, ColorBlack);
canvas_set_font(canvas, FontPrimary);
canvas_draw_str_aligned(canvas, 64, 10, AlignCenter, AlignTop, "HexViewer v2.0");
Expand All @@ -40,17 +44,17 @@ void hex_viewer_startscreen_draw(Canvas* canvas, HexViewerStartscreenModel* mode
} else {
canvas_set_color(canvas, ColorBlack);

elements_button_left(canvas, app->model->mode ? "Addr" : "Text");
elements_button_left(canvas, model->mode ? "Addr" : "Text");
elements_button_right(canvas, "Info");
elements_button_center(canvas, "Menu");

int ROW_HEIGHT = 12;
int TOP_OFFSET = 10;
int LEFT_OFFSET = 3;

uint32_t line_count = app->model->file_size / HEX_VIEWER_BYTES_PER_LINE;
if(app->model->file_size % HEX_VIEWER_BYTES_PER_LINE != 0) line_count += 1;
uint32_t first_line_on_screen = app->model->file_offset / HEX_VIEWER_BYTES_PER_LINE;
uint32_t line_count = model->file_size / HEX_VIEWER_BYTES_PER_LINE;
if(model->file_size % HEX_VIEWER_BYTES_PER_LINE != 0) line_count += 1;
uint32_t first_line_on_screen = 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(
Expand All @@ -63,24 +67,24 @@ void hex_viewer_startscreen_draw(Canvas* canvas, HexViewerStartscreenModel* mode
}

char temp_buf[32];
uint32_t row_iters = app->model->file_read_bytes / HEX_VIEWER_BYTES_PER_LINE;
if(app->model->file_read_bytes % HEX_VIEWER_BYTES_PER_LINE != 0) row_iters += 1;
uint32_t row_iters = model->file_read_bytes / HEX_VIEWER_BYTES_PER_LINE;
if(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 =
app->model->file_read_bytes - i * HEX_VIEWER_BYTES_PER_LINE;
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(app->model->mode) {
memcpy(temp_buf, app->model->file_bytes[i], bytes_left_per_row);
if(model->mode) {
memcpy(temp_buf, model->file_bytes[i], bytes_left_per_row);
temp_buf[bytes_left_per_row] = '\0';
for(uint32_t j = 0; j < bytes_left_per_row; ++j)
if(!isprint((int)temp_buf[j])) temp_buf[j] = '.';

canvas_set_font(canvas, FontKeyboard);
canvas_draw_str(canvas, LEFT_OFFSET, TOP_OFFSET + i * ROW_HEIGHT, temp_buf);
} else {
uint32_t addr = app->model->file_offset + i * HEX_VIEWER_BYTES_PER_LINE;
uint32_t addr = model->file_offset + i * HEX_VIEWER_BYTES_PER_LINE;
snprintf(temp_buf, 32, "%04lX", addr);

canvas_set_font(canvas, FontKeyboard);
Expand All @@ -89,7 +93,7 @@ void hex_viewer_startscreen_draw(Canvas* canvas, HexViewerStartscreenModel* mode

char* p = temp_buf;
for(uint32_t j = 0; j < bytes_left_per_row; ++j)
p += snprintf(p, 32, "%02X ", app->model->file_bytes[i][j]);
p += snprintf(p, 32, "%02X ", model->file_bytes[i][j]);

canvas_set_font(canvas, FontKeyboard);
canvas_draw_str(canvas, LEFT_OFFSET + 41, TOP_OFFSET + i * ROW_HEIGHT, temp_buf);
Expand All @@ -99,7 +103,20 @@ void hex_viewer_startscreen_draw(Canvas* canvas, HexViewerStartscreenModel* mode
}

static void hex_viewer_startscreen_model_init(HexViewerStartscreenModel* const model) {
model->some_value = 1;
memset(model->file_bytes, 0, sizeof(model->file_bytes));
model->file_offset = 0;
model->file_read_bytes = 0;
model->file_size = 0;
model->mode = false;
}

static void update_local_model_from_app(HexViewer* const app, HexViewerStartscreenModel* const model)
{
memcpy(model->file_bytes, app->model->file_bytes, sizeof(model->file_bytes));
model->file_offset = app->model->file_offset;
model->file_read_bytes = app->model->file_read_bytes;
model->file_size = app->model->file_size;
model->mode = app->model->mode;
}

bool hex_viewer_startscreen_input(InputEvent* event, void* context) {
Expand All @@ -112,20 +129,21 @@ bool hex_viewer_startscreen_input(InputEvent* event, void* context) {
instance->view,
HexViewerStartscreenModel * model,
{
UNUSED(model);
instance->callback(HexViewerCustomEventStartscreenBack, instance->context);
update_local_model_from_app(instance->context, model);
},
true);
break;
case InputKeyLeft:
with_view_model(
instance->view,
HexViewerStartscreenModel * model,
{
UNUSED(model);
instance->callback(HexViewerCustomEventStartscreenLeft, instance->context);
},
true);
instance->view,
HexViewerStartscreenModel * model,
{
UNUSED(model);
instance->callback(HexViewerCustomEventStartscreenLeft, instance->context);
update_local_model_from_app(instance->context, model);
},
true);
break;
case InputKeyRight:
with_view_model(
Expand All @@ -134,6 +152,7 @@ bool hex_viewer_startscreen_input(InputEvent* event, void* context) {
{
UNUSED(model);
instance->callback(HexViewerCustomEventStartscreenRight, instance->context);
update_local_model_from_app(instance->context, model);
},
true);
break;
Expand All @@ -144,6 +163,7 @@ bool hex_viewer_startscreen_input(InputEvent* event, void* context) {
{
UNUSED(model);
instance->callback(HexViewerCustomEventStartscreenUp, instance->context);
update_local_model_from_app(instance->context, model);
},
true);
break;
Expand All @@ -154,6 +174,7 @@ bool hex_viewer_startscreen_input(InputEvent* event, void* context) {
{
UNUSED(model);
instance->callback(HexViewerCustomEventStartscreenDown, instance->context);
update_local_model_from_app(instance->context, model);
},
true);
break;
Expand All @@ -164,6 +185,7 @@ bool hex_viewer_startscreen_input(InputEvent* event, void* context) {
{
UNUSED(model);
instance->callback(HexViewerCustomEventStartscreenOk, instance->context);
update_local_model_from_app(instance->context, model);
},
true);
break;
Expand All @@ -185,7 +207,8 @@ void hex_viewer_startscreen_enter(void* context) {
instance->view,
HexViewerStartscreenModel * model,
{
hex_viewer_startscreen_model_init(model);
hex_viewer_startscreen_model_init(model);
// TODO update_local_model_from_app(instance->context, model);
},
true
);
Expand Down

0 comments on commit 142973c

Please sign in to comment.