adde pwm. UNTESTED

This commit is contained in:
p.loedige 2021-01-04 23:55:50 +01:00
parent d9cbbc0a33
commit 20ff46c31f

View File

@ -41,6 +41,8 @@ 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
""" """
@ -61,13 +63,17 @@ 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
#setup pins #setup pins
for pin in pins: for pin in pins:
if protocol == "DI": if protocol == "DI":
GPIO.setup(self.Pin[pin].value,GPIO.IN) GPIO.setup(self.Pin[pin].value,GPIO.IN)
elif protocol == "DO": elif protocol =="DO":
GPIO.setup(self.Pin[pin].value,GPIO.OUT) 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)
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)
@ -140,6 +146,18 @@ class Interface_handler:
value = GPIO.HIGH if output[pin] == '1' else GPIO.LOW value = GPIO.HIGH if output[pin] == '1' else GPIO.LOW
GPIO.output(self.Pin[pin].value,value) GPIO.output(self.Pin[pin].value,value)
return output return output
elif protocol == "PWM":
if not isinstance(output,dict):
raise TypeError("value must be a dictionary of GPIOs and values")
for pin in output:
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)
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)