using System.Text;
using System.Threading;
using HWModules;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SWModules;

namespace CPFUnitTests.PowerSwitchBoard
{
    [TestClass]
    public class UnitTest_PowerSwitchBoard
    {
        private bool _runonce = false;
        private void Init()
        {
            if (!_runonce)
            {
                _runonce = true;
            }

        }


        [TestMethod]
        public void TestSwitchPowerOnAllPorts()
        {
            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            Init();
            

            mb.Enable12V();
            mb.Enable5V();

            Debug.Print("TESTING CH 1 SLOT 1 PORT A\n");
            mb.EnableChannelPower(1);
            mb.ToggleDebugLED();
            Thread.Sleep(1000);
            mb.ToggleDebugLED();
            mb.DisableChannelPower(1);

            Debug.Print("TESTING CH2 SLOT 1 PORT B\n");
            mb.EnableChannelPower(2);
            mb.ToggleDebugLED();
            Thread.Sleep(1000);
            mb.ToggleDebugLED();
            mb.DisableChannelPower(2);

            Debug.Print("TESTING CH3 SLOT 1 PORT C\n");
            mb.EnableChannelPower(3);
            mb.ToggleDebugLED();
            Thread.Sleep(1000);
            mb.ToggleDebugLED();
            mb.DisableChannelPower(3);

            mb.ToggleDebugLED();
            Thread.Sleep(1000);
            mb.ToggleDebugLED();

            mb.Disable12V();
            mb.Disable5V();
        }

        [TestMethod]
        public void TestEnergyMonitorSlot1A()
        {
            Init();
            int i = 10;

            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.Enable12V();
            mb.Enable5V();
            mb.EnableChannelPower(1);
            
            EnergyMonitorLTC2946 em = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xCE >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625,1);

            //_psb3.Enable(PowerSwitchBoard.PowerLine.A);
            EnergyMonitorLTC2946 em2 = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xCE >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625,3);

            while (--i > 0)
            {
                EnergyMonitorLTC2946.EM_VandI data = em.getVandI();
                Debug.Print("SLOT1A DATA V/I:" + data.volts.ToString() + " / " + data.amps.ToString());
                Thread.Sleep(100);
            }

            mb.DisableChannelPower(1);
            mb.Disable12V();
            mb.Disable5V();
        }

        [TestMethod]
        public void TestEnergyMonitorSlot1B()
        {
            Init();
            EnergyMonitorLTC2946 em = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xD2 >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625,1);
            int i = 10;

            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.Enable12V();
            mb.Enable5V();
            mb.EnableChannelPower(2);
            
            em.ReadCtlARegister();
            
            while (--i > 0)
            {
                EnergyMonitorLTC2946.EM_VandI data = em.getVandI();
                Debug.Print("SLOT1B DATA V/I:" + data.volts.ToString() + " / " + data.amps.ToString());
                Thread.Sleep(100);
            }

            mb.DisableChannelPower(2);
            mb.Disable12V();
            mb.Disable5V();
        }

        [TestMethod]
        public void TestEnergyMonitorSlot1C()
        {
            Init();
            EnergyMonitorLTC2946 em = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xD8 >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625, 1);
            int i = 10;

            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.Enable12V();
            mb.Enable5V();
            mb.EnableI2CPort(3);
            
            em.ReadCtlARegister();

            while (--i > 0)
            {
                EnergyMonitorLTC2946.EM_VandI data = em.getVandI();
                Debug.Print("SLOT1C DATA V/I:" + data.volts.ToString() + " / " + data.amps.ToString());
                Thread.Sleep(100);
            }

            mb.DisableChannelPower(3);
            mb.Disable12V();
            mb.Disable5V();
        }

        [TestMethod]
        public void TestEnergyMonitorSlot1All()
        {
            Init();
            EnergyMonitorLTC2946 em1 = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xCE >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625,1);
            EnergyMonitorLTC2946 em2 = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xD2 >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625,1);
            EnergyMonitorLTC2946 em3 = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xD8 >> 1, 400), EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625, 1);
            int i = 10;
            I2CDevice.Configuration cfg = new I2CDevice.Configuration(0xDE >> 1, 400);
            EnergyMonitorLTC2946 em4 = new EnergyMonitorLTC2946(cfg, EnergyMonitorLTC2946.VoltageSource.Adin, 0.005865, 0.625,3);

            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.Enable12V();
            mb.Enable5V();
            mb.EnableChannelPower(1);
            mb.EnableChannelPower(2);
            mb.EnableChannelPower(3);
            
            //em1.ReadCtlARegister();
            //em2.ReadCtlARegister();
            //em3.ReadCtlARegister();

            EnergyMonitorLTC2946.EM_VandI data;

            while (--i > 0)
            {
                data = em1.getVandI();
                Debug.Print("SLOT1A DATA V/I:" + data.volts.ToString() + " / " + data.amps.ToString());

                data = em2.getVandI();
                Debug.Print("SLOT1B DATA V/I:" + data.volts.ToString() + " / " + data.amps.ToString());

                data = em4.getVandI();
                Debug.Print("BATT DATA V/I:" + data.volts.ToString() + " / " + data.amps.ToString());

                data = em3.getVandI();
                Debug.Print("SLOT1C DATA V/I:" + data.volts.ToString() + " / " + data.amps.ToString());
            }

            mb.DisableChannelPower(1);
            mb.DisableChannelPower(2);
            mb.DisableChannelPower(3);
            mb.Disable12V();
            mb.Disable5V();
        }


        [TestMethod]
        public void TestI2CUartSlot1A()
        {
            Init();
            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.EnableChannelPower(1);
           
            
            I2CUartBridge ub = new I2CUartBridge(new I2CDevice.Configuration(0x60 >> 1, 400));
            int i = 0;
            while (i++ < 10)
            {
                ub.get_i2c_buffer();
                Thread.Sleep(1000);
            }
            mb.DisableChannelPower(1);

        }

        [TestMethod]
        public void TestI2CUartSlot1B()
        {
            Init();
            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.EnableChannelPower(2);
            I2CUartBridge ub = new I2CUartBridge(new I2CDevice.Configuration(0x62 >> 1, 400));
            int i = 0;
            while (i++ < 10)
            {
                ub.get_i2c_buffer();
                Thread.Sleep(1000);
            }
            mb.DisableChannelPower(2);
        }
        [TestMethod]
        public void TestI2CUartSlot1C()
        {
            Init();
            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.EnableChannelPower(3);
            I2CUartBridge ub = new I2CUartBridge(new I2CDevice.Configuration(0x68 >> 1, 400),1);
            int i = 0;
            while (i++ < 10)
            {
                ub.get_i2c_buffer();
                byte[] msgBytes = UTF8Encoding.UTF8.GetBytes("hello world\n\r");

                ub.i2cprint(msgBytes, msgBytes.Length);
                Thread.Sleep(1000);
            }
            mb.DisableChannelPower(3);
            //_psb1.DisableI2C();

        }
        
        [TestMethod]
        public void TestI2CUartSlot3C()
        {
            Init();
            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.EnableChannelPower(9);

            I2CUartBridge ub = new I2CUartBridge(new I2CDevice.Configuration(0x68 >> 1, 400),3);
            int i = 0;
            while (i++ < 100)
            {
                ub.get_i2c_buffer();
                byte[] msgBytes = UTF8Encoding.UTF8.GetBytes("hello world\n\r");

                ub.i2cprint(msgBytes, msgBytes.Length);
                Thread.Sleep(1000);
            }
            mb.DisableChannelPower(9);

        }
        
        [TestMethod]
        public void TestI2CSerialPortSlot3C()
        {
            Init();
            HWModules.MotherBoard mb = HWModules.MotherBoard.Instance;
            mb.EnableChannelPower(9);

            var port = new I2CSerialPort("COM9");
            port.DataReceived += port_DataReceived;
            port.Open();

            int i = 30;
            var msgBytes = Encoding.UTF8.GetBytes("Hello Port 3c\r\n");

            while (--i > 0)
            {
                port.Write(msgBytes, 0, msgBytes.Length);
                Thread.Sleep(1000);
            }
            mb.DisableChannelPower(9);

        }

        private StringBuilder _sbTemp = new StringBuilder();
        private void port_DataReceived(object sender, EventArgs e)
        {
            var inbuff = new byte[1024];

            var port = (I2CSerialPort) sender;
            port.Read(inbuff, 0, port.BytesToRead);
            _sbTemp.Append(SafeEncoding.GetChars(inbuff));
            Debug.Print("Got Line: " + _sbTemp.ToString());

        }
    
    }
}
