#!/usr/bin/python
# From https://raspberry.tips/en/raspberrypi-tutorials/raspberry-pi-gpio-python
import lgpio
import time

# Open GPIO chip (0 = /dev/gpiochip0, on Pi 5 gpiochip4 also possible)
h = lgpio.gpiochip_open(0)
# Hardware PWM on GPIO18 (pin 12) must be PWM-capable
pwm_pin = 18
pwm_freq = 1000
try:
    while True:
        for pwm_level in range(0,100):
            lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level)
            pwm_level += 10
            time.sleep(0.05)
        for pwm_level in range(100,0,-1):
            lgpio.tx_pwm(h, pwm_pin, pwm_freq, pwm_level)
            pwm_level += 10
            time.sleep(0.05)

            
finally:
    lgpio.tx_pwm(h, pwm_pin, 0, 0)   # Stop PWM
    lgpio.gpiochip_close(h)          # Always close!
