forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiquidLevelSwitch.cs
58 lines (50 loc) · 2.19 KB
/
LiquidLevelSwitch.cs
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.Gpio;
using System.Device.Model;
namespace Iot.Device.LiquidLevel
{
/// <summary>
/// Digital liquid level switch
/// </summary>
[Interface("Digital liquid level switch")]
public class LiquidLevelSwitch : IDisposable
{
private readonly int _dataPin;
private readonly PinValue _liquidPresentPinState;
private readonly bool _shouldDispose;
private GpioController _controller;
/// <summary>Creates a new instance of the LiquidLevelSwitch.</summary>
/// <param name="dataPin">The data pin</param>
/// <param name="liquidPresentPinState">Data pin state representing liquid being present</param>
/// <param name="pinNumberingScheme">Use the logical or physical pin layout</param>
/// <param name="gpioController">A Gpio Controller if you want to use a specific one</param>
/// <param name="shouldDispose">True to dispose the Gpio Controller</param>
public LiquidLevelSwitch(int dataPin, PinValue liquidPresentPinState, GpioController? gpioController = null, PinNumberingScheme pinNumberingScheme = PinNumberingScheme.Logical, bool shouldDispose = true)
{
_controller = gpioController ?? new GpioController(pinNumberingScheme);
_shouldDispose = shouldDispose || gpioController is null;
_dataPin = dataPin;
_liquidPresentPinState = liquidPresentPinState;
_controller.OpenPin(_dataPin, PinMode.Input);
}
/// <summary>
/// Determines whether liquid is present.
/// </summary>
/// <returns><code>true</code> if liquid is present, otherwise <code>false</code>.</returns>
[Telemetry]
public bool IsLiquidPresent() => _controller.Read(_dataPin) == _liquidPresentPinState;
/// <summary>
/// Dispose Buzzer.
/// </summary>
public void Dispose()
{
if (_shouldDispose)
{
_controller?.Dispose();
_controller = null!;
}
}
}
}