Skip to content

Commit 68ae135

Browse files
authored
Merge pull request #1 from dhalbert/main
Add library code and tests
2 parents 081bd82 + 0821801 commit 68ae135

File tree

7 files changed

+115
-37
lines changed

7 files changed

+115
-37
lines changed

.github/workflows/test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# SPDX-FileCopyrightText: 2021 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
name: Run Tests
6+
7+
on: [pull_request, push]
8+
9+
jobs:
10+
test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- name: Set up Python 3.6
14+
uses: actions/setup-python@v1
15+
with:
16+
python-version: 3.6
17+
- name: Versions
18+
run: |
19+
python3 --version
20+
- name: Checkout Current Repo
21+
uses: actions/checkout@v1
22+
with:
23+
submodules: true
24+
- name: Install pytest
25+
run: pip install pytest
26+
- name: Install locally
27+
run: pip install .
28+
- name: Run tests
29+
run: pytest

README.rst

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ This is easily achieved by downloading
3232

3333
Installing from PyPI
3434
=====================
35-
.. note:: This library is not available on PyPI yet. Install documentation is included
36-
as a standard element. Stay tuned for PyPI availability!
37-
38-
.. todo:: Remove the above note if PyPI version is/will be available at time of release.
39-
If the library is not planned for PyPI, remove the entire 'Installing from PyPI' section.
40-
4135
On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
4236
PyPI <https://pypi.org/project/adafruit-circuitpython-simplemath/>`_. To install for current user:
4337

@@ -60,11 +54,6 @@ To install in a virtual environment in your current project:
6054
source .env/bin/activate
6155
pip3 install adafruit-circuitpython-simplemath
6256
63-
Usage Example
64-
=============
65-
66-
.. todo:: Add a quick, simple example. It and other examples should live in the examples folder and be included in docs/examples.rst.
67-
6857
Contributing
6958
============
7059

adafruit_simplemath.py

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
2-
# SPDX-FileCopyrightText: Copyright (c) 2021 Adafruit Industries for Adafruit Industries LLC
1+
# SPDX-FileCopyrightText: Copyright (c) 2021 Dan Halbert for Adafruit Industries LLC
32
#
43
# SPDX-License-Identifier: MIT
54
"""
@@ -14,23 +13,59 @@
1413
Implementation Notes
1514
--------------------
1615
17-
**Hardware:**
18-
19-
.. todo:: Add links to any specific hardware product page(s), or category page(s). Use unordered list & hyperlink rST
20-
inline format: "* `Link Text <url>`_"
21-
2216
**Software and Dependencies:**
2317
2418
* Adafruit CircuitPython firmware for the supported boards:
2519
https://github.com/adafruit/circuitpython/releases
26-
27-
.. todo:: Uncomment or remove the Bus Device and/or the Register library dependencies based on the library's use of either.
28-
29-
# * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
30-
# * Adafruit's Register library: https://github.com/adafruit/Adafruit_CircuitPython_Register
3120
"""
3221

33-
# imports
34-
3522
__version__ = "0.0.0-auto.0"
3623
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_SimpleMath.git"
24+
25+
26+
def map_range(
27+
x: float, in_min: float, in_max: float, out_min: float, out_max: float
28+
) -> float:
29+
"""
30+
Maps a number from one range to another. Somewhat similar to the Arduino ``map()`` function,
31+
but returns a floating point result, and constrains the output value to be between
32+
``out_min`` and ``out_max``.
33+
If ``in_min`` is greater than ``in_max`` or ``out_min`` is greater than ``out_max``,
34+
the corresponding range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.
35+
36+
:param float in_min: Start value of input range.
37+
:param float in_max: End value of input range.
38+
:param float out_min: Start value of output range.
39+
:param float out_max: End value of output range.
40+
:return: Returns value mapped to new range.
41+
:rtype: float
42+
"""
43+
in_range = in_max - in_min
44+
in_delta = x - in_min
45+
if in_range != 0:
46+
mapped = in_delta / in_range
47+
elif in_delta != 0:
48+
mapped = in_delta
49+
else:
50+
mapped = 0.5
51+
mapped *= out_max - out_min
52+
mapped += out_min
53+
if out_min <= out_max:
54+
return max(min(mapped, out_max), out_min)
55+
return min(max(mapped, out_max), out_min)
56+
57+
58+
def constrain(x: float, out_min: float, out_max: float) -> float:
59+
"""Constrains ``x`` to be within the inclusive range [``out_min``, ``out_max``].
60+
Sometimes called ``clip`` or ``clamp`` in other libraries.
61+
``out_min`` should be less than or equal to ``out_max``.
62+
If ``x`` is less than ``out_min``, return ``out_min``.
63+
If ``x`` is greater than ``out_max``, return ``out_max``.
64+
Otherwise just return ``x``.
65+
66+
:param float out_min: Lower bound of output range.
67+
:param float out_max: Upper bound of output range.
68+
:return: Returns value constrained to given range.
69+
:rtype: float
70+
"""
71+
return max(out_min, min(x, out_max))

docs/index.rst

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,6 @@ Table of Contents
2020

2121
api
2222

23-
.. toctree::
24-
:caption: Tutorials
25-
26-
.. todo:: Add any Learn guide links here. If there are none, then simply delete this todo and leave
27-
the toctree above for use later.
28-
29-
.. toctree::
30-
:caption: Related Products
31-
32-
.. todo:: Add any product links here. If there are none, then simply delete this todo and leave
33-
the toctree above for use later.
34-
3523
.. toctree::
3624
:caption: Other Links
3725

examples/simplemath_simpletest.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
22
#
33
# SPDX-License-Identifier: Unlicense
4+
5+
from adafruit_simplemath import map_range, constrain
6+
7+
# Map, say, a sensor value, from a range of 0-255 to 0-1023.
8+
print(map_range(30, 0, 255, 0, 1023))
9+
10+
# Constrain a value to a range.
11+
print(constrain(0, 1, 3)) # prints 1
12+
print(constrain(4, 1, 3)) # prints 3
13+
print(constrain(2, 2, 3)) # prints 2

tests/constrain_test.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# SPDX-FileCopyrightText: 2021 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
from adafruit_simplemath import constrain
6+
7+
8+
def test_constrain():
9+
assert constrain(1, 1, 10) == 1
10+
assert constrain(10, 1, 10) == 10
11+
assert constrain(0, 1, 10) == 1
12+
assert constrain(11, 1, 10) == 10

tests/map_range_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2021 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
from adafruit_simplemath import map_range
6+
7+
8+
def test_map_range():
9+
assert map_range(1, 0, 10, 0, 100) == 10.0
10+
assert map_range(-1, 0, 10, 0, 100) == 0
11+
assert map_range(11, 0, 10, 0, 100) == 100
12+
assert map_range(5, 0, 10, 0, 5) == 2.5
13+
assert map_range(1, 10, 0, 0, 5) == 4.5
14+
assert map_range(1, 0, 10, 10, 0) == 9.0
15+
assert map_range(10, 1, 10, 1, 20) == 20.0

0 commit comments

Comments
 (0)