Skip to content

Commit

Permalink
Merge pull request #70 from WildernessLabs/can-bus-wing
Browse files Browse the repository at this point in the history
Added CAN wing
  • Loading branch information
adrianstevens authored Oct 21, 2024
2 parents 2a03375 + 1438290 commit 943d183
Show file tree
Hide file tree
Showing 5 changed files with 162 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Source/CanBusWing/Driver/CanBusWing.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Meadow.Foundation.ICs.CAN;
using Meadow.Hardware;
using Meadow.Units;

namespace Meadow.Foundation.FeatherWings
{
/// <summary>
/// Represents an Adafruit CAN Bus featherwing
/// </summary>
public class CanBusWing : Mcp2515
{
/// <summary>
/// Creates a CanBusWing driver
/// </summary>
public CanBusWing(IF7FeatherMeadowDevice feather)
: base(
bus: feather.CreateSpiBus(3, 1_000_000.Hertz()),
chipSelect: feather.Pins.D09.CreateDigitalOutputPort(true),
interruptPort: feather.Pins.D10.CreateDigitalInterruptPort(InterruptMode.EdgeFalling),
oscillator: CanOscillator.Osc_16MHz)
{ }
}
}
28 changes: 28 additions & 0 deletions Source/CanBusWing/Driver/CanBusWing.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<Version>1.11.0</Version>
<Nullable>enable</Nullable>
<LangVersion>10.0</LangVersion>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<PackageIcon>icon.png</PackageIcon>
<Authors>Wilderness Labs, Inc</Authors>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
<AssemblyName>CanBusWing</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl>
<PackageId>Meadow.Foundation.FeatherWings.GPSWing</PackageId>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation.FeatherWings</RepositoryUrl>
<PackageTags>Meadow.Foundation, CanBusWing, CAN</PackageTags>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>AdaFruit CAN Bus FeatherWing</Description>
</PropertyGroup>
<ItemGroup>
<None Include="..\..\icon.png" Pack="true" PackagePath="" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\Meadow.Core\Source\implementations\f7\Meadow.F7\Meadow.F7.csproj" />
<ProjectReference Include="..\..\..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\ICs.CAN.Mcp2515\Driver\ICs.CAN.Mcp2515.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<OutputType>Library</OutputType>
<AssemblyName>App</AssemblyName>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\..\..\Meadow.Core\Source\implementations\f7\Meadow.F7\Meadow.F7.csproj" />
<ProjectReference Include="..\..\Driver\CanBusWing.csproj" />
</ItemGroup>
</Project>
67 changes: 67 additions & 0 deletions Source/CanBusWing/Samples/CanBusWing_Sample/MeadowApp.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Meadow;
using Meadow.Devices;
using Meadow.Foundation.FeatherWings;
using Meadow.Hardware;
using System;
using System.Threading.Tasks;

namespace MeadowApp
{
public class MeadowApp : App<F7FeatherV2>
{
//<!=SNIP=>

private CanBusWing wing;

public override Task Initialize()
{
Console.WriteLine("Initialize...");

wing = new CanBusWing(Device);

return Task.CompletedTask;
}

public override async Task Run()
{
var bus = wing.CreateCanBus(CanBitrate.Can_250kbps);

Console.WriteLine($"Listening for CAN data...");

var tick = 0;

while (true)
{
var frame = bus.ReadFrame();
if (frame != null)
{
if (frame is StandardDataFrame sdf)
{
Console.WriteLine($"Standard Frame: {sdf.ID:X3} {BitConverter.ToString(sdf.Payload)}");
}
else if (frame is ExtendedDataFrame edf)
{
Console.WriteLine($"Extended Frame: {edf.ID:X8} {BitConverter.ToString(edf.Payload)}");
}
}
else
{
await Task.Delay(100);
}

if (tick++ % 50 == 0)
{
Console.WriteLine($"Sending Standard Frame...");

bus.WriteFrame(new StandardDataFrame
{
ID = 0x700,
Payload = new byte[] { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, (byte)(tick & 0xff) }
});
}
}
}

//<!=SNOP=>
}
}
32 changes: 32 additions & 0 deletions Source/FeatherWings.sln
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Displays.Sh110x", "..\..\Me
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Servos", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Libraries_and_Frameworks\Servos\Driver\Servos.csproj", "{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "CanBusWing", "CanBusWing", "{1F1C8ECF-78B9-499D-9C88-CD15CA67C8C2}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanBusWing", "CanBusWing\Driver\CanBusWing.csproj", "{6C912C2A-2B18-49B5-A87D-C9DC38913F03}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ICs.CAN.Mcp2515", "..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\ICs.CAN.Mcp2515\Driver\ICs.CAN.Mcp2515.csproj", "{CE4884C9-410E-4BA2-985A-F8777BDE3AEC}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Sample", "Sample", "{828928C2-ABC1-4565-AEE1-D98550C70552}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CanBusWing_Sample", "CanBusWing\Samples\CanBusWing_Sample\CanBusWing_Sample.csproj", "{3B2AE891-DC5B-44FB-861D-528D9EF16010}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -405,6 +415,24 @@ Global
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Release|Any CPU.Build.0 = Release|Any CPU
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B}.Release|Any CPU.Deploy.0 = Release|Any CPU
{6C912C2A-2B18-49B5-A87D-C9DC38913F03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6C912C2A-2B18-49B5-A87D-C9DC38913F03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6C912C2A-2B18-49B5-A87D-C9DC38913F03}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{6C912C2A-2B18-49B5-A87D-C9DC38913F03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6C912C2A-2B18-49B5-A87D-C9DC38913F03}.Release|Any CPU.Build.0 = Release|Any CPU
{6C912C2A-2B18-49B5-A87D-C9DC38913F03}.Release|Any CPU.Deploy.0 = Release|Any CPU
{CE4884C9-410E-4BA2-985A-F8777BDE3AEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CE4884C9-410E-4BA2-985A-F8777BDE3AEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CE4884C9-410E-4BA2-985A-F8777BDE3AEC}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{CE4884C9-410E-4BA2-985A-F8777BDE3AEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CE4884C9-410E-4BA2-985A-F8777BDE3AEC}.Release|Any CPU.Build.0 = Release|Any CPU
{CE4884C9-410E-4BA2-985A-F8777BDE3AEC}.Release|Any CPU.Deploy.0 = Release|Any CPU
{3B2AE891-DC5B-44FB-861D-528D9EF16010}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3B2AE891-DC5B-44FB-861D-528D9EF16010}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3B2AE891-DC5B-44FB-861D-528D9EF16010}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{3B2AE891-DC5B-44FB-861D-528D9EF16010}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3B2AE891-DC5B-44FB-861D-528D9EF16010}.Release|Any CPU.Build.0 = Release|Any CPU
{3B2AE891-DC5B-44FB-861D-528D9EF16010}.Release|Any CPU.Deploy.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -465,6 +493,10 @@ Global
{7068B557-1B72-44C1-BF2B-36EFBB8E78D3} = {8AEC28BA-BACD-47A5-BFEC-95F7624B0B7F}
{640E9537-46D5-4CCE-9A2A-DEAE752B0FC8} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{0A969417-E71C-45AC-8C7F-0B0328FDCE0B} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{6C912C2A-2B18-49B5-A87D-C9DC38913F03} = {1F1C8ECF-78B9-499D-9C88-CD15CA67C8C2}
{CE4884C9-410E-4BA2-985A-F8777BDE3AEC} = {BB1A7E97-4559-459B-BDF8-4ED41758AB29}
{828928C2-ABC1-4565-AEE1-D98550C70552} = {1F1C8ECF-78B9-499D-9C88-CD15CA67C8C2}
{3B2AE891-DC5B-44FB-861D-528D9EF16010} = {828928C2-ABC1-4565-AEE1-D98550C70552}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {673CD5FD-E4C7-48D7-85EA-3CB6FAD87939}
Expand Down

0 comments on commit 943d183

Please sign in to comment.