adafruit_simplemath¶
Math utility functions
Author(s): Dan Halbert, James Carr
Implementation Notes¶
Software and Dependencies:
Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases
- adafruit_simplemath.constrain(x: float, out_min: float, out_max: float) → float¶
Constrains
xto be within the inclusive range [out_min,out_max]. Sometimes calledcliporclampin other libraries.out_minshould be less than or equal toout_max. Ifxis less thanout_min, returnout_min. Ifxis greater thanout_max, returnout_max. Otherwise just returnx. Ifmax_valueis less thanmin_value, they will be swapped.
- adafruit_simplemath.map_range(x: float, in_min: float, in_max: float, out_min: float, out_max: float) → float¶
Maps a number from one range to another. Somewhat similar to the Arduino
map()function, but returns a floating point result, and constrains the output value to be betweenout_minandout_max. Ifin_minis greater thanin_maxorout_minis greater thanout_max, the corresponding range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.See also
map_unconstrained_range()from adafruit_simplemath import map_range 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)
- adafruit_simplemath.map_unconstrained_range(x: float, in_min: float, in_max: float, out_min: float, out_max: float) → float¶
Maps a number from one range to another. Somewhat similar to the Arduino
map()function, but returns a floating point result, and does not constrain the output value to be betweenout_minandout_max. Ifin_minis greater thanin_maxorout_minis greater thanout_max, the corresponding range is reversed, allowing, for example, mapping a range of 0-10 to 50-0.See also
map_range()from adafruit_simplemath import map_unconstrained_range celsius = -20 fahrenheit = map_unconstrained_range(celsius, 0, 100, 32, 212) print(celsius, "degress Celsius =", fahrenheit, "degrees Fahrenheit")