-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsphinx_example.py
More file actions
executable file
·124 lines (90 loc) · 3.05 KB
/
Copy pathsphinx_example.py
File metadata and controls
executable file
·124 lines (90 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
"""An example Python program with Sphinx style comments.
Description
-----------
An example Python program that demonstrates how to use Sphinx (reStructuredText)
style comments.
Libraries/Modules
-----------------
- *time* Standard Library (https://docs.python.org/3/library/time.html)
- Provides access to the *sleep* function.
- *sensors* Module (local)
- Provides access to the *Sensor* and *TempSensor* classes.
Notes
-----
- Comments are Sphinx (reStructuredText) compatible.
TODO
----
- None.
Author(s)
---------
- Created by John Woolsey on 05/27/2020.
- Modified by John Woolsey on 04/26/2023.
Copyright (c) 2020 Woolsey Workshop. All rights reserved.
Members
-------
"""
# Imports
from time import sleep
import sensors
# Global Constants
DEBUG: bool = True
"""The mode of operation; `False` = normal, `True` = debug."""
MIN_BASE: int = 1
"""The minimum number to map."""
MAX_BASE: int = 10
"""The maximum number to map."""
MIN_MAPPED: int = 0
"""The minimum mapped value."""
MAX_MAPPED: int = 255
"""The maximum mapped value."""
# Functions
def map_range(number: float, in_min: float, in_max: float, out_min: float, out_max: float, constrained: bool = True) -> float:
"""Maps a value from one range to another.
This function takes a value within an input range and maps it to the
equivalent value within an output range, maintaining the relative position
of the value within the range.
:param number: The value to be mapped.
:type number: float
:param in_min: The minimum value of the input range.
:type in_min: float
:param in_max: The maximum value of the input range.
:type in_max: float
:param out_min: The minimum value of the output range.
:type out_min: float
:param out_max: The maximum value of the output range.
:type out_max: float
:param constrained: If `True`, the mapped value is constrained to the output
range; default is `True`.
:type constrained: bool
:return: The mapped value.
:rtype: float
"""
mapped = out_min
if in_max - in_min != 0:
mapped = (number - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
if out_min <= out_max:
mapped = max(min(mapped, out_max), out_min)
else:
mapped = min(max(mapped, out_max), out_min)
return mapped
def main() -> None:
"""The main program entry."""
if DEBUG:
print("Running in DEBUG mode. Turn off for normal operation.")
# Map numbers
for i in range(MIN_BASE, MAX_BASE + 1):
print(
f"Base: {i:2d}, Mapped: "
f"{round(map_range(i, MIN_BASE, MAX_BASE, MIN_MAPPED, MAX_MAPPED)):3d}"
)
sleep(0.25) # wait 250 milliseconds
# Sensors
sensor: int = sensors.Sensor("MySensor")
print(sensor)
temp_in: int = sensors.TempSensor("Inside")
print(temp_in)
temp_out: int = sensors.TempSensor("Outside", "C")
print(temp_out)
if __name__ == "__main__": # required for generating Sphinx documentation
main()