Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core 2.1 GPIO filesystemwatcher on Raspberry Pi

I have written up a .Net Core 2.1 application for my raspberry pi, but having an issue with filesystemwatcher not calling a event handler when the GPIO is driven high/low via a button.

static void Main(string[] args)
{
    Console.WriteLine("Hello World!");

    GPIO.PinMode(21, GPIO.Direction.Input, GPIO.Edge.Both);


    var fileSystemWatcher = new FileSystemWatcher();

    // Associate event handlers with the events
    fileSystemWatcher.Created += FileSystemWatcher_Created;
    fileSystemWatcher.Changed += FileSystemWatcher_Changed;
    fileSystemWatcher.Deleted += FileSystemWatcher_Deleted;
    fileSystemWatcher.Renamed += FileSystemWatcher_Renamed;

    // tell the watcher where to look
    fileSystemWatcher.Path = "/sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21";

    // You must add this line - this allows events to fire.
    fileSystemWatcher.EnableRaisingEvents = true;


    Console.ReadLine();

}

private static void FileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
    Console.WriteLine($"A new file has been renamed from {e.OldName} to {e.Name}");
}

private static void FileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
    Console.WriteLine($"A new file has been deleted - {e.Name}");
}

private static void FileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
{
    Console.WriteLine($"A new file has been changed - {e.Name}");
}

private static void FileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
    Console.WriteLine($"A new file has been created - {e.Name}");
}

I am aware the filesystemwatcher does not function on symlinks at this point in time, hence using the full system path.

Basically none of the events are called when GPIO 21 physically changes state.

I have confirmed that the value is changing using:

cat /sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21/value

However, the above events do get called when I edit the files in that directory e.g When I issue commands from another bash terminal:

echo out > /sys/devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio21/direction

I get from my running program:

A new file has been changed - direction

So has anyone gotten GPIO inputs to call a filesystemwatcher event?

like image 560
Lawrence Lee Avatar asked Nov 07 '22 06:11

Lawrence Lee


1 Answers

Firs of all yes, .NET Core FileSystemWatcher seems to have no support for checking changes in Linux sysfs, I tried the same for entries in "/dev/input" and got the same result.

Second, for your problem, GPIO control, use this .NET Foundation project: https://github.com/dotnet/iot

dotnet/iot is maintened by Microsoft employees, and the .NET Foundation community, and should become the default API for GPIO, PWM, i2c controller in .NET Core ...

And start using libgpiod on Linux, dotnet/iot already has LibGpiodDriver. The /sys/class/gpio is deprecated and the driver that controls these sysfs will be removed from the Linux kernel by 2020: https://www.kernel.org/doc/Documentation/ABI/obsolete/sysfs-gpio

like image 136
Matheus Castello Avatar answered Nov 15 '22 06:11

Matheus Castello