# -*- coding: cp1252 -*- ''' Created on 11.10.2011 @author: sth2mt ''' GPIO_DIRECT_OUT = True GPIO_DIRECT_IN = False class Treiber(object): ''' classdocs ''' stout = False def __init__(self,i_Pin,b_Direction,b_Val=False): ''' Constructor ''' self.printf( "Gpio_CL\n") self.i_PinNr = i_Pin self.b_Direction = b_Direction self.i_ERROR=0 self.a_c_ExportFilePath = "/sys/class/gpio/export" self.a_c_UnexportFilePath = "/sys/class/gpio/unexport" self.a_c_PinFilePath = "sys/class/gpio%d" self.a_c_PinValuePath = "/sys/class/gpio/gpio%d/value" self.a_c_PinDirectionPath = "/sys/class/gpio/gpio%d/direction" self.a_c_File = None self.F_PinValueFile = file self.GPIO_DIRECT_OUT = True self.GPIO_DIRECT_IN = False #self.f_WriteUnexportFile() #print "Pin in /sys/class/gpio/export File eintragen" if self.f_WriteExportFile()!=0: self.i_ERROR=-1 return None #print"Direction festlegen" if self.f_SetDirection(self.b_Direction)!=0: self.i_ERROR=-2 return None #print"Pin Value File oeffnen" if self.f_OpenPinValueFile()!=0: self.i_ERROR=-3 return self.i_ERROR #print "Wert setzen" if self.b_Direction == self.GPIO_DIRECT_OUT: if self.f_WritePin(b_Val)!=0: self.i_ERROR=-4 return None pass def __del__(self): self.printf( "~Gpio_CL\n") #self.f_WriteUnexportFile() ''' Destructor ''' if not self.F_PinValueFile.closed: self.F_PinValueFile.close() self.printf( "~Gpio_CL Ende\n") pass def f_OpenPinValueFile(self): self.a_c_File = self.a_c_PinValuePath % self.i_PinNr try: self.F_PinValueFile = open(self.a_c_File,"w+") return 0 except IOError,e: print "Gpio.f_OpenPinValueFile()", e return -1 pass def f_ReadPin(self): if self.F_PinValueFile.closed: self.f_OpenPinValueFile() try: c_Value = self.i_PinValueFile.read(1) return (c_Value=='1') except IOError,e: print "Gpio.f_ReadPin()", e return -1 pass def f_WritePin(self,b_Val): c_Val= "%d" % b_Val # if self.F_PinValueFile.closed: self.f_OpenPinValueFile() try: #print self.PinValueFile #print "Pin %d -> %s" % (self.i_PinNr,c_Val) #self.F_PinValueFile = open ("Test","w") self.F_PinValueFile.write(c_Val) return 0 except IOError,e: print "Gpio.f_WritePin()", e return -1 pass def f_WriteExportFile(self): try: File = open(self.a_c_ExportFilePath,"w") s_Buf = "%d" % self.i_PinNr File.write(s_Buf) File.close() return 0 except (IOError), e: #print "Gpio.f_WriteExportFile(%s %s)" % (self.a_c_ExportFilePath,s_Buf), e if e.args[0] == 22: print "Gpio.f_WriteExportFile(%s %s)" % (self.a_c_ExportFilePath,s_Buf), e print " GPIO nicht vorhanden" return -1 pass def f_WriteUnexportFile(self): try: File = open(self.a_c_UnexportFilePath,"w") s_Buf = "%d" % self.i_PinNr File.write(s_Buf) File.close() return 0 except (IOError), e: print "Gpio.f_WriteUnexportFile()", e if e.args[0] == 22: print " GPIO nicht vorhanden" return -1 def f_SetDirection(self,b_Direction): if b_Direction: s_Buf = "out" else: s_Buf = "in" self.b_Direction = b_Direction a_c_File = self.a_c_PinDirectionPath % self.i_PinNr #print a_c_File,s_Buf try: File = open(a_c_File,"w") File.write(s_Buf) File.close() return 0 except (IOError), e: print "Gpio.f_SetDirection()", e return -1 def printf(self,string): if Treiber.stout: print string pass