Skip to content

Commit

Permalink
add mac adress redactor
Browse files Browse the repository at this point in the history
  • Loading branch information
karasevia committed Jul 4, 2023
1 parent 688a8af commit c217027
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 10 deletions.
76 changes: 73 additions & 3 deletions eth_view_process.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "eth_view_process.h"
#include "eth_worker.h"
#include "eth_worker_i.h"
#include "finik_eth_icons.h"

#include <furi_hal.h>
#include <gui/gui.h>
Expand All @@ -11,20 +12,44 @@

#define TAG "EthView"

EthViewProcess* ethernet_view_process_malloc() {
EthViewProcess* ethernet_view_process_malloc(EthWorkerProcess type) {
EthViewProcess* evp = malloc(sizeof(EthViewProcess));
evp->type = type;
evp->autofill = 1;
evp->carriage = 0;
evp->position = 0;
evp->x = 27;
evp->y = 6;

if(type == EthWorkerProcessInit) {
evp->y += 22;
evp->draw_struct = malloc(sizeof(EthViewDrawInit));
memset(evp->draw_struct, 0, sizeof(EthViewDrawInit));
}
return evp;
}

void ethernet_view_process_free(EthViewProcess* evp) {
if(evp->type == EthWorkerProcessInit) {
free(evp->draw_struct);
}
free(evp);
}

static void draw_hex_digit(Canvas* canvas, uint8_t x, uint8_t y, uint8_t digit) {
char digit_str[] = "0";
if(digit < 0xA) {
digit_str[0] += digit;
} else if(digit < 0x10) {
digit_str[0] = 'A';
digit_str[0] += digit - 0xA;
} else {
return;
}

canvas_draw_str(canvas, x, y, digit_str);
}

void ethernet_view_process_draw(EthViewProcess* process, Canvas* canvas) {
furi_assert(canvas);
furi_assert(process);
Expand All @@ -46,6 +71,51 @@ void ethernet_view_process_draw(EthViewProcess* process, Canvas* canvas) {
uint8_t y1 = y + (i + 1) * str_height;
canvas_draw_str(canvas, x, y1, process->fifo[(position + i) % SCREEN_STRINGS_COUNT]);
}

if(process->type == EthWorkerProcessInit) {
uint8_t editing = process->editing;
canvas_draw_icon(canvas, 27, 10, &I_init_100x19px);
for(uint8_t i = 0; i < 6; ++i) {
uint8_t x1 = 29 + i * 17;
uint8_t x2 = x1 + 6;
uint8_t mac = ((EthViewDrawInit*)process->draw_struct)->mac[i];
uint8_t octet = ((EthViewDrawInit*)process->draw_struct)->current_octet;
draw_hex_digit(canvas, x1, 25, (mac & 0x0F));
draw_hex_digit(canvas, x2, 25, (mac & 0xF0) >> 4);
if(editing && (octet / 2 == i)) {
uint8_t x = octet & 1 ? x2 : x1;
canvas_draw_line(canvas, x, 26, x + 4, 26);
canvas_draw_line(canvas, x, 27, x + 4, 27);
}
}
}
}

static void mac_change_hex_digit(uint8_t* mac, uint8_t octet, int8_t diff) {
uint8_t digit = (octet & 1) ? (mac[octet / 2] >> 4) : (mac[octet / 2]);
digit = (digit + diff) & 0xF;
mac[octet / 2] = (mac[octet / 2] & ((octet & 1) ? 0x0F : 0xF0)) |
(digit << ((octet & 1) ? 4 : 0));
}

void ethernet_view_process_keyevent(EthViewProcess* process, InputKey key) {
furi_assert(process);
if(process->type == EthWorkerProcessInit) {
uint8_t octet = ((EthViewDrawInit*)process->draw_struct)->current_octet;
uint8_t* mac = ((EthViewDrawInit*)process->draw_struct)->mac;
if(key == InputKeyLeft) {
if(octet > 0) octet -= 1;
} else if(key == InputKeyRight) {
if(octet < 12) octet += 1;
} else if(key == InputKeyUp) {
mac_change_hex_digit(mac, octet, 1);
} else if(key == InputKeyDown) {
mac_change_hex_digit(mac, octet, -1);
} else if(key == InputKeyOk) {
process->editing = 0;
}
((EthViewDrawInit*)process->draw_struct)->current_octet = octet;
}
}

void ethernet_view_process_move(EthViewProcess* process, int8_t shift) {
Expand All @@ -69,7 +139,7 @@ void ethernet_view_process_autofill(EthViewProcess* process, uint8_t state) {

static uint16_t get_string_with_width(const char* str, uint16_t width) {
u8g2_t canvas_memory;
Canvas* canvas = &canvas_memory; // grazniy hack
Canvas* canvas = (Canvas*)&canvas_memory; // grazniy hack
canvas_set_font(canvas, FontSecondary);

uint8_t end = 0;
Expand Down Expand Up @@ -113,4 +183,4 @@ void ethernet_view_process_print(EthViewProcess* process, const char* str) {
memcpy(process->fifo[carriage], str + start, ptr - start);
process->carriage = carriage1;
}
}
}
8 changes: 7 additions & 1 deletion eth_view_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
#define SCREEN_SYMBOLS_WIDTH 30
#define SCREEN_STRINGS_COUNT 40

EthViewProcess* ethernet_view_process_malloc();
EthViewProcess* ethernet_view_process_malloc(EthWorkerProcess type);
void ethernet_view_process_free(EthViewProcess* evp);

void ethernet_view_process_draw(EthViewProcess* process, Canvas* canvas);
void ethernet_view_process_keyevent(EthViewProcess* process, InputKey key);
void ethernet_view_process_print(EthViewProcess* process, const char* str);
void ethernet_view_process_move(EthViewProcess* process, int8_t shift);

struct EthViewDrawInit {
uint8_t mac[6];
uint8_t current_octet;
};
10 changes: 5 additions & 5 deletions eth_worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ EthWorker* eth_worker_alloc() {

eth_worker_change_state(eth_worker, EthWorkerStateModuleInit);

eth_worker->init_process = ethernet_view_process_malloc();
eth_worker->dhcp_process = ethernet_view_process_malloc();
eth_worker->stat_process = ethernet_view_process_malloc();
eth_worker->ping_process = ethernet_view_process_malloc();
eth_worker->reset_process = ethernet_view_process_malloc();
eth_worker->init_process = ethernet_view_process_malloc(EthWorkerProcessInit);
eth_worker->dhcp_process = ethernet_view_process_malloc(EthWorkerProcessDHCP);
eth_worker->stat_process = ethernet_view_process_malloc(EthWorkerProcessStatic);
eth_worker->ping_process = ethernet_view_process_malloc(EthWorkerProcessPing);
eth_worker->reset_process = ethernet_view_process_malloc(EthWorkerProcessReset);
eth_worker->active_process = eth_worker->init_process;

//eth_worker->callback = eth_worker_change_state;
Expand Down
3 changes: 3 additions & 0 deletions eth_worker.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
#pragma once

#include <stdint.h>

typedef struct EthWorker EthWorker;
typedef struct EthViewProcess EthViewProcess;
typedef struct EthViewDrawInit EthViewDrawInit;

typedef enum {
EthWorkerStateNotInited = 0,
Expand Down
3 changes: 3 additions & 0 deletions eth_worker_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct EthViewProcess {
uint8_t carriage;
uint8_t position;
uint8_t autofill;
uint8_t editing;
EthWorkerProcess type;
void* draw_struct;
};

struct EthWorker {
Expand Down
15 changes: 14 additions & 1 deletion finik_eth_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,26 +152,39 @@ int32_t finik_eth_app(void* p) {
app->cursor_position = CURSOR_CLICK_PROCESS;
view_port_update(app->view_port);
furi_delay_ms(150);
app->eth_worker->active_process->editing = 1;
char str[] = "test string 0 W5500 Test BIG CHARACTERS AND long string";
str[12] += cnt % 10;
cnt += 1;
ethernet_view_process_print(app->eth_worker->init_process, str);
app->cursor_position = CURSOR_INSIDE_PROCESS;
} else if(event.key == InputKeyRight) {
eth_worker_set_active_process(
app->eth_worker, (EthWorkerProcess)app->draw_process);
app->cursor_position = CURSOR_INSIDE_PROCESS;
} else if(event.key == InputKeyBack) {
app->cursor_position = CURSOR_EXIT_APP;
}
} else if(event.type == InputTypePress && app->cursor_position == CURSOR_INSIDE_PROCESS) {
if(event.key == InputKeyLeft) {
if(app->eth_worker->active_process->editing) {
if(event.key == InputKeyBack) {
app->eth_worker->active_process->editing = 0;
} else {
ethernet_view_process_keyevent(app->eth_worker->active_process, event.key);
}
} else if(event.key == InputKeyLeft) {
app->eth_worker->active_process->editing = 0;
app->cursor_position = CURSOR_CHOOSE_PROCESS;
} else if(event.key == InputKeyBack) {
ethernet_view_process_move(app->eth_worker->active_process, 0);
app->eth_worker->active_process->editing = 0;
app->cursor_position = CURSOR_CHOOSE_PROCESS;
} else if(event.key == InputKeyUp) {
ethernet_view_process_move(app->eth_worker->active_process, -1);
} else if(event.key == InputKeyDown) {
ethernet_view_process_move(app->eth_worker->active_process, 1);
} else if(event.key == InputKeyOk) {
app->eth_worker->active_process->editing = 1;
}
} else if(event.type == InputTypePress && app->cursor_position == CURSOR_EXIT_APP) {
if(event.key == InputKeyBack) {
Expand Down
Binary file added images/init_100x19px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c217027

Please sign in to comment.