I2cSubWelton board, also used for input

The SubWelton board is usually used for output, but can also be used for input. A class is provided to read the inputs. It is also possible to use this board as some outputs and some inputs.

# ioinput.py
# MIT License (MIT)
# Copyright (c) 2025 Microtron Ltd NZ - Stephen Eichler
# https://opensource.org/licenses/MIT

from machine import Pin, I2C
import mcp23017

class IOINPUT():
    def __init__(self, i2c, address=0x20):
        self._i2c = i2c
        #i2c = I2C(id=0, scl=Pin(1), sda=Pin(0))
        self.mcp = mcp23017.MCP23017(i2c, address)
        self.mcp.mode = 0xffff
        self.mcp.pullup = 0xffff
        
    def read(self):
        return self.mcp.gpio
    
    def readbindig(self):
        readval = self.read()
        return "0b" + '{:08b}'.format(readval >> 8) + "   0b" + '{:08b}'.format(readval & 0xff)
        #return "0b" + f"{readval >> 8:08b}" + "   0b" + f"{readval & 0xff:08b}"

    def readhexdig(self):
        return "0x" + ("%04x" % self.mcp.gpio)

=========================================================================
# ioinputMain.py
# MIT License (MIT)
# Copyright (c) 2025 Microtron Ltd NZ - Stephen Eichler
# https://opensource.org/licenses/MIT

from machine import Pin, I2C
import ioinput
import time

i2c = I2C(id=0, scl=Pin(1), sda=Pin(0))

def i2c_scan(i2c):
    try:
        devices = i2c.scan()
        if len(devices) == 0:
            print("No I2C devices found.")
        else:
            print("Found the following I2C devices:")
            for device in devices:
                print(f"Device address: 0x{device:02X}")
    except OSError as e:
        print(f"An error occurred during the I2C scan: {e}")

i2c_scan(i2c)

ioinput1 = ioinput.IOINPUT(i2c, 0x23)
ioinput2 = ioinput.IOINPUT(i2c, 0x24)

while True:
    print(ioinput1.read())
    print(ioinput1.readbindig())
    print(ioinput1.readhexdig())
    print(ioinput2.read())
    print(ioinput2.readbindig())
    print(ioinput2.readhexdig())
    time.sleep(1)