Skip to content

Commit

Permalink
Merge pull request #38 from WildernessLabs/develop
Browse files Browse the repository at this point in the history
Merge to main for RC2-2
  • Loading branch information
jorgedevs authored Mar 5, 2023
2 parents 95b916f + 6606bfe commit b69c4a5
Show file tree
Hide file tree
Showing 8 changed files with 217 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
with:
repository: WildernessLabs/Meadow.Core
path: Meadow.Core
ref: ${{ env.GITHUB_REF_NAME }} # match Core branch to this branch
ref: develop
token: ${{ secrets.CI_ACCESS_TOKEN }}
- name: Checkout Meadow.Foundation
uses: actions/checkout@v3
Expand Down
26 changes: 26 additions & 0 deletions Source/Meadow.ProjectLab/IProjectLabHardware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,31 @@ public interface IProjectLabHardware
public PushButton? DownButton { get; }

public string RevisionString { get; }

public (IPin AN,
IPin RST,
IPin CS,
IPin SCK,
IPin CIPO,
IPin COPI,
IPin PWM,
IPin INT,
IPin RX,
IPin TX,
IPin SCL,
IPin SCA) MikroBus1Pins { get; }

public (IPin AN,
IPin RST,
IPin CS,
IPin SCK,
IPin CIPO,
IPin COPI,
IPin PWM,
IPin INT,
IPin RX,
IPin TX,
IPin SCL,
IPin SCA) MikroBus2Pins { get; }
}
}
18 changes: 11 additions & 7 deletions Source/Meadow.ProjectLab/ProjectLab.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static IProjectLabHardware Create()
ISpiBus spiBus;

// v2+ stuff
Mcp23008? mcp_1 = null;
Mcp23008? mcp1 = null;

logger?.Debug("Initializing Project Lab...");

Expand Down Expand Up @@ -59,31 +59,35 @@ public static IProjectLabHardware Create()

logger?.Debug("I2C Bus instantiated");

IDigitalInputPort? mcp1Interrupt = null;
IDigitalOutputPort? mcp1Reset = null;

try
{
// MCP the First
IDigitalInputPort mcp1_int = device.CreateDigitalInputPort(
device.Pins.D09, InterruptMode.EdgeRising, ResistorMode.InternalPullDown);
IDigitalOutputPort mcp_Reset = device.CreateDigitalOutputPort(device.Pins.D14);
mcp1Interrupt = device.CreateDigitalInputPort(device.Pins.D09, InterruptMode.EdgeRising, ResistorMode.InternalPullDown);
mcp1Reset = device.CreateDigitalOutputPort(device.Pins.D14);

mcp_1 = new Mcp23008(i2cBus, address: 0x20, mcp1_int, mcp_Reset);
mcp1 = new Mcp23008(i2cBus, address: 0x20, mcp1Interrupt, mcp1Reset);

logger?.Trace("Mcp_1 up");
}
catch (Exception e)
{
logger?.Debug($"Failed to create MCP1: {e.Message}, could be a v1 board");
mcp1Interrupt?.Dispose();
mcp1Reset?.Dispose();
}

if (mcp_1 == null)
if (mcp1 == null)
{
logger?.Debug("Instantiating Project Lab v1 specific hardware");
hardware = new ProjectLabHardwareV1(device, spiBus, i2cBus);
}
else
{
logger?.Info("Instantiating Project Lab v2 specific hardware");
hardware = new ProjectLabHardwareV2(device, spiBus, i2cBus, mcp_1);
hardware = new ProjectLabHardwareV2(device, spiBus, i2cBus, mcp1);
}

return hardware;
Expand Down
81 changes: 34 additions & 47 deletions Source/Meadow.ProjectLab/ProjectLabHardwareBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,56 @@ namespace Meadow.Devices
/// </summary>
public abstract class ProjectLabHardwareBase : IProjectLabHardware
{
/// <summary>
/// Get a reference to Meadow Logger
/// </summary>
protected Logger? Logger { get; } = Resolver.Log;

//==== properties

/// <summary>
/// Gets the SPI Bus
/// </summary>
public ISpiBus SpiBus { get; }

/// <summary>
/// Gets the I2C Bus
/// </summary>
public II2cBus I2cBus { get; }

/// <summary>
/// Gets the BH1750 Light Sensor on the Project Lab board
/// </summary>
public Bh1750? LightSensor { get; }

/// <summary>
/// Gets the BME688 environmental sensor on the Project Lab board
/// </summary>
public Bme688? EnvironmentalSensor { get; }

/// <summary>
/// Gets the Piezo noise maker on the Project Lab board
/// </summary>
public PiezoSpeaker? Speaker { get; }

/// <summary>
/// Gets the BMI inertial movement unit (IMU) on the Project Lab board
/// </summary>
public Bmi270? MotionSensor { get; }

/// <summary>
/// Gets the ST7789 Display on the Project Lab board
/// </summary>
public abstract St7789? Display { get; }

/// <summary>
/// Gets the Up PushButton on the Project Lab board
/// </summary>
public abstract PushButton? UpButton { get; }

/// <summary>
/// Gets the Down PushButton on the Project Lab board
/// </summary>
public abstract PushButton? DownButton { get; }

/// <summary>
/// Gets the Left PushButton on the Project Lab board
/// </summary>
Expand All @@ -64,17 +74,27 @@ public abstract class ProjectLabHardwareBase : IProjectLabHardware
/// Gets the Right PushButton on the Project Lab board
/// </summary>
public abstract PushButton? RightButton { get; }

/// <summary>
/// Gets the ProjectLab board hardware revision
/// </summary>
public virtual string RevisionString { get; set; } = "unknown";

public ProjectLabHardwareBase(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2cBus i2cBus)
/// <summary>
/// Get the ProjectLab pins for mikroBUS header 1
/// </summary>
public abstract (IPin AN, IPin RST, IPin CS, IPin SCK, IPin CIPO, IPin COPI, IPin PWM, IPin INT, IPin RX, IPin TX, IPin SCL, IPin SCA) MikroBus1Pins { get; protected set; }

/// <summary>
/// Get the ProjectLab pins for mikroBUS header 1
/// </summary>
public abstract (IPin AN, IPin RST, IPin CS, IPin SCK, IPin CIPO, IPin COPI, IPin PWM, IPin INT, IPin RX, IPin TX, IPin SCL, IPin SCA) MikroBus2Pins { get; protected set; }

internal ProjectLabHardwareBase(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2cBus i2cBus)
{
SpiBus = spiBus;
I2cBus = i2cBus;

//==== Initialize the shared/common stuff
try
{
Logger?.Trace("Instantiating light sensor");
Expand Down Expand Up @@ -104,7 +124,7 @@ public ProjectLabHardwareBase(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2
try
{
Logger?.Trace("Instantiating speaker");
Speaker = new PiezoSpeaker(device, device.Pins.D11);
Speaker = new PiezoSpeaker(device.Pins.D11);
Logger?.Trace("Speaker up");
}
catch (Exception ex)
Expand All @@ -122,7 +142,6 @@ public ProjectLabHardwareBase(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2
{
Resolver.Log.Error($"Unable to create the BMI270 IMU: {ex.Message}");
}

}

/// <summary>
Expand All @@ -139,53 +158,21 @@ public ProjectLabHardwareBase(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2
/// Gets the pin definitions for the Project Lab board
/// </summary>
public static (
IPin MB1_CS,
IPin MB1_INT,
IPin MB1_PWM,
IPin MB1_AN,
IPin MB1_SO,
IPin MB1_SI,
IPin MB1_SCK,
IPin MB1_SCL,
IPin MB1_SDA,

IPin MB2_CS,
IPin MB2_INT,
IPin MB2_PWM,
IPin MB2_AN,
IPin MB2_SO,
IPin MB2_SI,
IPin MB2_SCK,
IPin MB2_SCL,
IPin MB2_SDA,

IPin A0,
IPin A1,
IPin A2,
IPin D03,
IPin D04
IPin D04,
IPin D12,
IPin D13
) Pins = (
Resolver.Device.GetPin("D14"),
Resolver.Device.GetPin("D03"),
Resolver.Device.GetPin("D04"),
Resolver.Device.GetPin("A00"),
Resolver.Device.GetPin("CIPO"),
Resolver.Device.GetPin("COPI"),
Resolver.Device.GetPin("SCK"),
Resolver.Device.GetPin("D08"),
Resolver.Device.GetPin("D07"),

Resolver.Device.GetPin("A02"),
Resolver.Device.GetPin("D04"),
Resolver.Device.GetPin("D03"),
Resolver.Device.GetPin("A01"),
Resolver.Device.GetPin("CIPO"),
Resolver.Device.GetPin("COPI"),
Resolver.Device.GetPin("SCK"),
Resolver.Device.GetPin("D08"),
Resolver.Device.GetPin("D07"),

Resolver.Device.GetPin("A00"),
Resolver.Device.GetPin("A02"),
Resolver.Device.GetPin("D03"),
Resolver.Device.GetPin("D04")
Resolver.Device.GetPin("D04"),
Resolver.Device.GetPin("D12"),
Resolver.Device.GetPin("D13")
);
}
}
59 changes: 52 additions & 7 deletions Source/Meadow.ProjectLab/ProjectLabHardwareV1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,51 @@

namespace Meadow.Devices
{
internal class ProjectLabHardwareV1 : ProjectLabHardwareBase
public class ProjectLabHardwareV1 : ProjectLabHardwareBase
{
private string revision = "v1.x";

/// <summary>
/// Gets the ST7789 Display on the Project Lab board
/// </summary>
public override St7789? Display { get; }

/// <summary>
/// Gets the Up PushButton on the Project Lab board
/// </summary>
public override PushButton? UpButton { get; }

/// <summary>
/// Gets the Down PushButton on the Project Lab board
/// </summary>
public override PushButton? DownButton { get; }

/// <summary>
/// Gets the Left PushButton on the Project Lab board
/// </summary>
public override PushButton? LeftButton { get; }

/// <summary>
/// Gets the Right PushButton on the Project Lab board
/// </summary>
public override PushButton? RightButton { get; }

public ProjectLabHardwareV1(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2cBus i2cBus)
/// <summary>
/// Get the ProjectLab pins for mikroBUS header 1
/// </summary>
public override (IPin AN, IPin RST, IPin CS, IPin SCK, IPin CIPO, IPin COPI, IPin PWM, IPin INT, IPin RX, IPin TX, IPin SCL, IPin SCA) MikroBus1Pins { get; protected set; }

/// <summary>
/// Get the ProjectLab pins for mikroBUS header 2
/// </summary>
public override (IPin AN, IPin RST, IPin CS, IPin SCK, IPin CIPO, IPin COPI, IPin PWM, IPin INT, IPin RX, IPin TX, IPin SCL, IPin SCA) MikroBus2Pins { get; protected set; }

internal ProjectLabHardwareV1(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2cBus i2cBus)
: base(device, spiBus, i2cBus)
{
//---- create our display
Logger?.Trace("Instantiating display");
Display = new St7789(
device: device,
spiBus: SpiBus,
chipSelectPin: device.Pins.A03,
dcPin: device.Pins.A04,
Expand All @@ -57,25 +70,57 @@ public ProjectLabHardwareV1(IF7FeatherMeadowDevice device, ISpiBus spiBus, II2cB
UpButton = GetPushButton(device, device.Pins.D15);
DownButton = GetPushButton(device, device.Pins.D02);
Logger?.Trace("Buttons up");

SetMikroBusPins();
}

void SetMikroBusPins()
{
MikroBus1Pins =
(Resolver.Device.GetPin("A00"),
null,
Resolver.Device.GetPin("D14"),
Resolver.Device.GetPin("SCK"),
Resolver.Device.GetPin("CIPO"),
Resolver.Device.GetPin("COPI"),
Resolver.Device.GetPin("D04"),
Resolver.Device.GetPin("D03"),
Resolver.Device.GetPin("D12"),
Resolver.Device.GetPin("D13"),
Resolver.Device.GetPin("D07"),
Resolver.Device.GetPin("D08"));

MikroBus2Pins =
(Resolver.Device.GetPin("A01"),
null,
Resolver.Device.GetPin("A02"),
Resolver.Device.GetPin("SCK"),
Resolver.Device.GetPin("CIPO"),
Resolver.Device.GetPin("COPI"),
Resolver.Device.GetPin("D03"),
Resolver.Device.GetPin("D04"),
Resolver.Device.GetPin("D12"),
Resolver.Device.GetPin("D13"),
Resolver.Device.GetPin("D07"),
Resolver.Device.GetPin("D08"));
}

public override string RevisionString => revision;

private PushButton GetPushButton(IF7FeatherMeadowDevice device, IPin pin)
=> new PushButton(Resolver.Device, pin, ResistorMode.InternalPullDown);
=> new PushButton(pin, ResistorMode.InternalPullDown);

public override ModbusRtuClient GetModbusRtuClient(int baudRate = 19200, int dataBits = 8, Parity parity = Parity.None, StopBits stopBits = StopBits.One)
{
if (Resolver.Device is F7FeatherV1 device)
if (Resolver.Device is F7FeatherBase device)
{
var portName = device.PlatformOS.GetSerialPortName("com4");
var port = device.CreateSerialPort(portName, baudRate, dataBits, parity, stopBits);
port.WriteTimeout = port.ReadTimeout = TimeSpan.FromSeconds(5);
var serialEnable = device.CreateDigitalOutputPort(device.Pins.D09, false);
return new ModbusRtuClient(port, serialEnable);
return new ProjectLabModbusRtuClient(port, serialEnable);
}

// this is v1 instance hardware, so we should never get here
throw new NotSupportedException();
}
}
Expand Down
Loading

0 comments on commit b69c4a5

Please sign in to comment.