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 xml_reader import Xml_reader
|
||||
import RPi.GPIO as GPIO
|
||||
import sys
|
||||
|
||||
sys.path.append('/home/pi/aped_device')
|
||||
|
||||
from xml_reader import Xml_reader
|
||||
class Interface_handler:
|
||||
"""handles the access to the various interfaces (e.g. GPIO)
|
||||
"""
|
||||
@ -11,38 +14,41 @@ class Interface_handler:
|
||||
|
||||
enables restricting methods to these pins
|
||||
"""
|
||||
GIO_2 = 3
|
||||
GIO_3 = 5
|
||||
GIO_4 = 7
|
||||
GIO_5 = 29
|
||||
GIO_6 = 31
|
||||
GIO_7 = 26
|
||||
GIO_8 = 24
|
||||
GIO_9 = 21
|
||||
GIO_10 = 19
|
||||
GIO_11 = 23
|
||||
GIO_12 = 32
|
||||
GIO_13 = 33
|
||||
GIO_14 = 8
|
||||
GIO_15 = 10
|
||||
GIO_16 = 36
|
||||
GIO_17 = 11
|
||||
GIO_18 = 12
|
||||
GIO_19 = 35
|
||||
GIO_20 = 38
|
||||
GIO_21 = 40
|
||||
GIO_22 = 15
|
||||
GIO_23 = 16
|
||||
GIO_24 = 18
|
||||
GIO_25 = 22
|
||||
GIO_26 = 37
|
||||
GIO_27 = 13
|
||||
GPIO_2 = 3
|
||||
GPIO_3 = 5
|
||||
GPIO_4 = 7
|
||||
GPIO_5 = 29
|
||||
GPIO_6 = 31
|
||||
GPIO_7 = 26
|
||||
GPIO_8 = 24
|
||||
GPIO_9 = 21
|
||||
GPIO_10 = 19
|
||||
GPIO_11 = 23
|
||||
GPIO_12 = 32
|
||||
GPIO_13 = 33
|
||||
GPIO_14 = 8
|
||||
GPIO_15 = 10
|
||||
GPIO_16 = 36
|
||||
GPIO_17 = 11
|
||||
GPIO_18 = 12
|
||||
GPIO_19 = 35
|
||||
GPIO_20 = 38
|
||||
GPIO_21 = 40
|
||||
GPIO_22 = 15
|
||||
GPIO_23 = 16
|
||||
GPIO_24 = 18
|
||||
GPIO_25 = 22
|
||||
GPIO_26 = 37
|
||||
GPIO_27 = 13
|
||||
|
||||
def __init__(self, xml_reader):
|
||||
def __init__(self, xml_reader: Xml_reader):
|
||||
"""inits the interface_handler
|
||||
"""
|
||||
#type check xml_reader
|
||||
if not isinstance(xml_reader, Xml_reader):
|
||||
raise TypeError('xml_reader must be an instance of XML_reader')
|
||||
|
||||
#initialise interface_handler
|
||||
self.xml_reader = xml_reader
|
||||
self.init_gpio()
|
||||
|
||||
@ -51,17 +57,21 @@ class Interface_handler:
|
||||
"""
|
||||
GPIO.setmode(GPIO.BOARD)
|
||||
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:
|
||||
if protocol == "AI" or "DI":
|
||||
GPIO.setup(self.Pin(pin),GPIO.IN)
|
||||
elif protocol == "AO" or "DO":
|
||||
GPIO.setup(self.Pin(pin),GPIO.OUT)
|
||||
if protocol == "DI":
|
||||
GPIO.setup(self.Pin[pin].value,GPIO.IN)
|
||||
elif protocol == "DO":
|
||||
GPIO.setup(self.Pin[pin].value,GPIO.OUT)
|
||||
else:
|
||||
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
|
||||
|
||||
Args:
|
||||
@ -73,20 +83,27 @@ class Interface_handler:
|
||||
Returns:
|
||||
dict: {'pin': 'value'}
|
||||
"""
|
||||
#check device_name is valid
|
||||
if self.xml_reader.get_port(device_name) == None:
|
||||
raise NameError('the device with the name %s is unknown' % device_name)
|
||||
|
||||
returnValues = dict()
|
||||
protocol, pins = self.xml_reader.get_port(device_name)
|
||||
if not protocol == "AI" or "DI":
|
||||
raise ValueError("you can not read from device %s" % device_name)
|
||||
|
||||
port_info = self.xml_reader.get_port(device_name)
|
||||
protocol = port_info["protocol"]
|
||||
pins = port_info["pins"]
|
||||
|
||||
for pin in self.xml_reader.get_port(device_name)["pins"]:
|
||||
returnValues.update({
|
||||
pin,
|
||||
GPIO.read(self.Pin(pin))
|
||||
})
|
||||
if protocol == "DI":
|
||||
returnValues.update({
|
||||
pin:GPIO.input(self.Pin[pin].value)
|
||||
})
|
||||
else: #throw error for protocols without write functionality
|
||||
raise ValueError("you can not read from device %s" % device_name)
|
||||
|
||||
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
|
||||
|
||||
Args:
|
||||
@ -96,13 +113,31 @@ class Interface_handler:
|
||||
Raises:
|
||||
NameError: device not found in config.xml
|
||||
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:
|
||||
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')
|
||||
protocol, pins = self.xml_reader.get_port(device_name)
|
||||
if not protocol == "AO" or "DO":
|
||||
raise ValueError("you cannot write to device %s" % device_name)
|
||||
|
||||
port_info = self.xml_reader.get_port(device_name)
|
||||
protocol = port_info["protocol"]
|
||||
pins = port_info["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
|
@ -2,7 +2,7 @@ from lxml import etree
|
||||
class Xml_reader:
|
||||
""" reader for the XML configuration """
|
||||
|
||||
def __init__(self, xml_path:str, xsd_path:str):
|
||||
def __init__(self, xml_path: str, xsd_path: str):
|
||||
"""inits the reader
|
||||
|
||||
Args:
|
||||
@ -17,7 +17,7 @@ class Xml_reader:
|
||||
raise SyntaxError('the XML config file has an invalid syntax')
|
||||
self.root = etree.parse(xml_path).getroot()
|
||||
|
||||
def validate(self, xml_path:str, xsd_path: str) -> bool:
|
||||
def validate(self, xml_path: str, xsd_path: str) -> bool:
|
||||
"""validates a XML by using a XSD file
|
||||
|
||||
Args:
|
||||
@ -38,7 +38,7 @@ class Xml_reader:
|
||||
"""
|
||||
return [device.get("name") for device in self.root.findall('Device')]
|
||||
|
||||
def get_value_info(self,device_name:str) -> dict:
|
||||
def get_value_info(self,device_name: str) -> dict:
|
||||
"""returns the value information for a given device
|
||||
|
||||
Args:
|
||||
@ -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