Simple test¶
Ensure your device works with this simple test.
1# SPDX-FileCopyrightText: 2021 Dan Halbert for Adafruit Industries
2# SPDX-FileCopyrightText: 2021 James Carr
3#
4# SPDX-License-Identifier: Unlicense
5
6from adafruit_simplemath import map_range, map_unconstrained_range, constrain
7
8print("map_range() examples")
9# Map, say, a sensor value, from a range of 0-255 to 0-1023.
10sensor_input_value = 30
11sensor_converted_value = map_range(sensor_input_value, 0, 255, 0, 1023)
12print(
13 "Sensor input value:",
14 sensor_input_value,
15 "Converted value:",
16 sensor_converted_value,
17)
18
19percent = 23
20screen_width = 320 # or board.DISPLAY.width
21x = map_range(percent, 0, 100, 0, screen_width - 1)
22print("X position", percent, "% from the left of screen is", x)
23
24print("\nmap_unconstrained_range() examples")
25celsius = 20
26fahrenheit = map_unconstrained_range(celsius, 0, 100, 32, 212)
27print(celsius, "degress Celsius =", fahrenheit, "degrees Fahrenheit")
28
29celsius = -20
30fahrenheit = map_unconstrained_range(celsius, 0, 100, 32, 212)
31print(celsius, "degress Celsius =", fahrenheit, "degrees Fahrenheit")
32
33print("\nconstrain() examples")
34# Constrain a value to a range.
35def constrain_example(value, min_value, max_value):
36 constrained_value = constrain(value, min_value, max_value)
37 print(
38 "Constrain",
39 value,
40 "between [",
41 min_value,
42 "and",
43 max_value,
44 "] gives",
45 constrained_value,
46 )
47
48
49constrain_example(0, 1, 3) # expects 1
50constrain_example(0, 3, 1) # expects 1
51constrain_example(4, 1, 3) # expects 3
52constrain_example(4, 3, 1) # expects 3
53constrain_example(2, 2, 3) # expects 2
54constrain_example(2, 3, 2) # expects 2