예제 #1
0
def GPIO_IO_TESTING():
    print('== Testing GPIO INPUT/OUTPUT ==')
    for mode in ['phys', 'TB', 'BCM']:
        GPIO.setmode(modeMap[mode])
        LPin = [pinTable[pins[0] - 1][mode] for pins in pairPins]
        RPin = [pinTable[pins[1] - 1][mode] for pins in pairPins]
        if (-1 in LPin or -1 in RPin):
            print('Some pins use the 3.3V or GND pin.')
            exit()
        for IPin, OPin in [(LPin, RPin), (RPin, LPin)]:
            GPIO.setup(IPin, GPIO.IN)
            GPIO.setup(OPin, GPIO.OUT)
            if (False in [GPIO.gpio_function(pin) == GPIO.IN for pin in IPin]
                    or False
                    in [GPIO.gpio_function(pin) == GPIO.OUT for pin in OPin]):
                print('Check GPIO.gpio_function or GPIO.setup.')
                exit()
            for volt in [GPIO.HIGH, GPIO.LOW]:
                GPIO.output(OPin, volt)
                OResult = [GPIO.input(pin) == volt for pin in OPin]
                IResult = [
                    GPIO.input(IPin[i]) == GPIO.input(OPin[i])
                    for i in range(len(IPin))
                ]
                if (False in OResult):
                    print('Check Pin[%d].' % (OPin[OResult.index(False)]))
                    exit()
                if (False in IResult):
                    print('Check Pin[%d].' % (IPin[IResult.index(False)]))
                    exit()
        print("[PASS] GPIO.setmode(%s)" % (modeNameMap[mode]))
        GPIO.cleanup()
    print('===============================')
예제 #2
0
 def __getFunction__(self, channel):
     try:
         offset = FSEL_OFFSET + int(channel / 10) * 4
         shift = int(channel % 10) * 3
         self.gpio_map.seek(offset)
         value = int.from_bytes(self.gpio_map.read(4), byteorder='little')
         value >>= shift
         value &= 7
         return value  # 0=input, 1=output, 4=alt0
     except:
         pass
     self.__checkFilesystemFunction__(channel)
     self.checkDigitalChannelExported(channel)
     try:
         # If we haven't already set the channel function on an ASUS device, we use the GPIO
         # library to get the function. Otherwise we just fall through and read the file itself
         # since we can assume the pin is a GPIO pin and reading the function file is quicker
         #  than launching a separate process.
         if gpio_library and channel not in self.pinFunctionSet:
             if os.geteuid() == 0:
                 value = gpio_library.gpio_function(channel)
             else:
                 value, err = executeCommand(
                     'sudo python3 -m myDevices.devices.readvalue -c {}'.
                     format(channel))
                 return int(value.splitlines()[0])
             # If this is not a GPIO function return it, otherwise check the function file to see
             # if it is an IN or OUT pin since the ASUS library doesn't provide that info.
             if value != self.ASUS_GPIO:
                 return value
         function = self.functionFile[channel].read()
         self.functionFile[channel].seek(0)
         if function.startswith("out"):
             return self.OUT
         else:
             return self.IN
     except Exception as ex:
         error('Failed on __getFunction__: ' + str(channel) + ' ' + str(ex))
         return -1