Pin setup.

This commit is contained in:
Filip Strajnar 2024-09-24 23:44:41 +02:00
parent fdfb0ea2ef
commit 96c7e17da0
3 changed files with 42 additions and 4 deletions

View file

@ -1,11 +1,46 @@
using System.Device.Gpio;
namespace Proculite.GpioRest.Services namespace Proculite.GpioRest.Services
{ {
public class GpioService public class GpioService
{ {
public readonly ILogger<GpioService> _logger; public readonly ILogger<GpioService> _logger;
public GpioService(ILogger<GpioService> logger) private readonly int[] _pins;
private readonly GpioController _gpioController;
public GpioService(ILogger<GpioService> logger, IConfiguration configuration)
{ {
_logger = logger; _logger = logger;
_pins = configuration
.GetSection("Pins")
.GetChildren()
.Select(pin => pin.Get<int>())
.ToArray();
_gpioController = new GpioController();
SetupPins();
}
private void SetupPins()
{
_logger.LogInformation("Setting up {PinCount} pins", _pins.Length);
PinMode pinMode = PinMode.Input;
foreach (var pin in _pins)
{
if (_gpioController.IsPinOpen(pin))
{
_logger.LogWarning("Pin number {Pin} is already open.", pin);
continue;
}
if (!_gpioController.IsPinModeSupported(pin, pinMode))
{
_logger.LogWarning("Pin number {Pin} does not support {PinMode}", pin, pinMode);
}
_gpioController.OpenPin(pin, pinMode, PinValue.Low);
}
} }
} }
} }

View file

@ -5,5 +5,8 @@
"Microsoft.AspNetCore": "Warning" "Microsoft.AspNetCore": "Warning"
} }
}, },
"AllowedHosts": "*" "AllowedHosts": "*",
"Pins": [
1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26
]
} }