using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace _7K87K_PWM
{
    public partial class Form1 : Form
    {
        private IntPtr hPort;
        private static byte[] pwmStartBit = new byte[8];
        private static bool activated = false;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            string connStr = "COM" + txtPort.Text + "," + txtBaudrate.Text;
            //Step1:Open com port
            hPort = PACNET.UART.Open(connStr);
            if (hPort == (IntPtr)(-1))
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            PACNET.UART.Close(hPort);
        }

        private void ConfigPWMSettings(object sender, EventArgs e)
        {
            TextBox[] hzArr = new TextBox[] { txtHz0, txtHz1, txtHz2, txtHz3, txtHz4, txtHz5, txtHz6, txtHz7 };
            TextBox[] dutyArr = new TextBox[] { txtDuty0, txtDuty1, txtDuty2, txtDuty3, txtDuty4, txtDuty5, txtDuty6, txtDuty7 };
            ComboBox[] modeArr = new ComboBox[] { cboMode0, cboMode1, cboMode2, cboMode3, cboMode4, cboMode5, cboMode6, cboMode7 };
            TextBox[] cntArr = new TextBox[] { txtCnt0, txtCnt1, txtCnt2, txtCnt3, txtCnt4, txtCnt5, txtCnt6, txtCnt7 };

            Int16 chIndex = 0;
            chIndex = Int16.Parse(((Button)sender).Tag.ToString());
            bool ret = false;

            ret = PACNET.PWM.SetPWMFrequency(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, Convert.ToUInt32(hzArr[chIndex].Text));

            if (!ret)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
            else
            {
                ret = PACNET.PWM.SetPWMDuty(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, Convert.ToSingle(dutyArr[chIndex].Text));
                ret = PACNET.PWM.SetPWMPulseCount(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, Convert.ToInt32(cntArr[chIndex].Text));

                ret = PACNET.PWM.SetPWMMode(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, (Int16)modeArr[chIndex].SelectedIndex);

                GetPWMConfig(chIndex);
            }
        }

        private void GetPWMConfig(Int16 chIndex)
        {
            TextBox[] hzArr = new TextBox[] { txtHz0, txtHz1, txtHz2, txtHz3, txtHz4, txtHz5, txtHz6, txtHz7 };
            TextBox[] dutyArr = new TextBox[] { txtDuty0, txtDuty1, txtDuty2, txtDuty3, txtDuty4, txtDuty5, txtDuty6, txtDuty7 };
            ComboBox[] modeArr = new ComboBox[] { cboMode0, cboMode1, cboMode2, cboMode3, cboMode4, cboMode5, cboMode6, cboMode7 };
            TextBox[] cntArr = new TextBox[] { txtCnt0, txtCnt1, txtCnt2, txtCnt3, txtCnt4, txtCnt5, txtCnt6, txtCnt7 };
            UInt32 freq = 0;

            bool ret = false;

            ret = PACNET.PWM.GetPWMFrequency(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, ref freq);
            if (!ret)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
            else
            {
                hzArr[chIndex].Text = freq.ToString();
                float duty = 0;
                ret = PACNET.PWM.GetPWMDuty(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, ref duty);
                dutyArr[chIndex].Text = String.Format("{0:00.0}", duty);
                Int32 cnt = 0;
                ret = PACNET.PWM.GetPWMPulseCount(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, ref cnt);
                cntArr[chIndex].Text = cnt.ToString();

                int mode = 0;
                ret = PACNET.PWM.GetPWMMode(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), chIndex, ref mode);
                modeArr[chIndex].SelectedIndex = mode;
            }
        }

        private void btnSavePWM_Click(object sender, EventArgs e)
        {
            bool ret = PACNET.PWM.SavePWMConfig(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)));
            if (!ret)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            ReadPWM_DIStatus();
        }

        private void ReadPWM_DIStatus()
        {
            Label[] lbDI = { lbDI0, lbDI1, lbDI2, lbDI3, lbDI4, lbDI5, lbDI6, lbDI7 };
            Label[] lbPWM = { pwm0, pwm1, pwm2, pwm3, pwm4, pwm5, pwm6, pwm7 };
            CheckBox[] enPWM = { ckEnPWM0, ckEnPWM1, ckEnPWM2, ckEnPWM3, ckEnPWM4, ckEnPWM5, ckEnPWM6, ckEnPWM7 };

            byte[] pwmBit = new byte[8];
            byte[] diBit = new byte[8]; ;
            bool ret = false;

            ret = PACNET.PWM.GetPWMDIOStatus(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), pwmBit, diBit);
            if (!ret)
            {
                uint ec = PACNET.ErrHandling.GetLastError();
                MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
            }
            else
            {
                for (int i = 0; i < 8; i++)
                {
                    if (pwmBit[i] == 1)
                    {
                        lbPWM[i].Text = "ON";
                        lbPWM[i].BackColor = Color.Red;
                        activated = false;
                        enPWM[i].Checked = true;
                        activated = true;
                    }
                    else
                    {
                        lbPWM[i].Text = "OFF";
                        lbPWM[i].BackColor = Color.Gray;
                        activated = false;
                        enPWM[i].Checked = false;
                        activated = true;
                    }

                    if (diBit[i] == 1)
                    {
                        lbDI[i].Text = "ON";
                        lbDI[i].BackColor = Color.Green;
                    }
                    else
                    {
                        lbDI[i].Text = "OFF";
                        lbDI[i].BackColor = Color.Gray;
                    }
                }
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            activated = true;
        }
        private void EnablePWM(object sender, EventArgs e)
        {
            if (activated)
            {
                Int16 chIndex = Int16.Parse(((CheckBox)sender).Tag.ToString());
                bool ret = false;
                CheckBox enBox = (CheckBox)sender;
                Int16 status = 0;

                if (enBox.Checked)
                {
                    pwmStartBit[chIndex] = 1;
                }
                else
                {
                    pwmStartBit[chIndex] = 0;
                }

                for (int i = 0; i < 8; i++)
                    status += (Int16)(pwmStartBit[i] << i);
                ret = PACNET.PWM.SetPWMStart(hPort, PACNET.PAC_IO.PAC_REMOTE_IO(Convert.ToInt16(txtAddress.Text)), status);
                if (!ret)
                {
                    uint ec = PACNET.ErrHandling.GetLastError();
                    MessageBox.Show(((PACNET.ErrCode)ec).ToString() + "\nError Code: 0x" + ec.ToString("X"));
                }
            }
        }
      
    }
}