Skip to content

Commit 8e30bcb

Browse files
committed
Add library code and tests
1 parent 081bd82 commit 8e30bcb

File tree

7 files changed

+97
-37
lines changed

7 files changed

+97
-37
lines changed

.github/workflows/test.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Run Tests
2+
3+
on: [pull_request, push]
4+
5+
jobs:
6+
test:
7+
runs-on: ubuntu-latest
8+
steps:
9+
- name: Set up Python 3.6
10+
uses: actions/setup-python@v1
11+
with:
12+
python-version: 3.6
13+
- name: Versions
14+
run: |
15+
python3 --version
16+
- name: Checkout Current Repo
17+
uses: actions/checkout@v1
18+
with:
19+
submodules: true
20+
- name: Install pytest
21+
run: pip install pytest
22+
- name: Install locally
23+
run: pip install .
24+
- name: Run tests
25+
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: 37 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,47 @@
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(x, in_min, in_max, out_min, out_max):
27+
"""
28+
Maps a number from one range to another. Somewhat similar to the Arduino ``map()`` function, but
29+
returns a floating point result, and constrains the output value to be between ``out_min`` and ``out_max``.
30+
31+
:return: Returns value mapped to new range
32+
:rtype: float
33+
"""
34+
in_range = in_max - in_min
35+
in_delta = x - in_min
36+
if in_range != 0:
37+
mapped = in_delta / in_range
38+
elif in_delta != 0:
39+
mapped = in_delta
40+
else:
41+
mapped = 0.5
42+
mapped *= out_max - out_min
43+
mapped += out_min
44+
if out_min <= out_max:
45+
return max(min(mapped, out_max), out_min)
46+
return min(max(mapped, out_max), out_min)
47+
48+
49+
def constrain(x, out_min, out_max):
50+
"""Constrains ``x`` to be within the inclusive range ``[out_min, out_max]``.
51+
Sometimes called ``clip`` or ``clamp`` in other libraries.
52+
``out_min`` should be less than or equal to ``out_max``.
53+
If ``x`` is less than ``out_min``, return ``out_min``.
54+
If ``x`` is greater than ``out_max``, return ``out_max``.
55+
Otherwise just return ``x``.
56+
57+
:return: Returns value constrained to given range
58+
"""
59+
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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-FileCopyrightText: 2021 Dan Halbert for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
5+
from adafruit_simplemath import constrain
6+
7+
def test_constrain():
8+
assert constrain(1, 1, 10) == 1
9+
assert constrain(10, 1, 10) == 10
10+
assert constrain(0, 1, 10) == 1
11+
assert constrain(11, 1, 10) == 10

tests/map_range_test.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
def test_map_range():
8+
assert map_range(1, 0, 10, 0, 100) == 10.0
9+
assert map_range(-1, 0, 10, 0, 100) == 0
10+
assert map_range(11, 0, 10, 0, 100) == 100
11+
assert map_range(5, 0, 10, 0, 5) == 2.5
12+
assert map_range(1, 10, 0, 0, 5) == 4.5
13+
assert map_range(1, 0, 10, 10, 0) == 9.0
14+
assert map_range(10, 1, 10, 1, 20) == 20.0

0 commit comments

Comments
 (0)