Skip to content

Commit

Permalink
Add Display Connector
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianstevens committed Oct 7, 2023
1 parent 093a1cf commit f4282d3
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 9 deletions.
102 changes: 102 additions & 0 deletions Source/Juego/DisplayConnector.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System;
using static Meadow.Hardware.DisplayConnector;

namespace Meadow.Hardware;

/// <summary>
/// Represents the display connector on Juego
/// </summary>
public class DisplayConnector : Connector<DisplayConnectorPinDefinitions>
{
/// <summary>
/// The set of Display connector connector pins
/// </summary>
public static class PinNames
{
/// <summary>
/// Chip Select pin
/// </summary>
public const string CS = "CS";
/// <summary>
/// Reset pin
/// </summary>
public const string RST = "RST";
/// <summary>
/// Data/Command pin
/// </summary>
public const string DC = "DC";
/// <summary>
/// SPI Clock pin
/// </summary>
public const string CLK = "CLK";
/// <summary>
/// SPI controller ouy, peripheral in pin
/// </summary>
public const string COPI = "COPI";
}

/// <summary>
/// Represents the pins definitions for the Display connector on Juego
/// </summary>
public class DisplayConnectorPinDefinitions : PinDefinitionBase
{
private readonly IPin? _cs;
private readonly IPin? _rst;
private readonly IPin? _dc;
private readonly IPin? _clk;
private readonly IPin? _copi;

/// <summary>
/// Chip Select pin
/// </summary>
public IPin CS => _cs ?? throw new PlatformNotSupportedException("Pin not connected");
/// <summary>
/// Reset pin
/// </summary>
public IPin RST => _rst ?? throw new PlatformNotSupportedException("Pin not connected");
/// <summary>
/// Data/Command pin
/// </summary>
public IPin DC => _dc ?? throw new PlatformNotSupportedException("Pin not connected");
/// <summary>
/// SPI Clock pin
/// </summary>
public IPin CLK => _clk ?? throw new PlatformNotSupportedException("Pin not connected");
/// <summary>
/// SPI controller in, peripheral out pin
/// </summary>
public IPin COPI => _copi ?? throw new PlatformNotSupportedException("Pin not connected");

internal DisplayConnectorPinDefinitions(PinMapping mapping)
{
foreach (var m in mapping)
{
switch (m.PinName)
{
case PinNames.CS:
_cs = m.ConnectsTo;
break;
case PinNames.RST:
_rst = m.ConnectsTo;
break;
case PinNames.DC:
_dc = m.ConnectsTo;
break;
case PinNames.CLK:
_clk = m.ConnectsTo;
break;
case PinNames.COPI:
_copi = m.ConnectsTo;
break;
}
}
}
}

/// <param name="name">The connector name</param>
/// <param name="mapping">The mappings to the host controller</param>
public DisplayConnector(string name, PinMapping mapping)
: base(name, new DisplayConnectorPinDefinitions(mapping))
{
}
}
52 changes: 52 additions & 0 deletions Source/Juego/IJuegoHardware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,81 @@
using Meadow.Foundation.Graphics;
using Meadow.Foundation.Leds;
using Meadow.Foundation.Sensors.Buttons;
using Meadow.Hardware;

namespace WildernessLabs.Hardware.Juego
{
/// <summary>
/// Represents the hardware interface for the Juego device.
/// </summary>
public interface IJuegoHardware
{
/// <summary>
/// Gets the graphics display interface.
/// </summary>
public IGraphicsDisplay? Display { get; }

/// <summary>
/// Gets the right/up button
/// </summary>
public PushButton? Right_UpButton { get; }
/// <summary>
/// Gets the right/down button
/// </summary>
public PushButton? Right_DownButton { get; }
/// <summary>
/// Gets the right/left button
/// </summary>
public PushButton? Right_LeftButton { get; }
/// <summary>
/// Gets the right/right button
/// </summary>
public PushButton? Right_RightButton { get; }

/// <summary>
/// Gets the left/up button
/// </summary>
public PushButton? Left_UpButton { get; }
/// <summary>
/// Gets the left/down button
/// </summary>
public PushButton? Left_DownButton { get; }
/// <summary>
/// Gets the left/left button
/// </summary>
public PushButton? Left_LeftButton { get; }
/// <summary>
/// Gets the left/right button
/// </summary>
public PushButton? Left_RightButton { get; }

/// <summary>
/// Gets the start button
/// </summary>
public PushButton? StartButton { get; }
/// <summary>
/// Gets the select button
/// </summary>
public PushButton? SelectButton { get; }

// Speakers
/// <summary>
/// Gets the left speaker
/// </summary>
public PiezoSpeaker? LeftSpeaker { get; }
/// <summary>
/// Gets the right speaker
/// </summary>
public PiezoSpeaker? RightSpeaker { get; }

/// <summary>
/// Gets the PWM LED
/// </summary>
public PwmLed? BlinkyLed { get; }

/// <summary>
/// Gets the display header connector
/// </summary>
public DisplayConnector DisplayHeader { get; }
}
}
48 changes: 42 additions & 6 deletions Source/Juego/JuegoHardwareV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class JuegoHardwareV1 : IJuegoHardware

public IGraphicsDisplay Display { get; }

protected ISpiBus Spi { get; }
protected ISpiBus SpiBus { get; }

public AnalogJoystick? AnalogJoystick { get; protected set; }

Expand All @@ -39,6 +39,30 @@ public class JuegoHardwareV1 : IJuegoHardware

public PwmLed? BlinkyLed => null;

/// <summary>
/// Gets the display header connector on the Juego board
/// </summary>
public DisplayConnector DisplayHeader => (DisplayConnector)Connectors[0];

/// <summary>
/// Collection of connectors on the Juego board
/// </summary>
public IConnector?[] Connectors
{
get
{
if (_connectors == null)
{
_connectors = new IConnector[1];
_connectors[1] = CreateDisplayConnector();
}

return _connectors;
}
}

private IConnector?[]? _connectors;

public JuegoHardwareV1(IF7FeatherMeadowDevice device)
{
Device = device;
Expand All @@ -65,7 +89,7 @@ public JuegoHardwareV1(IF7FeatherMeadowDevice device)
try
{
var config = new SpiClockConfiguration(new Frequency(48000, Frequency.UnitType.Kilohertz), SpiClockConfiguration.Mode.Mode0);
Spi = Device.CreateSpiBus(Device.Pins.SCK, Device.Pins.COPI, Device.Pins.CIPO, config);
SpiBus = Device.CreateSpiBus(Device.Pins.SCK, Device.Pins.COPI, Device.Pins.CIPO, config);
Resolver.Log.Info("SPI initialized");
}
catch (Exception e)
Expand All @@ -78,7 +102,7 @@ public JuegoHardwareV1(IF7FeatherMeadowDevice device)
var resetPort = Device.CreateDigitalOutputPort(Device.Pins.D14);

Display = new St7789(
spiBus: Spi,
spiBus: SpiBus,
chipSelectPort: chipSelectPort,
dataCommandPort: dcPort,
resetPort: resetPort,
Expand All @@ -91,10 +115,22 @@ public JuegoHardwareV1(IF7FeatherMeadowDevice device)
Right_RightButton = new PushButton(device.Pins.D11, ResistorMode.InternalPullDown);
StartButton = new PushButton(device.Pins.D13, ResistorMode.InternalPullDown);
SelectButton = new PushButton(device.Pins.D15, ResistorMode.InternalPullDown);
}

//ToDo confirm analog pins and wire it up
//AnalogJoystick = new AnalogJoystick(Device.Pins.A00.CreateAnalogInputPort(),
// Device.CreateAnalogInputPort(Device.Pins.A01));
internal DisplayConnector CreateDisplayConnector()
{
Resolver.Log.Trace("Creating display connector");

return new DisplayConnector(
"Display",
new PinMapping
{
new PinMapping.PinAlias(DisplayConnector.PinNames.CS, Device.Pins.D03),
new PinMapping.PinAlias(DisplayConnector.PinNames.RST, Device.Pins.D14),
new PinMapping.PinAlias(DisplayConnector.PinNames.DC, Device.Pins.D04),
new PinMapping.PinAlias(DisplayConnector.PinNames.CLK, Device.Pins.SCK),
new PinMapping.PinAlias(DisplayConnector.PinNames.COPI, Device.Pins.COPI),
});
}
}
}
46 changes: 43 additions & 3 deletions Source/Juego/JuegoHardwareV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Meadow.Foundation.Leds;
using Meadow.Foundation.Sensors.Buttons;
using Meadow.Hardware;
using Meadow.Logging;
using Meadow.Units;
using System;
using System.Threading;
Expand All @@ -26,7 +27,7 @@ public class JuegoHardwareV2 : IJuegoHardware
public IDigitalOutputPort DisplayBacklightPort { get; }

protected II2cBus I2cBus { get; }
protected ISpiBus Spi { get; }
protected ISpiBus SpiBus { get; }

public Mcp23008 Mcp_1 { get; protected set; }
public Mcp23008 Mcp_2 { get; protected set; }
Expand All @@ -50,6 +51,30 @@ public class JuegoHardwareV2 : IJuegoHardware

public PwmLed? BlinkyLed { get; protected set; }

/// <summary>
/// Gets the display header connector on the Juego board
/// </summary>
public DisplayConnector DisplayHeader => (DisplayConnector)Connectors[0];

/// <summary>
/// Collection of connectors on the Juego board
/// </summary>
public IConnector?[] Connectors
{
get
{
if (_connectors == null)
{
_connectors = new IConnector[1];
_connectors[1] = CreateDisplayConnector();
}

return _connectors;
}
}

private IConnector?[]? _connectors;

public JuegoHardwareV2(IF7CoreComputeMeadowDevice device)
{
Device = device;
Expand Down Expand Up @@ -130,7 +155,7 @@ public JuegoHardwareV2(IF7CoreComputeMeadowDevice device)
try
{
var config = new SpiClockConfiguration(new Frequency(48000, Frequency.UnitType.Kilohertz), SpiClockConfiguration.Mode.Mode0);
Spi = Device.CreateSpiBus(Device.Pins.SPI5_SCK, Device.Pins.SPI5_COPI, Device.Pins.SPI5_CIPO, config);
SpiBus = Device.CreateSpiBus(Device.Pins.SPI5_SCK, Device.Pins.SPI5_COPI, Device.Pins.SPI5_CIPO, config);
}
catch (Exception e)
{
Expand All @@ -149,7 +174,7 @@ public JuegoHardwareV2(IF7CoreComputeMeadowDevice device)
Thread.Sleep(50);

Display = new Ili9341(
spiBus: Spi,
spiBus: SpiBus,
chipSelectPort: chipSelectPort,
dataCommandPort: dcPort,
resetPort: resetPort,
Expand Down Expand Up @@ -193,5 +218,20 @@ public JuegoHardwareV2(IF7CoreComputeMeadowDevice device)
SelectButton = new PushButton(selectPort);
}
}
internal DisplayConnector CreateDisplayConnector()
{
Resolver.Log.Trace("Creating display connector");

return new DisplayConnector(
"Display",
new PinMapping
{
new PinMapping.PinAlias(DisplayConnector.PinNames.CS, Mcp_1.Pins.GP5),
new PinMapping.PinAlias(DisplayConnector.PinNames.RST, Mcp_1.Pins.GP7),
new PinMapping.PinAlias(DisplayConnector.PinNames.DC, Mcp_1.Pins.GP6),
new PinMapping.PinAlias(DisplayConnector.PinNames.CLK, Device.Pins.SCK),
new PinMapping.PinAlias(DisplayConnector.PinNames.COPI, Device.Pins.COPI),
});
}
}
}

0 comments on commit f4282d3

Please sign in to comment.