#!/usr/bin/python
# This test assumes you are trying to control a motor through a DRV8833 motor
# controller chip.  This general approach should work for other motors as well.
import lgpio
import time

# pin assignments
gpio_pwm = 18  #MOTA1
gpio_reverse = 16 #MOTA2
gpio_notsleep = 24 #MSLEEP
# parameters
pwm_freq = 1000


h = lgpio.gpiochip_open(0)
try:
    lgpio.gpio_claim_output(h, gpio_reverse)
    lgpio.gpio_claim_output(h, gpio_notsleep)
    lgpio.gpio_write(h, gpio_reverse, 0)   # low on reverse pin to turn off h-bridge MOTA2
    lgpio.gpio_write(h, gpio_notsleep, 1)   # high on /sleep to enable both h-bridges
    
    while True:
        for pwm_level in range(0,100):
            lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level)
            pwm_level += 10
            time.sleep(0.05)
        for pwm_level in range(100,0,-1):
            lgpio.tx_pwm(h, gpio_pwm, pwm_freq, pwm_level)
            pwm_level += 10
            time.sleep(0.05)

finally:
    lgpio.gpio_write(h, gpio_notsleep, 1)   # high on /sleep to disable    
    lgpio.gpiochip_close(h)          # Always close!
