checked and improved PWM functionality

This commit is contained in:
p.loedige 2021-01-05 14:37:19 +01:00
parent d27366d553
commit 3fe412e324

View File

@ -41,17 +41,16 @@ class Interface_handler:
GPIO_26 = 37 GPIO_26 = 37
GPIO_27 = 13 GPIO_27 = 13
pwms = dict
def __init__(self, xml_reader: Xml_reader): def __init__(self, xml_reader: Xml_reader):
"""inits the interface_handler """inits the interface_handler
""" """
#type check xml_reader #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 #initialise interface_handler
self.xml_reader = xml_reader self.xml_reader = xml_reader
self.pwms = [None] * 41
self.pwm_dutycycles = [None] * 41
self.init_gpio() self.init_gpio()
@ -63,7 +62,7 @@ class Interface_handler:
port_info = self.xml_reader.get_port(device_name) port_info = self.xml_reader.get_port(device_name)
protocol = port_info["protocol"] protocol = port_info["protocol"]
pins = port_info["pins"] pins = port_info["pins"]
frequency = int(port_info["frequency"]) if port_info["frequency"] != '' else 0 frequency = int(port_info["frequency"]) if port_info["frequency"] != '' else 1
#setup pins #setup pins
for pin in pins: for pin in pins:
@ -73,7 +72,7 @@ class Interface_handler:
GPIO.setup(self.Pin[pin].value,GPIO.OUT) GPIO.setup(self.Pin[pin].value,GPIO.OUT)
elif protocol =="PWM": elif protocol =="PWM":
GPIO.setup(self.Pin[pin].value,GPIO.OUT) GPIO.setup(self.Pin[pin].value,GPIO.OUT)
self.pwms[pin] = GPIO.PWM(self.Pin[pin].value,frequency) self.pwms[self.Pin[pin].value] = GPIO.PWM(self.Pin[pin].value,frequency)
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)
@ -101,10 +100,14 @@ class Interface_handler:
pins = port_info["pins"] 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" or "DO": if protocol in ["DO","DI"]:
returnValues.update({ returnValues.update({
pin : GPIO.input(self.Pin[pin].value) pin : GPIO.input(self.Pin[pin].value)
}) })
if protocol in ["PWM"]:
returnValues.update({
pin : self.pwm_dutycycles[self.Pin[pin].value]
})
else: #throw error for protocols without write functionality else: #throw error for protocols without write functionality
raise ValueError("you can not read from device %s" % device_name) raise ValueError("you can not read from device %s" % device_name)
return returnValues return returnValues
@ -153,10 +156,11 @@ class Interface_handler:
if not pin in self.xml_reader.get_port(device_name)["pins"]: if not pin in self.xml_reader.get_port(device_name)["pins"]:
raise ValueError(str("pin %s is not a part of device %s",pin,device_name)) raise ValueError(str("pin %s is not a part of device %s",pin,device_name))
dutycycle = int(output[pin]) dutycycle = int(output[pin])
if not dutycycle in range(0,1000,1): self.pwm_dutycycles[self.Pin[pin].value] = dutycycle
raise TypeError("value must be a whole number between 0 and 1000") if not dutycycle in range(1,100,1):
pwms[pin].stop() raise TypeError("value must be a whole number between 0 and 100")
pwms[pin].start(dutycycle) self.pwms[self.Pin[pin].value].stop()
self.pwms[self.Pin[pin].value].start(dutycycle)
return output return output
else: #throw error for protocols without write functionality else: #throw error for protocols without write functionality
raise ValueError("you cannot write to device %s" % device_name) raise ValueError("you cannot write to device %s" % device_name)