diff --git a/Proculite.GpioRest/Controllers/BasicController.cs b/Proculite.GpioRest/Controllers/BasicController.cs index a9f5df0..1815f17 100644 --- a/Proculite.GpioRest/Controllers/BasicController.cs +++ b/Proculite.GpioRest/Controllers/BasicController.cs @@ -10,4 +10,4 @@ namespace Proculite.GpioRest.Controllers return Ok("Pong"); } } -} \ No newline at end of file +} diff --git a/Proculite.GpioRest/Services/GpioService.cs b/Proculite.GpioRest/Services/GpioService.cs index d1c5d80..b2e89bb 100644 --- a/Proculite.GpioRest/Services/GpioService.cs +++ b/Proculite.GpioRest/Services/GpioService.cs @@ -1,11 +1,46 @@ +using System.Device.Gpio; + namespace Proculite.GpioRest.Services { public class GpioService { public readonly ILogger _logger; - public GpioService(ILogger logger) + private readonly int[] _pins; + private readonly GpioController _gpioController; + + public GpioService(ILogger logger, IConfiguration configuration) { _logger = logger; + _pins = configuration + .GetSection("Pins") + .GetChildren() + .Select(pin => pin.Get()) + .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); + } } } -} \ No newline at end of file +} diff --git a/Proculite.GpioRest/appsettings.json b/Proculite.GpioRest/appsettings.json index 4d56694..2f2b67a 100644 --- a/Proculite.GpioRest/appsettings.json +++ b/Proculite.GpioRest/appsettings.json @@ -5,5 +5,8 @@ "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 + ] }