// ALL RIGHTS RESERVED:
// ====================
// The contents of this file, and associated files in this directory, are 
// Copyright (C) SPT , all rights reserved, 2007.
// All software Source Code, Images, Database-Design and code, Graphics Design 
// and source files, and related content (collectively referred to as SOURCE) are 
// Copyright (c) SPT, 2007, All Rights Reserved.
// http://SerialPortTool.com

// PTZ Controller is a Registered Trademark of SerialPortTool.com.
using System;
using System.Text;
using System.Collections.Generic;
using SPT.PTZ.Core;
using System.Threading;

namespace HelloPTZConsole
{
    /// <summary>
    /// This is a quick start addressing how to use Spt.PTZ.Core module. 
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            // Check port existence
            if (Controller.PortNames.Length == 0 )
            {
                Console.WriteLine("No COM port in your computer. ");
                Console.ReadKey();

                return;
            }

            // Dump all serial ports
            string[] portNames = Controller.PortNames;

            foreach (string i in portNames)
            {
                Console.WriteLine("{0}", i);
            }

            // Initialize controller instance
            Controller ptz = new Controller();

            // Open serial port
            try
            {
                ptz.OpenPort(Controller.PortNames[0], 2400);
            }
            catch (System.Exception e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                Console.ReadKey();

                return;
            }

            // set speed range to 0~100
            ptz.SetSpeedRange(0, 100);

            // Set up protocol and address
            ptz.ChangeProtocol(Controller.PelcoD);
            ptz.Address = 1;

            byte[] data = new byte[3];
            byte coordinateX = (byte)Math.Abs(5);
            byte coordinateY = (byte)Math.Abs(5);

            data[0] = (byte)Direction.NorthWest;
            data[1] = (byte)(coordinateY);
            data[2] = (byte)(coordinateX);

            ptz.SendCommand(PtzFunction.FreeMove, data);
            Thread.Sleep(2000);
            ptz.SendCommand(PtzFunction.ZoomIn);

            Console.Write("Press any key to quit... ");
            Console.ReadKey();

            // continuous command should be followed by a stop action command
            ptz.SendCommand(PtzFunction.StopAction);

            // Close serial port
            ptz.ClosePort();
        }
    }
}
