-
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathProgram.cs
72 lines (62 loc) · 2.41 KB
/
Program.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
using nanoFramework.Hardware.Esp32;
using System;
using System.Device.Gpio;
using System.Device.Pwm;
using System.Diagnostics;
using System.Threading;
namespace TestEsp32Counter
{
public class Program
{
public static void Main()
{
Debug.WriteLine("Hello pulse counter!");
Configuration.SetPinFunction(27, DeviceFunction.PWM1);
PwmChannel pwm = PwmChannel.CreateFromPin(27, 1, 0.5);
pwm.Start();
GpioPulseCounter counter = new GpioPulseCounter(26);
Debug.Write("Starting PWM and Counter. With 1 Hz and counting on rising");
counter.Polarity = GpioPulsePolarity.Rising;
counter.FilterPulses = 0;
counter.Start();
int inc = 0;
GpioPulseCount counterCount;
while (inc++ < 20)
{
counterCount = counter.Read();
Console.WriteLine($"{counterCount.RelativeTime}: {counterCount.Count}");
Thread.Sleep(1000);
}
counterCount = counter.Reset();
Console.WriteLine($"{counterCount.RelativeTime}: {counterCount.Count}");
Console.WriteLine("Reset read, that should then restart at 0");
inc = 0;
while (inc++ < 20)
{
counterCount = counter.Read();
Console.WriteLine($"{counterCount.RelativeTime}: {counterCount.Count}");
Thread.Sleep(1000);
}
counter.Stop();
counter.Dispose();
pwm.Stop();
Console.WriteLine("Using 2 pins for a rotary encoder, changing the direction will show increase and decreas in the count.");
GpioPulseCounter encoder = new GpioPulseCounter(12, 14);
encoder.Polarity = GpioPulsePolarity.Rising;
encoder.Start();
int incEncod = 0;
GpioPulseCount counterCountEncode;
while (incEncod++ < 20)
{
counterCountEncode = encoder.Read();
Console.WriteLine($"{counterCountEncode.RelativeTime}: {counterCountEncode.Count}");
Thread.Sleep(1000);
}
encoder.Stop();
encoder.Dispose();
Thread.Sleep(Timeout.Infinite);
}
}
}