Skip to content

Commit 5b46ab4

Browse files
committed
Add DAC sample pack
Signed-off-by: José Simões <[email protected]>
1 parent 54d5a32 commit 5b46ab4

13 files changed

+418
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Feel free to browse, take what you like and contribute back if you want.
3838
<td><a href="samples/RTC">RTC Sample</a></td>
3939
</tr>
4040
<td><a href="samples/SPI">SPI</a></td>
41-
<td><!--<a href="PWM">PWM</a>--></td>
41+
<td><a href="DAC">DAC</a></td>
4242
<td><!--<a href="RTXC">RTC Sample</a>--></td>
4343
</tr>
4444
</table>

samples/DAC/DacSample.sln

+27
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.705
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "DacSample", "DacSample\DacSample.nfproj", "{ACD083DD-5FCF-4E14-A1D0-AEDE3E63EE4C}"
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+
{ACD083DD-5FCF-4E14-A1D0-AEDE3E63EE4C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{ACD083DD-5FCF-4E14-A1D0-AEDE3E63EE4C}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{ACD083DD-5FCF-4E14-A1D0-AEDE3E63EE4C}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{ACD083DD-5FCF-4E14-A1D0-AEDE3E63EE4C}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{ACD083DD-5FCF-4E14-A1D0-AEDE3E63EE4C}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{ACD083DD-5FCF-4E14-A1D0-AEDE3E63EE4C}.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 = {C473C2A3-0D26-4443-952F-48122E878D1B}
26+
EndGlobalSection
27+
EndGlobal
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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>acd083dd-5fcf-4e14-a1d0-aede3e63ee4c</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>DacSample</RootNamespace>
16+
<AssemblyName>DacSample</AssemblyName>
17+
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
18+
</PropertyGroup>
19+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
20+
<ItemGroup>
21+
<Compile Include="Scenario1ConfigureDac.cs" />
22+
<Compile Include="Properties\AssemblyInfo.cs" />
23+
<Compile Include="Scenario4SineWave.cs" />
24+
<Compile Include="Scenario3SquareWave.cs" />
25+
<Compile Include="Scenario2TriangleWave.cs" />
26+
</ItemGroup>
27+
<ItemGroup>
28+
<Reference Include="mscorlib, Version=1.2.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
29+
<HintPath>..\packages\nanoFramework.CoreLibrary.1.2.6-preview.7\lib\mscorlib.dll</HintPath>
30+
<Private>True</Private>
31+
<SpecificVersion>True</SpecificVersion>
32+
</Reference>
33+
<Reference Include="System.Devices.Dac, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
34+
<HintPath>..\packages\nanoFramework.System.Devices.Dac.1.0.0-preview.17\lib\System.Devices.Dac.dll</HintPath>
35+
<Private>True</Private>
36+
<SpecificVersion>True</SpecificVersion>
37+
</Reference>
38+
<Reference Include="System.Math, Version=1.0.6.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
39+
<HintPath>..\packages\nanoFramework.System.Math.1.0.6-preview.15\lib\System.Math.dll</HintPath>
40+
<Private>True</Private>
41+
<SpecificVersion>True</SpecificVersion>
42+
</Reference>
43+
</ItemGroup>
44+
<ItemGroup>
45+
<None Include="packages.config" />
46+
</ItemGroup>
47+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
48+
<ProjectExtensions>
49+
<ProjectCapabilities>
50+
<ProjectConfigurationsDeclaredAsItems />
51+
</ProjectCapabilities>
52+
</ProjectExtensions>
53+
</Project>
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,53 @@
1+
//
2+
// Copyright (c) 2019 The nanoFramework project contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Devices.Dac;
8+
using System.Threading;
9+
10+
namespace DacSample
11+
{
12+
public class Scenario1ConfigureDac
13+
{
14+
static internal int dacResolution;
15+
16+
/// <summary>
17+
/// Time resolution to update the DAC output in milliseconds.
18+
/// </summary>
19+
/// <remarks>
20+
/// Default is 10ms.
21+
/// </remarks>
22+
internal const int s_timeResolution = 5;
23+
24+
public static void Main()
25+
{
26+
string devices = DacController.GetDeviceSelector();
27+
Console.WriteLine("DAC controllers: " + devices);
28+
29+
// get default controller
30+
DacController dac = DacController.GetDefault();
31+
32+
// open channel 0
33+
DacChannel dacChannel = dac.OpenChannel(0);
34+
35+
// get DAC resolution
36+
dacResolution = dac.ResolutionInBits;
37+
38+
// uncomment the scenario to test
39+
/////////////////////////////////////////
40+
// !!note that none of these returns!! //
41+
/////////////////////////////////////////
42+
43+
Scenario2TriangleWave.Execute(ref dacChannel);
44+
45+
//Scenario3SquareWave.Execute(ref dacChannel);
46+
47+
//Scenario4SineWave.Execute(ref dacChannel);
48+
49+
50+
Thread.Sleep(Timeout.Infinite);
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Copyright (c) 2019 The nanoFramework project contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Devices.Dac;
8+
using System.Threading;
9+
10+
namespace DacSample
11+
{
12+
internal static class Scenario2TriangleWave
13+
{
14+
public static void Execute(ref DacChannel channel)
15+
{
16+
UInt32 value = 0;
17+
uint increment = 0;
18+
bool goingUp = false;
19+
int upperValue;
20+
21+
// get upper value from DAC resolution
22+
upperValue = (int)Math.Pow(2, Scenario1ConfigureDac.dacResolution);
23+
24+
// compute a reasonable increment value from the the DAC resolution
25+
increment = (uint)Math.Pow(2, (Scenario1ConfigureDac.dacResolution / 2));
26+
27+
28+
for (; ; )
29+
{
30+
if (value == upperValue)
31+
{
32+
// tweak the value so it doesn't overflow the DAC
33+
value--;
34+
35+
channel.WriteValue((ushort)value);
36+
37+
value++;
38+
39+
// invert to go down
40+
goingUp = false;
41+
}
42+
else if (value == 0)
43+
{
44+
channel.WriteValue((ushort)value);
45+
46+
// invert to go up
47+
goingUp = true;
48+
}
49+
else
50+
{
51+
channel.WriteValue((ushort)value);
52+
}
53+
54+
if (goingUp)
55+
{
56+
value += increment;
57+
}
58+
else
59+
{
60+
value -= increment;
61+
}
62+
63+
Thread.Sleep(Scenario1ConfigureDac.s_timeResolution);
64+
}
65+
}
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Copyright (c) 2019 The nanoFramework project contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Devices.Dac;
8+
using System.Threading;
9+
10+
namespace DacSample
11+
{
12+
internal static class Scenario3SquareWave
13+
{
14+
public static void Execute(ref DacChannel channel)
15+
{
16+
int value = 0;
17+
int upperValue;
18+
int periodCounter = 0;
19+
int halfPeriod;
20+
21+
// get upper value from DAC resolution
22+
upperValue = (int)Math.Pow(2, Scenario1ConfigureDac.dacResolution);
23+
24+
// figure out an expedite way to get a more or less square wave from the DAC and time resolution
25+
halfPeriod = ( upperValue / (Scenario1ConfigureDac.s_timeResolution * 10) ) / 2;
26+
27+
for (;;)
28+
{
29+
if (periodCounter == halfPeriod)
30+
{
31+
// tweak the value so it doesn't overflow the DAC
32+
value = upperValue - 1;
33+
}
34+
else if (periodCounter == halfPeriod * 2)
35+
{
36+
value = 0;
37+
38+
periodCounter = 0;
39+
}
40+
41+
channel.WriteValue((ushort)value);
42+
43+
Thread.Sleep(Scenario1ConfigureDac.s_timeResolution);
44+
45+
periodCounter++;
46+
}
47+
}
48+
}
49+
}
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//
2+
// Copyright (c) 2019 The nanoFramework project contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Devices.Dac;
8+
using System.Threading;
9+
10+
namespace DacSample
11+
{
12+
internal static class Scenario4SineWave
13+
{
14+
const float maxRads = (float)(2 * Math.PI);
15+
16+
public static void Execute(ref DacChannel channel)
17+
{
18+
UInt32 value = 0;
19+
float increment = 0;
20+
int upperValue, midRange;
21+
float radian = 0;
22+
23+
// get upper value from DAC resolution
24+
upperValue = (int)Math.Pow(2, Scenario1ConfigureDac.dacResolution);
25+
26+
// compute a reasonable increment value from the resolution
27+
increment = maxRads / (Scenario1ConfigureDac.dacResolution * 10) ;
28+
midRange = upperValue / 2;
29+
30+
for (; ; )
31+
{
32+
// because the DAC can't output negative values
33+
// we have to offset the sine wave to half the DAC output range
34+
value = (uint)((Math.Sin(radian) * (midRange - 1)) + midRange);
35+
36+
// output to DAC
37+
channel.WriteValue((ushort)value);
38+
39+
// increment angle
40+
radian += increment;
41+
42+
if (radian >= maxRads)
43+
{
44+
// tweak the value so it doesn't overflow the DAC
45+
radian = 0;
46+
}
47+
48+
Thread.Sleep(Scenario1ConfigureDac.s_timeResolution);
49+
}
50+
}
51+
}
52+
}

samples/DAC/DacSample/packages.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="nanoFramework.CoreLibrary" version="1.2.6-preview.7" targetFramework="netnanoframework10" />
4+
<package id="nanoFramework.System.Devices.Dac" version="1.0.0-preview.17" targetFramework="netnanoframework10" />
5+
<package id="nanoFramework.System.Math" version="1.0.6-preview.15" targetFramework="netnanoframework10" />
6+
</packages>

0 commit comments

Comments
 (0)