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

namespace CPFUnitTests.UT_MotherBoard
{
    [TestClass]
    public class UnitTest_MotherBoard_I2CSlots
    {
        [TestMethod]
        public void TestI2CFailure()
        {
            var i2cport = new I2CPortBase(new I2CDevice.Configuration(0x6E >> 1, 400), 6);

            byte[] xmit = {0x1E};
            var recv = new byte[2];


            I2CDevice.I2CTransaction[] trans;

            var _wt = i2cport.CreateWriteTransaction(xmit);
            var _rt = i2cport.CreateReadTransaction(recv);
            trans = new I2CDevice.I2CTransaction[] {_wt, _rt};

            var i = 10;
            int ret;
            while (--i > 0)
            {
                ret = i2cport.Execute(trans, 500);
                Thread.Sleep(1000);
            }
        }

        [TestMethod]
        public void TestMultipleI2CDevices()
        {
            var emBus = EnergyMonitorHotel.Instance;

            var emSlot1 = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0xCE >> 1, 400), 1);

            var emSlotNone = new EnergyMonitorLTC2946(new I2CDevice.Configuration(0x25 >> 1, 400), 6 );

            var i2cport = new I2CPortBase(new I2CDevice.Configuration(0x25 >> 1, 400), 6);

            byte[] xmit = { 0x1E };
            var recv = new byte[2];


            I2CDevice.I2CTransaction[] trans;

            var _wt = i2cport.CreateWriteTransaction(xmit);
            var _rt = i2cport.CreateReadTransaction(recv);
            trans = new I2CDevice.I2CTransaction[] { _rt };

            int i = 10;
            int retc;
            EnergyMonitorLTC2946.EM_VandI ret;
            while (--i > 0)
            {
                ret = emBus.getVoltsAndMilliAmps(); // this should success, and the clock line should be high
                Thread.Sleep(1000);
                ret = emSlot1.getVandI();   // this should succeed, and we should observe that the clock line is left high.
                Thread.Sleep(1000);
                ret = emSlotNone.getVandI();   // this should fail, and we should observe that the clock line is left low.
                Thread.Sleep(1000);
                ret = emSlot1.getVandI();   // let's see if this fails or works..., and observe whether the port can enable

                retc = i2cport.Execute(trans, 500);
            }

        }
    }
}