Skip to content

Commit e37de3a

Browse files
authored
Add BSP for ATOM Matrix (#75)
1 parent bb725e7 commit e37de3a

19 files changed

+826
-142
lines changed

AtomCommon/AtomBase.cs

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using Iot.Device.Button;
5+
using nanoFramework.Hardware.Esp32;
6+
using nanoFramework.Hardware.Esp32.Rmt;
7+
using System;
8+
using System.Device.Adc;
9+
using System.Device.Dac;
10+
using System.Device.Gpio;
11+
using System.Device.I2c;
12+
using System.Device.Spi;
13+
14+
#if ATOM_MATRIX
15+
namespace nanoFramework.AtomMatrix
16+
{
17+
/// <summary>
18+
/// The AtomMatrix Board.
19+
/// </summary>
20+
public static partial class AtomMatrix
21+
#else
22+
namespace nanoFramework.AtomLite
23+
{
24+
/// <summary>
25+
/// M5Stack board
26+
/// </summary>
27+
public static partial class AtomLite
28+
#endif
29+
{
30+
private static GpioButton _button;
31+
private static GpioController _gpio;
32+
private static DacChannel _dac1;
33+
private static DacChannel _dac2;
34+
private static AdcController _adc;
35+
private static TransmitterChannel _irLed;
36+
37+
/// <summary>
38+
/// Main button.
39+
/// </summary>
40+
public static GpioButton Button
41+
{
42+
get
43+
{
44+
if (_button == null)
45+
{
46+
_button = new(39, _gpio, false);
47+
}
48+
49+
return _button;
50+
}
51+
}
52+
53+
/// <summary>
54+
/// Gets the main <see cref="GpioController"/>.
55+
/// </summary>
56+
public static GpioController GpioController => _gpio;
57+
58+
/// <summary>
59+
/// Gets <see cref="DacChannel"/> connected to GPIO 25.
60+
/// </summary>
61+
public static DacChannel Dac1
62+
{
63+
get
64+
{
65+
// We are creating it on demand
66+
if (_dac1 == null)
67+
{
68+
_dac1 = DacController.GetDefault().OpenChannel(0);
69+
}
70+
71+
return _dac1;
72+
}
73+
}
74+
75+
/// <summary>
76+
/// Gets <see cref="DacChannel"/> connected to GPIO 26.
77+
/// </summary>
78+
public static DacChannel Dac2
79+
{
80+
get
81+
{
82+
// We are creating it on demand
83+
if (_dac2 == null)
84+
{
85+
86+
_dac2 = DacController.GetDefault().OpenChannel(1);
87+
}
88+
89+
return _dac2;
90+
}
91+
}
92+
93+
/// <summary>
94+
/// Gets an <see cref="I2cDevice"/>.
95+
/// </summary>
96+
/// <param name="i2cDeviceAddress">The address of the <see cref="I2cDevice"/> on the bus.</param>
97+
/// <returns>The I2cDevice.</returns>
98+
public static I2cDevice GetI2cDevice(int i2cDeviceAddress) => new(new I2cConnectionSettings(1, i2cDeviceAddress));
99+
100+
/// <summary>
101+
/// Gets an <see cref="I2cDevice"/>.
102+
/// </summary>
103+
/// <param name="i2cDeviceAddress">The address of the <see cref="I2cDevice"/> on the bus.</param>
104+
/// <returns>The I2cDevice.</returns>
105+
public static I2cDevice GetGrove(int i2cDeviceAddress) => new(new I2cConnectionSettings(1, i2cDeviceAddress));
106+
107+
/// <summary>
108+
/// Gets the infrared led as a RMT transmitter channel.
109+
/// </summary>
110+
public static TransmitterChannel InfraredLed
111+
{
112+
get
113+
{
114+
if (_irLed == null)
115+
{
116+
_irLed = new(12);
117+
}
118+
119+
return _irLed;
120+
}
121+
}
122+
123+
/// <summary>
124+
/// Gets an <see cref="SpiDevice"/>.
125+
/// </summary>
126+
/// <param name="chipSelect">The chip select of the device, needs to be any valid GPIO.</param>
127+
/// <returns>An SpiDevice.</returns>
128+
public static SpiDevice GetSpiDevice(int chipSelect) => new(new SpiConnectionSettings(1, chipSelect));
129+
130+
/// <summary>
131+
/// Gets an <see cref="AdcChannel"/>
132+
/// </summary>
133+
/// <param name="gpioNumber">The GPIO pin number where the <see cref="AdcChannel"/> is connected to.</param>
134+
/// <returns>An AdcChannel</returns>
135+
public static AdcChannel GetAdcGpio(int gpioNumber)
136+
{
137+
if (_adc == null)
138+
{
139+
_adc = new();
140+
}
141+
142+
switch (gpioNumber)
143+
{
144+
case 33:
145+
Configuration.SetPinFunction(12, DeviceFunction.ADC1_CH5);
146+
return _adc.OpenChannel(5);
147+
case 32:
148+
Configuration.SetPinFunction(25, DeviceFunction.ADC1_CH4);
149+
return _adc.OpenChannel(4);
150+
default:
151+
throw new ArgumentException(nameof(gpioNumber));
152+
}
153+
}
154+
}
155+
}

AtomCommon/AtomCommon.projitems

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<MSBuildAllProjects Condition="'$(MSBuildVersion)' == '' Or '$(MSBuildVersion)' &lt; '16.0'">$(MSBuildAllProjects);$(MSBuildThisFileFullPath)</MSBuildAllProjects>
5+
<HasSharedItems>true</HasSharedItems>
6+
<SharedGUID>79f09006-ab5d-4e3e-ad12-2efbee536ca9</SharedGUID>
7+
</PropertyGroup>
8+
<PropertyGroup Label="Configuration">
9+
<Import_RootNamespace>AtomCommon</Import_RootNamespace>
10+
</PropertyGroup>
11+
<ItemGroup>
12+
<Compile Include="$(MSBuildThisFileDirectory)AtomBase.cs" />
13+
</ItemGroup>
14+
</Project>

AtomCommon/AtomCommon.shproj

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup Label="Globals">
4+
<ProjectGuid>79f09006-ab5d-4e3e-ad12-2efbee536ca9</ProjectGuid>
5+
<MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion>
6+
</PropertyGroup>
7+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
8+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" />
9+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" />
10+
<PropertyGroup />
11+
<Import Project="AtomCommon.projitems" Label="Shared" />
12+
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" />
13+
</Project>

README.md

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
| nanoFramework.M5Core2 (preview) | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_apis/build/status/nanoFramework.M5Stack?repoName=nanoframework%2FnanoFramework.M5Stack&branchName=develop)](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_build/latest?definitionId=52&repoName=nanoframework%2FnanoFramework.M5Stack&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.M5Core2.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.M5Core2/) |
2121
| nanoFramework.AtomLite | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_apis/build/status/nanoFramework.M5Stack?repoName=nanoframework%2FnanoFramework.M5Stack&branchName=main)](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_build/latest?definitionId=52&repoName=nanoframework%2FnanoFramework.M5Stack&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.AtomLite.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.AtomLite/) |
2222
| nanoFramework.AtomLite (preview) | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_apis/build/status/nanoFramework.M5Stack?repoName=nanoframework%2FnanoFramework.M5Stack&branchName=develop)](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_build/latest?definitionId=52&repoName=nanoframework%2FnanoFramework.M5Stack&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.AtomLite.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.AtomLite/) |
23+
| nanoFramework.AtomMatrix | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_apis/build/status/nanoFramework.M5Stack?repoName=nanoframework%2FnanoFramework.M5Stack&branchName=main)](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_build/latest?definitionId=52&repoName=nanoframework%2FnanoFramework.M5Stack&branchName=main) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.AtomMatrix.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.AtomMatrix/) |
24+
| nanoFramework.AtomMatrix (preview) | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_apis/build/status/nanoFramework.M5Stack?repoName=nanoframework%2FnanoFramework.M5Stack&branchName=develop)](https://dev.azure.com/nanoframework/nanoFramework.M5Stack/_build/latest?definitionId=52&repoName=nanoframework%2FnanoFramework.M5Stack&branchName=develop) | [![NuGet](https://img.shields.io/nuget/vpre/nanoFramework.AtomMatrix.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.AtomMatrix/) |
2325

2426
## Usage
2527

@@ -30,6 +32,7 @@ These NuGet packages provide a support for M5Stack products:
3032
- [M5StickCPlus](https://docs.m5stack.com/en/core/m5stickc_plus)
3133
- [M5Core2](https://docs.m5stack.com/en/core/core2)
3234
- [Atom Lite](https://docs.m5stack.com/en/core/atom_lite)
35+
- [Atom Matrix](https://docs.m5stack.com/en/core/atom_matrix)
3336

3437
> Note 1: Before trying to add NuGet packages to your projects and/or before flashing the devices (see next section) using MS Visual Studio (VS), open VS > Tools > Options > NuGet Package Manager > Package Sources and make sure that it contains an entry pointing to <https://api.nuget.org/v3/index.json> , otherwise add it.
3538
> Note 2: When invoking VS > Project > Manage NuGet Packages make sure that in the Package source drop-down menu (right upper corner) "nuget.org" is selected. Also if you're using preview version the "include prerelease" checkbox should be clicked/selected as well.
@@ -61,7 +64,7 @@ For the M5Core2:
6164
nanoff --target M5Core2 --update --preview --serialport COM3
6265
```
6366

64-
For the Atom Lite:
67+
For the Atom Lite and Matrix:
6568

6669
```shell
6770
nanoff --target ESP32_PICO --update --preview --serialport COM3
@@ -167,7 +170,7 @@ M5StickC.M5Button.Holding += (sender, e) =>
167170
};
168171
```
169172

170-
On the Atom Lite it's called `Button`. You can get access to the status of the button, the events and everything you need. For example:
173+
On the Atom Lite/Matrix it's called `Button`. You can get access to the status of the button, the events and everything you need. For example:
171174

172175
```csharp
173176
AtomLite.Button.Press +=> (sender, e)
@@ -188,7 +191,6 @@ AtomLite.Button.Press +=> (sender, e)
188191
};
189192
```
190193

191-
192194
> Note: The M5Core2 has touch screen and the buttons are "virtual"". See next section to see how to use them.
193195
194196
### M5Core2 touch panel and buttons
@@ -320,7 +322,7 @@ Refer to the [SerialPort documentation](https://github.com/nanoframework/System.
320322

321323
### ADC Channels
322324

323-
ADC Channels are pre setup on the M5Core, M5Core2 and Atom Lite, access them like this:
325+
ADC Channels are pre setup on the M5Core, M5Core2 and Atom Lite/Matrix, access them like this:
324326

325327
```csharp
326328
// This will give you the ADC1 channel 7 which is on pin 35 of M5Core
@@ -341,7 +343,7 @@ I2cDevice myDevice = M5Core.GetGrove(0x42);
341343

342344
### SPI Device
343345

344-
The M5Core, M5Core2 and Atom Lite provides as well an `SpiDevice`:
346+
The M5Core, M5Core2 and Atom Lite/Matrix provides as well an `SpiDevice`:
345347

346348
```csharp
347349
// In this case GPIO5 will be used as chip select:
@@ -359,7 +361,7 @@ var pin5 = M5StickC.GpioController.OpenPin(36, PinMode.Output);
359361

360362
### DAC
361363

362-
The M5Core, M5Core2 and Atom Lite exposes 2 DAC and you can access them thru the `Dac1` and `Dac2` properties. Refer to the [DAC documentation](https://github.com/nanoframework/System.Device.Dac) for more information.
364+
The M5Core, M5Core2 and Atom Lite/Matrix exposes 2 DAC and you can access them thru the `Dac1` and `Dac2` properties. Refer to the [DAC documentation](https://github.com/nanoframework/System.Device.Dac) for more information.
363365

364366
### Led
365367

@@ -372,7 +374,7 @@ M5StickC.Led.Toggle();
372374

373375
### Infrared Led
374376

375-
The M5StickC/CPlus and Atom Lite exposes an infrared led. You can access it thru the `InfraredLed` property. This will give you a `TransmitterChannel`. Check out the [sample pack](https://github.com/nanoframework/Samples/tree/main/samples/Hardware.Esp32.Rmt) to understand how to use it.
377+
The M5StickC/CPlus and Atom Lite/Matrix exposes an infrared led. You can access it thru the `InfraredLed` property. This will give you a `TransmitterChannel`. Check out the [sample pack](https://github.com/nanoframework/Samples/tree/main/samples/Hardware.Esp32.Rmt) to understand how to use it.
376378

377379
### NeoPixel
378380

@@ -383,6 +385,25 @@ The Atom Lite exposes a rgb led. You can access it thru the `NeoPixel` property:
383385
AtomLite.NeoPixel.SetColor(Color.Green);
384386
```
385387

388+
### RGB LED matrix
389+
390+
The Atom Matrix has a matrix of 25 RGB LEDs.
391+
The position of the LEDs in the array follows their placement in the matrix, being 0 the one at the top left corner, growing left to right, top to bottom.
392+
393+
You can access it thru the `LedMatrix` property, like this:
394+
395+
```csharp
396+
// This will set the RGB LED at position 0 to green
397+
AtomMatrix.LedMatrix.SetColor(0, Color.Green);
398+
```
399+
400+
After you're done with updating all the LEDs that you want to change, flush the updated to the LEDs, like this:
401+
402+
```csharp
403+
// This will update all RGB LED
404+
AtomMatrix.LedMatrix.Update();
405+
```
406+
386407
## Feedback and documentation
387408

388409
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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>88f1d73a-1adf-4444-a031-024e570945cc</ProjectGuid>
12+
<OutputType>Exe</OutputType>
13+
<AppDesignerFolder>Properties</AppDesignerFolder>
14+
<FileAlignment>512</FileAlignment>
15+
<RootNamespace>AtomLMatrixTestApp</RootNamespace>
16+
<AssemblyName>AtomLMatrixTestApp</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+
<None Include="packages.config" />
26+
</ItemGroup>
27+
<ItemGroup>
28+
<Reference Include="Iot.Device.Button, Version=0.0.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
29+
<HintPath>..\..\packages\nanoFramework.Iot.Device.Button.1.0.288-preview.29\lib\Iot.Device.Button.dll</HintPath>
30+
<Private>True</Private>
31+
<SpecificVersion>True</SpecificVersion>
32+
</Reference>
33+
<Reference Include="mscorlib, Version=1.12.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
34+
<HintPath>..\..\packages\nanoFramework.CoreLibrary.1.12.0-preview.5\lib\mscorlib.dll</HintPath>
35+
<Private>True</Private>
36+
<SpecificVersion>True</SpecificVersion>
37+
</Reference>
38+
<Reference Include="nanoFramework.Runtime.Events, Version=1.10.0.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
39+
<HintPath>..\..\packages\nanoFramework.Runtime.Events.1.10.0-preview.6\lib\nanoFramework.Runtime.Events.dll</HintPath>
40+
<Private>True</Private>
41+
<SpecificVersion>True</SpecificVersion>
42+
</Reference>
43+
<Reference Include="System.Device.Gpio, Version=1.0.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
44+
<HintPath>..\..\packages\nanoFramework.System.Device.Gpio.1.0.3-preview.8\lib\System.Device.Gpio.dll</HintPath>
45+
<Private>True</Private>
46+
<SpecificVersion>True</SpecificVersion>
47+
</Reference>
48+
<Reference Include="System.Drawing">
49+
<HintPath>..\..\packages\nanoFramework.System.Drawing.1.0.288-preview.20\lib\System.Drawing.dll</HintPath>
50+
</Reference>
51+
</ItemGroup>
52+
<ItemGroup>
53+
<ProjectReference Include="..\..\nanoFramework.AtomMatrix\nanoFramework.AtomMatrix.nfproj" />
54+
</ItemGroup>
55+
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
56+
<ProjectExtensions>
57+
<ProjectCapabilities>
58+
<ProjectConfigurationsDeclaredAsItems />
59+
</ProjectCapabilities>
60+
</ProjectExtensions>
61+
</Project>

0 commit comments

Comments
 (0)