RaspberryPi.GpioSim
0.1.0
See the version list below for details.
dotnet add package RaspberryPi.GpioSim --version 0.1.0
NuGet\Install-Package RaspberryPi.GpioSim -Version 0.1.0
<PackageReference Include="RaspberryPi.GpioSim" Version="0.1.0" />
<PackageVersion Include="RaspberryPi.GpioSim" Version="0.1.0" />
<PackageReference Include="RaspberryPi.GpioSim" />
paket add RaspberryPi.GpioSim --version 0.1.0
#r "nuget: RaspberryPi.GpioSim, 0.1.0"
#:package RaspberryPi.GpioSim@0.1.0
#addin nuget:?package=RaspberryPi.GpioSim&version=0.1.0
#tool nuget:?package=RaspberryPi.GpioSim&version=0.1.0
This is provided as is. It's good enough for my needs right now. I may improve if it I have specific need, but at present I am not planing to add to it.
RaspberryPi.GpioSim: a Raspberry Pi GPIO simulator for .NET
Write and test Raspberry Pi GPIO code on any machine (Windows, macOS or Linux) without a Pi. The same app runs unchanged on a real Pi.
Your app uses the standard System.Device.Gpio
GpioController. Off the Pi, the simulated driver sits behind it. You can watch
the pins change on a live drawing of the 40-pin header, press simulated buttons from the keyboard,
or play inputs from a script. On a Pi, the real driver is used automatically.
Raspberry Pi 40-pin header (simulated)
┌─────┐ Recent events
3V3 1 │ ■ ■ │ 2 5V 16:46:07.7 » Waiting up to 30 s for the button…
... 16:46:09.3 GPIO17 (11) ▼ LOW
IN↑ GPIO17 11 │ ● ○ │ 12 GPIO18 16:46:09.3 » Button on GPIO 17 pressed
... 16:46:09.3 GPIO24 (18) ▲ HIGH
3V3 17 │ ■ ● │ 18 GPIO24 OUT
...
└─────┘
● HIGH ● LOW ○ unused ■ 3V3 ■ 5V ■ GND
Keys: [1] GPIO17
Contents
- Quick start
- Using RaspberryPi.GpioSim in your app
- How it works
- Command-line options
- Simulated inputs
- Using the simulator in unit tests
- Running on a real Raspberry Pi
- Repository layout
- Limitations
- License
Quick start
Requires the .NET 10 SDK.
git clone https://github.com/bryanjhogan/pi-simulator.git
cd pi-simulator
dotnet run --project samples/RaspberryPi.GpioSim.Demo
The demo app does four things:
- Blinks GPIO 24.
- Runs a light down GPIO 5, 6, 13, 19 and 26.
- Waits up to 30 seconds for a button on GPIO 17. Press
1to press the button. - Lights GPIO 24 for 2 seconds when the button is pressed.
Other ways to run it:
dotnet run --project samples/RaspberryPi.GpioSim.Demo -- --display log # scrolling log instead of the board
cd samples/RaspberryPi.GpioSim.Demo && dotnet run -- --script scripts/press-button.txt # a script presses the button
Using the simulator in your app
Add the RaspberryPi.GpioSim package to your app:
dotnet add package RaspberryPi.GpioSim
Then get your GpioController from GpioHost:
using System.Device.Gpio;
using RaspberryPi.GpioSim;
using var hw = GpioHost.Create(args); // real GPIO on a Pi, the simulator everywhere else
var gpio = hw.Controller; // a normal System.Device.Gpio.GpioController
gpio.OpenPin(24, PinMode.Output);
gpio.Write(24, PinValue.High);
await Task.Delay(1000);
gpio.Write(24, PinValue.Low);
From that line on, your code is plain GpioController code with nothing simulator-specific.
Print messages with Console.WriteLine as usual; the simulator's displays capture them and show
them among the pin events.
Dispose the host, not the controller. The host owns the controller and, in simulator mode, the display and inputs.
How it works
GpioHost chooses real or simulated
| Situation | Result |
|---|---|
--real passed |
real hardware: new GpioController() |
--simulate passed |
simulator |
| Neither flag, running on a Raspberry Pi | real hardware |
| Neither flag, anywhere else | simulator |
The Pi check reads /proc/device-tree/model and looks for "Raspberry Pi". It doesn't rely on
/dev/gpiochip* existing, because many ordinary Linux PCs have chipset GPIO controllers that show
up there too.
SimulatedGpioDriver stands in for the hardware
GpioController sends every call to a GpioDriver. The simulator is a GpioDriver subclass that
keeps each pin's mode and level in memory. It supports:
Output,Input,InputPullUpandInputPullDownmodes. A pull-up input reads High until something drives it Low.ReadandWrite. As on real hardware,GpioControllerignores a write to a pin that is set as an input.- Value-changed callbacks (
RegisterCallbackForPinValueChangedEvent) andWaitForEvent, which fire on rising and falling edges.
It also has an "outside world" API for displays, inputs and tests:
| Member | Purpose |
|---|---|
DriveInput(pin, value) |
drive an input High or Low; null releases it back to its pull-up or pull-down |
PressedLevel(pin) |
the level a button press produces: Low for a pull-up input, High otherwise |
GetLevel(pin) |
the current level of an open pin |
Snapshot() |
the mode and level of every open pin |
History |
every level change, with timestamps |
PinChanged, StateChanged |
events raised when anything changes |
Displays
--display |
What you see |
|---|---|
board (default in a terminal) |
a live drawing of the 40-pin header, redrawn in place, with recent events in a column to its right |
log (default when output is redirected to a file or pipe) |
a scrolling, timestamped, coloured log |
The board needs a terminal at least 74 columns wide to show the events column, and about 26 lines
tall to avoid scrolling. Lines that are too long are cut off with … so they don't wrap.
Colour turns off automatically when output is redirected or the NO_COLOR environment variable is
set.
Pin numbers
System.Device.Gpio uses GPIO (BCM) numbers, not physical pin numbers.
gpio.OpenPin(24, ...) means GPIO 24, which is physical pin 18 on the header.
RaspberryPi.GpioSim works the same way. The board and the log show both numbers (GPIO24 (18)),
so you can see where to connect the wire. PiHeader.PhysicalPinOf(gpio) gives you the mapping in
code.
Command-line options
| Option | Meaning |
|---|---|
--simulate |
force the simulator, even on a Pi |
--real |
force real hardware |
--display board\|log |
choose the simulator display |
--script <file> |
run a simulator input script |
GpioHost ignores options it doesn't recognise, so your app can have its own. With dotnet run,
put the options after --, for example dotnet run -- --display log.
Simulated inputs
Keyboard
Keys 1–9 are assigned to your app's open input pins in GPIO-number order. The board shows the
assignments ([1] GPIO17), and log mode prints them whenever they change. Each key press toggles
that button: the first press presses it, the next releases it.
Scripts
--script <file> plays a list of inputs. One command per line, and # starts a comment:
wait-open 17 # wait until the app opens GPIO 17
wait 1500 # pause 1500 ms
press 17 # press the button (Low for a pull-up input, High otherwise)
wait 300
release 17 # let go, back to the pull-up or pull-down
set 27 high # drive a pin to an exact level (high or low)
press and set wait for the app to open the pin first. The whole script is checked before the
app starts, and a mistake is reported with its line number. The keyboard and a script can be used
at the same time.
Using the simulator in unit tests
Reference RaspberryPi.GpioSim from your test project and use the driver directly:
var sim = new SimulatedGpioDriver();
using var gpio = new GpioController(sim);
gpio.OpenPin(24, PinMode.Output);
gpio.Write(24, PinValue.High);
Assert.Equal(PinValue.High, sim.GetLevel(24));
gpio.OpenPin(17, PinMode.InputPullUp);
bool pressed = false;
gpio.RegisterCallbackForPinValueChangedEvent(17, PinEventTypes.Falling, (_, _) => pressed = true);
sim.DriveInput(17, sim.PressedLevel(17)); // press the button
Assert.True(pressed);
Scripts work in tests too:
await SimScript.RunAsync(sim, SimScript.Parse(["press 17", "wait 100", "release 17"]));
See tests/RaspberryPi.GpioSim.Tests for more examples.
Running on a real Raspberry Pi
Option 1: leave the simulator in (simplest)
Nothing needs to change. On a Pi, GpioHost detects the hardware and uses the real driver, and the
simulator code is never used. Publish and copy the app:
# 64-bit Raspberry Pi OS; use linux-arm for 32-bit
dotnet publish samples/RaspberryPi.GpioSim.Demo -c Release -r linux-arm64 --self-contained -o publish
scp -r publish/ <user>@<your-pi>:~/gpio-app/
Then on the Pi run ~/gpio-app/RaspberryPi.GpioSim.Demo. Add --real if you want it to fail loudly instead of
ever falling back to the simulator.
On the Pi:
- libgpiod must be installed.
System.Device.Gpio4.2.0 talks to the GPIO chip through libgpiod and looks forlibgpiod.so.3(libgpiod v2) first. If you get aDllNotFoundExceptionmentioning libgpiod, install it (sudo apt install libgpiod3, orlibgpiod2on older releases) and check which version your OS provides. - Your user needs GPIO access. That normally means membership in the
gpiogroup, which the default user already has.
The only cost of this option is that RaspberryPi.GpioSim.dll is copied to the Pi along with your app.
Option 2: remove the simulator from the build entirely
If you don't want any simulator code on the Pi, make two small changes to your app:
Replace the host with a plain controller, and remove
using RaspberryPi.GpioSim;:// using var hw = GpioHost.Create(args); // var gpio = hw.Controller; using var gpio = new GpioController();In your
.csproj, replace theRaspberryPi.GpioSimpackage reference with a direct reference toSystem.Device.Gpio:<PackageReference Include="System.Device.Gpio" Version="4.2.0" />
The rest of your code is plain GpioController code and builds unchanged.
Repository layout
pi-simulator/
├── RaspberryPi.GpioSim.slnx solution
├── Directory.Build.props settings shared by all projects (net10.0, nullable, …)
├── src/RaspberryPi.GpioSim/ the library
│ ├── GpioHost.cs chooses real hardware or the simulator and sets it up
│ ├── SimulatedGpioDriver.cs the in-memory GPIO driver
│ ├── KeyboardInput.cs keys 1–9 press and release simulated buttons
│ ├── SimScript.cs script runner for simulated inputs
│ └── Display/ board and log displays, 40-pin header layout, colours
├── samples/RaspberryPi.GpioSim.Demo/ demo app
│ └── scripts/press-button.txt example input script
├── tests/RaspberryPi.GpioSim.Tests/ xUnit tests
└── .github/workflows/build.yml CI: build, test and pack on every push and pull request;
publish to nuget.org on v* tags
Build and test everything:
dotnet build
dotnet test
Limitations
The simulator models pin logic only:
- Timing is approximate (it follows
Task.Delayand thread scheduling, not hardware timing). - No PWM, I2C, SPI, UART or 1-Wire.
- No electrical behaviour (current, voltage levels, short circuits, switch bounce).
- A floating input (
PinMode.Inputwith nothing driving it) reads Low, whereas real hardware may read either value at random.
License
Released under the MIT License. You can copy, modify, use and distribute it for any purpose, as long as you keep the copyright and license notice.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- System.Device.Gpio (>= 4.2.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.