Compare commits
3 commits
0d715cf823
...
9d65315b48
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9d65315b48 | ||
|
|
c2c23f3cc5 | ||
|
|
42c3319709 |
|
|
@ -3,12 +3,10 @@ using System.Text.Json.Serialization;
|
|||
|
||||
namespace Proculite.GpioRest.Models
|
||||
{
|
||||
public class PinValueModel(int pinNumber, PinValue pinValue)
|
||||
public class PinValueModel(int pinNumber, double value)
|
||||
{
|
||||
public int PinNumber { get; set; } = pinNumber;
|
||||
|
||||
[JsonIgnore]
|
||||
public PinValue PinValue { get; set; } = pinValue;
|
||||
public int Value => PinValue == PinValue.High ? 1 : 0;
|
||||
public double PinValue { get; set; } = value;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Iot.Device.Bindings" Version="3.2.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.7" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="System.Device.Gpio" Version="3.2.0" />
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System.Device.Gpio;
|
||||
using Proculite.GpioRest.Models;
|
||||
using Proculite.GpioRest.Wrappers;
|
||||
|
||||
namespace Proculite.GpioRest.Services
|
||||
{
|
||||
|
|
@ -7,7 +8,7 @@ namespace Proculite.GpioRest.Services
|
|||
{
|
||||
public readonly ILogger<GpioService> _logger;
|
||||
private readonly int[] _pins;
|
||||
private readonly GpioController _gpioController;
|
||||
private readonly Dictionary<int, PwmWrapper> _pwmPins;
|
||||
|
||||
public GpioService(ILogger<GpioService> logger, IConfiguration configuration)
|
||||
{
|
||||
|
|
@ -18,7 +19,6 @@ namespace Proculite.GpioRest.Services
|
|||
.Select(pin => pin.Get<int>())
|
||||
.ToArray();
|
||||
|
||||
_gpioController = new GpioController();
|
||||
SetupPins();
|
||||
}
|
||||
|
||||
|
|
@ -29,29 +29,18 @@ namespace Proculite.GpioRest.Services
|
|||
|
||||
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);
|
||||
_pwmPins[pin] = new PwmWrapper(pin);
|
||||
}
|
||||
}
|
||||
|
||||
public PinValue CurrentPinValue(int pinNumber)
|
||||
public PinValueModel CurrentPinValue(int pinNumber)
|
||||
{
|
||||
return _gpioController.Read(pinNumber);
|
||||
return new PinValueModel(pinNumber, _pwmPins[pinNumber].Value);
|
||||
}
|
||||
|
||||
public PinValueModel PinValueModelOfPin(int pinNumber)
|
||||
{
|
||||
return new PinValueModel(pinNumber, _gpioController.Read(pinNumber));
|
||||
return new PinValueModel(pinNumber, _pwmPins[pinNumber].Value);
|
||||
}
|
||||
|
||||
public PinValueModel[] StateOfAllPins()
|
||||
|
|
@ -61,19 +50,18 @@ namespace Proculite.GpioRest.Services
|
|||
|
||||
public void SetPinHigh(int pinNumber)
|
||||
{
|
||||
_gpioController.Write(pinNumber, PinValue.High);
|
||||
_pwmPins[pinNumber].SetHigh();
|
||||
}
|
||||
|
||||
public void SetPinLow(int pinNumber)
|
||||
{
|
||||
_gpioController.Write(pinNumber, PinValue.Low);
|
||||
_pwmPins[pinNumber].SetLow();
|
||||
}
|
||||
|
||||
public void TogglePin(int pinNumber)
|
||||
{
|
||||
PinValue currentValue = CurrentPinValue(pinNumber);
|
||||
PinValue newValue = currentValue == PinValue.High ? PinValue.Low : PinValue.High;
|
||||
_gpioController.Write(pinNumber, newValue);
|
||||
double currentValue = _pwmPins[pinNumber].Value;
|
||||
_pwmPins[pinNumber].Value = currentValue > 0 ? 0 : 1;
|
||||
}
|
||||
|
||||
public PinValueModel SetPinHighReturning(int pinNumber)
|
||||
|
|
|
|||
64
Proculite.GpioRest/Wrappers/PwmWrapper.cs
Normal file
64
Proculite.GpioRest/Wrappers/PwmWrapper.cs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
using System.Device.Pwm.Drivers;
|
||||
|
||||
namespace Proculite.GpioRest.Wrappers
|
||||
{
|
||||
public class PwmWrapper
|
||||
{
|
||||
private readonly SoftwarePwmChannel _softwarePwmChannel;
|
||||
private readonly ILogger? _logger;
|
||||
public int PinNumber { get; }
|
||||
public double Value
|
||||
{
|
||||
get => _softwarePwmChannel.DutyCycle;
|
||||
set { _softwarePwmChannel.DutyCycle = value; }
|
||||
}
|
||||
|
||||
public PwmWrapper(int pinNumber, ILogger? logger = null)
|
||||
{
|
||||
PinNumber = pinNumber;
|
||||
_softwarePwmChannel = new SoftwarePwmChannel(pinNumber, usePrecisionTimer: true);
|
||||
_logger = logger;
|
||||
_softwarePwmChannel.DutyCycle = 0;
|
||||
_softwarePwmChannel.Start();
|
||||
}
|
||||
|
||||
public void SetValue(double value)
|
||||
{
|
||||
LogValue(value);
|
||||
if (value < 0 || value > 1)
|
||||
{
|
||||
LogValueOutOfBounds();
|
||||
return;
|
||||
}
|
||||
|
||||
_softwarePwmChannel.DutyCycle = value;
|
||||
}
|
||||
|
||||
public void SetHigh()
|
||||
{
|
||||
SetValue(1);
|
||||
}
|
||||
|
||||
public void SetLow()
|
||||
{
|
||||
SetValue(0);
|
||||
}
|
||||
|
||||
private void LogValue(double value)
|
||||
{
|
||||
if (_logger is null)
|
||||
return;
|
||||
_logger.LogDebug("New value for pin {PinNumber}: {Value}", PinNumber, value);
|
||||
}
|
||||
|
||||
private void LogValueOutOfBounds()
|
||||
{
|
||||
if (_logger is null)
|
||||
return;
|
||||
_logger.LogWarning(
|
||||
"Attempting to set value of pin {PinNumber}, but it's not between 0 and 1.",
|
||||
PinNumber
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue