-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from WildernessLabs/feature/chromatek
Added ChromaTek momentary button
- Loading branch information
Showing
9 changed files
with
370 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
using Meadow.Foundation.Leds; | ||
using Meadow.Hardware; | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
namespace Meadow.Foundation.Switches.ChromaTek; | ||
|
||
/// <summary> | ||
/// Represents a collection of ChromaTek buttons with WS2812 color LEDs | ||
/// </summary> | ||
public class ButtonCollection : IEnumerable<IChromaTekButton> | ||
{ | ||
private Ws2812 ws2812 = default!; | ||
private List<IChromaTekButton> buttons = new(); | ||
|
||
/// <summary> | ||
/// Creates a collection of buttons | ||
/// </summary> | ||
/// <param name="bus">The SPI bus that all buttons are connected to</param> | ||
/// <param name="buttons">The list of button on the bus</param> | ||
public ButtonCollection(ISpiBus bus, params IChromaTekButton[] buttons) | ||
{ | ||
ws2812 = new Ws2812(bus, buttons.Length); | ||
var index = 0; | ||
|
||
foreach (var button in buttons) | ||
{ | ||
switch (button) | ||
{ | ||
case MomentaryButton mb: | ||
mb.LedController = ws2812; | ||
mb.ButtonIndex = index; | ||
this.buttons.Add(mb); | ||
break; | ||
case LatchingButton lb: | ||
lb.LedController = ws2812; | ||
lb.ButtonIndex = index; | ||
this.buttons.Add(lb); | ||
break; | ||
default: throw new ArgumentException("Button is not a ChromaTek button"); | ||
|
||
}; | ||
index++; | ||
} | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IChromaTekButton this[int index] | ||
{ | ||
get => buttons[index]; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public IEnumerator<IChromaTekButton> GetEnumerator() | ||
{ | ||
return buttons.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
using Meadow.Hardware; | ||
using Meadow.Peripherals.Sensors; | ||
|
||
namespace Meadow.Foundation.Switches.ChromaTek; | ||
|
||
/// <summary> | ||
/// Represents a ChromaTek button with a WS2812 color LED | ||
/// </summary> | ||
public interface IChromaTekButton : IColorable, ISensor<bool> | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Meadow.Foundation.Leds; | ||
using Meadow.Foundation.Sensors.Switches; | ||
using Meadow.Hardware; | ||
using System; | ||
|
||
namespace Meadow.Foundation.Switches.ChromaTek; | ||
|
||
/// <summary> | ||
/// Represents a latching push button with a WS2812 color LED | ||
/// </summary> | ||
public class LatchingButton : SpstSwitch, IChromaTekButton | ||
{ | ||
private ISpiBus? bus = null; | ||
private Color color = Color.Black; | ||
|
||
internal Ws2812? LedController { get; set; } = default!; | ||
internal int ButtonIndex { get; set; } = 0; | ||
|
||
/// <summary> | ||
/// Creates an instance of a ChromaTekLatchingButton | ||
/// </summary> | ||
/// <param name="bus">The SPI bus COPI line connected to the WS2812 data line</param> | ||
/// <param name="inputPort">The interrupt port connected to the switch</param> | ||
public LatchingButton(IDigitalInterruptPort inputPort, ISpiBus? bus = null) | ||
: base(inputPort) | ||
{ | ||
this.bus = bus; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of a ChromaTekLatchingButton | ||
/// </summary> | ||
/// <param name="bus">The SPI bus COPI line connected to the WS2812 data line</param> | ||
/// <param name="pin">The IPin connected to the switch</param> | ||
/// <param name="interruptMode">The interrupt mode for the switch pin</param> | ||
/// <param name="resistorMode">The resistor mode for the switch pin</param> | ||
public LatchingButton(IPin pin, InterruptMode interruptMode, ResistorMode resistorMode, ISpiBus? bus = null) | ||
: base(pin, interruptMode, resistorMode) | ||
{ | ||
this.bus = bus; | ||
} | ||
|
||
private void Initialize() | ||
{ | ||
if (LedController != null) return; | ||
|
||
if (bus == null) | ||
{ | ||
throw new Exception("This button must either be constructed withan ISpiBus or added to a ButtonCollection"); | ||
} | ||
|
||
LedController = new Ws2812(bus, 1); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void SetColor(Color color) | ||
{ | ||
if (LedController == null) | ||
{ | ||
Initialize(); | ||
} | ||
|
||
LedController?.SetLed(ButtonIndex, color); | ||
LedController?.Show(); | ||
this.color = color; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public Color GetColor() => color; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using Meadow.Foundation.Leds; | ||
using Meadow.Foundation.Sensors.Buttons; | ||
using Meadow.Hardware; | ||
using System; | ||
|
||
namespace Meadow.Foundation.Switches.ChromaTek; | ||
|
||
/// <summary> | ||
/// Represents a momentary push button with a WS2812 color LED | ||
/// </summary> | ||
public class MomentaryButton : PushButton, IChromaTekButton | ||
{ | ||
private ISpiBus? bus = null; | ||
private Color color = Color.Black; | ||
|
||
internal Ws2812 LedController { get; set; } = default!; | ||
internal int ButtonIndex { get; set; } = 0; | ||
|
||
/// <summary> | ||
/// Creates an instance of a ChromaTekMomentaryButton | ||
/// </summary> | ||
/// <param name="bus">The SPI bus COPI line connected to the WS2812 data line</param> | ||
/// <param name="inputPort">The interrupt port connected to the switch</param> | ||
public MomentaryButton(IDigitalInterruptPort inputPort, ISpiBus? bus = null) | ||
: base(inputPort) | ||
{ | ||
this.bus = bus; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of a ChromaTekMomentaryButton | ||
/// </summary> | ||
/// <param name="bus">The SPI bus COPI line connected to the WS2812 data line</param> | ||
/// <param name="pin">The IPin connected to the switch</param> | ||
/// <param name="resistorMode">The resistor mode for the switch pin</param> | ||
public MomentaryButton(IPin pin, ResistorMode resistorMode, ISpiBus? bus = null) | ||
: base(pin, resistorMode) | ||
{ | ||
this.bus = bus; | ||
} | ||
|
||
/// <summary> | ||
/// Creates an instance of a ChromaTekMomentaryButton | ||
/// </summary> | ||
/// <param name="bus">The SPI bus COPI line connected to the WS2812 data line</param> | ||
/// <param name="pin">The IPin connected to the switch</param> | ||
/// <param name="resistorMode">The resistor mode for the switch pin</param> | ||
/// <param name="debounceDuration">Debounce duration for the interrupt pin</param> | ||
public MomentaryButton(IPin pin, ResistorMode resistorMode, TimeSpan debounceDuration, ISpiBus? bus = null) | ||
: base(pin, resistorMode, debounceDuration) | ||
{ | ||
this.bus = bus; | ||
} | ||
|
||
private void Initialize() | ||
{ | ||
if (LedController != null) return; | ||
|
||
if (bus == null) | ||
{ | ||
throw new Exception("This button must either be constructed withan ISpiBus or added to a ButtonCollection"); | ||
} | ||
|
||
LedController = new Ws2812(bus, 1); | ||
} | ||
|
||
/// <inheritdoc/> | ||
public void SetColor(Color color) | ||
{ | ||
if (LedController == null) | ||
{ | ||
Initialize(); | ||
} | ||
|
||
LedController?.SetLed(ButtonIndex, color); | ||
LedController?.Show(); | ||
this.color = color; | ||
} | ||
|
||
/// <inheritdoc/> | ||
public Color GetColor() => color; | ||
} |
28 changes: 28 additions & 0 deletions
28
Source/Switches.ChromaTek/Driver/Switches.ChromaTek.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
<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>Switches.ChromaTek</AssemblyName> | ||
<Company>Wilderness Labs, Inc</Company> | ||
<PackageProjectUrl>http://developer.wildernesslabs.co/Meadow/Meadow.Foundation/</PackageProjectUrl> | ||
<PackageId>Meadow.Foundation.Switches.ChromaTek</PackageId> | ||
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl> | ||
<PackageTags>Meadow.Foundation,Meadow,WS2812,Neopixel Button,ChromaTek</PackageTags> | ||
<GeneratePackageOnBuild>true</GeneratePackageOnBuild> | ||
<Description>SPI-driven ChromaTek multi-color illuminated switches</Description> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<None Include="..\..\icon.png" Pack="true" PackagePath="" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<ProjectReference Include="..\..\..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\ICs.IOExpanders.Pcx857x\Driver\ICs.IOExpanders.Pcx857x.csproj" /> | ||
<ProjectReference Include="..\..\..\..\Meadow.Foundation\Source\Meadow.Foundation.Peripherals\Leds.Ws2812\Driver\Leds.Ws2812.csproj" /> | ||
</ItemGroup> | ||
</Project> |
12 changes: 12 additions & 0 deletions
12
Source/Switches.ChromaTek/Samples/ChromaTek_Sample/ChromaTek_Sample.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\Switches.ChromaTek.csproj" /> | ||
</ItemGroup> | ||
</Project> |
43 changes: 43 additions & 0 deletions
43
Source/Switches.ChromaTek/Samples/ChromaTek_Sample/MeadowApp.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using Meadow; | ||
using Meadow.Devices; | ||
using Meadow.Foundation.Switches.ChromaTek; | ||
using Meadow.Units; | ||
using System; | ||
using System.Threading.Tasks; | ||
|
||
namespace Switches.ChromaTek_Sample | ||
{ | ||
public class MeadowApp : App<F7FeatherV2> | ||
{ | ||
//<!=SNIP=> | ||
|
||
private MomentaryButton _button = default!; | ||
private readonly Color _normalColor = Color.Green; | ||
private readonly Color _pressedColor = Color.Red; | ||
|
||
public override Task Initialize() | ||
{ | ||
Resolver.Log.Info("Initialize..."); | ||
|
||
var bus = Device.CreateSpiBus(new Frequency(2.5, Frequency.UnitType.Megahertz)); | ||
_button = new MomentaryButton(Device.Pins.D04, Meadow.Hardware.ResistorMode.InternalPullUp, bus); | ||
_button.SetColor(_normalColor); | ||
_button.Clicked += OnButtonClicked; | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
private void OnButtonClicked(object sender, EventArgs e) | ||
{ | ||
Resolver.Log.Info("Click"); | ||
Task.Run(async () => | ||
{ | ||
_button.SetColor(_pressedColor); | ||
await Task.Delay(3000); | ||
_button.SetColor(_normalColor); | ||
}); | ||
} | ||
|
||
//<!=SNOP=> | ||
} | ||
} |
Oops, something went wrong.