using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cosmos.Hardware;
using Cosmos.Core;
namespace TestRunner
{
///
/// Enables logging to a Com Port.
///
public class Logger
{
private readonly Cosmos.Core.IOGroup.COM iop;
///
/// The default constructor.
///
/// This should be either 1, 2, 3, or 4.
public Logger(byte comPort)
{
iop = new Cosmos.Core.IOGroup.COM(comPort);
Initialize();
}
private void Initialize()
{
iop.InterruptEnable.Byte = 0x00;
iop.LineControl.Byte = 0x80;
iop.Data.Byte = 0x03;
iop.InterruptEnable.Byte = 0x00;
iop.LineControl.Byte = 0x03;
iop.FIFOControl.Byte = 0xC7;
iop.ModemControl.Byte = 0x0B;
}
private int IsTransmitEmpty()
{
return iop.LineStatus.Byte & 0x20;
}
#region Write Data
///
/// Writes the specified byte to the Log.
///
/// The byte to write.
public void WriteData(byte b)
{
// Empty loop, will allow us to timeout
for (uint i = 0; i < 1000 && (IsTransmitEmpty() == 0); i++) ;
iop.Data.Byte = b;
}
///
/// Writes the specified sbyte to the Log.
///
/// The sbyte to write.
public void WriteData(sbyte b)
{
WriteData(unchecked((byte)b));
}
///
/// Writes the specified ushort to the Log.
///
/// The ushort to write.
public void WriteData(ushort s)
{
WriteData(unchecked((byte)(s & 0xFF)));
WriteData(unchecked((byte)((s >> 8) & 0xFF)));
}
///
/// Writes the specified short to the Log.
///
/// The short to write.
public void WriteData(short s)
{
WriteData(unchecked((byte)(s & 0xFF)));
WriteData(unchecked((byte)((s >> 8) & 0xFF)));
}
///
/// Writes the specified uint to the Log.
///
/// The uint to write.
public void WriteData(uint s)
{
WriteData(unchecked((byte)(s & 0xFF)));
WriteData(unchecked((byte)((s >> 8) & 0xFF)));
WriteData(unchecked((byte)((s >> 16) & 0xFF)));
WriteData(unchecked((byte)((s >> 24) & 0xFF)));
}
///
/// Writes the specified int to the Log.
///
/// The int to write.
public void WriteData(int s)
{
WriteData(unchecked((byte)(s & 0xFF)));
WriteData(unchecked((byte)((s >> 8) & 0xFF)));
WriteData(unchecked((byte)((s >> 16) & 0xFF)));
WriteData(unchecked((byte)((s >> 24) & 0xFF)));
}
///
/// Writes the specified ulong to the Log.
///
/// The ulong to write.
public void WriteData(ulong s)
{
WriteData(unchecked((byte)(s & 0xFF)));
WriteData(unchecked((byte)((s >> 8) & 0xFF)));
WriteData(unchecked((byte)((s >> 16) & 0xFF)));
WriteData(unchecked((byte)((s >> 24) & 0xFF)));
WriteData(unchecked((byte)((s >> 32) & 0xFF)));
WriteData(unchecked((byte)((s >> 40) & 0xFF)));
WriteData(unchecked((byte)((s >> 48) & 0xFF)));
WriteData(unchecked((byte)((s >> 56) & 0xFF)));
}
///
/// Writes the specified long to the Log.
///
/// The long to write.
public void WriteData(long s)
{
WriteData(unchecked((byte)(s & 0xFF)));
WriteData(unchecked((byte)((s >> 8) & 0xFF)));
WriteData(unchecked((byte)((s >> 16) & 0xFF)));
WriteData(unchecked((byte)((s >> 24) & 0xFF)));
WriteData(unchecked((byte)((s >> 32) & 0xFF)));
WriteData(unchecked((byte)((s >> 40) & 0xFF)));
WriteData(unchecked((byte)((s >> 48) & 0xFF)));
WriteData(unchecked((byte)((s >> 56) & 0xFF)));
}
///
/// Writes the specified float to the Log.
///
/// The float to write.
public void WriteData(float b)
{
byte[] s = BitConverter.GetBytes(b);
WriteData(s[0]);
WriteData(s[1]);
WriteData(s[2]);
WriteData(s[3]);
}
///
/// Writes the specified double to the Log.
///
/// The double to write.
public void WriteData(double b)
{
byte[] s = BitConverter.GetBytes(b);
WriteData(s[0]);
WriteData(s[1]);
WriteData(s[2]);
WriteData(s[3]);
WriteData(s[4]);
WriteData(s[5]);
WriteData(s[6]);
WriteData(s[7]);
}
#endregion
///
/// Writes the specified string to the Log, followed by EOL.
///
/// The string to write.
public void WriteLine(string s)
{
WriteString(s + "\r\n");
}
///
/// Writes the specified string to the Log.
///
/// The string to write.
public void WriteString(string s)
{
for (int i = 0; i < s.Length; i++)
{
WriteData((byte)s[i]);
}
}
}
}