Skip to content

Commit c17193d

Browse files
authored
Samples update for GpioChangeCounter (#22)
1 parent eb6d563 commit c17193d

16 files changed

+351
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Feel free to browse, take what you like and contribute back if you want.
3535
<tr>
3636
<td><a href="samples/ADC">ADC</a></td>
3737
<td><a href="samples/1-Wire">1-Wire</a></td>
38-
<td><a href="samples/Gpio%2BEvents">Gpio+Events Test</a></td>
38+
<td><a href="samples/Gpio">Gpio</a></td>
3939
</tr>
4040
<tr>
4141
<td><a href="samples/I2C">I2C</a></td>

README.zh-cn.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<tr>
3636
<td><a href="samples/ADC">ADC</a></td>
3737
<td><a href="samples/1-Wire">1-Wire</a></td>
38-
<td><a href="samples/Gpio%2BEvents">Gpio+Events</a></td>
38+
<td><a href="samples/Gpio">Gpio</a></td>
3939
</tr>
4040
<tr>
4141
<td><a href="samples/I2C">I2C</a></td>
File renamed without changes.

samples/Gpio+Events/README.md renamed to samples/Gpio/Gpio+Events/README.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,42 @@
22

33
Shows how to use the [Windows.Devices.Gpio]((http://docs.nanoframework.net/api/Windows.Devices.Gpio.html) API allowing your to set and read the state of GPIO pins.
44

5-
> **Note:** This sample is part of a large collection of nanoFramework feature samples.
6-
> If you are unfamiliar with Git and GitHub, you can download the entire collection as a
7-
> [ZIP file](https://github.com/nanoframework/Samples/archive/master.zip), but be
8-
> sure to unzip everything to access any shared dependencies.
5+
6+
> **Note:** This sample is part of a large collection of nanoFramework feature samples.
7+
> If you are unfamiliar with Git and GitHub, you can download the entire collection as a
8+
> [ZIP file](https://github.com/nanoframework/Samples/archive/master.zip), but be
9+
> sure to unzip everything to access any shared dependencies.
910
<!-- For more info on working with the ZIP file,
1011
> the samples collection, and GitHub, see [Get the UWP samples from GitHub](https://aka.ms/ovu2uq).
1112
> For more samples, see the [Samples portal](https://aka.ms/winsamples) on the Windows Dev Center. -->
1213

13-
Shows how to use the [Windows.Devices.Gpio]() API allowing your to:
1414

15-
* set the state of output GPIO pins
15+
Shows how to use the [Windows.Devices.Gpio]() API allowing your to:
16+
* set the state of output GPIO pins
1617
* read state of input GPIO pins
1718
* setup handlers to react to GPIO state change events on external stimulus
1819

20+
1921
## Hardware requirements
2022

2123
Any hardware device running a nanoFramework image built with GPIO support enabled.
2224

25+
2326
## Related topics
2427

28+
### Samples
29+
30+
[GPIO and events sample](/Gpio+Events)
31+
2532
### Reference
2633

27-
* [Windows.Devices.Gpio](http://docs.nanoframework.net/api/Windows.Devices.Gpio.html)
34+
[Windows.Devices.Gpio](http://docs.nanoframework.net/api/Windows.Devices.Gpio.html)
35+
36+
<!-- [nanoFramework app samples]() -->
37+
38+
## System requirements
39+
40+
**Client:** Windows 10
2841

2942
## Build the sample
3043

@@ -39,8 +52,8 @@ The next steps depend on whether you just want to deploy the sample or you want
3952

4053
### Deploying the sample
4154

42-
- Select Build > Deploy Solution.
55+
- Select Build > Deploy Solution.
4356

4457
### Deploying and running the sample
4558

46-
- To debug the sample and then run it, press F5 or select Debug > Start Debugging.
59+
- To debug the sample and then run it, press F5 or select Debug > Start Debugging. To run the sample without debugging, press Ctrl+F5 or selectDebug > Start Without Debugging.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.421
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "GpioChangeCounter", "GpioChangeCounter\GpioChangeCounter.nfproj", "{4FD0EEFE-B90A-43E7-9FDD-17FA413F2A44}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{4FD0EEFE-B90A-43E7-9FDD-17FA413F2A44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{4FD0EEFE-B90A-43E7-9FDD-17FA413F2A44}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{4FD0EEFE-B90A-43E7-9FDD-17FA413F2A44}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{4FD0EEFE-B90A-43E7-9FDD-17FA413F2A44}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{4FD0EEFE-B90A-43E7-9FDD-17FA413F2A44}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{4FD0EEFE-B90A-43E7-9FDD-17FA413F2A44}.Release|Any CPU.Deploy.0 = Release|Any CPU
20+
EndGlobalSection
21+
GlobalSection(SolutionProperties) = preSolution
22+
HideSolutionNode = FALSE
23+
EndGlobalSection
24+
GlobalSection(ExtensibilityGlobals) = postSolution
25+
SolutionGuid = {A2997D30-86B3-4A81-89FA-FEA86241AAA2}
26+
EndGlobalSection
27+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
5+
</PropertyGroup>
6+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
7+
<PropertyGroup>
8+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
9+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
10+
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
11+
<ProjectGuid>4fd0eefe-b90a-43e7-9fdd-17fa413f2a44</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>ChangeCounter</RootNamespace>
16+
<AssemblyName>ChangeCounter</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Program.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
</ItemGroup>
24+
<ItemGroup>
25+
<Reference Include="mscorlib, Version=1.2.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
26+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.2.6-preview.16\lib\mscorlib.dll</HintPath>
27+
<Private>True</Private>
28+
<SpecificVersion>True</SpecificVersion>
29+
</Reference>
30+
<Reference Include="nanoFramework.Runtime.Events, Version=1.0.8.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
31+
<HintPath>..\packages\nanoFramework.Runtime.Events.1.0.8-preview.20\lib\nanoFramework.Runtime.Events.dll</HintPath>
32+
<Private>True</Private>
33+
<SpecificVersion>True</SpecificVersion>
34+
</Reference>
35+
<Reference Include="Windows.Devices.Gpio, Version=1.2.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
36+
<HintPath>..\packages\nanoFramework.Windows.Devices.Gpio.1.2.0-preview.1\lib\Windows.Devices.Gpio.dll</HintPath>
37+
<Private>True</Private>
38+
<SpecificVersion>True</SpecificVersion>
39+
</Reference>
40+
<Reference Include="Windows.Devices.Pwm, Version=1.2.1.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
41+
<HintPath>..\packages\nanoFramework.Windows.Devices.Pwm.1.2.1-preview.17\lib\Windows.Devices.Pwm.dll</HintPath>
42+
<Private>True</Private>
43+
<SpecificVersion>True</SpecificVersion>
44+
</Reference>
45+
</ItemGroup>
46+
<ItemGroup>
47+
<None Include="packages.config" />
48+
</ItemGroup>
49+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
50+
<ProjectExtensions>
51+
<ProjectCapabilities>
52+
<ProjectConfigurationsDeclaredAsItems />
53+
</ProjectCapabilities>
54+
</ProjectExtensions>
55+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
using System;
2+
using System.Threading;
3+
using Windows.Devices.Gpio;
4+
using Windows.Devices.Pwm;
5+
6+
7+
//
8+
// This sample demostrates the GpioChangeCounter
9+
//
10+
// A GpioChangeCounter can be used to count pulses on a GPIO pin from things such as a flow meter, wind speed device or any other device that generates pulses.
11+
//
12+
// As the counter is low level it can handle much high frequency signals and avoids handling interupts in managed code.
13+
// Also its a 64 bit counter so is not going to overflow. Counting 1Mhz pulses would overflow in 292470 years.
14+
//
15+
// The counter can be set up to counter the raising, failing or both edges of a pulse.
16+
// This sample will uses a pwm signal generated on one pin to simulate a signal to count.
17+
18+
19+
// For counter sample to work a wire needs to be connected between the counter input pin and Pwm output pin.
20+
21+
namespace ChangeCounter
22+
{
23+
public class Program
24+
{
25+
// Set up the counter pin used
26+
const int COUNTER_INPUT_PIN = 5;
27+
28+
// Set up the PWM outout pin used
29+
const int PWM_OUTPUT_PIN = 18;
30+
31+
// Frequency used by PWM controller
32+
const int PWM_FREQUENCY = 1234;
33+
34+
public static void Main()
35+
{
36+
Console.WriteLine("Change Counter test running");
37+
38+
// Initialise PWM output pin
39+
PwmController pwmc = PwmController.GetDefault();
40+
pwmc.SetDesiredFrequency(PWM_FREQUENCY);
41+
42+
PwmPin pwmTestPin = pwmc.OpenPin(PWM_OUTPUT_PIN);
43+
pwmTestPin.SetActiveDutyCyclePercentage(0.5);
44+
45+
Console.WriteLine($"Open PWM pin {PWM_OUTPUT_PIN} frequency {pwmc.ActualFrequency}");
46+
Console.WriteLine($"This pin must be connected to GpioChangeCounter pin {COUNTER_INPUT_PIN}");
47+
48+
49+
// Initialise count pin by opening GPIO as input
50+
GpioPin countPin = GpioController.GetDefault().OpenPin(COUNTER_INPUT_PIN);
51+
countPin.SetDriveMode(GpioPinDriveMode.InputPullUp);
52+
53+
// Create a Counter passing in the GPIO pin
54+
GpioChangeCounter gpcc = new GpioChangeCounter(countPin);
55+
// Counter both raising and falling edges
56+
gpcc.Polarity = GpioChangePolarity.Both;
57+
58+
Console.WriteLine($"Counter pin {COUNTER_INPUT_PIN} created");
59+
60+
// Start counter
61+
gpcc.Start();
62+
63+
// Read count before we start PWM ( should be 0 )
64+
// We want to save the start relative time
65+
GpioChangeCount count1 = gpcc.Read();
66+
67+
// Start PWM signal
68+
pwmTestPin.Start();
69+
70+
// Wait 1 Sec
71+
Thread.Sleep(1000);
72+
73+
// Read current count
74+
GpioChangeCount count2 = gpcc.Read();
75+
76+
// Stop PWM signal & counter
77+
pwmTestPin.Stop();
78+
gpcc.Stop();
79+
80+
81+
// Change polarity of counter so only counting rising edges
82+
gpcc.Polarity = GpioChangePolarity.Rising;
83+
84+
gpcc.Start();
85+
GpioChangeCount count3 = gpcc.Reset();
86+
87+
pwmTestPin.Start();
88+
89+
// Wait 1 Sec
90+
Thread.Sleep(1000);
91+
92+
pwmTestPin.Stop();
93+
94+
// Read count
95+
GpioChangeCount count4 = gpcc.Read();
96+
97+
gpcc.Stop();
98+
99+
DisplayResults("Count pulses for 1 second with both edges", count1, count2);
100+
DisplayResults("Count pulses for 1 second with just rising edge", count3, count4);
101+
102+
// Next test tries to measure the frequncy of the PWM signal
103+
pwmTestPin.Start();
104+
gpcc.Start();
105+
106+
while (true)
107+
{
108+
// Reset Counter to zero
109+
GpioChangeCount countStart = gpcc.Reset();
110+
111+
Thread.Sleep(1000);
112+
113+
// Wait 1 sec and read again
114+
GpioChangeCount countEnd = gpcc.Read();
115+
116+
// Sleep is not accurate so calculate actual time in secounds based of relative time differences of the 2 counts
117+
// Ticks are in 100 nano sec increments
118+
double periodSecs = (double)(countEnd.RelativeTime.Ticks - countStart.RelativeTime.Ticks)/10000000.0;
119+
int frequecy = (int)((double)countEnd.Count / periodSecs);
120+
121+
Console.WriteLine($"Period {periodSecs:F6} Frequency {frequecy}");
122+
}
123+
}
124+
125+
126+
static void DisplayResults(string text, GpioChangeCount start, GpioChangeCount end)
127+
{
128+
TimeSpan period = end.RelativeTime - start.RelativeTime;
129+
Console.WriteLine($"Count {text} Time:{period} Count:{end.Count}");
130+
}
131+
}
132+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
13+
[assembly: AssemblyCopyright("Copyright © ")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// Version information for an assembly consists of the following four values:
23+
//
24+
// Major Version
25+
// Minor Version
26+
// Build Number
27+
// Revision
28+
//
29+
// You can specify all the values or you can default the Build and Revision Numbers
30+
// by using the '*' as shown below:
31+
// [assembly: AssemblyVersion("1.0.*")]
32+
[assembly: AssemblyVersion("1.0.0.0")]
33+
[assembly: AssemblyFileVersion("1.0.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.2.6-preview.16" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.Runtime.Events" version="1.0.8-preview.20" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.Windows.Devices.Gpio" version="1.2.0-preview.1" targetFramework="netnanoframework10" />
6+
<package id="nanoFramework.Windows.Devices.Pwm" version="1.2.1-preview.17" targetFramework="netnanoframework10" />
7+
</packages>

0 commit comments

Comments
 (0)