Skip to content

Commit

Permalink
add power consumption monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
karasevia committed Jul 2, 2023
1 parent c0684d7 commit 4500353
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 35 deletions.
1 change: 1 addition & 0 deletions application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ App(
cdefines=["FINIK_ETH"],
requires=[
"gui",
"power",
],
stack_size=10 * 1024,
order=90,
Expand Down
125 changes: 90 additions & 35 deletions finik_eth_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,69 @@

#define TAG "FinikEthApp"

static void draw_process_selector(Canvas* canvas, DrawProcess selector, CursorPosition cursor) {
uint8_t y = 0;
if(selector == PROCESS_INIT) y = 11;
if(selector == PROCESS_DHCP) y = 22;
if(selector == PROCESS_STATIC) y = 34;
if(selector == PROCESS_PING) y = 44;
if(selector == PROCESS_RESET) y = 55;

if(cursor != CURSOR_INSIDE_PROCESS) {
canvas_draw_line(canvas, 0, y + 1, 0, y + 7);
canvas_draw_line(canvas, 23, y + 1, 23, y + 7);
}

canvas_draw_line(canvas, 1, y, 22, y);
canvas_draw_line(canvas, 1, y + 8, 22, y + 8);

if(cursor == CURSOR_CLICK_PROCESS) {
canvas_draw_box(canvas, 1, y, 22, 9);
}
}

static void draw_battery_cunsumption(Canvas* canvas, double cons) {
FuriString* string = furi_string_alloc_set("aaaaaaaa");
if(cons >= 0) {
furi_string_printf(string, "--");
} else if(cons < -1) {
furi_string_printf(string, "%1.1fk", -cons);
} else {
furi_string_printf(string, "%3.f", -(cons * 1000));
}

canvas_draw_str(canvas, 112, 7, furi_string_get_cstr(string));
furi_string_free(string);
}

static void finik_eth_app_draw_callback(Canvas* canvas, void* ctx) {
furi_assert(ctx);
FinikEthApp* app = ctx;

canvas_clear(canvas);

DrawMode mode = app->draw_mode;
if(mode == DRAW_ONLY_PICTURES || mode == DRAW_ALL)
canvas_draw_icon(canvas, 0, 29, &I_amperka_ru_logo_128x35px);
if(mode == DRAW_ONLY_TEXT || mode == DRAW_ALL) {
canvas_set_font(canvas, FontPrimary);
canvas_draw_str(canvas, 4, 8, "This is an example app!");
canvas_set_font(canvas, FontSecondary);
elements_multiline_text_aligned(
canvas,
127,
15,
AlignRight,
AlignTop,
"Some long long long long \n aligned multiline text");
DrawProcess process = app->draw_process;
CursorPosition cursor = app->cursor_position;
float consumption = app->info.current_gauge;

canvas_set_font(canvas, FontSecondary);

if(cursor == CURSOR_EXIT_APP) {
canvas_draw_icon(canvas, 0, 0, &I_exit_128x64px);
} else {
canvas_draw_icon(canvas, 0, 0, &I_main_128x64px);
draw_process_selector(canvas, process, cursor);
draw_battery_cunsumption(canvas, (double)consumption);
}
}

static void finik_eth_battery_info_update_model(void* ctx) {
furi_assert(ctx);
FinikEthApp* app = ctx;
power_get_info(app->power, &app->info);
}

static void finik_eth_app_input_callback(InputEvent* input_event, void* ctx) {
furi_assert(ctx);

Expand All @@ -55,9 +95,10 @@ FinikEthApp* finik_eth_app_alloc() {

app->notifications = furi_record_open(RECORD_NOTIFICATION);

app->eth_worker = eth_worker_alloc();

eth_worker_task(app->eth_worker);
app->power = furi_record_open(RECORD_POWER);
//app->eth_worker = eth_worker_alloc();

//eth_worker_task(app->eth_worker);

return app;
}
Expand All @@ -75,34 +116,48 @@ void finik_eth_app_free(FinikEthApp* app) {
furi_record_close(RECORD_NOTIFICATION);
}

void finik_eth_update_consumtion(FinikEthApp* app) {
furi_assert(app);
}

int32_t finik_eth_app(void* p) {
UNUSED(p);
FinikEthApp* app = finik_eth_app_alloc();

InputEvent event;

while(1) {
if(furi_message_queue_get(app->event_queue, &event, 100) == FuriStatusOk) {
finik_eth_battery_info_update_model(app);
if(furi_message_queue_get(app->event_queue, &event, 300) == FuriStatusOk) {
if(event.type == InputTypePress) {
if(event.key == InputKeyBack)
break;
else if(event.key == InputKeyUp) {
FURI_LOG_I(TAG, "example_led_sequence");
notification_message(app->notifications, &example_led_sequence);
} else if(event.key == InputKeyDown) {
FURI_LOG_I(TAG, "example_vibro_sequence");
notification_message(app->notifications, &example_vibro_sequence);
} else if(event.key == InputKeyOk) {
FURI_LOG_I(TAG, "example_sound_sequence");
notification_message(app->notifications, &example_sound_sequence);
if(app->cursor_position == CURSOR_CHOOSE_PROCESS) {
if(event.key == InputKeyUp) {
app->draw_process =
(app->draw_process + PROCESS_RESET) % (PROCESS_RESET + 1);
} else if(event.key == InputKeyDown) {
app->draw_process =
(app->draw_process + PROCESS_RESET + 2) % (PROCESS_RESET + 1);
} else if(event.key == InputKeyRight) {
app->cursor_position = CURSOR_INSIDE_PROCESS;
} else if(event.key == InputKeyOk) {
app->cursor_position = CURSOR_CLICK_PROCESS;
view_port_update(app->view_port);
furi_delay_ms(150);
app->cursor_position = CURSOR_INSIDE_PROCESS;
} else if(event.key == InputKeyBack) {
app->cursor_position = CURSOR_EXIT_APP;
}
} else if(app->cursor_position == CURSOR_INSIDE_PROCESS) {
if(event.key == InputKeyLeft || event.key == InputKeyBack) {
app->cursor_position = CURSOR_CHOOSE_PROCESS;
}
} else if(app->cursor_position == CURSOR_EXIT_APP) {
if(event.key == InputKeyBack) {
break;
} else if(event.key == InputKeyOk) {
app->cursor_position = CURSOR_CHOOSE_PROCESS;
}
}
} else if(event.type == InputTypeLong) {
DrawMode mode = app->draw_mode;
if(event.key == InputKeyLeft)
app->draw_mode = (mode - 1 + TOTAL_DRAW_MODES) % TOTAL_DRAW_MODES;
else if(event.key == InputKeyRight)
app->draw_mode = (mode + 1) % TOTAL_DRAW_MODES;

view_port_update(app->view_port);
}
}
Expand Down
21 changes: 21 additions & 0 deletions finik_eth_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <furi.h>
#include <gui/gui.h>
#include <notification/notification_messages.h>
#include <power/power_service/power.h>

#include "eth_worker.h"
#include "finik_eth_icons.h"
Expand All @@ -14,14 +15,34 @@ typedef enum {
TOTAL_DRAW_MODES = 3,
} DrawMode;

typedef enum {
PROCESS_INIT,
PROCESS_DHCP,
PROCESS_STATIC,
PROCESS_PING,
PROCESS_RESET,
} DrawProcess;

typedef enum {
CURSOR_CHOOSE_PROCESS,
CURSOR_CLICK_PROCESS,
CURSOR_INSIDE_PROCESS,
CURSOR_EXIT_APP,
} CursorPosition;

struct FinikEthApp {
Gui* gui;
ViewPort* view_port;
FuriMessageQueue* event_queue;
NotificationApp* notifications;
EthWorker* eth_worker;

Power* power;
PowerInfo info;

DrawMode draw_mode;
DrawProcess draw_process;
CursorPosition cursor_position;
};

typedef struct FinikEthApp FinikEthApp;
Expand Down
Binary file added images/exit_128x64px.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/main_128x64px.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 4500353

Please sign in to comment.