basic interface handler implementation for the GPIOs. Untested!!
This commit is contained in:
		
							parent
							
								
									6fb6072aed
								
							
						
					
					
						commit
						f31a3b4616
					
				
							
								
								
									
										108
									
								
								interface_handler.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										108
									
								
								interface_handler.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,108 @@ | |||||||
|  | from enum import Enum | ||||||
|  | from xml_reader import Xml_reader | ||||||
|  | import RPi.GPIO as GPIO | ||||||
|  | 
 | ||||||
|  | class interface_handler: | ||||||
|  |     """handles the access to the various interfaces (e.g. GPIO) | ||||||
|  |     """ | ||||||
|  | 
 | ||||||
|  |     class Pin(Enum): | ||||||
|  |         """list of all usable pins | ||||||
|  | 
 | ||||||
|  |         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 | ||||||
|  | 
 | ||||||
|  |     def __init__(self, xml_reader): | ||||||
|  |         """inits the interface_handler | ||||||
|  |         """ | ||||||
|  |         if not isinstance(xml_reader, Xml_reader): | ||||||
|  |             raise TypeError('xml_reader must be an instance of XML_reader') | ||||||
|  |         self.xml_reader = xml_reader | ||||||
|  |         self.init_gpio() | ||||||
|  | 
 | ||||||
|  |     def init_gpio(self): | ||||||
|  |         """initialises the GPIO pins | ||||||
|  |         """ | ||||||
|  |         GPIO.setmode(GPIO.BOARD) | ||||||
|  |         for device_name in self.xml_reader.get_device_names(): | ||||||
|  |             protocol, pins in self.xml_reader.get_port(device_name) | ||||||
|  |             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) | ||||||
|  |                 else: | ||||||
|  |                     raise NotImplementedError('the protocol of the device %s is not implemented' % device_name) | ||||||
|  |                  | ||||||
|  | 
 | ||||||
|  |     def read(self, device_name): | ||||||
|  |         """reads the current value from the pins of one device | ||||||
|  | 
 | ||||||
|  |         Args: | ||||||
|  |             device_name (string): the name of the device | ||||||
|  | 
 | ||||||
|  |         Raises: | ||||||
|  |             NameError: the device cannot be found in the config.xml | ||||||
|  | 
 | ||||||
|  |         Returns: | ||||||
|  |             dict:  {'pin': 'value'} | ||||||
|  |         """ | ||||||
|  |         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) | ||||||
|  |         for pin in self.xml_reader.get_port(device_name)["pins"]: | ||||||
|  |             returnValues.update({ | ||||||
|  |                 pin, | ||||||
|  |                 GPIO.read(self.Pin(pin)) | ||||||
|  |             }) | ||||||
|  |         return returnValues | ||||||
|  | 
 | ||||||
|  |     def write(self, device_name, value): | ||||||
|  |         """writes a value to all the pins of a device | ||||||
|  | 
 | ||||||
|  |         Args: | ||||||
|  |             device_name (string): name of the device  | ||||||
|  |             value (float): the value to be written | ||||||
|  | 
 | ||||||
|  |         Raises: | ||||||
|  |             NameError: device not found in config.xml | ||||||
|  |             TypeError: value has wrong type | ||||||
|  |         """ | ||||||
|  |         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) | ||||||
|  |         for pin in self.xml_reader.get_port(device_name)["pins"]: | ||||||
|  |             GPIO.write(self.Pin(pin,value)) | ||||||
| @ -1,6 +1,6 @@ | |||||||
| import xml.etree.ElementTree as ET | import xml.etree.ElementTree as ET | ||||||
| 
 | 
 | ||||||
| class xml_reader: | class Xml_reader: | ||||||
|     """ reader for the XML configuration """ |     """ reader for the XML configuration """ | ||||||
| 
 | 
 | ||||||
|     def __init__(self, path): |     def __init__(self, path): | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user