Files
tbf/TBF/Rig/BuiltIn/ValveEx/Valve.cs
T

87 lines
2.3 KiB
C#

///
/// Copyright (c) 2015-2016 Sensus Slovensko a.s.
///
using System;
using System.Collections.Generic;
using log4net;
using TBF.Rig.GenericDevices;
using Dirichlet.Numerics;
namespace TBF.Rig.BuiltIn.ValveEx
{
public class Valve : ComponentBase, Generic.IComponent
{
private static readonly ILog log = LogManager.GetLogger(typeof(Valve));
public override string ToString() { return string.Format("{0}({1})", ClassName, Cfg.ToString(-1)); }
readonly ValveCfg valveCfg;
public readonly IValve[] InputValves;
public readonly UInt128 Mask; /// derived from bitPosition in the constructor
public Valve() { }
public Valve(Generic.IComponentCfg cfg, IList<Generic.IComponent> components)
: base(cfg)
{
valveCfg = cfg as ValveCfg;
InputValves = new IValve[valveCfg.InputValvesCount];
InputValves[0] = TbfComponents.FindComponent(valveCfg.Input1, components) as IValve;
InputValves[1] = TbfComponents.FindComponent(valveCfg.Input2, components) as IValve;
if (InputValves.Length > 2)
{
InputValves[2] = TbfComponents.FindComponent(valveCfg.Input3, components) as IValve;
}
if (InputValves.Length > 3)
{
InputValves[3] = TbfComponents.FindComponent(valveCfg.Input4, components) as IValve;
}
if (InputValves.Length > 4)
{
InputValves[4] = TbfComponents.FindComponent(valveCfg.Input5, components) as IValve;
}
Mask = (((UInt128)1) << valveCfg.BitPosition);
log.Warn(this.ToString());
}
public override void Initialize() { }
public void UpdateRoute(ref UInt128 route)
{
bool output;
if (valveCfg.LogicalOp == ValveExOperation.And)
{
output = true;
foreach (var valve in InputValves)
{
if (valve != null) output = output && valve.State;
}
}
else if (valveCfg.LogicalOp == ValveExOperation.Or)
{
output = false;
foreach (var valve in InputValves)
{
if (valve != null) output = output || valve.State;
}
}
else
return;
if (output != valveCfg.InvertOutput)
{
route = route | Mask; /// Set the output bit to true
}
else
{
route = route & (~Mask); /// Reset the output bit to false
}
}
}
}