using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace irisIoControll.Service.Impl
{
    public class GPIO
    {
        private const int KBC_KEY_CMD = 0x64;
        private const int KBC_KEY_DATA = 0x60;

        [DllImport("IoControl.dll")]
        public static extern bool IoInit();
        [DllImport("IoControl.dll")]
        public static extern bool setPortVal(int port, int val);
        [DllImport("IoControl.dll")]
        public static extern int getPortVal(int port);
        [DllImport("IoControl.dll")]
        public static extern void IoUnInit();


        private static bool IsInitialize { get; set; }

        private GPIO()
        {
            IsInitialize = true;
        }
        public static bool Initialize()
        {
            try
            {
                bool Result = IoInit();
                if (Result)
                {
                    //KBCWait4IBE();
                    IsInitialize = true;
                }
                else
                {
                    IsInitialize = false;
                    MessageBox.Show("GPIO INIT Failed!");
                }
            }
            catch (Exception e)
            {
                IsInitialize = false;
                MessageBox.Show(e.Message);
            }

            return IsInitialize;
        }
        public static int getSensorValue()
        {
            int portValue = -1;
            try
            {
                portValue = getPortVal(7);
            }
            catch (Exception e)
            {
                MessageBox.Show("getSensorValue " + e.Message);
                return -1;
            }


            return portValue;
        }

        public static void Shutdown()
        {
            try
            {
                if (IsInitialize)
                    IoUnInit();
                IsInitialize = false;
            }
            catch (Exception e) { }
        }
        

        /// <summary>
        /// 4号 bit7
        /// </summary>
        public static void redOn()
        {
            setPortVal(4, 1);
        }
        public static void redOff()
        {
            setPortVal(4, 0);
        }

        /// <summary>
        /// 6号 bit6
        /// </summary>
        public static void yellowOn()
        {
            setPortVal(6, 1);
        }
        public static void yellowOff()
        {
            setPortVal(6, 0);
        }

        /// <summary>
        /// 8号 bit4
        /// </summary>
        public static void greenOn()
        {
            setPortVal(8, 1);
        }
        public static void greenOff()
        {
            setPortVal(8, 0);
        }
        
        ///等待键盘缓冲区为空
        private static void KBCWait4IBE()
        {
            //int dwVal = 0;
            //do
            //{
            //    bool flag = GetPortVal((IntPtr)0x64, out dwVal, 1);
            //}
            //while ((dwVal & 0x2) > 0);
        }

    }
}
