From 808e0772c21e0915a506e5fe7b537a9e94e26727 Mon Sep 17 00:00:00 2001 From: Derek Jamison Date: Thu, 1 Jun 2023 17:16:55 -0500 Subject: [PATCH] Simple ViewPort demo - Hello World --- ui/viewport_demo/README.md | 6 +++ ui/viewport_demo/application.fam | 18 +++++++ ui/viewport_demo/demo.c | 89 +++++++++++++++++++++++++++++++ ui/viewport_demo/icon.png | Bin 0 -> 1916 bytes 4 files changed, 113 insertions(+) create mode 100644 ui/viewport_demo/README.md create mode 100644 ui/viewport_demo/application.fam create mode 100644 ui/viewport_demo/demo.c create mode 100644 ui/viewport_demo/icon.png diff --git a/ui/viewport_demo/README.md b/ui/viewport_demo/README.md new file mode 100644 index 0000000..1081441 --- /dev/null +++ b/ui/viewport_demo/README.md @@ -0,0 +1,6 @@ +# viewport demo +This is a UI demo of the ViewPort class. It is based off the wiki code for the [ViewPort] https://github.com/jamisonderek/flipper-zero-tutorials/wiki/User-Interface#viewport section of the User-Interface page. + +The draw callback does some basic text drawing on a [Canvas](https://github.com/jamisonderek/flipper-zero-tutorials/wiki/User-Interface#canvas). + +The input callback changes the X and Y position of a cursor "^" based on the key that is pressed. You can see that when the ViewPort orientation is rotated, both the screen and the keypress is translated automatically. Pressing the back button uses a [Message Queue](https://github.com/jamisonderek/flipper-zero-tutorials/wiki/Message-Queue) to signal that application should exit. diff --git a/ui/viewport_demo/application.fam b/ui/viewport_demo/application.fam new file mode 100644 index 0000000..f51c766 --- /dev/null +++ b/ui/viewport_demo/application.fam @@ -0,0 +1,18 @@ +App( + appid="viewport_demo", + name="Viewport demo", + apptype=FlipperAppType.EXTERNAL, + entry_point="viewport_demo_app", + cdefines=["VIEWPORT_DEMO_APP"], + requires=[ + "gui", + ], + stack_size=2 * 1024, + order=100, + fap_description = "Viewport demo", + fap_author = "codeallnight", + fap_weburl = "https://github.com/jamisonderek/Flipper-Zero-tutorials", + fap_category="UI", + fap_icon="icon.png", + fap_libs=["assets"], +) \ No newline at end of file diff --git a/ui/viewport_demo/demo.c b/ui/viewport_demo/demo.c new file mode 100644 index 0000000..cc3cbe4 --- /dev/null +++ b/ui/viewport_demo/demo.c @@ -0,0 +1,89 @@ +/* + +This is an example of using a ViewPort. It is a simple Hello World program that +allows you to move a cursor around the screen with the arrow keys. Pressing +the back button will exit the program. + +Uncomment the different view_port_set_orientation() calls to see how the +orientation of the screen and keypad change. + +The code is from the Message Queue wiki page +(https://github.com/jamisonderek/flipper-zero-tutorials/wiki/Message-Queue) and +also the ViewPort section of the User Interface wiki page +(https://github.com/jamisonderek/flipper-zero-tutorials/wiki/User-Interface#viewport). + +*/ + +#include +#include + +typedef enum { + MyEventTypeKey, + MyEventTypeDone, +} MyEventType; + +typedef struct { + MyEventType type; // The reason for this event. + InputEvent input; // This data is specific to keypress data. +} MyEvent; + +FuriMessageQueue* queue; +int x = 32; +int y = 48; + +static void my_draw_callback(Canvas* canvas, void* context) { + UNUSED(context); + canvas_set_font(canvas, FontPrimary); + canvas_draw_str(canvas, 5, 30, "Hello world"); + canvas_draw_str(canvas, x, y, "^"); +} + +static void my_input_callback(InputEvent* input_event, void* context) { + UNUSED(context); + if(input_event->type == InputTypeShort) { + if(input_event->key == InputKeyLeft) x--; + if(input_event->key == InputKeyRight) x++; + if(input_event->key == InputKeyUp) y--; + if(input_event->key == InputKeyDown) y++; + if(input_event->key == InputKeyBack) { + MyEvent event; + event.type = MyEventTypeDone; + furi_message_queue_put(queue, &event, FuriWaitForever); + } + } +} + +int32_t viewport_demo_app() { + void* my_context = NULL; + queue = furi_message_queue_alloc(8, sizeof(MyEvent)); + + ViewPort* view_port = view_port_alloc(); + view_port_draw_callback_set(view_port, my_draw_callback, my_context); + view_port_input_callback_set(view_port, my_input_callback, my_context); + //view_port_set_orientation(view_port, ViewPortOrientationHorizontal); + //view_port_set_orientation(view_port, ViewPortOrientationHorizontalFlip); // upside down + view_port_set_orientation(view_port, ViewPortOrientationVertical); // USB/keypad bottom + //view_port_set_orientation(view_port, ViewPortOrientationVerticalFlip); // USB/keypad top + + Gui* gui = furi_record_open(RECORD_GUI); + gui_add_view_port(gui, view_port, GuiLayerFullscreen); + + MyEvent event; + bool keep_processing = true; + while(keep_processing) { + if(furi_message_queue_get(queue, &event, FuriWaitForever) == FuriStatusOk) { + if(event.type == MyEventTypeDone) { + keep_processing = false; + } + } else { + keep_processing = false; + } + } + + furi_message_queue_free(queue); + view_port_enabled_set(view_port, false); + gui_remove_view_port(gui, view_port); + furi_record_close(RECORD_GUI); + view_port_free(view_port); + return 0; +} \ No newline at end of file diff --git a/ui/viewport_demo/icon.png b/ui/viewport_demo/icon.png new file mode 100644 index 0000000000000000000000000000000000000000..17dfac6650e2f6bcb208a720e9fcba3bd4bd87f6 GIT binary patch literal 1916 zcmcIl&2Jk;6yMMfl~n1ud>mF=4n<|W-=DG@96QvOvcXD?k{*y4&&=AZcz2fFP3+{r zp*>X;^~8-!CC)`$;Q&G$-~tE!3M3@{0YrGaYr94YjW4WZ&(7?d-}}AyesAV+Z};BD z`s?e0AZ&Cy?L8Qa_w{R+;QyOHHlM<&fxWfAAb7;hF6o$gG>-^{8;?fKEHG04Y2w(>>uO@{*Fg@RKt`HSZx|5U@Zvs z&1r(kAO&wS!w%vC`P0=)2 zv1C<8s_hw;ryAnyk-(oQ)>9gId+qmT{$SRSMtPoivOJkgY7@Q2)1j=ot}82=tZ4`= zP<9gMc#7ieRs}MHX|s%^VUmYD77JKB;755w0?Jh^QL?~}vsuC*b@CJ^vRYG$*ye`5 z|8mnPT3BcK_Az*wkvz^b{-eVa*N(tLOW^sON?kpMcoM$8cAKVt7|HY7!g~Q}1 z9hW(xL}p_a0d@wqYRND0S}DUhPy3vY=N0SCJ&KxMGsWBcVNCfXd%IvX+rZj5XAP;S z9#UMSntfIG4AV25JBscpN@ePUASll9YQ~&~!O1G7zVCJ8EXOfn-F8C)QnfIoo)tKT zJFqb_0?k5-ZZPCv0=gQQHYI_=bj>Mw!TvTUM@1%z{gr+xCxEfYriZbmsXArIrkEmI zHxx8b90M`UbOPJ8)xb38Xz!;X^bj5|qZX`a!Af&13!6G30ddekGca;k5Fmy%;+TeE z6Wf)<7k*oO%u}CJ$gVDllz0J8BMkZpBRphs5)bE+22q0(2&NNeP{EV3U8>$Go8~_I zcxh?Y0^?9D_2^+zv_V56Mg19-X3ODuiCrm#3U)GL0QwSLRAw0u@(E5^a|lJaSa-4OHV&77s-+ zTq0JhaK^(7LZ06+#cXj)u^1AT!X5PY7oR?Y`|GW4yV<|FPu_jyN$<&LKmT;?%V#^v mrDjX*tX=;8wc9&a;SsKUCER>woqkwcc-`&Y_VcZe9{vM;8ak~2 literal 0 HcmV?d00001