import time
OUTPUT = 'out' INPUT = 'in' HIGH = 1 LOW = 0
def reg(pin): try: exp = open('/sys/class/gpio/export','wb',0) exp.write(str(pin)) print('Registion Successful: pin%d' %pin) #exp.close() except IOError as e: #print('Registion Error: pin%d' %pin) print('Error: %d - %s' %(e.errno,e.strerror))
def unreg(pin): try: uexp = open('/sys/class/gpio/unexport','wb',0) uexp.write(str(pin)) print('Unregistion Successful: pin%d' %pin) uexp.close() except IOError as e: #print('Unregistion Error: pin%d' %pin) print('Error: %d - %s' %(e.errno,e.strerror))
def pinMode(pin, mode): try: direction = open('/sys/class/gpio/gpio%d/direction' %pin,'wb',0) direction.write('out' if mode == OUTPUT else 'in') #print('pin%d setted at %s' %(pin,'out' if mode == OUTPUT else 'in')) direction.close() except IOError as e: #print('CANNOT SET PIN MODE: pin%d' %pin) print('Error: %d - %s' %(e.errno,e.strerror))
def digitalWrite(pin,value): try: val = open('/sys/class/gpio/gpio%d/value' %pin,'wb',0) val.write(str(value)) val.close() except IOError as e: #print('WRITE ERROR: pin%d' %pin) print('Error: %d - %s' %(e.errno,e.strerror))
def digitalRead(pin): try: file = open('/sys/class/gpio/gpio%d/value' %pin,'rb',0) value = file.read() file.close() return int(value) except IOError as e: #print('READ ERROR') print('Error: %d - %s' %(e.errno,e.strerror)) return 0
def analogRead(pin): filePath = '/sys/devices/platform/c0000000.soc/c0053000.adc/iio:device0/in_voltage%d_raw' %pin print(filePath) try: file = open(filePath,"rb",0) value = file.read() file.close() return int(value) except IOError as e: print('Error: %d - %s' %(e.errno, e.strerror)) return 0
def mapValue(val, oldMin, oldMax, newMin, newMax): #Similar to map function in Arduino IDE return ( ( (val - oldMin) * (newMax - newMin) ) / (oldMax - oldMin) ) + newMin
###Buzzer buzzerPin = 128 #buzzer module signal pin on GPIO0 reg(buzzerPin) pinMode(buzzerPin, OUTPUT)
try: while True: digitalWrite(buzzerPin, HIGH) time.sleep(1) digitalWrite(buzzerPin, LOW) time.sleep(1) except KeyboardInterrupt: unreg(buzzerPin) print('Interrupted') |