//long currentValue; public void Run() { Debug.Print("Starting RangeFinder in a thread..."); var rangeFinderProcess = new HC_SR04(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1); OutputPort blueLED = new OutputPort(Pins.ONBOARD_LED, false); var period = 500; var ledState = true; long pingResult; while (true) { lock (monitor) { pingResult = rangeFinderProcess.Ping(); } Debug.Print("Reading = " + pingResult.ToString()); currentValue = pingResult; SourceBuffer4.HandlePut((object)currentValue); Thread.Sleep(period); object setpoint = SourceBuffer1.HandleGet(); if (setpoint != null) { period = (int)setpoint; period = period > 10000 ? 10000 : period; period = period < 100 ? 100 : period; } ledState = !ledState; blueLED.Write(ledState); } }
public void Run() { Debug.Print("Starting RangeFinder in a separate thread..."); var rangeFinderProcess = new HC_SR04(Pins.GPIO_PIN_D0, Pins.GPIO_PIN_D1); OutputPort blueLED = new OutputPort(Pins.ONBOARD_LED, false); OutputPort greenLED = new OutputPort(Pins.GPIO_PIN_D13, false); TimerCallback proximityAlarmTimerCallback = new TimerCallback(ManageProximityAlarm); TimerCallback proximityAlarmClearHoldOffTimerCallback = new TimerCallback(ManageProximityAlarm); //Timer belowThresholdTimer = null; //Timer proximityAlarmClearHoldOffTimer = null; int belowThresholdCrossingCounter = 0; bool ledState = true; long pingResult; object setpoint; while (true) { belowThreshold = false; lock (monitor) { pingResult = rangeFinderProcess.Ping(); } Debug.Print("Reading = " + pingResult.ToString()); previousValue = currentValue; currentValue = pingResult; sourceBufferCurrentValue.HandlePut((object)currentValue); if (currentValue < currentLowTrigLevel) { Debug.Print("BELOW THRESHOLD!"); belowThreshold = true; if (previousValue > currentLowTrigLevel && belowThresholdTimerActive == false) { TimerState belowThresholdState = TimerState.belowThreshold; belowThresholdTimer = new Timer(proximityAlarmTimerCallback, belowThresholdState, lowTrigDur, (int)1.5 * lowTrigDur); belowThresholdTimerActive = true; belowThresholdCrossingCounter += 1; Debug.Print("Low Level Threshold Crossed (count=" + belowThresholdCrossingCounter.ToString() + ")"); Debug.Print("belowThresholdTimer Started (fires in " + lowTrigDur.ToString() + "ms)"); } if (proximityAlarmClearHoldOffTimerActive == true) { proximityAlarmClearHoldOffTimer.Dispose(); proximityAlarmClearHoldOffTimerActive = false; Debug.Print("proximityAlarmClearHoldOffTimer Cancelled"); } } else { belowThreshold = false; if (previousValue < currentLowTrigLevel && belowThresholdTimerActive == true) { belowThresholdTimer.Dispose(); belowThresholdTimerActive = false; Debug.Print("belowThresholdTimer Cancelled"); } if (previousValue < currentLowTrigLevel && proximityAlarmClearHoldOffTimerActive == false && belowThresholdCrossingCounter > 0) { proximityAlarmClearHoldOffTimerActive = true; TimerState holdingOffState = TimerState.holdingOff; proximityAlarmClearHoldOffTimer = new Timer(proximityAlarmClearHoldOffTimerCallback, holdingOffState, holdOffTime, (int)1.5 * holdOffTime); Debug.Print("proximityAlarmClearHoldOffTimer Started (fires in " + holdOffTime.ToString() + "ms)"); } } Thread.Sleep(period); setpoint = sourceBufferMeasPeriod.HandleGet(); if (setpoint != null) { period = (int)setpoint; period = period > 10000 ? 10000 : period; period = period < 100 ? 100 : period; if (period != currentPeriod) { Debug.Print("Period changed to " + period.ToString() + "ms"); } currentPeriod = period; } setpoint = sourceBufferLowTrigLevel.HandleGet(); if (setpoint != null) { int i = (int)setpoint; lowTrigLevel = (long)i; lowTrigLevel = lowTrigLevel > 3000L ? 3000L : lowTrigLevel; lowTrigLevel = lowTrigLevel < 100L ? 100L : lowTrigLevel; if (lowTrigLevel != currentLowTrigLevel) { Debug.Print("Low Trigger Level changed to " + lowTrigLevel.ToString() + "mm"); } currentLowTrigLevel = lowTrigLevel; } setpoint = sourceBufferLowTrigDur.HandleGet(); if (setpoint != null) { lowTrigDur = (int)setpoint; lowTrigDur = lowTrigDur > 10000 ? 10000 : lowTrigDur; lowTrigDur = lowTrigDur < 1000 ? 1000 : lowTrigDur; if (lowTrigDur != currentLowTrigDur) { Debug.Print("Low Trigger Duration changed to " + lowTrigDur.ToString() + "ms"); } currentLowTrigDur = lowTrigDur; } ledState = !ledState; blueLED.Write(ledState); greenLED.Write(ledState); } }