GHIElectronics.Endpoint.Devices.Adc 0.5.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- GHIElectronics/Endpoint/Devices/Adc/__init__.py +176 -0
- ghielectronics_endpoint_devices_adc-0.5.1.dist-info/METADATA +20 -0
- ghielectronics_endpoint_devices_adc-0.5.1.dist-info/RECORD +5 -0
- ghielectronics_endpoint_devices_adc-0.5.1.dist-info/WHEEL +5 -0
- ghielectronics_endpoint_devices_adc-0.5.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"""The analog inputs of Endpoint, with the same API as .NET's
|
|
2
|
+
System.Device.Analog and Endpoint's AdcController:
|
|
3
|
+
|
|
4
|
+
from GHIElectronics.Endpoint.Core import EPM815
|
|
5
|
+
from GHIElectronics.Endpoint.Devices.Adc import AdcController
|
|
6
|
+
|
|
7
|
+
adc = AdcController()
|
|
8
|
+
pin = adc.OpenPin(EPM815.Adc.Pin.PA0)
|
|
9
|
+
print(pin.ReadVoltage().Volts, pin.ReadRaw())
|
|
10
|
+
|
|
11
|
+
Opening a pin makes it an analog input; closing it puts the pin back as it was.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
from GHIElectronics.Endpoint.Core._errors import NotSupportedException
|
|
15
|
+
from GHIElectronics.Endpoint.Core._native import native
|
|
16
|
+
|
|
17
|
+
__all__ = ["AdcController", "AnalogInputPin", "ElectricPotential"]
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class ElectricPotential:
|
|
21
|
+
"""A voltage, as .NET's UnitsNet.ElectricPotential (Volts, Millivolts)"""
|
|
22
|
+
|
|
23
|
+
def __init__(self, volts):
|
|
24
|
+
self._volts = float(volts)
|
|
25
|
+
|
|
26
|
+
@staticmethod
|
|
27
|
+
def FromVolts(volts):
|
|
28
|
+
return ElectricPotential(volts)
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def FromMillivolts(millivolts):
|
|
32
|
+
return ElectricPotential(millivolts / 1000)
|
|
33
|
+
|
|
34
|
+
@property
|
|
35
|
+
def Volts(self):
|
|
36
|
+
return self._volts
|
|
37
|
+
|
|
38
|
+
@property
|
|
39
|
+
def Millivolts(self):
|
|
40
|
+
return self._volts * 1000
|
|
41
|
+
|
|
42
|
+
def __eq__(self, other):
|
|
43
|
+
return isinstance(other, ElectricPotential) and other._volts == self._volts
|
|
44
|
+
|
|
45
|
+
def __lt__(self, other):
|
|
46
|
+
return self._volts < other._volts
|
|
47
|
+
|
|
48
|
+
def __repr__(self):
|
|
49
|
+
return f"{self._volts:.3f} V"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class AdcController:
|
|
53
|
+
"""All of Endpoint's analog inputs (both converters)"""
|
|
54
|
+
|
|
55
|
+
def __init__(self):
|
|
56
|
+
millivolts = native.out_double()
|
|
57
|
+
native.adc_voltage_reference(native.ref(millivolts))
|
|
58
|
+
self._reference = ElectricPotential.FromMillivolts(millivolts.value)
|
|
59
|
+
self._open = {}
|
|
60
|
+
|
|
61
|
+
@property
|
|
62
|
+
def PinCount(self):
|
|
63
|
+
count = native.out_int()
|
|
64
|
+
native.adc_pin_count(native.ref(count))
|
|
65
|
+
return count.value
|
|
66
|
+
|
|
67
|
+
@property
|
|
68
|
+
def VoltageReference(self):
|
|
69
|
+
"""The inputs measure 0 V to this (3.3 V)"""
|
|
70
|
+
return self._reference
|
|
71
|
+
|
|
72
|
+
def SupportsAnalogInput(self, pin):
|
|
73
|
+
supported = native.out_int()
|
|
74
|
+
native.adc_supports_analog_input(pin, native.ref(supported))
|
|
75
|
+
return bool(supported.value)
|
|
76
|
+
|
|
77
|
+
def ConvertPinNumberToAnalogChannelNumber(self, pinNumber):
|
|
78
|
+
"""The pin's channel in its converter (ADC1 or ADC2)"""
|
|
79
|
+
channel = native.out_int()
|
|
80
|
+
native.adc_convert_pin_to_channel(pinNumber, native.ref(channel))
|
|
81
|
+
return channel.value
|
|
82
|
+
|
|
83
|
+
def OpenPin(self, pinNumber):
|
|
84
|
+
pin = AnalogInputPin(self, pinNumber)
|
|
85
|
+
self._open[pinNumber] = pin
|
|
86
|
+
return pin
|
|
87
|
+
|
|
88
|
+
def ClosePin(self, pin):
|
|
89
|
+
"""pin is an AnalogInputPin (as .NET) or its pin number"""
|
|
90
|
+
pin = self._open.get(pin, pin) if isinstance(pin, int) else pin
|
|
91
|
+
if isinstance(pin, AnalogInputPin):
|
|
92
|
+
pin.Dispose()
|
|
93
|
+
|
|
94
|
+
def IsPinOpen(self, pinNumber):
|
|
95
|
+
return pinNumber in self._open
|
|
96
|
+
|
|
97
|
+
def Dispose(self):
|
|
98
|
+
for pin in list(self._open.values()):
|
|
99
|
+
pin.Dispose()
|
|
100
|
+
|
|
101
|
+
def __enter__(self):
|
|
102
|
+
return self
|
|
103
|
+
|
|
104
|
+
def __exit__(self, *exception):
|
|
105
|
+
self.Dispose()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
class AnalogInputPin:
|
|
109
|
+
"""An open analog input: the pin is in analog mode until it is disposed"""
|
|
110
|
+
|
|
111
|
+
def __init__(self, controller, pinNumber):
|
|
112
|
+
handle = native.out_handle()
|
|
113
|
+
native.adc_open_pin(pinNumber, native.ref(handle))
|
|
114
|
+
self._handle = handle
|
|
115
|
+
self._controller = controller
|
|
116
|
+
self.PinNumber = pinNumber
|
|
117
|
+
|
|
118
|
+
def _h(self):
|
|
119
|
+
if self._handle is None:
|
|
120
|
+
raise native.disposed("AnalogInputPin")
|
|
121
|
+
return self._handle
|
|
122
|
+
|
|
123
|
+
@property
|
|
124
|
+
def AdcResolutionBits(self):
|
|
125
|
+
bits = native.out_int()
|
|
126
|
+
native.adc_resolution_bits(self._h(), native.ref(bits))
|
|
127
|
+
return bits.value
|
|
128
|
+
|
|
129
|
+
@property
|
|
130
|
+
def VoltageReference(self):
|
|
131
|
+
return self._controller.VoltageReference
|
|
132
|
+
|
|
133
|
+
@property
|
|
134
|
+
def MinVoltage(self):
|
|
135
|
+
return ElectricPotential(0)
|
|
136
|
+
|
|
137
|
+
@property
|
|
138
|
+
def MaxVoltage(self):
|
|
139
|
+
return self._controller.VoltageReference
|
|
140
|
+
|
|
141
|
+
def ReadRaw(self):
|
|
142
|
+
"""A new sample, 0 to 2**AdcResolutionBits - 1"""
|
|
143
|
+
value = native.out_uint32()
|
|
144
|
+
native.adc_read_raw(self._h(), native.ref(value))
|
|
145
|
+
return value.value
|
|
146
|
+
|
|
147
|
+
def ReadVoltage(self):
|
|
148
|
+
"""A new sample as an ElectricPotential (the driver's exact scale)"""
|
|
149
|
+
millivolts = native.out_double()
|
|
150
|
+
native.adc_read_millivolts(self._h(), native.ref(millivolts))
|
|
151
|
+
return ElectricPotential.FromMillivolts(millivolts.value)
|
|
152
|
+
|
|
153
|
+
def EnableAnalogValueChangedEvent(self, masterController=None, masterPin=None):
|
|
154
|
+
raise NotSupportedException("Endpoint's analog inputs have no value-changed event: call ReadRaw or ReadVoltage")
|
|
155
|
+
|
|
156
|
+
def DisableAnalogValueChangedEvent(self):
|
|
157
|
+
pass
|
|
158
|
+
|
|
159
|
+
def Dispose(self):
|
|
160
|
+
"""Puts the pin back as it was before it was opened"""
|
|
161
|
+
if self._handle is not None:
|
|
162
|
+
native.adc_close_pin(self._handle)
|
|
163
|
+
self._handle = None
|
|
164
|
+
self._controller._open.pop(self.PinNumber, None)
|
|
165
|
+
|
|
166
|
+
def __enter__(self):
|
|
167
|
+
return self
|
|
168
|
+
|
|
169
|
+
def __exit__(self, *exception):
|
|
170
|
+
self.Dispose()
|
|
171
|
+
|
|
172
|
+
def __del__(self):
|
|
173
|
+
try:
|
|
174
|
+
self.Dispose()
|
|
175
|
+
except Exception:
|
|
176
|
+
pass
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: GHIElectronics.Endpoint.Devices.Adc
|
|
3
|
+
Version: 0.5.1
|
|
4
|
+
Summary: Analog inputs for GHI Electronics Endpoint, with the API of .NET's System.Device.Analog.
|
|
5
|
+
Author-email: "GHI Electronics, LLC" <support@ghielectronics.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://www.ghielectronics.com/
|
|
8
|
+
Project-URL: Repository, https://github.com/ghi-electronics/Endpoint-Libraries
|
|
9
|
+
Project-URL: Issues, https://github.com/ghi-electronics/Endpoint-Libraries/issues
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
12
|
+
Requires-Python: >=3.9
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
Requires-Dist: GHIElectronics.Endpoint.Core<0.6,>=0.5.1
|
|
15
|
+
|
|
16
|
+
# GHIElectronics.Endpoint.Devices.Adc
|
|
17
|
+
|
|
18
|
+
Analog inputs for GHI Electronics Endpoint, with the API of .NET's System.Device.Analog.
|
|
19
|
+
|
|
20
|
+
Part of the [GHI Electronics Endpoint](https://github.com/ghi-electronics/Endpoint-Libraries) libraries. See the repository README for the full package list and getting-started guide.
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
GHIElectronics/Endpoint/Devices/Adc/__init__.py,sha256=01Y9gw5T-Z5YNlDjO_LpeNARD6Whm2bj3spWtWSRNGo,5220
|
|
2
|
+
ghielectronics_endpoint_devices_adc-0.5.1.dist-info/METADATA,sha256=6BtVTlLiFHuSLGxAcs-irPMrsx8JttthVtpy7hGr5IU,1031
|
|
3
|
+
ghielectronics_endpoint_devices_adc-0.5.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
ghielectronics_endpoint_devices_adc-0.5.1.dist-info/top_level.txt,sha256=NpOZNgd0iquwrRPrTOhUDxc2V9TtNFLv5Z_Dx3YZtIU,15
|
|
5
|
+
ghielectronics_endpoint_devices_adc-0.5.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
GHIElectronics
|