private void PollingThreadProc() { WaitHandle[] waits = new WaitHandle[] { _pollFlag, _quitFlag }; int waitResult = WaitHandle.WaitAny(waits); if (waitResult != 0) { return; } long[] _portDebounce = new long[8]; GetAllPortState(ref _ioStateMap); _ioModeMap = GetAllPortModes(); while (!_quitFlag.WaitOne(0)) { if (_pollFlag.WaitOne(25)) { byte curStates = (byte)SimpleIOClass.ReadPortValue(); if (curStates != _ioStateMap) { DateTime now = DateTime.Now; for (uint i = 0; i < 8; i++) { // if ((_ioModeMap & PortMask[i]) > 0 && (_ioStateMap & PortMask[i]) != (curStates & PortMask[i])) if ((_ioStateMap & PortMask[i]) != (curStates & PortMask[i])) { if (now.Ticks - _portDebounce[i] > DEBOUNCE_MIN_TICKS) { FirePollEvent(i, ((_ioStateMap & PortMask[i]) == PortMask[i]) ? CtrlState.High : CtrlState.Low); } #if DEBUG Trace.WriteLine(string.Format("Firing Input Poll Event Port {0}, State {1}", i, ((_ioStateMap & PortMask[i]) == PortMask[i]) ? CtrlState.High : CtrlState.Low)); #endif } } _ioStateMap = curStates; } } Thread.Sleep(10); } }
public CtrlState GetPortState(uint port) { CtrlState retVal = CtrlState.Floating; if ((_ioModeMap & PortMask[port]) != PortMask[port]) { throw new ArgumentException("Port must be in input state to be read.", "port"); } int stateVal = SimpleIOClass.ReadPortValue(); if (stateVal == 0x8000) { throw new Exception("Error reading port values"); } else { _ioStateMap = (byte)stateVal; retVal = ((_ioStateMap & PortMask[port]) == PortMask[port]) ? CtrlState.High : CtrlState.Low; } return(retVal); }
public byte GetAllPortStates() { return((byte)SimpleIOClass.ReadPortValue()); }