Skip to content

Commit

Permalink
Moving GPIO to separate library, add settings for buzzer, use gpio LE…
Browse files Browse the repository at this point in the history
…D to manage buzzer
  • Loading branch information
Didosa committed Jul 9, 2023
1 parent 037bbc6 commit 8732b81
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 26 deletions.
6 changes: 6 additions & 0 deletions PiLot.sln
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sql", "sql", "{B438012B-443
installScripts\sql\setup-db.sh = installScripts\sql\setup-db.sh
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PiLotGPIO", "PiLotGPIO\PiLotGPIO.csproj", "{5519B99B-064C-4271-A4B8-73100EEA7386}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -201,6 +203,10 @@ Global
{74031253-0084-4FD8-BBF4-15E5E26FF65C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{74031253-0084-4FD8-BBF4-15E5E26FF65C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{74031253-0084-4FD8-BBF4-15E5E26FF65C}.Release|Any CPU.Build.0 = Release|Any CPU
{5519B99B-064C-4271-A4B8-73100EEA7386}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5519B99B-064C-4271-A4B8-73100EEA7386}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5519B99B-064C-4271-A4B8-73100EEA7386}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5519B99B-064C-4271-A4B8-73100EEA7386}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 1 addition & 5 deletions PiLotAPICore/PiLotAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<ProjectReference Include="..\PiLotConfig\PiLotConfig.csproj" />
<ProjectReference Include="..\PiLotDataFiles\PiLotDataFiles.csproj" />
<ProjectReference Include="..\PiLotDataPostgres\PiLotDataPostgres.csproj" />
<ProjectReference Include="..\PiLotGPIO\PiLotGPIO.csproj" />
<ProjectReference Include="..\PiLotModel\PiLotModel.csproj" />
<ProjectReference Include="..\PiLotUtils\PiLotUtils.csproj" />
</ItemGroup>
Expand All @@ -21,9 +22,4 @@
<Folder Include="App_Data\photos\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Iot.Device.Bindings" Version="2.2.0" />
<PackageReference Include="System.Device.Gpio" Version="2.2.0" />
</ItemGroup>

</Project>
50 changes: 39 additions & 11 deletions PiLotAPICore/Workers/AnchorWatchWorker.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;
using System.Threading;
using System.Device.Gpio;

using PiLot.API.Helpers;
using PiLot.Config;
using PiLot.Data.Files;
using PiLot.GPIO.Devices;
using PiLot.Model.Nav;

namespace PiLot.API.Workers {
Expand All @@ -15,21 +16,19 @@ namespace PiLot.API.Workers {
public class AnchorWatchWorker {

private const String APPKEY = "anchorWatchWorker";
private const String BUZZERKEY = "buzzer";

private AnchorWatchDataConnector dataConnector = null;
private AnchorWatch anchorWatch = null;
private Boolean observingGps = false;
private Boolean alarmPlaying = false;

GpioController controller = null;
LED buzzer = null;

/// <summary>
/// Private constructor. The Instance accessor should be used
/// </summary>
private AnchorWatchWorker() {
this.controller = new GpioController(PinNumberingScheme.Logical);
this.controller.OpenPin(24, PinMode.Output);
this.controller.Write(24, PinValue.Low);
this.dataConnector = new AnchorWatchDataConnector();
this.LoadAnchorWatch();
}
Expand Down Expand Up @@ -113,6 +112,7 @@ private void EnsureObserveGps() {
private void StopObserveGps() {
this.observingGps = false;
GpsCache.Instance.PositionChanged -= this.GpsDataRecieved;
this.buzzer?.Dispose();
}

/// <summary>
Expand All @@ -126,25 +126,53 @@ private void GpsDataRecieved(GpsRecord pRecord) {
LatLon anchorLatLon = new LatLon(this.anchorWatch.Latitude, this.anchorWatch.Longitude);
if(positionLatLon.DistanceTo(anchorLatLon) > this.anchorWatch.Radius) {
this.PlayAlarm();
} else {
this.StopAlarm();
}
} catch(Exception ex) {
PiLot.Utils.Logger.Logger.Log(ex, "AnchorWatchWorker.GpsDataRecieved");
throw;
}
}

/// <summary>
/// This instantiates the buzzer. If there is no config, no buzzer will be
/// instantiated, and this return false.
/// </summary>
/// <returns>True, if a buzzer is configured, else false</returns>
private Boolean EnsureBuzzer() {
Boolean result = true;
if(this.buzzer == null) {
Int32? buzzerPin = new GPIOConfigReader().ReadSetting(BUZZERKEY);
if(buzzerPin != null) {
this.buzzer = new LED(null, buzzerPin.Value, ConnectionTypes.Ground);
} else {
result = false;
}
}
return result;
}

/// <summary>
/// Plays a sound on the passive buzzer connected to PIN 24 (logical 24, physical 18)
/// </summary>
private void PlayAlarm() {
if (!this.alarmPlaying) {
this.alarmPlaying = true;
this.controller.Write(24, PinValue.High);
Thread.Sleep(1000);
this.controller.Write(24, PinValue.Low);
Thread.Sleep(1000);
this.alarmPlaying = false;
if (this.EnsureBuzzer()) {
this.buzzer.Blink(500, 1000);
this.alarmPlaying = true;
}
}
}

/// <summary>
/// Stops the alarm
/// </summary>
private void StopAlarm() {
if (this.alarmPlaying) {
this.alarmPlaying = false;
this.buzzer.TurnOff();
}
}
}
}
1 change: 1 addition & 0 deletions PiLotAPICore/config/gpio.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"buzzer": 23}
73 changes: 73 additions & 0 deletions PiLotConfig/GPIO/GPIOConfigReader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;

using PiLot.Model.Sensors;
using PiLot.Utils.Logger;

namespace PiLot.Config {

/// <summary>
/// Config connector to read information about GPIO settings (what is connected to
/// which pin)
/// </summary>
public class GPIOConfigReader {

public const String FILENAME = "gpio.json";

/// <summary>
/// Default constructor
/// </summary>
public GPIOConfigReader() { }

/// <summary>
/// Reads a dictionary of key and pin numbers. Returns an empty dictionary, if
/// no settings file exists.
/// </summary>
public Dictionary<String, Int32> ReadGPIOSettings() {
Dictionary<String, Int32> result;
FileInfo gpioFile = this.GetGPIOFile();
if(gpioFile.Exists){
String fileContent = File.ReadAllText(gpioFile.FullName);
result = JsonSerializer.Deserialize<Dictionary<String, Int32>>(fileContent);
} else {
result = new Dictionary<String, Int32>();
}
return result;
}

/// <summary>
/// Reads a single setting for a key.
/// </summary>
/// <param name="pKey">The key to look for</param>
/// <returns>The assigned value (pin number) or null</returns>
public Int32? ReadSetting(String pKey) {
Int32? result = null;
if(this.ReadGPIOSettings().TryGetValue(pKey, out Int32 setting)) {
result = setting;
}
return result;
}

/// <summary>
/// returns the file containing the GPIO settings. Return null, if no
/// file exists
/// </summary>
private FileInfo GetGPIOFile() {
String configRoot = ConfigHelper.GetConfigDirectory();
String path = Path.Combine(configRoot, FILENAME);
FileInfo result = new FileInfo(path);
if(!result.Exists){
Logger.Log($"GPIO file not found at {path}", LogLevels.WARNING);
Dictionary<String, Int32> dict = new Dictionary<string, int>();
dict.Add("test", 99);
dict.Add("test2", 100);
string serialized = JsonSerializer.Serialize(dict);
File.WriteAllText(path, serialized );
}
return result;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Device.Gpio;
using System.Threading;

namespace PiLot.PowerAPI.Devices {
namespace PiLot.GPIO.Devices {

/// <summary>
/// This represents an ADC0832 and allows to read the values. Code is taken from
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Device.Gpio;

namespace PiLot.PowerAPI.Devices {
namespace PiLot.GPIO.Devices {

public class Button {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PiLot.PowerAPI.Devices {
namespace PiLot.GPIO.Devices {

/// <summary>
/// We have two connection types: Plus means the element sits between GPIO and 3.3v,
Expand Down
6 changes: 3 additions & 3 deletions PiLotPowerAPI/Devices/LED.cs → PiLotGPIO/Devices/LED.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Device.Gpio;
using System.Threading;

namespace PiLot.PowerAPI.Devices {
namespace PiLot.GPIO.Devices {

/// <summary>
/// Represents a LED, which can be turned on, off and blink
Expand All @@ -26,7 +26,7 @@ public class LED: IDisposable {
/// <summary>
/// Default constructor
/// </summary>
/// <param name="pController">Pleaese pass a GpioController</param>
/// <param name="pController">Pleaese pass a GpioController, if you have one at hand. Otherwise a new one with logical numbering will be created</param>
/// <param name="pPinNumber">The Pin number the LED is connected to. Use numbering scheme from pController</param>
/// <param name="pConnectionType">Plus: the LED is connected to a GPIO Pin and 3.3v, Ground: GPIO and GND</param>
public LED(GpioController pController, Int32 pPinNumber, ConnectionTypes pConnectionType) {
Expand All @@ -37,7 +37,7 @@ public LED(GpioController pController, Int32 pPinNumber, ConnectionTypes pConnec
this.valueOn = PinValue.High;
this.valueOff = PinValue.Low;
}
this.controller = pController;
this.controller = pController ?? new GpioController(PinNumberingScheme.Logical);
this.pinNumber = pPinNumber;
this.controller.OpenPin(this.pinNumber, PinMode.Output);
}
Expand Down
12 changes: 12 additions & 0 deletions PiLotGPIO/PiLotGPIO.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<AssemblyName>PiLot.GPIO</AssemblyName>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Device.Gpio" Version="2.2.0" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion PiLotPowerAPI/ChargeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System.Device.Gpio;
using System.Linq;
using System.Threading;
using PiLot.PowerAPI.Devices;
using PiLot.GPIO.Devices;
using PiLot.Utils.DateAndTime;

namespace PiLot.PowerAPI {
Expand Down
5 changes: 3 additions & 2 deletions PiLotPowerAPI/PiLotPowerAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Configuration.ConfigurationManager" Version="6.0.0" />
<PackageReference Include="System.Device.Gpio" Version="1.5.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="7.0.0" />
<PackageReference Include="System.Device.Gpio" Version="2.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PiLotGPIO\PiLotGPIO.csproj" />
<ProjectReference Include="..\PiLotUtils\PiLotUtils.csproj" />
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion PiLotPowerAPI/PowerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Device.Gpio;
using System.Threading;

using PiLot.PowerAPI.Devices;
using PiLot.GPIO.Devices;
using PiLot.Utils.Logger;
using PiLot.Utils.OS;

Expand Down

0 comments on commit 8732b81

Please sign in to comment.