GHIElectronics.Endpoint.Devices.Gpio 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/Gpio/__init__.py +203 -0
- ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/METADATA +20 -0
- ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/RECORD +5 -0
- ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/WHEEL +5 -0
- ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
"""GPIO for Endpoint, with the same API as .NET's System.Device.Gpio:
|
|
2
|
+
|
|
3
|
+
from GHIElectronics.Endpoint.Core import EPM815
|
|
4
|
+
from GHIElectronics.Endpoint.Devices.Gpio import GpioController, PinMode, PinValue
|
|
5
|
+
|
|
6
|
+
gpio = GpioController()
|
|
7
|
+
gpio.OpenPin(EPM815.Gpio.Pin.PC0, PinMode.Output)
|
|
8
|
+
gpio.Write(EPM815.Gpio.Pin.PC0, PinValue.High)
|
|
9
|
+
|
|
10
|
+
Pins are the EPM815.Gpio.Pin constants. Open pins are reserved, so the other
|
|
11
|
+
Endpoint libraries cannot take them by mistake.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import datetime
|
|
15
|
+
import threading
|
|
16
|
+
from enum import IntEnum, IntFlag
|
|
17
|
+
|
|
18
|
+
from GHIElectronics.Endpoint.Core._native import GpioCallback, native
|
|
19
|
+
|
|
20
|
+
__all__ = ["GpioController", "GpioPin", "PinMode", "PinValue", "PinEventTypes",
|
|
21
|
+
"PinValueChangedEventArgs", "WaitForEventResult"]
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
class PinMode(IntEnum):
|
|
25
|
+
Input = 0
|
|
26
|
+
Output = 1
|
|
27
|
+
InputPullDown = 2
|
|
28
|
+
InputPullUp = 3
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class PinValue(IntEnum):
|
|
32
|
+
Low = 0
|
|
33
|
+
High = 1
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class PinEventTypes(IntFlag):
|
|
37
|
+
"""Python cannot name a member None: it is None_ here"""
|
|
38
|
+
None_ = 0
|
|
39
|
+
Rising = 1
|
|
40
|
+
Falling = 2
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class PinValueChangedEventArgs:
|
|
44
|
+
def __init__(self, changeType, pinNumber):
|
|
45
|
+
self.ChangeType = PinEventTypes(changeType)
|
|
46
|
+
self.PinNumber = pinNumber
|
|
47
|
+
|
|
48
|
+
def __repr__(self):
|
|
49
|
+
return f"PinValueChangedEventArgs({self.ChangeType!r}, {self.PinNumber})"
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
class WaitForEventResult:
|
|
53
|
+
def __init__(self, eventTypes, timedOut):
|
|
54
|
+
self.EventTypes = PinEventTypes(eventTypes)
|
|
55
|
+
self.TimedOut = timedOut
|
|
56
|
+
|
|
57
|
+
def __repr__(self):
|
|
58
|
+
return f"WaitForEventResult(EventTypes={self.EventTypes!r}, TimedOut={self.TimedOut})"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _milliseconds(timeout):
|
|
62
|
+
if timeout is None:
|
|
63
|
+
return -1
|
|
64
|
+
if isinstance(timeout, datetime.timedelta):
|
|
65
|
+
timeout = timeout.total_seconds()
|
|
66
|
+
return max(0, int(timeout * 1000))
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class GpioController:
|
|
70
|
+
"""GPIO on all of the EPM815's ports"""
|
|
71
|
+
|
|
72
|
+
def __init__(self):
|
|
73
|
+
handle = native.out_handle()
|
|
74
|
+
native.gpio_create(native.ref(handle))
|
|
75
|
+
self._handle = handle
|
|
76
|
+
self._lock = threading.Lock()
|
|
77
|
+
self._callbacks = {} # (pin, callback) -> (id, ctypes callback)
|
|
78
|
+
|
|
79
|
+
def _h(self):
|
|
80
|
+
if self._handle is None:
|
|
81
|
+
raise native.disposed("GpioController")
|
|
82
|
+
return self._handle
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def PinCount(self):
|
|
86
|
+
count = native.out_int()
|
|
87
|
+
native.gpio_pin_count(self._h(), native.ref(count))
|
|
88
|
+
return count.value
|
|
89
|
+
|
|
90
|
+
def OpenPin(self, pinNumber, mode=None, initialValue=None):
|
|
91
|
+
"""Opens the pin, optionally with a mode (and an output's first value: no glitch)"""
|
|
92
|
+
native.gpio_open_pin(self._h(), pinNumber, -1 if mode is None else int(mode),
|
|
93
|
+
-1 if initialValue is None else int(initialValue))
|
|
94
|
+
return GpioPin(self, pinNumber)
|
|
95
|
+
|
|
96
|
+
def ClosePin(self, pinNumber):
|
|
97
|
+
native.gpio_close_pin(self._h(), pinNumber)
|
|
98
|
+
with self._lock:
|
|
99
|
+
for key in [k for k in self._callbacks if k[0] == pinNumber]:
|
|
100
|
+
del self._callbacks[key]
|
|
101
|
+
|
|
102
|
+
def IsPinOpen(self, pinNumber):
|
|
103
|
+
is_open = native.out_int()
|
|
104
|
+
native.gpio_is_pin_open(self._h(), pinNumber, native.ref(is_open))
|
|
105
|
+
return bool(is_open.value)
|
|
106
|
+
|
|
107
|
+
def SetPinMode(self, pinNumber, mode, initialValue=None):
|
|
108
|
+
native.gpio_set_pin_mode(self._h(), pinNumber, int(mode), -1 if initialValue is None else int(initialValue))
|
|
109
|
+
|
|
110
|
+
def GetPinMode(self, pinNumber):
|
|
111
|
+
mode = native.out_int()
|
|
112
|
+
native.gpio_get_pin_mode(self._h(), pinNumber, native.ref(mode))
|
|
113
|
+
return PinMode(mode.value)
|
|
114
|
+
|
|
115
|
+
def IsPinModeSupported(self, pinNumber, mode):
|
|
116
|
+
supported = native.out_int()
|
|
117
|
+
native.gpio_is_pin_mode_supported(self._h(), pinNumber, int(mode), native.ref(supported))
|
|
118
|
+
return bool(supported.value)
|
|
119
|
+
|
|
120
|
+
def Read(self, pinNumber):
|
|
121
|
+
value = native.out_int()
|
|
122
|
+
native.gpio_read(self._h(), pinNumber, native.ref(value))
|
|
123
|
+
return PinValue(value.value)
|
|
124
|
+
|
|
125
|
+
def Write(self, pinNumber, value):
|
|
126
|
+
native.gpio_write(self._h(), pinNumber, int(bool(value)))
|
|
127
|
+
|
|
128
|
+
def Toggle(self, pinNumber):
|
|
129
|
+
self.Write(pinNumber, not self.Read(pinNumber))
|
|
130
|
+
|
|
131
|
+
def WaitForEvent(self, pinNumber, eventTypes, timeout):
|
|
132
|
+
"""Waits for a pin change; timeout is a timedelta (or seconds; None waits forever)"""
|
|
133
|
+
happened, timed_out = native.out_int(), native.out_int()
|
|
134
|
+
native.gpio_wait_for_event(self._h(), pinNumber, int(eventTypes), _milliseconds(timeout),
|
|
135
|
+
native.ref(happened), native.ref(timed_out))
|
|
136
|
+
return WaitForEventResult(happened.value, bool(timed_out.value))
|
|
137
|
+
|
|
138
|
+
def RegisterCallbackForPinValueChangedEvent(self, pinNumber, eventTypes, callback):
|
|
139
|
+
"""callback(sender, PinValueChangedEventArgs) runs on the controller's own thread"""
|
|
140
|
+
def on_change(context, pin, change):
|
|
141
|
+
callback(self, PinValueChangedEventArgs(change, pin))
|
|
142
|
+
c_callback = GpioCallback(on_change)
|
|
143
|
+
callback_id = native.out_int()
|
|
144
|
+
native.gpio_add_callback(self._h(), pinNumber, int(eventTypes), c_callback, None, native.ref(callback_id))
|
|
145
|
+
with self._lock:
|
|
146
|
+
old = self._callbacks.pop((pinNumber, callback), None)
|
|
147
|
+
self._callbacks[(pinNumber, callback)] = (callback_id.value, c_callback)
|
|
148
|
+
if old:
|
|
149
|
+
native.gpio_remove_callback(self._h(), old[0])
|
|
150
|
+
|
|
151
|
+
def UnregisterCallbackForPinValueChangedEvent(self, pinNumber, callback):
|
|
152
|
+
with self._lock:
|
|
153
|
+
registered = self._callbacks.pop((pinNumber, callback), None)
|
|
154
|
+
if registered:
|
|
155
|
+
native.gpio_remove_callback(self._h(), registered[0])
|
|
156
|
+
|
|
157
|
+
def Dispose(self):
|
|
158
|
+
"""Closes every pin and stops the controller"""
|
|
159
|
+
if self._handle is not None:
|
|
160
|
+
native.gpio_dispose(self._handle)
|
|
161
|
+
self._handle = None
|
|
162
|
+
self._callbacks.clear()
|
|
163
|
+
|
|
164
|
+
def __enter__(self):
|
|
165
|
+
return self
|
|
166
|
+
|
|
167
|
+
def __exit__(self, *exception):
|
|
168
|
+
self.Dispose()
|
|
169
|
+
|
|
170
|
+
def __del__(self):
|
|
171
|
+
try:
|
|
172
|
+
self.Dispose()
|
|
173
|
+
except Exception:
|
|
174
|
+
pass
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
class GpioPin:
|
|
178
|
+
"""An open pin, as returned by GpioController.OpenPin"""
|
|
179
|
+
|
|
180
|
+
def __init__(self, controller, pinNumber):
|
|
181
|
+
self._controller = controller
|
|
182
|
+
self.PinNumber = pinNumber
|
|
183
|
+
|
|
184
|
+
def GetPinMode(self):
|
|
185
|
+
return self._controller.GetPinMode(self.PinNumber)
|
|
186
|
+
|
|
187
|
+
def SetPinMode(self, mode):
|
|
188
|
+
self._controller.SetPinMode(self.PinNumber, mode)
|
|
189
|
+
|
|
190
|
+
def IsPinModeSupported(self, mode):
|
|
191
|
+
return self._controller.IsPinModeSupported(self.PinNumber, mode)
|
|
192
|
+
|
|
193
|
+
def Read(self):
|
|
194
|
+
return self._controller.Read(self.PinNumber)
|
|
195
|
+
|
|
196
|
+
def Write(self, value):
|
|
197
|
+
self._controller.Write(self.PinNumber, value)
|
|
198
|
+
|
|
199
|
+
def Toggle(self):
|
|
200
|
+
self._controller.Toggle(self.PinNumber)
|
|
201
|
+
|
|
202
|
+
def __repr__(self):
|
|
203
|
+
return f"GpioPin({self.PinNumber})"
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: GHIElectronics.Endpoint.Devices.Gpio
|
|
3
|
+
Version: 0.5.1
|
|
4
|
+
Summary: GPIO for GHI Electronics Endpoint, with the API of .NET's System.Device.Gpio.
|
|
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.Gpio
|
|
17
|
+
|
|
18
|
+
GPIO for GHI Electronics Endpoint, with the API of .NET's System.Device.Gpio.
|
|
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/Gpio/__init__.py,sha256=4ytSAK-GSSohwJ6sSk7NoOyBVl40ZtIGEemH1YMjdPQ,6839
|
|
2
|
+
ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/METADATA,sha256=XXt6pYSbN_KwAifSujlakpgZg4hS2RvjRcT_wE_YDVU,1011
|
|
3
|
+
ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
|
|
4
|
+
ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/top_level.txt,sha256=NpOZNgd0iquwrRPrTOhUDxc2V9TtNFLv5Z_Dx3YZtIU,15
|
|
5
|
+
ghielectronics_endpoint_devices_gpio-0.5.1.dist-info/RECORD,,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
GHIElectronics
|