-
Notifications
You must be signed in to change notification settings - Fork 133
Expand file tree
/
Copy pathProgram.cs
More file actions
157 lines (137 loc) · 5.27 KB
/
Program.cs
File metadata and controls
157 lines (137 loc) · 5.27 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using Raspberry.IO.Components.Sensors.Distance.HcSr04;
using Raspberry.IO.GeneralPurpose;
using UnitsNet;
using System.Threading;
namespace Test.Components.Sensors.Distance.HcSr04
{
class Program
{
private static GpioConnectionDriver driver;
private static HcSr04Connection sensor;
private static GpioOutputBinaryPin triggerGpioPin;
private static GpioInputBinaryPin echoGpioPin;
private static int triggerGpioPinSetting = 0;
private static int echoGpioPinSetting = 0;
private static int delay = 500;
private static int readError = 0;
static void Main(string[] args)
{
//Configure GPIO pins bellow or provide in console
//triggerGpioPinSetting = 20;
//echoGpioPinSetting = 26;
//Reads GPIO pins from console
if (triggerGpioPinSetting == 0)
triggerGpioPinSetting = GetGpioPin("trigger");
if (echoGpioPinSetting == 0)
echoGpioPinSetting = GetGpioPin("echo");
Console.CursorVisible = false;
//Creating sensor
driver = new GpioConnectionDriver();
triggerGpioPin = new GpioOutputBinaryPin(driver, (ProcessorPin)triggerGpioPinSetting);
echoGpioPin = new GpioInputBinaryPin(driver, (ProcessorPin)echoGpioPinSetting);
sensor = new HcSr04Connection(triggerGpioPin, echoGpioPin);
while(true)
{
if (Console.KeyAvailable)
KeyPressed();
try
{
//Get data from sensor and print them
Length distance = sensor.GetDistance();
PrintHeader();
PrintData(distance);
PrintFooter();
}
catch
{
readError++;
}
Thread.Sleep(delay);
}
}
/// <summary>
/// Console read key support
/// </summary>
private static void KeyPressed()
{
ConsoleKeyInfo cki = Console.ReadKey();
switch (cki.Key)
{
case ConsoleKey.UpArrow:
delay = delay > 50 ? delay - 50 : delay;
break;
case ConsoleKey.DownArrow:
delay = delay < 1000 ? delay + 50 : delay;
break;
case ConsoleKey.Escape:
Environment.Exit(0);
break;
}
if (Console.KeyAvailable)
KeyPressed();
}
/// <summary>
/// Get GPIO pins from console
/// </summary>
/// <param name="pinName">GPIO pin usage</param>
/// <param name="message">Error message</param>
/// <returns></returns>
private static int GetGpioPin(string pinName, string message = "")
{
PrintHeader();
Console.WriteLine();
if (!string.IsNullOrEmpty(message))
{
Console.WriteLine(message);
Console.WriteLine();
}
Console.Write(string.Format(" Please enter GPIO pin number for {0} pin: ", pinName));
string answer = Console.ReadLine();
int pin;
bool result = int.TryParse(answer, out pin);
if(result)
{
if (pin > 32)
return GetGpioPin(pinName, string.Format(" Error. Value {0} is to big", pin));
return pin;
}
else
{
return GetGpioPin(string.Format(pinName, "Error. Unable parse string {0}", answer));
}
}
#region Print Methods
private static void PrintHeader()
{
Console.Clear();
Console.WriteLine();
Console.WriteLine(" #################################");
Console.WriteLine(" # Demo pogram for sensor HCSR04 #");
Console.WriteLine(" #################################");
Console.WriteLine();
if(triggerGpioPinSetting != 0)
Console.WriteLine(" Trigger pin GPIO{0}", triggerGpioPinSetting);
if(echoGpioPinSetting != 0)
Console.WriteLine(" Echo pin GPIO{0}", echoGpioPinSetting);
}
private static void PrintData(Length length)
{
Console.WriteLine(" Reads {0:0.0} per secound [Hz]", 1000.0 / delay);
Console.WriteLine();
Console.WriteLine(" Read errors {0}", readError);
Console.WriteLine(" Length {0,5:0.0} cm", length.Centimeters);
Console.WriteLine(" Length {0,5:0.0} inch", length.Inches);
}
private static void PrintFooter()
{
if (Console.WindowHeight < 17)
return;
Console.SetCursorPosition(0, Console.WindowHeight - 4);
Console.WriteLine(" Press arrow up to increase read sppead");
Console.WriteLine(" Press arrow down to decrease read sppead");
Console.WriteLine(" Press escape key to exit");
}
#endregion
}
}