from time import sleep OUTPUT = 'out' INPUT = 'in' HIGH = 1 LOW = 0 def export(pin): try: exp = open('/sys/class/gpio/export','wb',0) exp.write(str(pin)) print('Registion Successful: pin%d' %pin) except IOError as e: print('Error: %d - %s' %(e.errno,e.strerror)) def unexport(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('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') direction.close() except IOError as e: 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('Error: %d - %s' %(e.errno,e.strerror)) def analogRead(pin): filePath = '/sys/devices/platform/c0000000.soc/c0053000.adc/iio:device0/in_voltage%d_raw' %pin 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 LEDpin = 128 LightSensor = 0 export(LEDpin) pinMode(LEDpin,OUTPUT) try: while True: raw = analogRead(LightSensor) volt = raw * 1.8/4095 print("Raw value: %d" %raw) print("Voltage: %0.2f" %volt) if raw <= 1024: digitalWrite(LEDpin, HIGH) else: digitalWrite(LEDpin, LOW) sleep(0.5) except KeyboardInterrupt: unexport(LEDpin) print("Stop!!!")
|
최신댓글