|
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 |
3 | 2 | #
|
4 | 3 | # SPDX-License-Identifier: MIT
|
5 | 4 | """
|
|
14 | 13 | Implementation Notes
|
15 | 14 | --------------------
|
16 | 15 |
|
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 |
| -
|
22 | 16 | **Software and Dependencies:**
|
23 | 17 |
|
24 | 18 | * Adafruit CircuitPython firmware for the supported boards:
|
25 | 19 | 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 |
31 | 20 | """
|
32 | 21 |
|
33 |
| -# imports |
34 |
| - |
35 | 22 | __version__ = "0.0.0-auto.0"
|
36 | 23 | __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)) |
0 commit comments