-
Notifications
You must be signed in to change notification settings - Fork 4
Add a constrained parameter to map_range() #2
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
dhalbert
merged 6 commits into
adafruit:main
from
lesamouraipourpre:unconstrained-map-range
Apr 14, 2021
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
edf90d6
Add a constrained parameter to map_range(), defaulting to True.
lesamouraipourpre dbc4877
Add tests that make use of constrained in map_range
lesamouraipourpre f4142be
* Auto-swap the limits passed to constrain if necessary (Closes #3)
lesamouraipourpre 92036d1
Remove commented out code
lesamouraipourpre e535518
Call constrain from map_range.
lesamouraipourpre 0218b0f
Rename unconstrained_map_range to map_unconstrained_range
lesamouraipourpre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,54 @@ | ||
# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries | ||
# SPDX-FileCopyrightText: 2021 Dan Halbert for Adafruit Industries | ||
# SPDX-FileCopyrightText: 2021 James Carr | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
|
||
from adafruit_simplemath import map_range, constrain | ||
from adafruit_simplemath import map_range, unconstrained_map_range, constrain | ||
|
||
print("map_range() examples") | ||
# Map, say, a sensor value, from a range of 0-255 to 0-1023. | ||
print(map_range(30, 0, 255, 0, 1023)) | ||
sensor_input_value = 30 | ||
sensor_converted_value = map_range(sensor_input_value, 0, 255, 0, 1023) | ||
print( | ||
"Sensor input value:", | ||
sensor_input_value, | ||
"Converted value:", | ||
sensor_converted_value, | ||
) | ||
|
||
percent = 23 | ||
screen_width = 320 # or board.DISPLAY.width | ||
x = map_range(percent, 0, 100, 0, screen_width - 1) | ||
print("X position", percent, "% from the left of screen is", x) | ||
|
||
print("\nunconstrained_map_range() examples") | ||
celsius = 20 | ||
fahrenheit = unconstrained_map_range(celsius, 0, 100, 32, 212) | ||
print(celsius, "degress Celsius =", fahrenheit, "degrees Fahrenheit") | ||
|
||
celsius = -20 | ||
fahrenheit = unconstrained_map_range(celsius, 0, 100, 32, 212) | ||
print(celsius, "degress Celsius =", fahrenheit, "degrees Fahrenheit") | ||
|
||
print("\nconstrain() examples") | ||
# Constrain a value to a range. | ||
print(constrain(0, 1, 3)) # prints 1 | ||
print(constrain(4, 1, 3)) # prints 3 | ||
print(constrain(2, 2, 3)) # prints 2 | ||
def constrain_example(value, min_value, max_value): | ||
constrained_value = constrain(value, min_value, max_value) | ||
print( | ||
"Constrain", | ||
value, | ||
"between [", | ||
min_value, | ||
"and", | ||
max_value, | ||
"] gives", | ||
constrained_value, | ||
) | ||
|
||
|
||
constrain_example(0, 1, 3) # expects 1 | ||
constrain_example(0, 3, 1) # expects 1 | ||
constrain_example(4, 1, 3) # expects 3 | ||
constrain_example(4, 3, 1) # expects 3 | ||
constrain_example(2, 2, 3) # expects 2 | ||
constrain_example(2, 3, 2) # expects 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# SPDX-FileCopyrightText: 2021 James Carr | ||
# | ||
# SPDX-License-Identifier: Unlicense | ||
|
||
from adafruit_simplemath import unconstrained_map_range | ||
|
||
|
||
def test_unconstrained_map_range(): | ||
assert unconstrained_map_range(-40, 32, 212, 0, 100) == -40.0 | ||
assert unconstrained_map_range(50, 32, 212, 0, 100) == 10.0 | ||
assert unconstrained_map_range(392, 32, 212, 0, 100) == 200.0 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.