Skip to content
This repository was archived by the owner on Aug 10, 2022. It is now read-only.

Commit b8ad806

Browse files
authored
Version 0.14.0 (#39)
* Code Review * Update nugets * Final review * Versioning * Adding generic button peripheral * Setup sshdeploy
1 parent 48abfd2 commit b8ad806

File tree

6 files changed

+134
-33
lines changed

6 files changed

+134
-33
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
namespace Unosquare.RaspberryIO.Peripherals
2+
{
3+
using System;
4+
using Gpio;
5+
using Native;
6+
7+
/// <summary>
8+
/// Implements a generic button attached to the GPIO
9+
/// </summary>
10+
public class Button
11+
{
12+
internal const ulong InterruptTime = 500;
13+
14+
private readonly GpioPin _gpioPin;
15+
private ulong _pressedLastInterrupt;
16+
private ulong _releasedLastInterrupt;
17+
18+
/// <summary>
19+
/// Initializes a new instance of the <see cref="Button"/> class.
20+
/// </summary>
21+
/// <param name="gpioPin">The gpio pin.</param>
22+
public Button(GpioPin gpioPin)
23+
{
24+
_gpioPin = gpioPin;
25+
26+
_gpioPin.InputPullMode = GpioPinResistorPullMode.PullDown;
27+
_gpioPin.PinMode = GpioPinDriveMode.Input;
28+
_gpioPin.RegisterInterruptCallback(EdgeDetection.RisingAndFallingEdges, HandleInterrupt);
29+
}
30+
31+
/// <summary>
32+
/// Occurs when [pressed].
33+
/// </summary>
34+
public event EventHandler<EventArgs> Pressed;
35+
36+
/// <summary>
37+
/// Occurs when [released].
38+
/// </summary>
39+
public event EventHandler<EventArgs> Released;
40+
41+
private void HandleInterrupt()
42+
{
43+
if (_gpioPin.Read())
44+
{
45+
HandleButtonPressed();
46+
}
47+
else
48+
{
49+
HandleButtonReleased();
50+
}
51+
}
52+
53+
private void HandleButtonPressed()
54+
{
55+
ulong interruptTime = WiringPi.Millis();
56+
57+
if (interruptTime - _pressedLastInterrupt <= InterruptTime) return;
58+
_pressedLastInterrupt = interruptTime;
59+
Pressed?.Invoke(this, new EventArgs());
60+
}
61+
62+
private void HandleButtonReleased()
63+
{
64+
ulong interruptTime = WiringPi.Millis();
65+
66+
if (interruptTime - _releasedLastInterrupt <= InterruptTime) return;
67+
_releasedLastInterrupt = interruptTime;
68+
Released?.Invoke(this, new EventArgs());
69+
}
70+
}
71+
}

src/Unosquare.RaspberryIO.Peripherals/RFIDControllerMfrc522.cs

+11-11
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class RFIDControllerMfrc522
1818

1919
#region Private Members
2020

21-
private readonly SpiChannel SpiPort = null;
22-
private readonly GpioPin OutputPort = null;
21+
private readonly SpiChannel _spiPort;
22+
private readonly GpioPin _outputPort;
2323

2424
#endregion
2525

@@ -43,8 +43,8 @@ public RFIDControllerMfrc522()
4343
public RFIDControllerMfrc522(SpiChannel spiPort, int spiFrquency, GpioPin outputPort)
4444
{
4545
Pi.Spi.Channel0Frequency = spiFrquency;
46-
SpiPort = spiPort;
47-
OutputPort = outputPort;
46+
_spiPort = spiPort;
47+
_outputPort = outputPort;
4848
InitializeComponent();
4949
}
5050

@@ -551,7 +551,7 @@ public byte ReadRegister(Register register)
551551
{
552552
var address = (byte)register;
553553
var addressPayload = (byte)(((address << 1) & 0x7E) | 0x80);
554-
var readValue = SpiPort.SendReceive(new byte[] { addressPayload, 0 });
554+
var readValue = _spiPort.SendReceive(new byte[] { addressPayload, 0 });
555555
return readValue[1];
556556
}
557557

@@ -563,7 +563,7 @@ public byte ReadRegister(Register register)
563563
/// <returns>A standard controller response</returns>
564564
public RfidResponse CardSendData(Command command, byte[] sendData)
565565
{
566-
const byte MaximumLength = 16;
566+
const byte maximumLength = 16;
567567

568568
var backData = new List<byte>();
569569
byte receivedBitCount = 0;
@@ -640,8 +640,8 @@ public RfidResponse CardSendData(Command command, byte[] sendData)
640640
if (currentRegisterValue == 0)
641641
currentRegisterValue = 1;
642642

643-
if (currentRegisterValue > MaximumLength)
644-
currentRegisterValue = MaximumLength;
643+
if (currentRegisterValue > maximumLength)
644+
currentRegisterValue = maximumLength;
645645

646646
i = 0;
647647
while (i < currentRegisterValue)
@@ -875,8 +875,8 @@ public byte[] DumpRegisters(byte[] authKey, byte[] cardUniqueId)
875875
/// </summary>
876876
private void InitializeComponent()
877877
{
878-
OutputPort.PinMode = GpioPinDriveMode.Output;
879-
OutputPort.Write(true);
878+
_outputPort.PinMode = GpioPinDriveMode.Output;
879+
_outputPort.Write(true);
880880

881881
Reset();
882882

@@ -897,7 +897,7 @@ private void InitializeComponent()
897897
/// <param name="value">The value.</param>
898898
private void WriteRegister(byte register, byte value)
899899
{
900-
SpiPort.Write(new byte[]
900+
_spiPort.Write(new byte[]
901901
{
902902
(byte)((register << 1) & 0x7E),
903903
value

src/Unosquare.RaspberryIO.Peripherals/Unosquare.RaspberryIO.Peripherals.csproj

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
<TargetFrameworks>net452;netstandard2.0</TargetFrameworks>
77
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
88
<LangVersion>7.1</LangVersion>
9-
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
10-
<Version>0.1.0</Version>
11-
<Company>Unosquare</Company>
12-
<Description>A set of classes for peripherals working with RaspberryIO.</Description>
9+
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
10+
<Version>0.1.0</Version>
11+
<Company>Unosquare</Company>
12+
<Description>A set of classes for peripherals working with RaspberryIO.</Description>
1313
</PropertyGroup>
1414

1515
<ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Unosquare.RaspberryIO.Playground
2+
{
3+
using Gpio;
4+
using Peripherals;
5+
using Swan;
6+
using System;
7+
8+
public partial class Program
9+
{
10+
/// <summary>
11+
/// Tests the button.
12+
/// </summary>
13+
public static void TestButton()
14+
{
15+
var relayChannel1 = Pi.Gpio.GetGpioPinByBcmPinNumber(17);
16+
relayChannel1.PinMode = GpioPinDriveMode.Output;
17+
18+
var buttonPin = Pi.Gpio.GetGpioPinByBcmPinNumber(26);
19+
buttonPin.PinMode = GpioPinDriveMode.Input;
20+
21+
var button = new Button(buttonPin);
22+
button.Pressed += (s, e) => { "Pressed".Info(); };
23+
24+
button.Released += (s, e) =>
25+
{
26+
"Released".Info();
27+
var value = relayChannel1.Read();
28+
relayChannel1.Write(!value);
29+
};
30+
31+
Console.ReadKey();
32+
}
33+
}
34+
}

src/Unosquare.RaspberryIO.Playground/Unosquare.RaspberryIO.Playground.csproj

+7-18
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,17 @@
66
<OutputType>Exe</OutputType>
77
<PackageId>Unosquare.RaspberryIO.Playground</PackageId>
88
<CodeAnalysisRuleSet>..\..\StyleCop.Analyzers.ruleset</CodeAnalysisRuleSet>
9-
<RuntimeIdentifiers>linux-arm;win10-x64;ubuntu.14.04-arm;ubuntu.16.04-arm</RuntimeIdentifiers>
10-
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
11-
</PropertyGroup>
12-
13-
<ItemGroup>
14-
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
15-
</ItemGroup>
16-
17-
<PropertyGroup>
18-
<SshDeployHost>10.168.1.112</SshDeployHost>
9+
<SshDeployHost>172.16.17.138</SshDeployHost>
10+
<SshDeployClean />
1911
<SshDeployTargetPath>/home/pi/rpio-playgound</SshDeployTargetPath>
2012
<SshDeployUsername>pi</SshDeployUsername>
2113
<SshDeployPassword>raspberry</SshDeployPassword>
14+
<SshDeployPostCommand>sudo dotnet /home/pi/rpio-playgound/Unosquare.RaspberryIO.Playground.dll</SshDeployPostCommand>
2215
</PropertyGroup>
2316

17+
<ItemGroup>
18+
<PackageReference Include="StyleCop.Analyzers" Version="1.0.2" />
19+
</ItemGroup>
2420
<ItemGroup>
2521
<None Update="fractal.jpg;spectrum.png">
2622
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
@@ -38,13 +34,6 @@
3834
</ItemGroup>
3935

4036
<ItemGroup>
41-
<DotNetCliToolReference Include="dotnet-sshdeploy" Version="0.1.6-r1" />
37+
<DotNetCliToolReference Include="dotnet-sshdeploy" Version="0.2.0" />
4238
</ItemGroup>
43-
44-
<ItemGroup Condition="'$(TargetFramework)' == 'netcoreapp2.0'">
45-
<PackageReference Include="dotnet-sshdeploy">
46-
<Version>0.2.0</Version>
47-
</PackageReference>
48-
</ItemGroup>
49-
5039
</Project>

src/Unosquare.RaspberryIO/Gpio/GpioController.cs

+7
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,13 @@ IEnumerator IEnumerable.GetEnumerator()
499499

500500
#region Helper and Init Methods
501501

502+
/// <summary>
503+
/// Gets the GPIO pin by BCM pin number.
504+
/// </summary>
505+
/// <param name="bcmPinNumber">The BCM pin number.</param>
506+
/// <returns>The GPIO pin</returns>
507+
public GpioPin GetGpioPinByBcmPinNumber(int bcmPinNumber) => this.First(pin => pin.BcmPinNumber == bcmPinNumber);
508+
502509
/// <summary>
503510
/// Converts the Wirings Pi pin number to the BCM pin number.
504511
/// </summary>

0 commit comments

Comments
 (0)