diff --git a/README.md b/README.md index b390647..48e6b58 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ This module also wraps machine functions in easy-to-use methods. ## Installation ### mip (MicroPython Package Manager) + This is the recommended method for boards which can connect to Internet. Run the following MicroPython script using your favourite editor: @@ -43,7 +44,9 @@ connect(SSID, PWD) mip.install('github:arduino/arduino-runtime-mpy') ``` + ### mpremote mip + You will need to have Python and `mpremote` installed on your system, [follow these instructions](https://docs.micropython.org/en/latest/reference/mpremote.html) to do so. Open a shell and run the following command: @@ -53,8 +56,8 @@ mpremote mip install "github:arduino/arduino-runtime-mpy" ``` ### Manual Installation -Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method. +Copy the folder `arduino` and its content into your board's `lib` folder using your preferred method. ## Usage @@ -219,10 +222,10 @@ delay(1000) # Delay the execution for 1 second Some utility methods are provided and are still in development: -* `map(x, in_min, in_max, out_min, out_max)` - Remaps the value `x` from its input range to an output range -* `mapi(x, in_min, in_max, out_min, out_max)` - same as `map` but always returns an integer +* `map_float(x, in_min, in_max, out_min, out_max)` + Remaps the value `x` from its input range to an output range as a float +* `map_int(x, in_min, in_max, out_min, out_max)` + same as `map_float` but always returns an integer * `random(low, high=None)` Returns a random number between `0` and `low` - 1 if no `high` is provided, otherwise a value between `low` and `high` - 1 * `constrain(val, min_val, max_val)` @@ -248,6 +251,6 @@ create_sketch('main') The method returns the Python file's full path. -### copy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False): +### copy_sketch(source_path = '', destination_path = '.', name = None, overwrite = False) -Wraps `create_sketch()` and provides a shortcut to copy a file to another path. \ No newline at end of file +Wraps `create_sketch()` and provides a shortcut to copy a file to another path. diff --git a/arduino/__init__.py b/arduino/__init__.py index ee3e327..e69c353 100644 --- a/arduino/__init__.py +++ b/arduino/__init__.py @@ -1,7 +1,7 @@ -__author__ = "Murilo Polese" -__credits__ = ["Murilo Polese", "Ubi de Feo", "Sebastian Romero"] +__author__ = "Ubi de Feo" +__credits__ = ["Ubi de Feo", "Sebastian Romero"] __license__ = "MPL 2.0" -__version__ = "0.1.0" +__version__ = "0.4.0" __maintainer__ = "Arduino" from .arduino import * \ No newline at end of file diff --git a/arduino/arduino.py b/arduino/arduino.py index 68c7067..9cb0fc8 100644 --- a/arduino/arduino.py +++ b/arduino/arduino.py @@ -15,11 +15,11 @@ LOW = 0 # Voltage level LOW # UTILITY -def map(x, in_min, in_max, out_min, out_max) -> int | float: +def map_float(x, in_min, in_max, out_min, out_max) -> int | float: return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min -def mapi(x, in_min, in_max, out_min, out_max) -> int: - return int(map(x, in_min, in_max, out_min, out_max)) +def map_int(x, in_min, in_max, out_min, out_max) -> int: + return int(map_float(x, in_min, in_max, out_min, out_max)) def random(low, high=None) -> int: if high == None: @@ -63,7 +63,7 @@ def analogRead(_pin) -> int: def analog_write(_pin, _duty_cycle) -> None: p = PWM(Pin(_pin)) - duty = mapi(_duty_cycle, 0, 255, 0, 65535) + duty = map_int(_duty_cycle, 0, 255, 0, 65535) p.duty_u16(floor(duty)) if(_duty_cycle == 0): @@ -82,7 +82,6 @@ def get_template_path(): return '/'.join(__file__.split('/')[:-1]) + '/template.tpl' def create_sketch(sketch_name = None, destination_path = '.', overwrite = False, source_path = None): - if sketch_name is None: sketch_name = 'main' new_sketch_path = f'{destination_path}/{sketch_name}.py' @@ -112,6 +111,8 @@ def copy_sketch(source_path = '', destination_path = '.', name = None, overwrite # RUNTIME def start(setup=None, loop=None, cleanup = None, preload = None): + if preload is not None: + preload() if setup is not None: setup() try: diff --git a/examples/00_basic.py b/arduino/examples/00_basic.py similarity index 100% rename from examples/00_basic.py rename to arduino/examples/00_basic.py diff --git a/examples/01_arduino_blink.py b/arduino/examples/01_arduino_blink.py similarity index 100% rename from examples/01_arduino_blink.py rename to arduino/examples/01_arduino_blink.py diff --git a/examples/02_nano_esp32_advanced.py b/arduino/examples/02_nano_esp32_advanced.py similarity index 100% rename from examples/02_nano_esp32_advanced.py rename to arduino/examples/02_nano_esp32_advanced.py diff --git a/install.sh b/install.sh index 499b29b..d46b61f 100755 --- a/install.sh +++ b/install.sh @@ -1,14 +1,22 @@ #!/bin/bash # -# Install Arduino Runtime to a MicroPython board using mpremote. +# MicroPython Package Installer +# Created by: Ubi de Feo and Sebastian Romero +# +# Installs a MicroPython Package to a board using mpremote. +# # This script accepts an optional argument to compile .py files to .mpy. # Simply run the script with the optional argument: # # ./install.sh mpy +# Name to display during installation PKGNAME="Arduino Runtime for MicroPython" +# Destination directory for the package on the board inside the library directory PKGDIR="arduino" +# Source directory for the package on the host SRCDIR=$PKGDIR +# Board library directory LIBDIR="lib" # File system operations such as "mpremote mkdir" or "mpremote rm" @@ -86,6 +94,8 @@ for filename in $SRCDIR/*; do source_extension="${f_name##*.}" destination_extension=$source_extension + # If examples are distributed within the package + # ensures they are copied but not compiled to .mpy if [[ -d $filename && "$f_name" == "examples" ]]; then if ! directory_exists "/${LIBDIR}/${PKGDIR}/examples"; then echo "Creating $LIBDIR/$PKGDIR/examples on board" diff --git a/package.json b/package.json index c997eaf..d9c61a2 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,11 @@ "urls": [ ["arduino/__init__.py", "github:arduino/arduino-runtime-mpy/arduino/__init__.py"], ["arduino/arduino.py", "github:arduino/arduino-runtime-mpy/arduino/arduino.py"], - ["arduino/template.tpl", "github:arduino/arduino-runtime-mpy/arduino/template.tpl"] + ["arduino/template.tpl", "github:arduino/arduino-runtime-mpy/arduino/template.tpl"], + ["arduino/examples/00_basic.py", "github:arduino/arduino-runtime-mpy/arduino/examples/00_basic.py"], + ["arduino/examples/01_arduino_blink.py", "github:arduino/arduino-runtime-mpy/arduino/examples/01_arduino_blink.py"], + ["arduino/examples/02_nano_esp32_advanced.py", "github:arduino/arduino-runtime-mpy/arduino/examples/02_nano_esp32_advanced.py"] ], "deps": [], - "version": "0.1.0" + "version": "0.4.0" } \ No newline at end of file