tested and improved interface_handler and xml_reader on raspberry
This commit is contained in:
parent
1df94d8f44
commit
b1cb29e024
@ -1,7 +1,10 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from xml_reader import Xml_reader
|
|
||||||
import RPi.GPIO as GPIO
|
import RPi.GPIO as GPIO
|
||||||
|
import sys
|
||||||
|
|
||||||
|
sys.path.append('/home/pi/aped_device')
|
||||||
|
|
||||||
|
from xml_reader import Xml_reader
|
||||||
class Interface_handler:
|
class Interface_handler:
|
||||||
"""handles the access to the various interfaces (e.g. GPIO)
|
"""handles the access to the various interfaces (e.g. GPIO)
|
||||||
"""
|
"""
|
||||||
@ -11,38 +14,41 @@ class Interface_handler:
|
|||||||
|
|
||||||
enables restricting methods to these pins
|
enables restricting methods to these pins
|
||||||
"""
|
"""
|
||||||
GIO_2 = 3
|
GPIO_2 = 3
|
||||||
GIO_3 = 5
|
GPIO_3 = 5
|
||||||
GIO_4 = 7
|
GPIO_4 = 7
|
||||||
GIO_5 = 29
|
GPIO_5 = 29
|
||||||
GIO_6 = 31
|
GPIO_6 = 31
|
||||||
GIO_7 = 26
|
GPIO_7 = 26
|
||||||
GIO_8 = 24
|
GPIO_8 = 24
|
||||||
GIO_9 = 21
|
GPIO_9 = 21
|
||||||
GIO_10 = 19
|
GPIO_10 = 19
|
||||||
GIO_11 = 23
|
GPIO_11 = 23
|
||||||
GIO_12 = 32
|
GPIO_12 = 32
|
||||||
GIO_13 = 33
|
GPIO_13 = 33
|
||||||
GIO_14 = 8
|
GPIO_14 = 8
|
||||||
GIO_15 = 10
|
GPIO_15 = 10
|
||||||
GIO_16 = 36
|
GPIO_16 = 36
|
||||||
GIO_17 = 11
|
GPIO_17 = 11
|
||||||
GIO_18 = 12
|
GPIO_18 = 12
|
||||||
GIO_19 = 35
|
GPIO_19 = 35
|
||||||
GIO_20 = 38
|
GPIO_20 = 38
|
||||||
GIO_21 = 40
|
GPIO_21 = 40
|
||||||
GIO_22 = 15
|
GPIO_22 = 15
|
||||||
GIO_23 = 16
|
GPIO_23 = 16
|
||||||
GIO_24 = 18
|
GPIO_24 = 18
|
||||||
GIO_25 = 22
|
GPIO_25 = 22
|
||||||
GIO_26 = 37
|
GPIO_26 = 37
|
||||||
GIO_27 = 13
|
GPIO_27 = 13
|
||||||
|
|
||||||
def __init__(self, xml_reader):
|
def __init__(self, xml_reader: Xml_reader):
|
||||||
"""inits the interface_handler
|
"""inits the interface_handler
|
||||||
"""
|
"""
|
||||||
|
#type check xml_reader
|
||||||
if not isinstance(xml_reader, Xml_reader):
|
if not isinstance(xml_reader, Xml_reader):
|
||||||
raise TypeError('xml_reader must be an instance of XML_reader')
|
raise TypeError('xml_reader must be an instance of XML_reader')
|
||||||
|
|
||||||
|
#initialise interface_handler
|
||||||
self.xml_reader = xml_reader
|
self.xml_reader = xml_reader
|
||||||
self.init_gpio()
|
self.init_gpio()
|
||||||
|
|
||||||
@ -51,17 +57,21 @@ class Interface_handler:
|
|||||||
"""
|
"""
|
||||||
GPIO.setmode(GPIO.BOARD)
|
GPIO.setmode(GPIO.BOARD)
|
||||||
for device_name in self.xml_reader.get_device_names():
|
for device_name in self.xml_reader.get_device_names():
|
||||||
protocol, pins in self.xml_reader.get_port(device_name)
|
port_info = self.xml_reader.get_port(device_name)
|
||||||
|
protocol = port_info["protocol"]
|
||||||
|
pins = port_info["pins"]
|
||||||
|
|
||||||
|
#setup pins
|
||||||
for pin in pins:
|
for pin in pins:
|
||||||
if protocol == "AI" or "DI":
|
if protocol == "DI":
|
||||||
GPIO.setup(self.Pin(pin),GPIO.IN)
|
GPIO.setup(self.Pin[pin].value,GPIO.IN)
|
||||||
elif protocol == "AO" or "DO":
|
elif protocol == "DO":
|
||||||
GPIO.setup(self.Pin(pin),GPIO.OUT)
|
GPIO.setup(self.Pin[pin].value,GPIO.OUT)
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError('the protocol of the device %s is not implemented' % device_name)
|
raise NotImplementedError('the protocol of the device %s is not implemented' % device_name)
|
||||||
|
|
||||||
|
|
||||||
def read(self, device_name):
|
def read(self, device_name:str) -> dict:
|
||||||
"""reads the current value from the pins of one device
|
"""reads the current value from the pins of one device
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -73,20 +83,27 @@ class Interface_handler:
|
|||||||
Returns:
|
Returns:
|
||||||
dict: {'pin': 'value'}
|
dict: {'pin': 'value'}
|
||||||
"""
|
"""
|
||||||
|
#check device_name is valid
|
||||||
if self.xml_reader.get_port(device_name) == None:
|
if self.xml_reader.get_port(device_name) == None:
|
||||||
raise NameError('the device with the name %s is unknown' % device_name)
|
raise NameError('the device with the name %s is unknown' % device_name)
|
||||||
|
|
||||||
returnValues = dict()
|
returnValues = dict()
|
||||||
protocol, pins = self.xml_reader.get_port(device_name)
|
|
||||||
if not protocol == "AI" or "DI":
|
port_info = self.xml_reader.get_port(device_name)
|
||||||
raise ValueError("you can not read from device %s" % device_name)
|
protocol = port_info["protocol"]
|
||||||
|
pins = port_info["pins"]
|
||||||
|
|
||||||
for pin in self.xml_reader.get_port(device_name)["pins"]:
|
for pin in self.xml_reader.get_port(device_name)["pins"]:
|
||||||
|
if protocol == "DI":
|
||||||
returnValues.update({
|
returnValues.update({
|
||||||
pin,
|
pin:GPIO.input(self.Pin[pin].value)
|
||||||
GPIO.read(self.Pin(pin))
|
|
||||||
})
|
})
|
||||||
|
else: #throw error for protocols without write functionality
|
||||||
|
raise ValueError("you can not read from device %s" % device_name)
|
||||||
|
|
||||||
return returnValues
|
return returnValues
|
||||||
|
|
||||||
def write(self, device_name, value):
|
def write(self, device_name:str, value):
|
||||||
"""writes a value to all the pins of a device
|
"""writes a value to all the pins of a device
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -96,13 +113,31 @@ class Interface_handler:
|
|||||||
Raises:
|
Raises:
|
||||||
NameError: device not found in config.xml
|
NameError: device not found in config.xml
|
||||||
TypeError: value has wrong type
|
TypeError: value has wrong type
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
value if everything went well, None otherwise
|
||||||
"""
|
"""
|
||||||
|
#check device_name is valid
|
||||||
if self.xml_reader.get_port(device_name) == None:
|
if self.xml_reader.get_port(device_name) == None:
|
||||||
raise NameError('the device with the name %s is unknown' % device_name)
|
raise NameError('the device with the name %s is unknown' % device_name)
|
||||||
if not isinstance(value,float):
|
|
||||||
raise TypeError('the value to write must be of type float')
|
port_info = self.xml_reader.get_port(device_name)
|
||||||
protocol, pins = self.xml_reader.get_port(device_name)
|
protocol = port_info["protocol"]
|
||||||
if not protocol == "AO" or "DO":
|
pins = port_info["pins"]
|
||||||
raise ValueError("you cannot write to device %s" % device_name)
|
|
||||||
for pin in self.xml_reader.get_port(device_name)["pins"]:
|
for pin in self.xml_reader.get_port(device_name)["pins"]:
|
||||||
GPIO.write(self.Pin(pin,value))
|
if protocol == "DO":
|
||||||
|
#type check input value
|
||||||
|
if not value in [GPIO.HIGH,GPIO.LOW]:
|
||||||
|
raise ValueError("value must be GPIO.HIGH or GPIO.LOW")
|
||||||
|
GPIO.output(self.Pin[pin].value,value)
|
||||||
|
return value
|
||||||
|
else: #throw error for protocols without write functionality
|
||||||
|
raise ValueError("you cannot write to device %s" % device_name)
|
||||||
|
|
||||||
|
|
||||||
|
# ##test
|
||||||
|
# interface_handler = Interface_handler(Xml_reader('XML/config.xml','XML/config.xsd'))
|
||||||
|
# print(interface_handler.read('example'))
|
||||||
|
# print(interface_handler.read('sensorarray'))
|
||||||
|
# print(interface_handler.write('example',"test")) #shuld throw not writable error
|
@ -89,3 +89,11 @@ class Xml_reader:
|
|||||||
)
|
)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# ##test
|
||||||
|
# xml_reader = Xml_reader('XML/config.xml','XML/config.xsd')
|
||||||
|
# print(xml_reader.get_device_names)
|
||||||
|
# print(xml_reader.get_value_info("example"))
|
||||||
|
# print(xml_reader.get_port("example"))
|
||||||
|
# print(xml_reader.get_value_info("sensorarray"))
|
||||||
|
# print(xml_reader.get_port("sensorarray"))
|
Loading…
x
Reference in New Issue
Block a user