示例#1
0
def test_open_close():
    ptest()

    # Open non-existent device
    with AssertRaises("non-existent device", periphery.I2CError):
        periphery.I2C("/foo/bar")

    # Open legitimate device
    i2c = periphery.I2C(i2c_devpath)
    passert("fd >= 0", i2c.fd >= 0)

    # Close I2C
    i2c.close()
def test_open_close():
    print("Starting open/close test...")

    # Open non-existent device
    with AssertRaises(periphery.I2CError):
        periphery.I2C("/foo/bar")

    # Open legitimate device
    i2c = periphery.I2C(i2c_devpath)
    assert i2c.fd > 0

    # Close I2C
    i2c.close()

    print("Open/close test passed.")
def test_interactive():
    print("Starting interactive test...")

    # Open device 2
    i2c = periphery.I2C(i2c_devpath)

    print("")
    print("Starting interactive test. Get out your logic analyzer, buddy!")
    raw_input("Press enter to continue...")

    # Check tostring
    print("I2C description: {}".format(str(i2c)))
    assert raw_input("I2C description looks ok? y/n ") == "y"

    # There isn't much we can do without assuming a device on the other end,
    # because I2C needs an acknowledgement bit on each transferred byte.
    #
    # But we can send a transaction and expect it to time out.

    # S [ 0x7a W ] [0xaa] [0xbb] [0xcc] [0xdd] NA
    messages = [periphery.I2C.Message([0xaa, 0xbb, 0xcc, 0xdd])]

    raw_input("Press enter to start transfer...")

    # Transfer to non-existent device
    with AssertRaises(periphery.I2CError):
        i2c.transfer(0x7a, messages)

    i2c.close()

    success = raw_input("I2C transfer occurred? y/n ")
    assert success == "y"

    print("Interactive test passed.")
def test_arguments():
    print("Starting arguments test...")

    # Open with invalid type
    with AssertRaises(TypeError):
        periphery.I2C(123)

    print("Arguments test passed.")
示例#5
0
def test_debug_mode_on_i2c():
    periphery = Mock()  # noqa: F811
    port = periphery.I2C("dev/i2c-foo")
    port.try_lock.return_value = True

    nCard = notecard.OpenI2C(port, 0x17, 255, debug=True)

    assert nCard._debug
示例#6
0
def get_i2c_and_port():
    periphery = Mock()  # noqa: F811
    port = periphery.I2C("dev/i2c-foo")
    port.try_lock.return_value = True

    nCard = notecard.OpenI2C(port, 0x17, 255)

    return (nCard, port)
示例#7
0
def test_loopback():
    print("Starting loopback test...")

    # "Loopback" plan
    # 1. Read EEPROM via sysfs
    # 2. Read EEPROM via I2C
    # 3. Compare data

    # Read EEPROM via sysfs
    sysfs_path = "/sys/bus/i2c/devices/%s-00%02x/eeprom" % (i2c_devpath1[-1],
                                                            i2c_eeprom_addr)
    with open(sysfs_path, "rb") as f:
        eeprom_data = f.read(256)

    # Read EEPROM via I2C
    data = bytearray()
    i2c = periphery.I2C(i2c_devpath1)
    for addr in range(0, 256):
        msgs = [
            periphery.I2C.Message([0x00, addr]),
            periphery.I2C.Message([0x00], read=True)
        ]
        i2c.transfer(i2c_eeprom_addr, msgs)
        data.append(msgs[1].data[0])

    # Compare data
    assert eeprom_data == data

    # Try bytes read
    msgs = [
        periphery.I2C.Message(b"\x00\x00"),
        periphery.I2C.Message(b"\x00", read=True)
    ]
    i2c.transfer(i2c_eeprom_addr, msgs)
    assert isinstance(msgs[1].data, bytes)
    assert bytearray(msgs[1].data)[0] == data[0]

    # Try bytearray read
    msgs = [
        periphery.I2C.Message(bytearray([0x00, 0x00])),
        periphery.I2C.Message(bytearray([0x00]), read=True)
    ]
    i2c.transfer(i2c_eeprom_addr, msgs)
    assert isinstance(msgs[1].data, bytearray)
    assert msgs[1].data[0] == data[0]

    i2c.close()

    print("Loopback test passed.")
示例#8
0
def test():
    global i2c
    i2c = periphery.I2C(ic2dev)
    i2cwrite(0x6b, 0x00)
    print('PM1 is [0x%02x].' % i2cread(0x6b))
    print('I2C address is [0x%02x].' % i2cread(0x75))
    setscale(3, 3)
    getscale()
    print('Sample rate: \t' + str(8000 // (1 + samplerate(50))) + 'Hz')
    #sys.stdout.write('x_a_h\tx_a_l\ty_a_h\ty_a_l\tz_a_h\tz_a_l\tt_h\tt_l\tx_g_h\tx_g_l\ty_g_h\ty_g_l\tz_g_h\tz_g_l\n')
    sys.stdout.write('acc_x\tacc_y\tacc_z\ttemp\tgyr_x\tgyr_y\tgyr_z\n')
    while (True):
        a = []
        for i in range(0x3b, 0x49):
            #sys.stdout.write(str(i2cread(i))+'\t')
            #time.sleep(0.001)
            a.append(i2cread(i))
        #print('\n')
        #print(a)
        accx = getvalue(uint8toint16(a[0], a[1]), scaleacc)
        accy = getvalue(uint8toint16(a[2], a[3]), scaleacc)
        accz = getvalue(uint8toint16(a[4], a[5]), scaleacc)
        temp = (uint8toint16(a[6], a[7]) / 340) + 36.53
        gyrx = getvalue(uint8toint16(a[8], a[9]), scalegyr)
        gyry = getvalue(uint8toint16(a[10], a[11]), scalegyr)
        gyrz = getvalue(uint8toint16(a[12], a[13]), scalegyr)
        sys.stdout.write('%04.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f\t%.2f' %
                         (accx, accy, accz, temp, gyrz, gyry, gyrz))
        sys.stdout.flush()
        time.sleep(0.5)
        #print('\n')
        for i in range(0x3b, 0x49):
            sys.stdout.write('\r')
            #sys.stdout.write('\b'*56)
            sys.stdout.write(' ' * 128)
            sys.stdout.write('\r')
            pass
        sys.stdout.flush()
    i2c.close()
    return (0)
示例#9
0
 def __init__(self, bus):
     self.bus = periphery.I2C('/dev/i2c-%d' % bus)
示例#10
0
def test_arguments():
    ptest()

    # Open with invalid type
    with AssertRaises("open invalid type", TypeError):
        periphery.I2C(123)
示例#11
0
    def __init__(self, *, source_address, device="/dev/i2c-6"):
        self._i2c = periphery.I2C(device)
        self._source_address = source_address

        self._sequence_number = 0