Skip to content

Commit

Permalink
update micropython port code
Browse files Browse the repository at this point in the history
  • Loading branch information
ofabel committed Aug 4, 2024
1 parent 86de410 commit 3df1e96
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 7 deletions.
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ App(
entry_point="mp_flipper_app",
stack_size=4 * 1024,
fap_category="Tools",
fap_version="0.4",
fap_version="0.5",
fap_description="compile and execute MicroPython scripts",
fap_icon="icon.png",
fap_author="Oliver Fabel",
Expand Down
5 changes: 5 additions & 0 deletions lib/micropython-port/mp_flipper_context.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#include <furi.h>
#include <gui/gui.h>
#include <dialogs/dialogs.h>

typedef struct {
Gui* gui;
ViewPort* view_port;
Canvas* canvas;
FuriPubSub* input_event_queue;
FuriPubSubSubscription* input_event;
DialogMessage* dialog_message;
const char* dialog_message_button_left;
const char* dialog_message_button_center;
const char* dialog_message_button_right;
} mp_flipper_context_t;
11 changes: 5 additions & 6 deletions lib/micropython-port/mp_flipper_modflipperzero_canvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ inline void mp_flipper_canvas_draw_disc(uint8_t x, uint8_t y, uint8_t r) {
inline void mp_flipper_canvas_set_font(uint8_t font) {
mp_flipper_context_t* ctx = mp_flipper_context;

canvas_set_font(
ctx->canvas, font == MP_FLIPPER_CANVAS_FONT_PRIMARY ? FontPrimary : FontSecondary);
canvas_set_font(ctx->canvas, font == MP_FLIPPER_FONT_PRIMARY ? FontPrimary : FontSecondary);
}

inline void mp_flipper_canvas_set_color(uint8_t color) {
Expand All @@ -88,11 +87,11 @@ inline void mp_flipper_canvas_set_text(uint8_t x, uint8_t y, const char* text) {
}

inline void mp_flipper_canvas_set_text_align(uint8_t x, uint8_t y) {
Align align_x = x == MP_FLIPPER_CANVAS_ALIGN_BEGIN ? AlignLeft : AlignRight;
Align align_y = y == MP_FLIPPER_CANVAS_ALIGN_BEGIN ? AlignTop : AlignBottom;
Align align_x = x == MP_FLIPPER_ALIGN_BEGIN ? AlignLeft : AlignRight;
Align align_y = y == MP_FLIPPER_ALIGN_BEGIN ? AlignTop : AlignBottom;

text_align_x = x == MP_FLIPPER_CANVAS_ALIGN_CENTER ? AlignCenter : align_x;
text_align_y = y == MP_FLIPPER_CANVAS_ALIGN_CENTER ? AlignCenter : align_y;
text_align_x = x == MP_FLIPPER_ALIGN_CENTER ? AlignCenter : align_x;
text_align_y = y == MP_FLIPPER_ALIGN_CENTER ? AlignCenter : align_y;
}

inline void mp_flipper_canvas_update() {
Expand Down
101 changes: 101 additions & 0 deletions lib/micropython-port/mp_flipper_modflipperzero_dialog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <dialogs/dialogs.h>

#include <mp_flipper_modflipperzero.h>
#include <mp_flipper_runtime.h>

#include "mp_flipper_context.h"

void mp_flipper_dialog_message_set_text(
const char* text,
uint8_t x,
uint8_t y,
uint8_t h,
uint8_t v) {
mp_flipper_context_t* ctx = mp_flipper_context;

Align align_x = x == MP_FLIPPER_ALIGN_BEGIN ? AlignLeft : AlignRight;
Align align_y = y == MP_FLIPPER_ALIGN_BEGIN ? AlignTop : AlignBottom;

align_x = x == MP_FLIPPER_ALIGN_CENTER ? AlignCenter : align_x;
align_y = y == MP_FLIPPER_ALIGN_CENTER ? AlignCenter : align_y;

dialog_message_set_text(ctx->dialog_message, text, x, y, align_x, align_y);
}

void mp_flipper_dialog_message_set_header(
const char* text,
uint8_t x,
uint8_t y,
uint8_t h,
uint8_t v) {
mp_flipper_context_t* ctx = mp_flipper_context;

Align align_x = x == MP_FLIPPER_ALIGN_BEGIN ? AlignLeft : AlignRight;
Align align_y = y == MP_FLIPPER_ALIGN_BEGIN ? AlignTop : AlignBottom;

align_x = x == MP_FLIPPER_ALIGN_CENTER ? AlignCenter : align_x;
align_y = y == MP_FLIPPER_ALIGN_CENTER ? AlignCenter : align_y;

dialog_message_set_header(ctx->dialog_message, text, x, y, align_x, align_y);
}

void mp_flipper_dialog_message_set_button(const char* text, uint8_t button) {
mp_flipper_context_t* ctx = mp_flipper_context;

// left button
if(button == MP_FLIPPER_INPUT_BUTTON_LEFT) {
ctx->dialog_message_button_left = text;
}
// center button
else if(button == MP_FLIPPER_INPUT_BUTTON_OK) {
ctx->dialog_message_button_center = text;
}
// right button
else if(button == MP_FLIPPER_INPUT_BUTTON_RIGHT) {
ctx->dialog_message_button_right = text;
}

dialog_message_set_buttons(
ctx->dialog_message,
ctx->dialog_message_button_left,
ctx->dialog_message_button_center,
ctx->dialog_message_button_right);
}

uint8_t mp_flipper_dialog_message_show() {
mp_flipper_context_t* ctx = mp_flipper_context;

gui_direct_draw_release(ctx->gui);

DialogsApp* dialog = furi_record_open(RECORD_DIALOGS);

uint8_t button = dialog_message_show(dialog, ctx->dialog_message);

furi_record_close(RECORD_DIALOGS);

ctx->canvas = gui_direct_draw_acquire(ctx->gui);

switch(button) {
case DialogMessageButtonLeft:
return MP_FLIPPER_INPUT_BUTTON_LEFT;
case DialogMessageButtonCenter:
return MP_FLIPPER_INPUT_BUTTON_OK;
case DialogMessageButtonRight:
return MP_FLIPPER_INPUT_BUTTON_RIGHT;
case DialogMessageButtonBack:
default:
return MP_FLIPPER_INPUT_BUTTON_BACK;
}
}

void mp_flipper_dialog_message_clear() {
mp_flipper_context_t* ctx = mp_flipper_context;

dialog_message_free(ctx->dialog_message);

ctx->dialog_message = dialog_message_alloc();

ctx->dialog_message_button_left = NULL;
ctx->dialog_message_button_center = NULL;
ctx->dialog_message_button_right = NULL;
}
7 changes: 7 additions & 0 deletions lib/micropython-port/mp_flipper_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ void* mp_flipper_context_alloc() {

ctx->canvas = gui_direct_draw_acquire(ctx->gui);

ctx->dialog_message = dialog_message_alloc();
ctx->dialog_message_button_left = NULL;
ctx->dialog_message_button_center = NULL;
ctx->dialog_message_button_right = NULL;

return ctx;
}

Expand All @@ -93,6 +98,8 @@ void mp_flipper_context_free(void* context) {

view_port_free(ctx->view_port);

dialog_message_free(ctx->dialog_message);

furi_record_close(RECORD_GUI);
furi_record_close(RECORD_INPUT_EVENTS);

Expand Down

0 comments on commit 3df1e96

Please sign in to comment.