Skip to content

Commit bba1e63

Browse files
authored
Add I2S samples (#257)
1 parent bd54a56 commit bba1e63

19 files changed

+1212
-1
lines changed

device-listing/Configuration.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
{
88
"Name": "device",
9-
"Description": "Gpio, I2C, Spi, Pwm, Adc, Dac, 1-Wire, Serial"
9+
"Description": "Gpio, I2C, I2S, Spi, Pwm, Adc, Dac, 1-Wire, Serial"
1010
},
1111

1212
{

samples/I2S/Input/MicrophoneIn.nfproj

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<NanoFrameworkProjectSystemPath>$(MSBuildExtensionsPath)\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>a2d510ad-1108-4beb-be32-49b8d9b93f6d</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>MicrophoneIn</RootNamespace>
16+
<AssemblyName>MicrophoneIn</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+
<Compile Include="WavFileHeader.cs" />
24+
</ItemGroup>
25+
<ItemGroup>
26+
<Reference Include="mscorlib">
27+
<HintPath>packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
28+
</Reference>
29+
<Reference Include="nanoFramework.Hardware.Esp32">
30+
<HintPath>packages\nanoFramework.Hardware.Esp32.1.4.8\lib\nanoFramework.Hardware.Esp32.dll</HintPath>
31+
</Reference>
32+
<Reference Include="nanoFramework.Runtime.Events">
33+
<HintPath>packages\nanoFramework.Runtime.Events.1.11.1\lib\nanoFramework.Runtime.Events.dll</HintPath>
34+
</Reference>
35+
<Reference Include="nanoFramework.System.Text">
36+
<HintPath>packages\nanoFramework.System.Text.1.2.22\lib\nanoFramework.System.Text.dll</HintPath>
37+
</Reference>
38+
<Reference Include="System.Device.I2s">
39+
<HintPath>packages\nanoFramework.System.Device.I2s.1.0.3\lib\System.Device.I2s.dll</HintPath>
40+
</Reference>
41+
<Reference Include="System.IO.FileSystem">
42+
<HintPath>packages\nanoFramework.System.IO.FileSystem.1.1.15\lib\System.IO.FileSystem.dll</HintPath>
43+
</Reference>
44+
<Reference Include="System.IO.Streams">
45+
<HintPath>packages\nanoFramework.System.IO.Streams.1.1.27\lib\System.IO.Streams.dll</HintPath>
46+
</Reference>
47+
</ItemGroup>
48+
<ItemGroup>
49+
<None Include="packages.config" />
50+
</ItemGroup>
51+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
52+
<ProjectExtensions>
53+
<ProjectCapabilities>
54+
<ProjectConfigurationsDeclaredAsItems />
55+
</ProjectCapabilities>
56+
</ProjectExtensions>
57+
</Project>

samples/I2S/Input/MicrophoneIn.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 Version 17
4+
VisualStudioVersion = 17.3.32929.385
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{11A8DD76-328B-46DF-9F39-F559912D0360}") = "MicrophoneIn", "MicrophoneIn.nfproj", "{A2D510AD-1108-4BEB-BE32-49B8D9B93F6D}"
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+
{A2D510AD-1108-4BEB-BE32-49B8D9B93F6D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A2D510AD-1108-4BEB-BE32-49B8D9B93F6D}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A2D510AD-1108-4BEB-BE32-49B8D9B93F6D}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
17+
{A2D510AD-1108-4BEB-BE32-49B8D9B93F6D}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{A2D510AD-1108-4BEB-BE32-49B8D9B93F6D}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{A2D510AD-1108-4BEB-BE32-49B8D9B93F6D}.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 = {05B851F2-DD45-4A13-A28A-B898207A497B}
26+
EndGlobalSection
27+
EndGlobal

samples/I2S/Input/Program.cs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System;
7+
using System.Device.I2s;
8+
using System.Diagnostics;using System.IO;
9+
using System.Threading;
10+
using MicrophoneIn;
11+
using nanoFramework.Hardware.Esp32;
12+
using nanoFramework.System.IO.FileSystem;
13+
14+
15+
// This Sample will record 1s of audio input and then store it to the
16+
// attached SD card.
17+
18+
Debug.WriteLine("Hello from I2S!");
19+
20+
// SD Card (for WAV file output):
21+
uint cs = 5;
22+
Configuration.SetPinFunction(23, DeviceFunction.SPI1_MOSI);
23+
Configuration.SetPinFunction(18, DeviceFunction.SPI1_CLOCK);
24+
Configuration.SetPinFunction(19, DeviceFunction.SPI1_MISO);
25+
var sdCard = new SDCard(new SDCard.SDCardSpiParameters { spiBus = 1, chipSelectPin = cs });
26+
sdCard.Mount();
27+
28+
// configure I2s for recording:
29+
Configuration.SetPinFunction(0, DeviceFunction.I2S1_WS);
30+
Configuration.SetPinFunction(34, DeviceFunction.I2S1_MDATA_IN);
31+
32+
I2sDevice i2s = new(new I2sConnectionSettings(1)
33+
{
34+
BitsPerSample = I2sBitsPerSample.Bit8,
35+
ChannelFormat = I2sChannelFormat.OnlyLeft,
36+
Mode = I2sMode.Master | I2sMode.Rx | I2sMode.Pdm,
37+
CommunicationFormat = I2sCommunicationFormat.I2S,
38+
SampleRate = 8_000
39+
});
40+
41+
// should be one second of sound data:
42+
SpanByte buff = new byte[8000];
43+
i2s.Read(buff);
44+
i2s.Dispose();
45+
46+
var header = new WavFileHeader
47+
{
48+
AudioFormat = 1,
49+
SampleRate = 8000,
50+
BitsPerSample = 8,
51+
BytesPerSampleFrame = 1,
52+
BytesPerSecond = 8000,
53+
DataChunkSize = 8000,
54+
FormatChunkSize = 16,
55+
NumberOfChannels = 1,
56+
FileSize = 36 + 8000
57+
};
58+
59+
var outputFile = new FileStream("D:\\output.wav", FileMode.CreateNew, FileAccess.ReadWrite);
60+
var headerData = header.GetHeaderData();
61+
outputFile.Write(headerData, 0, headerData.Length);
62+
63+
var buffer = buff.ToArray();
64+
outputFile.Write(buffer, 0, buffer.Length);
65+
66+
outputFile.Flush();
67+
outputFile.Close();
68+
outputFile.Dispose();
69+
70+
Thread.Sleep(Timeout.Infinite);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
5+
6+
using System.Reflection;
7+
using System.Runtime.InteropServices;
8+
9+
// General Information about an assembly is controlled through the following
10+
// set of attributes. Change these attribute values to modify the information
11+
// associated with an assembly.
12+
[assembly: AssemblyTitle("CSharp.BlankApplication")]
13+
[assembly: AssemblyDescription("")]
14+
[assembly: AssemblyConfiguration("")]
15+
[assembly: AssemblyCompany("")]
16+
[assembly: AssemblyProduct("CSharp.BlankApplication")]
17+
[assembly: AssemblyCopyright("Copyright © ")]
18+
[assembly: AssemblyTrademark("")]
19+
[assembly: AssemblyCulture("")]
20+
21+
// Setting ComVisible to false makes the types in this assembly not visible
22+
// to COM components. If you need to access a type in this assembly from
23+
// COM, set the ComVisible attribute to true on that type.
24+
[assembly: ComVisible(false)]
25+
26+
// Version information for an assembly consists of the following four values:
27+
//
28+
// Major Version
29+
// Minor Version
30+
// Build Number
31+
// Revision
32+
//
33+
// You can specify all the values or you can default the Build and Revision Numbers
34+
// by using the '*' as shown below:
35+
// [assembly: AssemblyVersion("1.0.*")]
36+
[assembly: AssemblyVersion("1.0.0.0")]
37+
[assembly: AssemblyFileVersion("1.0.0.0")]

samples/I2S/Input/README.md

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 🌶️🌶️ - I2S Microphone sample
2+
3+
Shows how to use the [System.Device.I2s](http://docs.nanoframework.net/api/System.Device.I2s.html) API to read from an I2S device.
4+
In this sample we'll be using a [PDM Microphone](https://www.adafruit.com/product/3492) breakout board to record 1s of audio data to a SD card.
5+
6+
## Hardware requirements
7+
8+
- A [PDM Microphone](https://www.adafruit.com/product/3492) breakout board,
9+
- a [microSD card](https://www.adafruit.com/product/254) breakout board and
10+
- a microSD card to store the recorded audio to.
11+
12+
The code sample is demonstrative of the use of the I2S API.
13+
14+
## Related topics
15+
16+
### Reference
17+
18+
- [System.Device.I2s](http://docs.nanoframework.net/api/System.Device.I2s.html)
19+
20+
## Build the sample
21+
22+
1. Start Microsoft Visual Studio 2019 (VS 2017 should be OK too) and select `File > Open > Project/Solution`.
23+
1. Starting in the folder where you unzipped the samples/cloned the repository, go to the subfolder for this specific sample. Double-click the Visual Studio Solution (.sln) file.
24+
1. Press `Ctrl+Shift+B`, or select `Build > Build Solution`.
25+
26+
## Run the sample
27+
28+
The next steps depend on whether you just want to deploy the sample or you want to both deploy and run it.
29+
30+
### Deploying the sample
31+
32+
- Select `Build > Deploy Solution`.
33+
34+
### Deploying and running the sample
35+
36+
- To debug the sample and then run it, press F5 or select `Debug > Start Debugging`.
37+
38+
> **Important**: Before deploying or running the sample, please make sure your device is visible in the Device Explorer.
39+
40+
> **Tip**: To display the Device Explorer, go to Visual Studio menus: `View > Other Windows > Device Explorer`.

0 commit comments

Comments
 (0)