From af926c5f194546bb6bf6dded64d69d33651d70e7 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Fri, 20 Mar 2020 13:57:11 +0100 Subject: [PATCH] snippets/ev3/buttons_quickstart: initial project This is a demo of: - multi-file project - using images - creating a "menu" This program can be started away from the competition table. Then a mission can be quickly activated by pressing one of the buttons. --- snippets/ev3/buttons_quickstart/.gitignore | 3 + .../.vscode/extensions.json | 13 +++ .../buttons_quickstart/.vscode/launch.json | 14 +++ .../buttons_quickstart/.vscode/settings.json | 7 ++ snippets/ev3/buttons_quickstart/buttons.png | Bin 0 -> 2651 bytes snippets/ev3/buttons_quickstart/buttons.svg | 101 ++++++++++++++++++ snippets/ev3/buttons_quickstart/main.py | 27 +++++ snippets/ev3/buttons_quickstart/menu.py | 30 ++++++ 8 files changed, 195 insertions(+) create mode 100644 snippets/ev3/buttons_quickstart/.gitignore create mode 100644 snippets/ev3/buttons_quickstart/.vscode/extensions.json create mode 100644 snippets/ev3/buttons_quickstart/.vscode/launch.json create mode 100644 snippets/ev3/buttons_quickstart/.vscode/settings.json create mode 100644 snippets/ev3/buttons_quickstart/buttons.png create mode 100644 snippets/ev3/buttons_quickstart/buttons.svg create mode 100644 snippets/ev3/buttons_quickstart/main.py create mode 100644 snippets/ev3/buttons_quickstart/menu.py diff --git a/snippets/ev3/buttons_quickstart/.gitignore b/snippets/ev3/buttons_quickstart/.gitignore new file mode 100644 index 0000000..9b5f630 --- /dev/null +++ b/snippets/ev3/buttons_quickstart/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +venv/ diff --git a/snippets/ev3/buttons_quickstart/.vscode/extensions.json b/snippets/ev3/buttons_quickstart/.vscode/extensions.json new file mode 100644 index 0000000..f8f1a44 --- /dev/null +++ b/snippets/ev3/buttons_quickstart/.vscode/extensions.json @@ -0,0 +1,13 @@ +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations. + // Extension identifier format: ${publisher}.${name}. Example: vscode.csharp + + // List of extensions which should be recommended for users of this workspace. + "recommendations": [ + "lego-education.ev3-micropython" + ], + // List of extensions recommended by VS Code that should not be recommended for users of this workspace. + "unwantedRecommendations": [ + "ms-python.python" + ] +} \ No newline at end of file diff --git a/snippets/ev3/buttons_quickstart/.vscode/launch.json b/snippets/ev3/buttons_quickstart/.vscode/launch.json new file mode 100644 index 0000000..af12883 --- /dev/null +++ b/snippets/ev3/buttons_quickstart/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Download and Run", + "type": "ev3devBrowser", + "request": "launch", + "program": "/home/robot/${workspaceRootFolderName}/main.py" + } + ] +} diff --git a/snippets/ev3/buttons_quickstart/.vscode/settings.json b/snippets/ev3/buttons_quickstart/.vscode/settings.json new file mode 100644 index 0000000..466469b --- /dev/null +++ b/snippets/ev3/buttons_quickstart/.vscode/settings.json @@ -0,0 +1,7 @@ +// Place your settings in this file to overwrite default and user settings. +{ + "files.eol": "\n", + "debug.openDebug": "neverOpen", + "python.linting.enabled": false, + "ev3devBrowser.download.exclude": "{**/.*,**/*.svg}" +} diff --git a/snippets/ev3/buttons_quickstart/buttons.png b/snippets/ev3/buttons_quickstart/buttons.png new file mode 100644 index 0000000000000000000000000000000000000000..232026b8ff0d0c13be2963415b18dfdca0df2f64 GIT binary patch literal 2651 zcmb_e2{)VD8kW{Pl_Q4AF@y?Qt|XKFOZL0lIxk5(0q7 z@o0Cva}*9w@Q)4RBM=B@LLBqBY+T+!dN*f( zBj53?J0Di&M-{b%7m=#+Q~IQk)%KpDFp=9hKctU~d%U!jO^I$rJ}SsH3n_ftAw%Gy z_82*|%)hK`L9FfpCfIK0pzC2p#~`V?WoN3Hm(mI8{hCCH>ZY2u{Gn)Dd_s!RWAsQl zT`Kt=MMPdJuuDL+G}{08D(DV_$uz8~squ@5uwxnwRv2YwW${lI42g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W>cxugxw8jOSy4w>cY}ez>lwMdChfs6p-q< zb6{Cn*=PbG*i_U*waGOfxC}K99%#Q{h9L#hmzP6;PW6AsyxNF?8gA|EV1j})7z{>9 zBdwkS^6S1SQCnAMiAD=lRaG@cYNIEHL(TymjJC7{d3t(EDJcy^$SwXc`zAL(ALZ@+ z>&@(JM_=DU;OgId@?^YuCv%b^+gZVF1i8`#|9Y<7*(b8QN<*tGcHI!e>1v_AbUrJsc5SB^d9n$BIA2TzB zjxm%`j)Iw>($doSpL3KcVR?piwIz{grmwH>URb4|tE=nc>UuIBb5^YDhPYjC%FplD zDiDdqJT1od))w;eW!aq{*}}*5;Yx8pT>z6Jx-mN9_>BnlfH!nHJ+H}D z+6x2KO+P%}{(jlqQZ=fIF~*0#-KTJV9$OxNguwySoKF0i^luwd8l7Q(TKGSfB?Afb ze)N2JXLt80XMiSBl>hthqiy?EcR3Am0GTFi%w^07s`tpbHG7wi@2lg#k)tyPi~ehP i4x|6a|NNhqyDBz + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + diff --git a/snippets/ev3/buttons_quickstart/main.py b/snippets/ev3/buttons_quickstart/main.py new file mode 100644 index 0000000..23116fe --- /dev/null +++ b/snippets/ev3/buttons_quickstart/main.py @@ -0,0 +1,27 @@ +#!/usr/bin/env pybricks-micropython + +from pybricks.hubs import EV3Brick +from pybricks.parameters import Button + +from menu import wait_for_button + +# Initialize the EV3. +ev3 = EV3Brick() + +while True: + # Show the menu and wait for one button to be selected. + button = wait_for_button(ev3) + + # Now you can do something, based on which button was pressed. + + # In this demo, we just play a different sound for each button. + if button == Button.LEFT: + ev3.speaker.beep(200) + elif button == Button.RIGHT: + ev3.speaker.beep(400) + elif button == Button.UP: + ev3.speaker.beep(600) + elif button == Button.DOWN: + ev3.speaker.beep(800) + elif button == Button.CENTER: + ev3.speaker.beep(1000) diff --git a/snippets/ev3/buttons_quickstart/menu.py b/snippets/ev3/buttons_quickstart/menu.py new file mode 100644 index 0000000..e3789a3 --- /dev/null +++ b/snippets/ev3/buttons_quickstart/menu.py @@ -0,0 +1,30 @@ +def wait_for_button(ev3): + """ + This function shows a picture of the buttons on the EV3 screen. + + Then it waits until you press a button. + + It returns which button was pressed. + """ + + # Show a picture of the buttons on the screen. + ev3.screen.load_image('buttons.png') + + # Tip: add text or icons to the image to help you + # remember what each button will do in your program. + + # Wait for a single button to be pressed and save the result. + pressed = [] + while len(pressed) != 1: + pressed = ev3.buttons.pressed() + button = pressed[0] + + # Print which button was pressed + ev3.screen.draw_text(2, 100, button) + + # Now wait for the button to be released. + while any(ev3.buttons.pressed()): + pass + + # Return which button was pressed. + return button