public void SetPinModeSetsDefaultValue() { using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver())) { int testPin = OutputPin; // Set value to low prior to test, so that we have a defined start situation controller.OpenPin(testPin, PinMode.Output); controller.Write(testPin, PinValue.Low); controller.ClosePin(testPin); // For this test, we use the input pin as an external pull-up controller.OpenPin(InputPin, PinMode.Output); controller.Write(InputPin, PinValue.High); Thread.Sleep(2); controller.OpenPin(testPin, PinMode.Input); Thread.Sleep(50); // It's not possible to change the direction while listening to events (causes an error). Therefore the real behavior of the driver // can only be tested with a scope (or if we had a third pin connected in the lab hardware) // We do another test here and make sure the // pin is really high now controller.Write(testPin, PinValue.High); controller.SetPinMode(testPin, PinMode.Output); controller.SetPinMode(InputPin, PinMode.Input); Assert.True(controller.Read(InputPin) == PinValue.High); controller.ClosePin(OutputPin); controller.ClosePin(InputPin); } }
//protected static PinNumberingScheme GetTestNumberingScheme() => PinNumberingScheme.Logical; public static string InputPullResistorsWork(bool state) { Log = "Start"; try { using (GpioController controller = new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver())) { controller.OpenPin(OpenPin, PinMode.InputPullUp); var initstate = controller.Read(OpenPin); switch (state) { case false: controller.SetPinMode(OpenPin, PinMode.InputPullDown); break; case true: controller.SetPinMode(OpenPin, PinMode.InputPullUp); break; } var finalstate = controller.Read(OpenPin); Log = $"Pin state was read as {initstate} Was set to {state} and then read as {finalstate}"; } } catch (Exception ex) { Log = ex.Source + " " + ex.Message; } return(Log); }
private void SetupGpio() { gpio.OpenPin(DAT); gpio.OpenPin(CLK); gpio.SetPinMode(DAT, PinMode.Output); gpio.SetPinMode(CLK, PinMode.Output); }
internal override bool SetGpioValue(int pin, GpioPinMode mode) { if (!PinController.IsValidPin(pin) || !IsDriverInitialized) { return(false); } try { if (DriverController == null) { return(false); } if (!DriverController.IsPinModeSupported(pin, (PinMode)mode)) { return(false); } if (!DriverController.IsPinOpen(pin)) { DriverController.OpenPin(pin); } if (!DriverController.IsPinOpen(pin)) { return(false); } DriverController.SetPinMode(pin, (PinMode)mode); return(true); } finally { ClosePin(pin); } }
public int module_init() { _gpio.SetPinMode(RST_PIN, PinMode.Output); _gpio.SetPinMode(DC_PIN, PinMode.Output); _gpio.SetPinMode(CS_PIN, PinMode.Output); _gpio.SetPinMode(BUSY_PIN, PinMode.Input); _spi.ConnectionSettings.ClockFrequency = 4_000_000; _spi.ConnectionSettings.Mode = SpiMode.Mode0; return(0); }
public void InitialResetState(TestDevice testDevice) { s_gpioMock.OpenPin(1, PinMode.Input); Assert.Equal(PinValue.Low, s_gpioMock.Read(1)); s_gpioMock.SetPinMode(1, PinMode.Output); testDevice.Device.Enable(); s_gpioMock.SetPinMode(1, PinMode.Input); Assert.Equal(PinValue.High, s_gpioMock.Read(1)); s_gpioMock.ClosePin(1); s_driverMock.Reset(); }
public void PinCanChangeStateWhileItIsOpen() { using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver())) { controller.OpenPin(OutputPin, PinMode.Input); Thread.SpinWait(100); controller.SetPinMode(OutputPin, PinMode.Output); controller.Write(OutputPin, PinValue.High); controller.SetPinMode(OutputPin, PinMode.Input); controller.ClosePin(OutputPin); } }
public void InputPullResistorsWork() { using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver())) { controller.OpenPin(OpenPin, PinMode.InputPullUp); Assert.Equal(PinValue.High, controller.Read(OpenPin)); controller.SetPinMode(OpenPin, PinMode.InputPullDown); Assert.Equal(PinValue.Low, controller.Read(OpenPin)); controller.SetPinMode(OpenPin, PinMode.InputPullUp); Assert.Equal(PinValue.High, controller.Read(OpenPin)); } }
protected virtual void TickleSensor() { gpioController.SetPinMode(pinNumber, PinMode.Output); gpioController.Write(pinNumber, PinValue.High); Thread.Sleep(250); gpioController.Write(pinNumber, PinValue.Low); Thread.Sleep(20); gpioController.Write(pinNumber, PinValue.High); WaitMicroseconds(40); gpioController.SetPinMode(pinNumber, PinMode.Input); WaitMicroseconds(10); }
/// <summary> /// Displays nodes in their current configuration for the specified duration. /// </summary> /// <param name="duration">Time to display segment.</param> public void DisplaySegment(TimeSpan duration) { /* * Cases to consider * node.Cathode == _lastNode.Cathode * node.Cathode == _lastNode.Anode -- drop low * node.Anode == _lastNode.Cathode * node.Anode == _lastNode.Anode * node.Anode != _lastNode.Cathode | _lastNode.Anode * node.Cathode != _lastNode.Cathode | _lastNode.Anode */ Stopwatch watch = Stopwatch.StartNew(); do { for (int i = 0; i < _nodes.Length; i++) { CharlieplexSegmentNode node = _nodes[i]; // skip updating pinmode when possible if (_lastNode.Anode != node.Anode && _lastNode.Anode != node.Cathode) { _gpioController.SetPinMode(_lastNode.Anode, PinMode.Input); } if (_lastNode.Cathode != node.Anode && _lastNode.Cathode != node.Cathode) { _gpioController.SetPinMode(_lastNode.Cathode, PinMode.Input); } if (node.Cathode != _lastNode.Anode && node.Cathode != _lastNode.Cathode) { _gpioController.SetPinMode(node.Cathode, PinMode.Output); } if (node.Anode != _lastNode.Anode && node.Anode != _lastNode.Cathode) { _gpioController.SetPinMode(node.Anode, PinMode.Output); } _gpioController.Write(node.Anode, node.Value); // It is necessary to sleep for the LED to be seen with full brightness // It may be possible to sleep less than 1ms -- this API has ms granularity Thread.Sleep(1); _gpioController.Write(node.Anode, 0); _lastNode.Anode = node.Anode; _lastNode.Cathode = node.Cathode; } }while (watch.Elapsed < duration); }
private PinValue WriteByte(byte data) { // We send data by 8 bits for (byte i = 0; i < 8; i++) { _controller.Write(_pinClk, PinValue.Low); DelayHelper.DelayMicroseconds(ClockWidthMicroseconds, true); // LSB first if ((data & 0x01) == 0x01) { _controller.Write(_pinDio, PinValue.High); } else { _controller.Write(_pinDio, PinValue.Low); } // LSB first data >>= 1; _controller.Write(_pinClk, PinValue.High); DelayHelper.DelayMicroseconds(ClockWidthMicroseconds, true); } // Wait for the acknowledge _controller.Write(_pinClk, PinValue.Low); _controller.Write(_pinDio, PinValue.High); _controller.Write(_pinClk, PinValue.High); DelayHelper.DelayMicroseconds(ClockWidthMicroseconds, true); _controller.SetPinMode(_pinDio, PinMode.Input); // Wait 1 µs, it's the waiting time between clk up and down // That's according to the documentation DelayHelper.DelayMicroseconds(ClockWidthMicroseconds, true); var ack = _controller.Read(_pinDio); if (ack == PinValue.Low) { // We get acknoledge from the device _controller.SetPinMode(_pinDio, PinMode.Output); _controller.Write(_pinDio, PinValue.Low); } _controller.Write(_pinClk, PinValue.High); DelayHelper.DelayMicroseconds(ClockWidthMicroseconds, true); _controller.Write(_pinClk, PinValue.Low); DelayHelper.DelayMicroseconds(ClockWidthMicroseconds, true); _controller.SetPinMode(_pinDio, PinMode.Output); return(ack); }
/// <summary> /// Displays nodes in their current configuration for the specified duration. /// </summary> /// <param name="token">CancellationToken used to signal when method should exit.</param> public void Display(CancellationToken token) { /* * Cases to consider * node.Cathode == _lastNode.Cathode * node.Cathode == _lastNode.Anode -- drop low * node.Anode == _lastNode.Cathode * node.Anode == _lastNode.Anode * node.Anode != _lastNode.Cathode | _lastNode.Anode * node.Cathode != _lastNode.Cathode | _lastNode.Anode */ while (!token.IsCancellationRequested) { for (int i = 0; i < _nodes.Length; i++) { CharlieplexSegmentNode node = _nodes[i]; // skip updating pinmode when possible if (_lastNode.Anode != node.Anode && _lastNode.Anode != node.Cathode) { _gpioController.SetPinMode(_lastNode.Anode, PinMode.Input); } if (_lastNode.Cathode != node.Anode && _lastNode.Cathode != node.Cathode) { _gpioController.SetPinMode(_lastNode.Cathode, PinMode.Input); } if (node.Cathode != _lastNode.Anode && node.Cathode != _lastNode.Cathode) { _gpioController.SetPinMode(node.Cathode, PinMode.Output); } if (node.Anode != _lastNode.Anode && node.Anode != _lastNode.Cathode) { _gpioController.SetPinMode(node.Anode, PinMode.Output); } _gpioController.Write(node.Anode, node.Value); // It is necessary to sleep for the LED to be seen with full brightness Thread.SpinWait(1); _gpioController.Write(node.Anode, 0); _lastNode.Anode = node.Anode; _lastNode.Cathode = node.Cathode; } } }
[Trait("SkipOnTestRun", "Windows_NT")] // Currently, the Windows Driver is defaulting to InputPullDown, and it seems this cannot be changed public void OpenPinDefaultsModeToLastMode(PinMode modeToTest) { var driver = GetTestDriver(); if (driver is SysFsDriver) { // See issue #1581. There seems to be a library-version issue or some other random cause for this test to act differently on different hardware. return; } // This works for input/output on most systems, but not on pullup/down, since sometimes the hardware doesn't have read possibilities for those (ie. the Raspi 3) using (GpioController controller = new GpioController(GetTestNumberingScheme(), driver)) { controller.OpenPin(OutputPin); controller.SetPinMode(OutputPin, modeToTest); Assert.Equal(modeToTest, controller.GetPinMode(OutputPin)); controller.ClosePin(OutputPin); } // Close controller, to make sure we're not caching using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver())) { controller.OpenPin(OutputPin); Thread.Sleep(100); Assert.Equal(modeToTest, controller.GetPinMode(OutputPin)); } }
static void Main(string[] args) { const int buttonPin = 5; PinValue lastValue = PinValue.Low; // Set up our controller using (GpioController controller = new GpioController()) { // Set our pin to export controller.OpenPin(buttonPin); // Set our pin to input controller.SetPinMode(buttonPin, PinMode.InputPullUp); while (true) { PinValue pinValue = controller.Read(buttonPin); if (pinValue != lastValue) { lastValue = pinValue; Console.WriteLine("Button pin went " + pinValue); } Thread.Sleep(100); } } }
private static void PwmRaspiTest(RaspberryPiBoard raspi) { int pinNumber = 12; // PWM0 pin Console.WriteLine("Blinking and dimming an LED - Press any key to quit"); while (!Console.KeyAvailable) { GpioController ctrl = raspi.CreateGpioController(); ctrl.OpenPin(pinNumber); ctrl.SetPinMode(pinNumber, PinMode.Output); ctrl.Write(pinNumber, PinValue.Low); Thread.Sleep(500); ctrl.Write(pinNumber, PinValue.High); Thread.Sleep(1000); ctrl.ClosePin(pinNumber); ctrl.Dispose(); var pwm = raspi.CreatePwmChannel(0, 0, 9000, 0.1); pwm.Start(); for (int i = 0; i < 10; i++) { pwm.DutyCycle = i * 0.1; Thread.Sleep(500); } pwm.Stop(); pwm.Dispose(); } Console.ReadKey(true); }
public void SetupValve(Valve valve, bool isValueHigh = false) { valve.PinValue = isValueHigh; mGpioController.SetPinMode(valve.PinNumber, PinMode.Output); mGpioController.Write(valve.PinNumber, valve.PinValue); mValves[valve] = valve.PinNumber; }
/// <summary> /// reads the given setting from user input and sends it to the Strompi3-port /// <para>MOD (Chapter 8.1): additional cable soldering required between Reset-Pin (Jumper ON) and chosen GPIO-Pin (40)</para> /// <para>SERIALLESS ON: turns off the serial comm-port and sets a GPIO-pin (40) as alternative to read</para> /// <para>SERIALLESS OFF: turns on the serial comm-port and resets a GPIO-pin (40)</para> /// </summary> /// <param name="ups"></param> public static void Serialless(StromPi3 ups) { Console.WriteLine($"StromPi Serialless-Enable: {ups.Settings.SerialLessEnable}"); int serialLessEnable = ConverterHelper.ReadInt(0, 1, "0 = False, 1 = True"); ups.Settings.SetSerialLessEnable(serialLessEnable.ToString()); if (ups.Settings.SerialLessEnable) // use serial port { Console.WriteLine($"Transfer Serial-Less Enable: {ups.Settings.SerialLessEnable}"); ups.Port.SendConfigElement(EConfigElement.SerialLessMode, ups.Settings.SerialLessEnable.ToNumber()); ups.Port.ReceiveConfiguration(ups.Settings); } else // reset to serial port { using (var gpioController = new GpioController(PinNumberingScheme.Board)) { gpioController.OpenPin(GPIOShutdownPinBoardNumber); gpioController.SetPinMode(GPIOShutdownPinBoardNumber, PinMode.Output); gpioController.Write(GPIOShutdownPinBoardNumber, PinValue.High); Console.WriteLine($"set pin {GPIOShutdownPinBoardNumber} to HIGH (3 secs)"); Thread.Sleep(3000); gpioController.Write(GPIOShutdownPinBoardNumber, PinValue.Low); Console.WriteLine($"set pin {GPIOShutdownPinBoardNumber} to LOW to Disable Serialless Mode."); Console.WriteLine($"This will take approx. 10 seconds.."); Thread.Sleep(10000); Console.WriteLine($"Serialless Mode is Disabled!"); } } }
public static PinState ReadState(int pin) { PinState state = new PinState { Pin = pin, StateOn = false }; try { GpioController controller = new GpioController(); if (!controller.IsPinOpen(pin)) { controller.OpenPin(pin, PinMode.Input); } var mode = controller.GetPinMode(pin); if (!PinMode.Input.Equals(mode)) { controller.SetPinMode(pin, PinMode.Input); } var x = controller.Read(pin); if (x.Equals(PinValue.High)) { state.StateOn = true; } } catch (Exception ex) { //TODO: log Error } return(state); }
// test pressione pulsante public static void Test05() { GpioController controller = new GpioController(PinNumberingScheme.Board); var pinIN1 = 18; // PIn 18 input. Pulsante var lightTime = 1000; Console.WriteLine("Test Lettura Pulsante"); controller.OpenPin(pinIN1, PinMode.InputPullDown); controller.SetPinMode(pinIN1, PinMode.InputPullDown); //provo a impostare il pin prima di "aprirlo". Non funziona e genera runtime error controller.RegisterCallbackForPinValueChangedEvent(pinIN1, PinEventTypes.Rising, onPushButton); try { while (true) { Thread.Sleep(lightTime); //Console.WriteLine("Attendo la pressione del pulsante " + DateTime.Now.ToString()); Console.WriteLine("Pin18 in stato " + controller.Read(pinIN1).ToString()); } } finally { controller.ClosePin(pinIN1); } }
static int rcTime(int ldr) { int count = 0; GpioController gpioController = new GpioController(); gpioController.SetPinMode(ldr, PinMode.Output); gpioController.Write(ldr, false); System.Threading.Thread.Sleep(2); gpioController.SetPinMode(ldr, PinMode.Input); while (gpioController.Read(ldr) == 0) { count++; } return(count); }
public void Dispose() { _measureTemperature = false; if (IsWorked) { Stop(); while (IsWorked) { Task.Delay(100).GetAwaiter().GetResult(); } } if (_cts != null) { _cts.Dispose(); } foreach (var item in _ovenSettings.OvenSettings.RelayPins) { _gpioController.SetPinMode(item, PinMode.Input); _gpioController.ClosePin(item); } if (_max6675 != null) { _max6675.Dispose(); } }
protected virtual void InitializeGPIO() { gpioController = new GpioController(PinNumberingScheme.Board); gpioController.OpenPin(pinNumber); gpioController.SetPinMode(pinNumber, PinMode.Output); gpioController.Write(pinNumber, PinValue.High); }
static void Main() { const int ledPin = 20; // Set up our controller using (GpioController controller = new GpioController()) { // Set our pin to export controller.OpenPin(ledPin); // Set our pin to output controller.SetPinMode(ledPin, PinMode.Output); for (int i = 0; i < 3; i++) { // Turn our pin on controller.Write(ledPin, PinValue.High); Thread.Sleep(1000); // Turn our pin off controller.Write(ledPin, PinValue.Low); Thread.Sleep(1000); } // Close our pin controller.ClosePin(ledPin); } }
public void HighPulledPinDoesNotChangeToLowWhenChangedToOutput() { using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver())) { bool didTriggerToLow = false; int testPin = OutputPin; // Set value to low prior to test, so that we have a defined start situation controller.OpenPin(testPin, PinMode.Output); controller.Write(testPin, PinValue.Low); controller.ClosePin(testPin); // For this test, we use the input pin as an external pull-up controller.OpenPin(InputPin, PinMode.Output); controller.Write(InputPin, PinValue.High); Thread.Sleep(2); // If we were to use InputPullup here, this would work around the problem it seems, but it would also make our test pass under almost all situations controller.OpenPin(testPin, PinMode.Input); Thread.Sleep(50); controller.RegisterCallbackForPinValueChangedEvent(testPin, PinEventTypes.Falling, (sender, args) => { if (args.ChangeType == PinEventTypes.Falling) { didTriggerToLow = true; } }); controller.Write(testPin, PinValue.High); controller.SetPinMode(testPin, PinMode.Output); Thread.Sleep(50); Assert.False(didTriggerToLow); controller.ClosePin(OutputPin); controller.ClosePin(InputPin); } }
protected override void OpenChannel(int pinNumber, int pwmChannel) { _servoPin = pinNumber; _controller.OpenPin(_servoPin); _controller.SetPinMode(_servoPin, PinMode.Output); _runningThread = new Thread(RunSoftPWM); _runningThread.Start(); }
public SwitchService(GpioController gpioController) { this.gpioController = gpioController; gpioController.OpenPin(InPinNumber); gpioController.SetPinMode(InPinNumber, PinMode.InputPullDown); gpioController.RegisterCallbackForPinValueChangedEvent(InPinNumber, PinEventTypes.Falling | PinEventTypes.Rising, OnPinChanged); }
public DisplaySpiDriver() { gpioController = new GpioController(); var spiConnectionSettings = new SpiConnectionSettings(0, 0); spiDevice = SpiDevice.Create(spiConnectionSettings); // open pins gpioController.OpenPin(PIN_RESET); gpioController.OpenPin(PIN_DC); gpioController.OpenPin(PIN_CS); gpioController.OpenPin(PIN_BUSY); gpioController.SetPinMode(PIN_RESET, PinMode.Output); gpioController.SetPinMode(PIN_DC, PinMode.Output); gpioController.SetPinMode(PIN_CS, PinMode.Output); gpioController.SetPinMode(PIN_BUSY, PinMode.Input); gpioController.Write(PIN_CS, PinValue.High); }
public Task StartAsync(CancellationToken cancellationToken) { if (gpio == null) { gpio = new GpioController(); } logger.LogTrace("Strating kbd watcher"); gpio.OpenPin(5); gpio.SetPinMode(5, PinMode.InputPullUp); gpio.OpenPin(6); gpio.SetPinMode(6, PinMode.InputPullUp); gpio.OpenPin(13); gpio.SetPinMode(13, PinMode.InputPullUp); gpio.OpenPin(19); gpio.SetPinMode(19, PinMode.InputPullUp); gpio.RegisterCallbackForPinValueChangedEvent(5, PinEventTypes.Rising, Pin5ChangeCallback); gpio.RegisterCallbackForPinValueChangedEvent(6, PinEventTypes.Rising, Pin6ChangeCallback); //gpio.RegisterCallbackForPinValueChangedEvent(13, PinEventTypes.Rising, PinChangeCallback); gpio.RegisterCallbackForPinValueChangedEvent(19, PinEventTypes.Rising, Pin19ChangeCallback); logger.LogTrace("kbd watcher started"); return(Task.CompletedTask); }
public void ThrowsInvalidOperationExceptionWhenPinIsNotOpened() { using (GpioController controller = new GpioController(GetTestNumberingScheme(), GetTestDriver())) { Assert.Throws <InvalidOperationException>(() => controller.Write(OutputPin, PinValue.High)); Assert.Throws <InvalidOperationException>(() => controller.Read(InputPin)); Assert.Throws <InvalidOperationException>(() => controller.ClosePin(OutputPin)); Assert.Throws <InvalidOperationException>(() => controller.SetPinMode(OutputPin, PinMode.Output)); Assert.Throws <InvalidOperationException>(() => controller.GetPinMode(OutputPin)); } }
/// <summary> /// Ensures pin is open and is set to output mode /// </summary> private void PreparePin(int pinNumber) { if (!_gpioController.IsPinOpen(pinNumber)) { _gpioController.OpenPin(pinNumber, PinMode.Output); } else if (_gpioController.GetPinMode(pinNumber) != PinMode.Output) { _gpioController.SetPinMode(pinNumber, PinMode.Output); } }