2023-12-18 16:39:16 +08:00
|
|
|
using System;
|
2023-04-13 11:51:03 +08:00
|
|
|
using System.Diagnostics;
|
|
|
|
|
|
|
|
namespace Aitex.Core.RT.IOCore
|
|
|
|
{
|
2023-12-18 16:39:16 +08:00
|
|
|
public abstract class IOAccessor<T>: IIOAccessor where T: unmanaged
|
2023-04-13 11:51:03 +08:00
|
|
|
{
|
2023-12-18 16:39:16 +08:00
|
|
|
#region Variables
|
2023-04-13 11:51:03 +08:00
|
|
|
|
2024-01-07 11:29:22 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
private readonly string _name;
|
|
|
|
private readonly IOType _type;
|
|
|
|
private readonly string _addr;
|
|
|
|
private readonly int _index;
|
2024-01-07 11:29:22 +08:00
|
|
|
protected readonly bool IsSimulator;
|
|
|
|
protected T[] Buffer;
|
2023-08-10 16:44:46 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
#endregion
|
2023-04-13 11:51:03 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
#region Constructors
|
2023-04-13 11:51:03 +08:00
|
|
|
|
2024-01-11 15:19:20 +08:00
|
|
|
public IOAccessor(string name, int index, string address, IOType type, bool isSimulator, bool disImmCache)
|
2023-12-18 16:39:16 +08:00
|
|
|
{
|
2024-01-07 11:29:22 +08:00
|
|
|
IsSimulator = isSimulator;
|
2023-12-18 16:39:16 +08:00
|
|
|
_name = name;
|
|
|
|
_index = index;
|
|
|
|
_addr = address;
|
|
|
|
_type = type;
|
2024-01-11 15:19:20 +08:00
|
|
|
DisableImmediatelyCache = disImmCache;
|
2023-12-18 16:39:16 +08:00
|
|
|
}
|
2023-04-13 11:51:03 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
#endregion
|
2023-04-13 11:51:03 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
public object NonTypedValue => Value;
|
2023-04-23 16:43:08 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
/// <summary>
|
|
|
|
/// 返回当前IO的值。
|
|
|
|
/// </summary>
|
2023-04-13 11:51:03 +08:00
|
|
|
public T Value
|
|
|
|
{
|
2023-12-18 16:39:16 +08:00
|
|
|
get => GetValue(_index);
|
|
|
|
set => SetValue(_index, value);
|
2023-04-13 11:51:03 +08:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
public string Name => _name;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
public IOType Type => _type;
|
2023-08-10 16:44:46 +08:00
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
public int Index => _index;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
public int BlockOffset { get; set; }
|
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
public string Addr => _addr;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
public string Provider { get; set; }
|
|
|
|
|
|
|
|
public bool Visible { get; set; }
|
|
|
|
|
|
|
|
public string Description { get; set; }
|
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
public string StringIndex => _name.Substring(0, 2) + "-" + _index;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
public int IoTableIndex { get; set; }
|
|
|
|
|
2024-01-11 15:19:20 +08:00
|
|
|
public bool DisableImmediatelyCache { get; }
|
2023-12-18 16:39:16 +08:00
|
|
|
|
|
|
|
#region Methods
|
|
|
|
|
|
|
|
protected virtual T GetValue(int index)
|
2023-04-13 11:51:03 +08:00
|
|
|
{
|
2023-12-18 16:39:16 +08:00
|
|
|
Debug.Assert(Buffer != null && index >= 0 && index < Buffer.Length);
|
|
|
|
return Buffer[index];
|
2023-04-13 11:51:03 +08:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
protected virtual void SetValue(int index, T value)
|
2023-04-13 11:51:03 +08:00
|
|
|
{
|
2023-12-18 16:39:16 +08:00
|
|
|
Debug.Assert(Buffer != null && index >= 0 && index < Buffer.Length);
|
|
|
|
Buffer[index] = value;
|
2023-04-13 11:51:03 +08:00
|
|
|
}
|
|
|
|
|
2023-12-18 16:39:16 +08:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public void BindBuffer(object buff)
|
2023-04-13 11:51:03 +08:00
|
|
|
{
|
2023-12-18 16:39:16 +08:00
|
|
|
Buffer = (T[])Convert.ChangeType(buff, typeof(T[]));
|
2023-04-13 11:51:03 +08:00
|
|
|
}
|
2023-12-18 16:39:16 +08:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
2023-04-13 11:51:03 +08:00
|
|
|
public override string ToString()
|
|
|
|
{
|
|
|
|
return $"{Name}";
|
|
|
|
}
|
2023-12-18 16:39:16 +08:00
|
|
|
|
|
|
|
#endregion
|
2023-04-13 11:51:03 +08:00
|
|
|
}
|
|
|
|
}
|