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 0000000..232026b Binary files /dev/null and b/snippets/ev3/buttons_quickstart/buttons.png differ diff --git a/snippets/ev3/buttons_quickstart/buttons.svg b/snippets/ev3/buttons_quickstart/buttons.svg new file mode 100644 index 0000000..be1478d --- /dev/null +++ b/snippets/ev3/buttons_quickstart/buttons.svg @@ -0,0 +1,101 @@ + + + + + + + + + + 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