"""
Copyright © 2024 Walkline Wang (https://walkline.wang)
"""
from machine import SPI, Pin


print()
using_spi = True

if using_spi:
    spi = SPI(1, baudrate=4_000_000)
    spi.init(polarity=0, phase=0, bits=8, firstbit=SPI.MSB)
else:
    pin_miso = Pin(2, Pin.IN)
    pin_sck  = Pin(6, Pin.OUT, value=0)

pin_ce = Pin(5, Pin.OUT, value=0)
pin_pl = Pin(3, Pin.OUT, value=1)

def load_data_spi():
    # load data
    pin_pl.off()
    pin_pl.on()

    # shift all data
    data = bytearray([0])
    spi.readinto(data)

    return data

def load_data_for():
    # load data
    pin_pl.off()
    pin_pl.on()

    data = 0

    for _ in range(8):
        data <<= 1
        data |= pin_miso.value()

        # shift data
        pin_sck.off()
        pin_sck.on()

    return bytearray([data])


if __name__ == '__main__':
    last_data = b'\x00'

    while True:
        data = load_data_spi() if using_spi else load_data_for()

        if last_data != data:
            last_data = data

            print(' ',
                  ' '.join(reversed(f'{data[0]:08b}')),
                  end='\r')
Loading
esp32-c3-devkitm-1
74HC165