# cPy - RPi i2c smbus test
# Copyright by Anthony Vogelaar, Perth Scotland
# rev 0.10  2024-12-12  initial release

import	time
# from	smbus		import	SMBus			# precompile wheel
from	smbus2		import	SMBus			# full python
# from	smbus3		import	SMBus           # full python with context manager a.o.

# ============================= tests =================================

# # apt install i2c-tools
# $ i2cdetect -y 1

# see: https://pypi.org/project/smbus2/
#      https://www.raspberrypi-spy.co.uk/2018/02/change-raspberry-pi-i2c-bus-speed/
# 1) # apt install python3-smbus   precompiled wheel
#    (venv) pip install smbus
# 2) (venv) pip install smbus3     for context manager and 10 bit addressing
# flashing 8 outputs on PCF8547 at $20, SCL = 100 kHz
def test1():
    bus = SMBus(1)
    addr = 0x20
    while True:
        bus.write_byte(addr, 0xF0)
        time.sleep(0.5)
        bus.write_byte(addr, 0x0F)
        time.sleep(0.5)
    bus.close()                             # as reminder, or use smbus3 with context manager

# ============================= main ===================================

try:
    test1()
except KeyboardInterrupt:
    print('terminated by Ctrl-C')
    
# =============================== end ==================================
