# md13s_demo.py
#
# Demonstrate operation of one high-power DC motor using:
#
#  Cytron Maker Pi Pico with Raspberry Pi Pico installed
#  Cytron MD13S motor driver on Grove 6 connector
#
# The MD13S connected via Grove 6 uses the following I/O:
#   GPIO26       -> PWM
#   GPIO26       -> DIR
#

################################################################################
# print a banner as reminder of what code is loaded
print("Starting md13s_demo script.")

# load standard Python modules
import time

# load CircuitPython hardware modules
import board
import digitalio

# load the local md13s module
import md13s

#--------------------------------------------------------------------------------
# Create an object to represent the motor driver.
print("Creating driver objects.")
driver = md13s.MD13S(PWM=board.GP26, DIR=board.GP27)

#--------------------------------------------------------------------------------
# Begin the main processing loop.

print("Starting main script.")

# initial pause
time.sleep(2.0)

while True:
    print("Starting motor test.")
    driver.write(1.0)
    time.sleep(2.0)

    driver.write(0.0)
    time.sleep(2.0)

    driver.write(-1.0)
    time.sleep(2.0)

    driver.write(0.0)
    time.sleep(2.0)

    driver.write(0.5)
    time.sleep(2.0)

    driver.write(-0.5)
    time.sleep(2.0)

    print("Ramp test.")

    for i in range(10):
        driver.write(i*0.1)
        time.sleep(0.5)

    driver.write(0.0)
    time.sleep(2.0)

    for i in range(10):
        driver.write(-i*0.1)
        time.sleep(0.5)

    driver.write(0.0)
    time.sleep(2.0)
