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