public SimCounter(int pin) { int index = -1; for (int i = 0; i < 8; i++) { var counter = SimData.Counter[i]; if (!counter.Initialized) continue; if (counter.UpSourceChannel == pin || counter.DownSourceChannel == pin) { index = i; } } if (index == -1) { throw new InvalidOperationException($"Counter not found for pin {pin}"); } m_counterData = SimData.Counter[index]; m_k2X = m_counterData.UpFallingEdge; }
private static void SetCounterDownAsTwoPulseAnalog(CounterData counter, int trigIndex) { if (!counter.DownSourceTrigger) { throw new InvalidOperationException("Analog should only be called for Analog triggers"); } int analogIn = SimData.AnalogTrigger[trigIndex].AnalogPin; if (analogIn == -1) { throw new InvalidOperationException("Analog Trigger has not been allocated"); } int status = 0; bool prevTrigValue = HALAnalog.getAnalogTriggerTriggerState((IntPtr)SimData.AnalogTrigger[trigIndex].TriggerPointer, ref status); double prevAnalogVoltage = AnalogIn[analogIn].Voltage; Action<dynamic, dynamic> downCallback = (key, value) => { //If our IsAnalog has actually changed if (prevAnalogVoltage != value) { //Grab our trigger state. bool trigValue = HALAnalog.getAnalogTriggerTriggerState( (IntPtr)SimData.AnalogTrigger[trigIndex].TriggerPointer, ref status); //Was low if (!prevTrigValue) { //if still low ignore if (!trigValue) return; //Otherwise if we count on rising edge add 1 if (counter.DownRisingEdge) { counter.Count--; } } //Was High else { //if still high ignore if (trigValue) return; //Otherwise if we count on falling edge add 1 if (counter.DownFallingEdge) { counter.Count--; } } prevTrigValue = trigValue; prevAnalogVoltage = value; } }; counter.DownCallback = downCallback; AnalogIn[analogIn].Register("Voltage", downCallback); }
private static void SetCounterDownAsTwoPulseDigital(CounterData counter, int pin) { bool prevValue = DIO[pin].Value; Action<string, dynamic> downCallback = (key, value) => { //Was low if (!prevValue) { //if still low ignore if (!value) return; //Otherwise if we count on rising edge add 1 if (counter.DownRisingEdge) { counter.Count--; } } //Was High else { //if still high ignore if (value) return; //Otherwise if we count on falling edge add 1 if (counter.DownFallingEdge) { counter.Count--; } } prevValue = value; }; counter.DownCallback = downCallback; DIO[pin].Register("Value", downCallback); }
private static void SetCounterUpAsTwoPulseDigital(CounterData counter, int pin) { if (counter.UpSourceTrigger) { throw new InvalidOperationException("Digital should only be called for digital ports"); } bool prevValue = DIO[pin].Value; Action<string, dynamic> upCallback = (key, value) => { //Was low if (!prevValue) { //if still low ignore if (!value) return; //Otherwise if we count on rising edge add 1 if (counter.UpRisingEdge) { counter.Count++; } } //Was High else { //if still high ignore if (value) return; //Otherwise if we count on falling edge add 1 if (counter.UpFallingEdge) { counter.Count++; } } prevValue = value; }; counter.UpCallback = upCallback; DIO[pin].Register("Value", upCallback); }