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
35
36# Constrain a value to a range.
37def constrain_example(value, min_value, max_value):
38 constrained_value = constrain(value, min_value, max_value)
39 print(
40 "Constrain",
41 value,
42 "between [",
43 min_value,
44 "and",
45 max_value,
46 "] gives",
47 constrained_value,
48 )
49
50
51constrain_example(0, 1, 3) # expects 1
52constrain_example(0, 3, 1) # expects 1
53constrain_example(4, 1, 3) # expects 3
54constrain_example(4, 3, 1) # expects 3
55constrain_example(2, 2, 3) # expects 2
56constrain_example(2, 3, 2) # expects 2