Jelajahi Sumber

improve splash screen, add examples

Oliver Fabel 1 tahun lalu
induk
melakukan
9237153eaa

+ 1 - 1
.gitignore

@@ -1,7 +1,7 @@
 dist/*
 dist/*
 .vscode
 .vscode
 .clang-format
 .clang-format
+.clangd
 .editorconfig
 .editorconfig
 .env
 .env
 .ufbt
 .ufbt
-/temp

+ 23 - 6
examples/flipperzero_canvas_test.py

@@ -3,14 +3,31 @@ import flipperzero
 
 
 color = False
 color = False
 
 
-for x in range(0, 128):
-  color = not color
+def draw_action():
+  print('on draw')
 
 
-  for y in range(0, 64):
-    flipperzero.canvas_draw_dot(x, y, color)
+  global color
 
 
+  for x in range(0, 128):
     color = not color
     color = not color
 
 
-flipperzero.canvas_update()
+    for y in range(0, 64):
+      flipperzero.canvas_set_color(flipperzero.CANVAS_BLACK if color else flipperzero.CANVAS_WHITE)
+      flipperzero.canvas_draw_dot(x, y)
+
+      color = not color
+  
+  color = not color
+
+  flipperzero.canvas_set_text(64, 32, "Test")
+
+  flipperzero.canvas_update()
+
+print('start')
+
+draw_action()
+
+for _ in range(1, 5):
+  time.sleep(1)
 
 
-time.sleep(2)
+  draw_action()

+ 9 - 0
examples/flipperzero_dialog_message_test.py

@@ -0,0 +1,9 @@
+import flipperzero as f0
+
+f0.dialog_message_set_header('Important',64, 12)
+f0.dialog_message_set_text('Shutdown?', 64, 24)
+f0.dialog_message_set_button('Yes', f0.INPUT_BUTTON_LEFT)
+f0.dialog_message_set_button('No', f0.INPUT_BUTTON_RIGHT)
+
+while f0.dialog_message_show() is not f0.INPUT_BUTTON_LEFT:
+  pass

+ 12 - 0
examples/flipperzero_draw_on_input_test.py

@@ -0,0 +1,12 @@
+import time
+import flipperzero
+
+@flipperzero.on_input
+def on_input(button, type):
+  flipperzero.canvas_clear()
+  flipperzero.canvas_set_color(flipperzero.CANVAS_BLACK)
+  flipperzero.canvas_set_text(64, 32, '{button} - {type}'.format(button=button, type=type))
+  flipperzero.canvas_update()
+
+for _ in range(1,1000):
+  time.sleep_ms(10)

+ 9 - 0
examples/flipperzero_input_test.py

@@ -0,0 +1,9 @@
+import time
+import flipperzero
+
+@flipperzero.on_input
+def on_input(button, type):
+  print('{button} - {type}'.format(button=button, type=type))
+
+for _ in range(1,1000):
+  time.sleep_ms(10)

+ 1 - 0
examples/import_error_test.py

@@ -0,0 +1 @@
+import unknown_module

+ 1 - 0
examples/raise_test.py

@@ -0,0 +1 @@
+raise Exception('something went wrong')

+ 75 - 0
examples/tic_tac_toe.py

@@ -0,0 +1,75 @@
+import time
+import flipperzero as f0
+
+def init_grid():
+  return [
+    [' ', ' ', ' '],
+    [' ', ' ', ' '],
+    [' ', ' ', ' '],
+  ]
+
+m_exit = False
+
+m_grid = init_grid()
+
+m_x = 1
+m_y = 1
+
+m_is_cross = True
+
+@f0.on_input
+def input_handler(button, type):
+  global m_exit
+  global m_grid
+  global m_x
+  global m_y
+  global m_is_cross
+
+  if button == f0.INPUT_BUTTON_BACK and type == f0.INPUT_TYPE_LONG:
+    m_exit = True
+  elif button == f0.INPUT_BUTTON_BACK and type == f0.INPUT_TYPE_SHORT:
+    m_grid = init_grid()
+  elif button == f0.INPUT_BUTTON_LEFT and type == f0.INPUT_TYPE_SHORT:
+    m_x = (m_x - 1) % 3
+  elif button == f0.INPUT_BUTTON_RIGHT and type == f0.INPUT_TYPE_SHORT:
+    m_x = (m_x + 1) % 3
+  elif button == f0.INPUT_BUTTON_UP and type == f0.INPUT_TYPE_SHORT:
+    m_y = (m_y - 1) % 3
+  elif button == f0.INPUT_BUTTON_DOWN and type == f0.INPUT_TYPE_SHORT:
+    m_y = (m_y + 1) % 3
+  elif button == f0.INPUT_BUTTON_OK and type == f0.INPUT_TYPE_SHORT:
+    m_grid[m_x][m_y] = 'X' if m_is_cross else 'O'
+    m_is_cross = not m_is_cross
+
+def draw_grid():
+  global m_grid
+  global m_x
+  global m_y
+
+  f0.canvas_clear()
+  f0.canvas_draw_frame(2, 2, 60, 60)
+  
+  f0.canvas_draw_line(22, 2, 22, 62)
+  f0.canvas_draw_line(42, 2, 42, 62)
+  
+  f0.canvas_draw_line(2, 22, 62, 22)
+  f0.canvas_draw_line(2, 42, 62, 42)
+
+  px = m_x * 20 + 4
+  py = m_y * 20 + 4
+
+  f0.canvas_draw_frame(px, py, 16, 16)
+  
+  f0.canvas_set_text_align(f0.ALIGN_CENTER, f0.ALIGN_CENTER)
+
+  for x in range(0, 3):
+    for y in range(0, 3):
+      px = x * 20 + 10 + 2
+      py = y * 20 + 10 + 2
+      f0.canvas_set_text(px, py, m_grid[x][y])
+  
+  f0.canvas_update()
+
+while not m_exit:
+  draw_grid()
+  time.sleep_ms(25)

+ 4 - 0
examples/try_except_test.py

@@ -0,0 +1,4 @@
+try:
+  raise Exception('something went wrong')
+except Exception as e:
+  print(e)

TEMPAT SAMPAH
images/ButtonCenter_7x7.png


TEMPAT SAMPAH
images/Pin_back_arrow_10x8.png


TEMPAT SAMPAH
images/splash.png


+ 3 - 20
splash.svg

@@ -2,9 +2,9 @@
 <!-- Created with Inkscape (http://www.inkscape.org/) -->
 <!-- Created with Inkscape (http://www.inkscape.org/) -->
 
 
 <svg
 <svg
-   width="128"
+   width="63"
    height="64"
    height="64"
-   viewBox="0 0 33.866666 16.933333"
+   viewBox="0 0 16.66875 16.933333"
    version="1.1"
    version="1.1"
    id="svg1"
    id="svg1"
    inkscape:version="1.3.2 (091e20ef0f, 2023-11-25)"
    inkscape:version="1.3.2 (091e20ef0f, 2023-11-25)"
@@ -29,7 +29,7 @@
      showguides="true"
      showguides="true"
      inkscape:zoom="6.7496002"
      inkscape:zoom="6.7496002"
      inkscape:cx="64.003791"
      inkscape:cx="64.003791"
-     inkscape:cy="32.001896"
+     inkscape:cy="32.001895"
      inkscape:window-width="2000"
      inkscape:window-width="2000"
      inkscape:window-height="1252"
      inkscape:window-height="1252"
      inkscape:window-x="0"
      inkscape:window-x="0"
@@ -162,22 +162,5 @@
        height="1.0583333"
        height="1.0583333"
        x="14.419791"
        x="14.419791"
        y="14.287499" />
        y="14.287499" />
-    <text
-       xml:space="preserve"
-       style="font-style:normal;font-variant:normal;font-weight:normal;font-stretch:normal;font-size:4.23333px;line-height:1;font-family:Sans;-inkscape-font-specification:Sans;letter-spacing:-0.0529167px;fill:#000000;fill-opacity:1;stroke:none;stroke-width:2;stroke-linecap:round"
-       x="18.140495"
-       y="7.1850882"
-       id="text16"><tspan
-         sodipodi:role="line"
-         style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333px;line-height:1;font-family:'Open Sans';-inkscape-font-specification:'Open Sans Bold';letter-spacing:-0.0529167px;fill:#000000;fill-opacity:1;stroke-width:2"
-         x="18.140495"
-         y="7.1850882"
-         id="tspan17"
-         dy="0">Micro</tspan><tspan
-         sodipodi:role="line"
-         style="font-style:normal;font-variant:normal;font-weight:bold;font-stretch:normal;font-size:4.23333px;line-height:1;font-family:'Open Sans';-inkscape-font-specification:'Open Sans Bold';letter-spacing:-0.0529167px;fill:#000000;fill-opacity:1;stroke-width:2"
-         x="18.140495"
-         y="11.519282"
-         id="tspan18">Python</tspan></text>
   </g>
   </g>
 </svg>
 </svg>

+ 68 - 7
upython.c

@@ -12,6 +12,14 @@
 
 
 #define TAG "uPython"
 #define TAG "uPython"
 
 
+typedef enum {
+    ActionNone,
+    ActionOpen,
+    ActionExit
+} Action;
+
+static Action action = ActionNone;
+
 static void execute_file(FuriString* file) {
 static void execute_file(FuriString* file) {
     size_t stack;
     size_t stack;
 
 
@@ -71,8 +79,34 @@ static bool select_python_file(FuriString* file_path) {
     return result;
     return result;
 }
 }
 
 
+static void on_input(const void* event, void* ctx) {
+    UNUSED(ctx);
+
+    InputKey key = ((InputEvent*)event)->key;
+    InputType type = ((InputEvent*)event)->type;
+
+    if(type != InputTypeRelease) {
+        return;
+    }
+
+    switch(key) {
+    case InputKeyOk:
+        action = ActionOpen;
+        break;
+    case InputKeyBack:
+        action = ActionExit;
+        break;
+    default:
+        action = ActionNone;
+        break;
+    }
+}
+
 static void show_splash_screen() {
 static void show_splash_screen() {
     Gui* gui = furi_record_open(RECORD_GUI);
     Gui* gui = furi_record_open(RECORD_GUI);
+    FuriPubSub* input_event_queue = furi_record_open(RECORD_INPUT_EVENTS);
+    FuriPubSubSubscription* input_event = furi_pubsub_subscribe(input_event_queue, on_input, NULL);
+
     ViewPort* view_port = view_port_alloc();
     ViewPort* view_port = view_port_alloc();
 
 
     gui_add_view_port(gui, view_port, GuiLayerFullscreen);
     gui_add_view_port(gui, view_port, GuiLayerFullscreen);
@@ -80,30 +114,57 @@ static void show_splash_screen() {
     Canvas* canvas = gui_direct_draw_acquire(gui);
     Canvas* canvas = gui_direct_draw_acquire(gui);
 
 
     canvas_draw_icon(canvas, 0, 0, &I_splash);
     canvas_draw_icon(canvas, 0, 0, &I_splash);
+    canvas_set_color(canvas, ColorBlack);
+    canvas_set_font(canvas, FontSecondary);
+    canvas_draw_str_aligned(canvas, 66, 0, AlignLeft, AlignTop, "Micro");
+    canvas_set_font(canvas, FontPrimary);
+    canvas_draw_str_aligned(canvas, 66, 10, AlignLeft, AlignTop, "Python");
+
+    canvas_set_font(canvas, FontSecondary);
+
+    canvas_draw_icon(canvas, 75, 36, &I_ButtonCenter_7x7);
+    canvas_draw_str_aligned(canvas, 87, 36, AlignLeft, AlignTop, "Open");
+
+    canvas_draw_icon(canvas, 73, 50, &I_Pin_back_arrow_10x8);
+    canvas_draw_str_aligned(canvas, 87, 51, AlignLeft, AlignTop, "Exit");
+
     canvas_commit(canvas);
     canvas_commit(canvas);
 
 
-    furi_delay_ms(5000);
+    while(action == ActionNone) {
+        furi_delay_ms(1);
+    }
+
+    furi_pubsub_unsubscribe(input_event_queue, input_event);
 
 
     gui_direct_draw_release(gui);
     gui_direct_draw_release(gui);
     gui_remove_view_port(gui, view_port);
     gui_remove_view_port(gui, view_port);
 
 
     view_port_free(view_port);
     view_port_free(view_port);
 
 
+    furi_record_close(RECORD_INPUT_EVENTS);
     furi_record_close(RECORD_GUI);
     furi_record_close(RECORD_GUI);
 }
 }
 
 
 int32_t upython(void* p) {
 int32_t upython(void* p) {
     UNUSED(p);
     UNUSED(p);
 
 
-    show_splash_screen();
+    do {
+        show_splash_screen();
 
 
-    FuriString* file_path = furi_string_alloc();
+        if(action == ActionExit) {
+            break;
+        }
 
 
-    if(select_python_file(file_path)) {
-        execute_file(file_path);
-    }
+        FuriString* file_path = furi_string_alloc();
 
 
-    furi_string_free(file_path);
+        if(select_python_file(file_path)) {
+            execute_file(file_path);
+        }
+
+        furi_string_free(file_path);
+
+        action = ActionNone;
+    } while(true);
 
 
     return 0;
     return 0;
 }
 }