Пример #1
0
def request_slave_func(tout, addresses, delay_ms):
    import board
    import time
    from i2cslave import I2CSlave

    with I2CSlave(board.SCL, board.SDA, addresses, smbus=tout) as slave:
        while True:
            r = slave.request()
            if not r:
                time.sleep(delay_ms / 1000)
                continue

            try:
                rd = None
                with r:
                    rd = r.is_read
                    if (r.is_restart):
                        print('RESTART')

                    if r.address == 0x40:
                        if r.is_read:
                            r.write(bytes([1, 2, 3, 4, 5, 6, 7, 8]))
                        else:
                            r.read()

                print('CLOSED', 'READ' if rd else 'WRITE')
            except OSError as e:
                print('ERROR:', e)
Пример #2
0
    def __init__(self, clock_pin, data_pin, address, debug=False):
        self.clock_pin = clock_pin
        self.data_pin = data_pin
        self.address = address
        self.debug = debug

        self.last_activity = time.monotonic()

        self.last_packet_sent = 0
        self.last_packet_ack = None
        self.last_packet_rcvd = None

        self.audio_track = 0
        self.audio_global = False
        self.audio_action = 0
        self.points_activated = 0

        # data that comes from central
        self.time_since_global_boot = None
        self.time_since_global_activity = None
        self.system_mode = EarToHear.MODE_NOT_RECEIVED
        self.day_time = EarToHear.TIME_UNKNOWN
        self.light_level = EarToHear.LIGHT_NIGHT

        self.slave = I2CSlave(self.clock_pin, self.data_pin, (self.address, ))
Пример #3
0
def read_byte_slave_func(tout, addresses, data):
    import board
    from i2cslave import I2CSlave

    data = bytes(data)
    print('read_byte_slave_func:', tout, repr(data))

    with I2CSlave(board.SCL, board.SDA, addresses, smbus=tout) as slave:
        while True:
            try:
                r = slave.request()
                if not r:
                    continue
            except OSError as e:
                if e.args and e.args[
                        0] == 116:  #  Why is timeout 116 and not 110?
                    continue

            try:
                with r:
                    #print('address==0x%02x' % r.address)
                    #print('is_read', r.is_read)
                    #print('is_restart', r.is_restart)

                    if r.address == 0x40:
                        n = r.write(data)
                        #print('write(0x40)', n, repr(list(data)[:n]))
                print('CLOSED')
            except OSError as e:
                print('ERROR:', e)
Пример #4
0
def write_byte_slave_func(tout, addresses, num):
    import board
    from i2cslave import I2CSlave

    print('write_byte_slave_func: num=%d' % num)

    with I2CSlave(board.SCL, board.SDA, addresses, smbus=tout) as slave:
        while True:
            try:
                r = slave.request()
                if not r:
                    continue
            except OSError as e:
                if e.args and e.args[
                        0] == 116:  #  Why is timeout 116 and not 110?
                    continue

            try:
                with r:
                    print('address==0x%02x' % r.address)
                    print('is_read', r.is_read)
                    print('is_restart', r.is_restart)

                    if r.address == 0x40:

                        b = r.read(num)

                        print('read(0x40)', repr(list(b)))
            except OSError as e:
                print('ERROR:', e)
Пример #5
0
def at24slave_func(address):
    import board
    import at24slave
    from i2cslave import I2CSlave

    # Adapted from CPython
    class BytesIO:
        def __init__(self, initial_bytes):
            self._buffer = initial_bytes
            self._pos = 0

        def read(self, size):
            if len(self._buffer) <= self._pos:
                return b''
            newpos = min(len(self._buffer), self._pos + size)
            b = self._buffer[self._pos:newpos]
            self._pos = newpos
            return bytes(b)

        def write(self, b):
            n = len(b)
            pos = self._pos
            if n == 0 or pos + n > len(self._buffer):
                return 0
            self._buffer[pos:pos + n] = b
            self._pos += n

        def seek(self, pos, whence=0):
            if whence == 0:
                self._pos = pos
            elif whence == 2:
                if pos != 0:
                    raise ValueError("pos unsupported on whence==2")
                self._pos = max(0, len(self._buffer) + pos)
            else:
                raise ValueError("unsupported whence value")
            return self._pos

        def tell(self):
            return self._pos

    file = BytesIO(bytearray(128))
    at24 = at24slave.AT24Slave(file)
    #at24.debug= True

    with I2CSlave(board.SCL, board.SDA, (address, ), smbus=False) as slave:
        while True:
            try:
                r = slave.request()
                if not r:
                    continue
                with r:
                    if r.address == address:
                        at24.process(r)
            except OSError as e:
                print('ERROR:', e)
Пример #6
0
def slave_func(tout, addresses, protocol):
    import board
    from i2cslave import I2CSlave
    from smbusslave import SMBusSlave

    class Slave(SMBusSlave):
        def __init__(self, prot):
            super().__init__()
            self.print = False  # Slows down too much for the seq tests resulting in a timeout
            if prot == 0:
                self.protocol = SMBusSlave.SMBUS_BYTE
                self.print = True
            elif prot == 1:
                self.protocol = SMBusSlave.SMBUS_BYTE_SEQ
            elif prot == 2:
                self.protocol = SMBusSlave.SMBUS_WORD
                self.print = True
            elif prot == 3:
                self.protocol = SMBusSlave.SMBUS_WORD_SEQ
            self.max_reg = 7
            self.regs = [0] * (self.max_reg + 1)

        def readreg(self, reg):
            val = self.regs[reg]
            if self.print == True:
                print(' 0x%02x==0x%x' % (reg, val))
            return val

        def writereg(self, reg, val):
            if self.print == True:
                print(' 0x%02x=0x%x' % (reg, val))
            self.regs[reg] = val

    print('\nboard: slave_func:', ','.join(['0x%02x' % (addr,) for addr in addresses]), 'prot:', protocol)
    bs = Slave(protocol)

    with I2CSlave(board.SCL, board.SDA, addresses, smbus=tout) as slave:
        while True:
            try:
                r = slave.request()
                if not r:
                    continue

                with r:
                    #print('address==0x%02x' % r.address)
                    #print('is_read', r.is_read)
                    #print('is_restart', r.is_restart)

                    if r.address == addresses[0]:
                        bs.process(r)

            except OSError as e:
                print('ERROR:', e)
Пример #7
0
def ds1307slave_func():
    import board
    import time
    import ds1307slave
    import rtc
    from i2cslave import I2CSlave

    # Adapted from CPython
    class BytesIO:
        def __init__(self, initial_bytes):
            self._buffer = initial_bytes
            self._pos = 0

        def read(self, size):
            if len(self._buffer) <= self._pos:
                return b''
            newpos = min(len(self._buffer), self._pos + size)
            b = self._buffer[self._pos:newpos]
            self._pos = newpos
            return bytes(b)

        def write(self, b):
            n = len(b)
            pos = self._pos
            if n == 0 or pos + n > len(self._buffer):
                return 0
            self._buffer[pos:pos + n] = b
            self._pos += n

        def seek(self, pos):
            self._pos = pos

    ram = BytesIO(bytearray(56))
    ds1307 = ds1307slave.DS1307Slave(rtc.RTC(), ram=ram)

    with I2CSlave(board.SCL, board.SDA, (0x68, ), smbus=False) as slave:
        while True:
            try:
                r = slave.request()
                if not r:
                    continue
                with r:
                    if r.address == 0x68:
                        ds1307.process(r)
            except OSError as e:
                print('ERROR:', e)
Пример #8
0
def mcp23008slave_func(tout, address, pins, intpin):
    import board
    import digitalio
    import mcp23008slave
    from i2cslave import I2CSlave


    tout = False  # Needed while printing stuff for debugging


    pins = [getattr(board, p) if p else None for p in pins]
    intpin = getattr(board, intpin) if intpin else None
    print('mcp23008slave_func', tout, '0x%02x' % (address,), repr(pins), repr(intpin))
    mcp23008 = mcp23008slave.MCP23008Slave(pins, intpin)

    mcp23008.debug2 = True
    for pin in mcp23008.pins:
        if pin:
            pin.debug = True

    once = True

    def dump_regs():
        for i in range(mcp23008.max_reg + 1):
            print('%02x: 0x%02x  %s' % (i, mcp23008.regs[i], bin(mcp23008.regs[i])))

    def dump_regs_once():
        if not once:
            return
        once = False
        dump_regs()


    with I2CSlave(board.SCL, board.SDA, (address,), smbus=tout) as slave:
        while True:
            mcp23008.check_events()
            try:
                r = slave.request()
                if not r:
                    continue
                with r:
                    if r.address == address:
                        mcp23008.process(r)
            except OSError as e:
                print('ERROR:', e)
Пример #9
0
def write_read_slave_func(tout, addresses, num):
    import board
    from i2cslave import I2CSlave

    data = b''

    print('\nwrite_read_slave_func: num=%d' % num, repr(data))

    with I2CSlave(board.SCL, board.SDA, addresses, smbus=tout) as slave:
        while True:
            try:
                r = slave.request()
                if not r:
                    continue
            except OSError as e:
                if e.args and e.args[
                        0] == 116:  #  Why is timeout 116 and not 110?
                    continue

            try:
                rd = None
                with r:
                    #print('address==0x%02x' % r.address)
                    #print('is_read', r.is_read)
                    #print('is_restart', r.is_restart)
                    rd = r.is_read
                    if (r.is_restart):
                        print('RESTART')

                    if r.address == 0x40:
                        if r.is_read:
                            if len(data):
                                data = data * (round(512 / len(data)) + 1)
                                n = r.write(data)
                                #print('write(0x40)', n, repr(list(data)[:n]))
                            else:
                                print('ERROR: len(data) == 0')
                        else:
                            data = r.read(num)
                            #print('read(0x40)', repr(list(b)))

                print('CLOSED', 'READ' if rd else 'WRITE')
            except OSError as e:
                print('ERROR:', e)
Пример #10
0
def slave_func(tout, addresses):
    import board
    from i2cslave import I2CSlave

    with I2CSlave(board.SCL, board.SDA, addresses, smbus=tout) as slave:
        while True:
            try:
                r = slave.request()
                if not r:
                    continue
            except OSError as e:
                if e.args and e.args[
                        0] == 116:  #  Why is timeout 116 and not 110?
                    continue

            with r:
                print('address==0x%02x' % r.address)
                print('is_read', r.is_read)
                print('is_restart', r.is_restart)
Пример #11
0
def ds1307slave_func(addresses):
    import board
    import time
    import ds1307slave
    import rtc
    from i2cslave import I2CSlave

    ds1307 = ds1307slave.DS1307Slave(rtc.RTC())

    with I2CSlave(board.SCL, board.SDA, addresses, smbus=False) as slave:
        while True:
            try:
                r = slave.request(timeout=2)

                with r:
                    if r.address == 0x68:
                        ds1307.process(r)

            except OSError as e:
                print('ERROR:', e)
Пример #12
0
import board
from i2cslave import I2CSlave

regs = [0] * 16
index = 0

i2c.writeto(0x40, bytes([0x05]), stop=False)



with I2CSlave(board.SCL, board.SDA, (0x40, 0x41)) as slave:
    print("Waiting for i2c command")
    while True:
        r = slave.request()
        if not r:
            # Maybe do some housekeeping
            continue
        with r:  # Closes the transfer if necessary by sending a NACK or feeding the master dummy bytes
            print("got request! {}".format(repr(r)))
            if r.address == 0x40:
                if not r.is_read:  # Master write which is Slave read
                    b = r.read(1)
                    if not b or b[0] > 15:
                        break
                    index = b[0]
                    b = r.read(1)
                    if b:
                        regs[index] = b[0]
                elif r.is_restart:  # Combined transfer: This is the Master read message
                    n = r.write(bytes([regs[index]]))
                #else:
Пример #13
0
seesawRegs = [0] * 16  # base registers

# Neopixel Register
register_neopixel = [0] * 6

# template registers
regs = [0] * 16
index = 0

# Data to send back on next i2cget command.
# TODO: implement a proper register map
data = [0] * 16

led = pulseio.PWMOut(board.SERVO1, frequency=5000, duty_cycle=0)

with I2CSlave(board.SCL, board.SDA, [0x49]) as slave:
    while True:
        r = slave.request()
        if not r:
            # Maybe do some housekeeping
            # TODO:  Working functions in here for keeping things going like NeoPixels, etc

            continue
        with r:  # Closes the transfer if necessary by sending a NACK or feeding the master dummy bytes
            if DEBUG: print("BEGIN request")

            if r.address == 0x49:
                # This is used by i2cset commands (or similar) and sets which register is next to be read from.
                if not r.is_read:  # Master write which is Slave read
                    if DEBUG: print("slave read")