Skip to content

merge development into main #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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:
Expand All @@ -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

Expand Down Expand Up @@ -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)`
Expand All @@ -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.
Wraps `create_sketch()` and provides a shortcut to copy a file to another path.
6 changes: 3 additions & 3 deletions arduino/__init__.py
Original file line number Diff line number Diff line change
@@ -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 *
11 changes: 6 additions & 5 deletions arduino/arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand All @@ -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'
Expand Down Expand Up @@ -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:
Expand Down
File renamed without changes.
File renamed without changes.
12 changes: 11 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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"
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}