#!/usr/bin/env python3
"""Example script to transmit a sequence of OSC messages over time.  Runs from
the command line, no GUI.  Uses the python-osc package for output.
"""
################################################################
# Written in 2018 by Garth Zeglin <garthz@cmu.edu>

# To the extent possible under law, the author has dedicated all copyright
# and related and neighboring rights to this software to the public domain
# worldwide. This software is distributed without any warranty.

# You should have received a copy of the CC0 Public Domain Dedication along with this software.
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.

################################################################
# Standard Python libraries.
import argparse, time

# This uses python-osc to encode and send UDP packets containing OSC messages.
#   installation:      pip3 install python-osc
#   source code:       https://github.com/attwad/python-osc
#   pypi description:  https://pypi.org/project/python-osc/
import pythonosc.udp_client

################################################################
if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="""Demonstration of sending a timed sequence of OSC messages.""")
    parser.add_argument( '-v', '--verbose', action='store_true', help='Enable more detailed output.' )    
    parser.add_argument("--addr", default="127.0.0.1", help = "The network address of the OSC receiver (default: %(default)s).")
    parser.add_argument("--port", type=int, default = 16375, help = "The UDP port number of the OSC receiver (default: %(default)d).")
    args = parser.parse_args()

    client = pythonosc.udp_client.SimpleUDPClient(args.addr, args.port)

    if args.verbose:
        print(f"Created UDP OSC client {client}.")

    start = time.time()
    msgaddr = "/sim/targets"

    client.send_message("/sim/start", ())

    if args.verbose:
        print(f"Starting message sequence.")

    client.send_message(msgaddr, (0, 0, 0, 0))
    time.sleep(1.0)
    client.send_message(msgaddr, (180, 0, 0, 0))
    time.sleep(1.0)
    client.send_message(msgaddr, (0, 180, 0, 0))
    time.sleep(1.0)
    client.send_message(msgaddr, (0, 0, 180, 0))
    time.sleep(1.0)
    client.send_message(msgaddr, (0, 0, 0, 180))
    time.sleep(1.0)
    client.send_message(msgaddr, (0, 0, 0, 0))
    time.sleep(1.0)
    client.send_message("/sim/end", ())
    
    if args.verbose:
        print(f"Sent end message, done.")
