platypush/plugins/gpio/zeroborg/lib/__init__.py
def SetNewAddress(newAddress, oldAddress=-1, busNumber=1):
    """
SetNewAddress(newAddress, [oldAddress], [busNumber])

Scans the I?C bus for the first ZeroBorg and sets it to a new I2C address
If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus
The busNumber if supplied is which I?C bus to scan, 0 for Rev 1 boards, 1 for Rev 2 boards, if not supplied
the default is 1.
Warning, this new I?C address will still be used after resetting the power on the device
    """
    if newAddress < 0x03:
        print('Error, I?C addresses below 3 (0x03) are reserved, use an address between 3 (0x03) and 119 (0x77)')
        return
    elif newAddress > 0x77:
        print('Error, I?C addresses above 119 (0x77) are reserved, use an address between 3 (0x03) and 119 (0x77)')
        return
    if oldAddress < 0x0:
        found = ScanForZeroBorg(busNumber)
        if len(found) < 1:
            print('No ZeroBorg boards found, cannot set a new I?C address!')
            return
        else:
            oldAddress = found[0]
    print('Changing I2C address from %02X to %02X (bus #%d)' % (oldAddress, newAddress, busNumber))
    bus = ZeroBorg()
    bus.InitBusOnly(busNumber, oldAddress)
    # noinspection PyBroadException
    try:
        i2cRecv = bus.RawRead(COMMAND_GET_ID, I2C_NORM_LEN)
        if len(i2cRecv) == I2C_NORM_LEN:
            if i2cRecv[1] == I2C_ID_ZEROBORG:
                foundChip = True
                print('Found ZeroBorg at %02X' % oldAddress)
            else:
                foundChip = False
                print('Found a device at %02X, but it is not a ZeroBorg (ID %02X instead of %02X)' % (
                    oldAddress, i2cRecv[1], I2C_ID_ZEROBORG))
        else:
            foundChip = False
            print('Missing ZeroBorg at %02X' % oldAddress)
    except KeyboardInterrupt:
        raise
    except Exception as e:
        foundChip = False
        print(str(e))
        print('Missing ZeroBorg at %02X' % oldAddress)
    if foundChip:
        bus.RawWrite(COMMAND_SET_I2C_ADD, [newAddress])
        time.sleep(0.1)
        print('Address changed to %02X, attempting to talk with the new address' % newAddress)
        # noinspection PyBroadException
        try:
            bus.InitBusOnly(busNumber, newAddress)
            i2cRecv = bus.RawRead(COMMAND_GET_ID, I2C_NORM_LEN)
            if len(i2cRecv) == I2C_NORM_LEN:
                if i2cRecv[1] == I2C_ID_ZEROBORG:
                    foundChip = True
                    print('Found ZeroBorg at %02X' % newAddress)
                else:
                    foundChip = False
                    print('Found a device at %02X, but it is not a ZeroBorg (ID %02X instead of %02X)' % (
                        newAddress, i2cRecv[1], I2C_ID_ZEROBORG))
            else:
                foundChip = False
                print('Missing ZeroBorg at %02X' % newAddress)
        except KeyboardInterrupt:
            raise
        except Exception as e:
            foundChip = False
            print(str(e))
            print('Missing ZeroBorg at %02X' % newAddress)
    if foundChip:
        print('New I?C address of %02X set successfully' % newAddress)
    else:
        print('Failed to set new I?C address...')

SetNewAddress(newAddress, [oldAddress], [busNumber])

Scans the I?C bus for the first ZeroBorg and sets it to a new I2C address
If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus
The busNumber if supplied is which I?C bus to scan, 0 for Rev 1 boards, 1 for Rev 2 boards, if not supplied
the default is 1.
Warning, this new I?C address will still be used after resetting the power on the device
    
SetNewAddress(newAddress, [oldAddress], [busNumber])
Scans the I?C bus for the first ZeroBorg and sets it to a new I2C address
If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus
The busNumber if supplied is which I?C bus to scan, 0 for Rev 1 boards, 1 for Rev 2 boards, if not supplied
the default is 1.
Warning, this new I?C address will still be used after resetting the power on the device
SetNewAddress ( newAddress [ oldAddress ] [ busNumber ] ) Scans the I?C bus for the first ZeroBorg and sets it to a new I2C address If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus The busNumber if supplied is which I?C bus to scan 0 for Rev 1 boards 1 for Rev 2 boards if not supplied the default is 1 . Warning this new I?C address will still be used after resetting the power on the device
def SetNewAddress(newAddress, oldAddress=-1, busNumber=1):
    if newAddress < 0x03:
        print('Error, I?C addresses below 3 (0x03) are reserved, use an address between 3 (0x03) and 119 (0x77)')
        return
    elif newAddress > 0x77:
        print('Error, I?C addresses above 119 (0x77) are reserved, use an address between 3 (0x03) and 119 (0x77)')
        return
    if oldAddress < 0x0:
        found = ScanForZeroBorg(busNumber)
        if len(found) < 1:
            print('No ZeroBorg boards found, cannot set a new I?C address!')
            return
        else:
            oldAddress = found[0]
    print('Changing I2C address from %02X to %02X (bus #%d)' % (oldAddress, newAddress, busNumber))
    bus = ZeroBorg()
    bus.InitBusOnly(busNumber, oldAddress)
    try:
        i2cRecv = bus.RawRead(COMMAND_GET_ID, I2C_NORM_LEN)
        if len(i2cRecv) == I2C_NORM_LEN:
            if i2cRecv[1] == I2C_ID_ZEROBORG:
                foundChip = True
                print('Found ZeroBorg at %02X' % oldAddress)
            else:
                foundChip = False
                print('Found a device at %02X, but it is not a ZeroBorg (ID %02X instead of %02X)' % (
                    oldAddress, i2cRecv[1], I2C_ID_ZEROBORG))
        else:
            foundChip = False
            print('Missing ZeroBorg at %02X' % oldAddress)
    except KeyboardInterrupt:
        raise
    except Exception as e:
        foundChip = False
        print(str(e))
        print('Missing ZeroBorg at %02X' % oldAddress)
    if foundChip:
        bus.RawWrite(COMMAND_SET_I2C_ADD, [newAddress])
        time.sleep(0.1)
        print('Address changed to %02X, attempting to talk with the new address' % newAddress)
        try:
            bus.InitBusOnly(busNumber, newAddress)
            i2cRecv = bus.RawRead(COMMAND_GET_ID, I2C_NORM_LEN)
            if len(i2cRecv) == I2C_NORM_LEN:
                if i2cRecv[1] == I2C_ID_ZEROBORG:
                    foundChip = True
                    print('Found ZeroBorg at %02X' % newAddress)
                else:
                    foundChip = False
                    print('Found a device at %02X, but it is not a ZeroBorg (ID %02X instead of %02X)' % (
                        newAddress, i2cRecv[1], I2C_ID_ZEROBORG))
            else:
                foundChip = False
                print('Missing ZeroBorg at %02X' % newAddress)
        except KeyboardInterrupt:
            raise
        except Exception as e:
            foundChip = False
            print(str(e))
            print('Missing ZeroBorg at %02X' % newAddress)
    if foundChip:
        print('New I?C address of %02X set successfully' % newAddress)
    else:
        print('Failed to set new I?C address...')
def SetNewAddress ( newAddress , oldAddress = - 1 , busNumber = 1 ) : if newAddress < 0x03 : print ( 'Error, I?C addresses below 3 (0x03) are reserved, use an address between 3 (0x03) and 119 (0x77)' ) return elif newAddress > 0x77 : print ( 'Error, I?C addresses above 119 (0x77) are reserved, use an address between 3 (0x03) and 119 (0x77)' ) return if oldAddress < 0x0 : found = ScanForZeroBorg ( busNumber ) if len ( found ) < 1 : print ( 'No ZeroBorg boards found, cannot set a new I?C address!' ) return else : oldAddress = found [ 0 ] print ( 'Changing I2C address from %02X to %02X (bus #%d)' % ( oldAddress , newAddress , busNumber ) ) bus = ZeroBorg ( ) bus . InitBusOnly ( busNumber , oldAddress ) try : i2cRecv = bus . RawRead ( COMMAND_GET_ID , I2C_NORM_LEN ) if len ( i2cRecv ) == I2C_NORM_LEN : if i2cRecv [ 1 ] == I2C_ID_ZEROBORG : foundChip = True print ( 'Found ZeroBorg at %02X' % oldAddress ) else : foundChip = False print ( 'Found a device at %02X, but it is not a ZeroBorg (ID %02X instead of %02X)' % ( oldAddress , i2cRecv [ 1 ] , I2C_ID_ZEROBORG ) ) else : foundChip = False print ( 'Missing ZeroBorg at %02X' % oldAddress ) except KeyboardInterrupt : raise except Exception as e : foundChip = False print ( str ( e ) ) print ( 'Missing ZeroBorg at %02X' % oldAddress ) if foundChip : bus . RawWrite ( COMMAND_SET_I2C_ADD , [ newAddress ] ) time . sleep ( 0.1 ) print ( 'Address changed to %02X, attempting to talk with the new address' % newAddress ) try : bus . InitBusOnly ( busNumber , newAddress ) i2cRecv = bus . RawRead ( COMMAND_GET_ID , I2C_NORM_LEN ) if len ( i2cRecv ) == I2C_NORM_LEN : if i2cRecv [ 1 ] == I2C_ID_ZEROBORG : foundChip = True print ( 'Found ZeroBorg at %02X' % newAddress ) else : foundChip = False print ( 'Found a device at %02X, but it is not a ZeroBorg (ID %02X instead of %02X)' % ( newAddress , i2cRecv [ 1 ] , I2C_ID_ZEROBORG ) ) else : foundChip = False print ( 'Missing ZeroBorg at %02X' % newAddress ) except KeyboardInterrupt : raise except Exception as e : foundChip = False print ( str ( e ) ) print ( 'Missing ZeroBorg at %02X' % newAddress ) if foundChip : print ( 'New I?C address of %02X set successfully' % newAddress ) else : print ( 'Failed to set new I?C address...' )
SetNewAddress(newAddress, [oldAddress], [busNumber])
Scans the I?C bus for the first ZeroBorg and sets it to a new I2C address
If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus
The busNumber if supplied is which I?C bus to scan, 0 for Rev 1 boards, 1 for Rev 2 boards, if not supplied
the default is 1.
SetNewAddress ( newAddress [ oldAddress ] [ busNumber ] ) Scans the I?C bus for the first ZeroBorg and sets it to a new I2C address If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus The busNumber if supplied is which I?C bus to scan 0 for Rev 1 boards 1 for Rev 2 boards if not supplied the default is 1 .
"""
SetNewAddress(newAddress, [oldAddress], [busNumber])

Scans the I?C bus for the first ZeroBorg and sets it to a new I2C address
If oldAddress is supplied it will change the address of the board at that address rather than scanning the bus
The busNumber if supplied is which I?C bus to scan, 0 for Rev 1 boards, 1 for Rev 2 boards, if not supplied
the default is 1.
Warning, this new I?C address will still be used after resetting the power on the device
    """ # noinspection PyBroadException # noinspection PyBroadException
newAddress
oldAddress
busNumber