From 297aa884595eabb5319974b32bd4e8c42e1900c9 Mon Sep 17 00:00:00 2001 From: Laurens Valk Date: Tue, 25 Feb 2020 10:15:59 +0100 Subject: [PATCH] snippets/ev3/ev3devsensor: initial examples --- snippets/ev3/ev3devsensor/.gitignore | 3 ++ .../ev3/ev3devsensor/.vscode/extensions.json | 13 ++++++ snippets/ev3/ev3devsensor/.vscode/launch.json | 20 ++++++++ .../ev3/ev3devsensor/.vscode/settings.json | 6 +++ snippets/ev3/ev3devsensor/class_example.py | 46 +++++++++++++++++++ snippets/ev3/ev3devsensor/main.py | 19 ++++++++ 6 files changed, 107 insertions(+) create mode 100644 snippets/ev3/ev3devsensor/.gitignore create mode 100644 snippets/ev3/ev3devsensor/.vscode/extensions.json create mode 100644 snippets/ev3/ev3devsensor/.vscode/launch.json create mode 100644 snippets/ev3/ev3devsensor/.vscode/settings.json create mode 100644 snippets/ev3/ev3devsensor/class_example.py create mode 100644 snippets/ev3/ev3devsensor/main.py diff --git a/snippets/ev3/ev3devsensor/.gitignore b/snippets/ev3/ev3devsensor/.gitignore new file mode 100644 index 0000000..9b5f630 --- /dev/null +++ b/snippets/ev3/ev3devsensor/.gitignore @@ -0,0 +1,3 @@ +__pycache__/ +*.pyc +venv/ diff --git a/snippets/ev3/ev3devsensor/.vscode/extensions.json b/snippets/ev3/ev3devsensor/.vscode/extensions.json new file mode 100644 index 0000000..f8f1a44 --- /dev/null +++ b/snippets/ev3/ev3devsensor/.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/ev3devsensor/.vscode/launch.json b/snippets/ev3/ev3devsensor/.vscode/launch.json new file mode 100644 index 0000000..7dc1149 --- /dev/null +++ b/snippets/ev3/ev3devsensor/.vscode/launch.json @@ -0,0 +1,20 @@ +{ + // 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": "Main Example", + "type": "ev3devBrowser", + "request": "launch", + "program": "/home/robot/${workspaceRootFolderName}/main.py" + }, + { + "name": "Class Example", + "type": "ev3devBrowser", + "request": "launch", + "program": "/home/robot/${workspaceRootFolderName}/class_example.py" + } + ] +} diff --git a/snippets/ev3/ev3devsensor/.vscode/settings.json b/snippets/ev3/ev3devsensor/.vscode/settings.json new file mode 100644 index 0000000..37c9a5d --- /dev/null +++ b/snippets/ev3/ev3devsensor/.vscode/settings.json @@ -0,0 +1,6 @@ +// Place your settings in this file to overwrite default and user settings. +{ + "files.eol": "\n", + "debug.openDebug": "neverOpen", + "python.linting.enabled": false +} diff --git a/snippets/ev3/ev3devsensor/class_example.py b/snippets/ev3/ev3devsensor/class_example.py new file mode 100644 index 0000000..95dfe40 --- /dev/null +++ b/snippets/ev3/ev3devsensor/class_example.py @@ -0,0 +1,46 @@ +#!/usr/bin/env pybricks-micropython +from pybricks.parameters import Port +from pybricks.iodevices import Ev3devSensor + + +class MySensor(Ev3devSensor): + """Example of extending the Ev3devSensor class.""" + + def __init__(self, port): + """Initialize the sensor.""" + + # Initialize the parent class. + super().__init__(port) + + # Get the sysfs path. + self.path = '/sys/class/lego-sensor/sensor' + str(self.sensor_index) + + def get_modes(self): + """Get a list of mode strings so we don't have to look them up.""" + + # The path of the modes file. + modes_path = self.path + '/modes' + + # Open the modes file. + with open(modes_path, 'r') as m: + + # Read the contents. + contents = m.read() + + # Strip the newline symbol, and split at every space symbol. + return contents.strip().split(' ') + + +# Initialize the sensor +sensor = MySensor(Port.S3) + +# Show where this sensor can be found +print(sensor.path) + +# Print the available modes +modes = sensor.get_modes() +print(modes) + +# Read mode 0 of this sensor +val = sensor.read(modes[0]) +print(val) diff --git a/snippets/ev3/ev3devsensor/main.py b/snippets/ev3/ev3devsensor/main.py new file mode 100644 index 0000000..58a6d7c --- /dev/null +++ b/snippets/ev3/ev3devsensor/main.py @@ -0,0 +1,19 @@ +#!/usr/bin/env pybricks-micropython +from pybricks.parameters import Port +from pybricks.tools import wait +from pybricks.iodevices import Ev3devSensor + +# Initialize an Ev3devSensor. +# In this example we use the +# LEGO MINDSTORMS EV3 Color Sensor. +sensor = Ev3devSensor(Port.S3) + +while True: + # Read the raw RGB values + r, g, b = sensor.read('RGB-RAW') + + # Print results + print('R: {0}\t G: {1}\t B: {2}'.format(r, g, b)) + + # Wait + wait(200)