From 20ff46c31fdd2234f699b5af5ae2b150322775b6 Mon Sep 17 00:00:00 2001 From: "p.loedige" Date: Mon, 4 Jan 2021 23:55:50 +0100 Subject: [PATCH] adde pwm. UNTESTED --- interface_handler.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/interface_handler.py b/interface_handler.py index 7421f69..8b7007d 100644 --- a/interface_handler.py +++ b/interface_handler.py @@ -41,6 +41,8 @@ class Interface_handler: GPIO_26 = 37 GPIO_27 = 13 + pwms = dict + def __init__(self, xml_reader: Xml_reader): """inits the interface_handler """ @@ -61,13 +63,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 #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) else: 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 GPIO.output(self.Pin[pin].value,value) 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 raise ValueError("you cannot write to device %s" % device_name)