Skip to content

Commit

Permalink
Refactor GNSS Tracker (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgedevs committed Feb 8, 2024
1 parent ff3217c commit 126d0e5
Show file tree
Hide file tree
Showing 7 changed files with 524 additions and 184 deletions.
5 changes: 2 additions & 3 deletions Source/GnssTracker/GnssTracker.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using Meadow;
using Meadow.Hardware;
using Meadow.Hardware;
using Meadow.Logging;
using System;

namespace WildernessLabs.Hardware.GnssTracker
namespace Meadow.Devices
{
/// <summary>
/// Represents a Gnss Tracker IoT acclerator
Expand Down
103 changes: 54 additions & 49 deletions Source/GnssTracker/GnssTrackerHardareBase.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
using Meadow;
using Meadow.Foundation.Displays;
using Meadow.Foundation.Displays;
using Meadow.Foundation.Leds;
using Meadow.Foundation.Sensors.Accelerometers;
using Meadow.Foundation.Sensors.Atmospheric;
using Meadow.Foundation.Sensors.Environmental;
using Meadow.Foundation.Sensors.Gnss;
using Meadow.Hardware;
using Meadow.Logging;
using Meadow.Peripherals.Displays;
using Meadow.Peripherals.Leds;
using Meadow.Peripherals.Sensors;
using Meadow.Peripherals.Sensors.Atmospheric;
using Meadow.Peripherals.Sensors.Environmental;
using Meadow.Peripherals.Sensors.Motion;
using Meadow.Units;
using System;

namespace WildernessLabs.Hardware.GnssTracker
namespace Meadow.Devices
{
/// <summary>
/// Represents a Gnss Tracker Hardware base class
/// </summary>
public abstract class GnssTrackerHardwareBase : IGnssTrackerHardware
{
private IPixelDisplay? _display;




/// <inheritdoc/>
protected Logger Log = Resolver.Log;
protected Logger? Logger = Resolver.Log;

/// <inheritdoc/>
public II2cBus? I2cBus { get; protected set; }
Expand All @@ -28,37 +34,49 @@ public abstract class GnssTrackerHardwareBase : IGnssTrackerHardware
public ISpiBus? SpiBus { get; protected set; }

/// <inheritdoc/>
public PwmLed? OnboardLed { get; protected set; }
public IPwmLed? OnboardLed { get; protected set; }

/// <inheritdoc/>
public Bme688? AtmosphericSensor { get; protected set; }
public NeoM8? GnssSensor { get; protected set; }

/// <inheritdoc/>
public NeoM8? Gnss { get; protected set; }
public IPixelDisplay? Display { get; protected set; }

/// <inheritdoc/>
public abstract Scd40? EnvironmentalSensor { get; protected set; }
public IAnalogInputPort? SolarVoltageInput { get; protected set; }

/// <inheritdoc/>
public abstract Bmi270? MotionSensor { get; protected set; }
public I2cConnector I2cHeader => (I2cConnector)Connectors[1]!;

/// <inheritdoc/>
public IPixelDisplay? Display { get; protected set; }
public UartConnector UartHeader => (UartConnector)Connectors[0]!;

/// <inheritdoc/>
public IAnalogInputPort? SolarVoltageInput { get; protected set; }
public DisplayConnector DisplayHeader => (DisplayConnector)Connectors[2]!;

/// <inheritdoc/>
public abstract IAnalogInputPort? BatteryVoltageInput { get; protected set; }
public abstract IAnalogInputPort? BatteryVoltageInput { get; }

/// <inheritdoc/>
public I2cConnector I2cHeader => (I2cConnector)Connectors[1]!;
public abstract ITemperatureSensor? TemperatureSensor { get; }

/// <inheritdoc/>
public UartConnector UartHeader => (UartConnector)Connectors[0]!;
public abstract IHumiditySensor? HumiditySensor { get; }

/// <inheritdoc/>
public DisplayConnector DisplayHeader => (DisplayConnector)Connectors[2]!;
public abstract IBarometricPressureSensor? BarometricPressureSensor { get; }

/// <inheritdoc/>
public abstract IGasResistanceSensor? GasResistanceSensor { get; }

/// <inheritdoc/>
public abstract ICO2ConcentrationSensor? CO2ConcentrationSensor { get; }

/// <inheritdoc/>
public abstract IGyroscope? Gyroscope { get; }

/// <inheritdoc/>
public abstract IAccelerometer? Accelerometer { get; }

/// <summary>
/// Collection of connectors on the GNSS Tracker
Expand All @@ -78,14 +96,13 @@ public IConnector?[] Connectors
return _connectors;
}
}

private IConnector?[]? _connectors;

private readonly IF7CoreComputeMeadowDevice _device;

internal UartConnector CreateUartConnector()
{
Log?.Trace("Creating Uart connector");
Logger?.Trace("Creating Uart connector");

return new UartConnector(
"Uart",
Expand All @@ -99,7 +116,7 @@ internal UartConnector CreateUartConnector()

internal I2cConnector CreateI2cConnector()
{
Log?.Trace("Creating I2C connector");
Logger?.Trace("Creating I2C connector");

return new I2cConnector(
"I2C",
Expand All @@ -113,7 +130,7 @@ internal I2cConnector CreateI2cConnector()

internal DisplayConnector CreateDisplayConnector()
{
Log?.Trace("Creating display connector");
Logger?.Trace("Creating display connector");

return new DisplayConnector(
"Display",
Expand All @@ -136,52 +153,39 @@ internal DisplayConnector CreateDisplayConnector()
/// <param name="i2cBus">The I2C bus</param>
public GnssTrackerHardwareBase(IF7CoreComputeMeadowDevice device, II2cBus i2cBus)
{
Log.Debug("Initialize hardware...");
Logger?.Debug("Initialize hardware...");
_device = device;
I2cBus = i2cBus;

try
{
Log.Debug("Initializing Onboard LED");
Logger?.Debug("Initializing Onboard LED");

OnboardLed = new PwmLed(device.Pins.D20, TypicalForwardVoltage.Green);

Log.Debug("Onboard LED initialized");
}
catch (Exception e)
{
Log.Error($"Err initializing onboard LED: {e.Message}");
}

try
{
Resolver.Log.Debug("Initializing GNSS");

Gnss = new NeoM8(device, device.PlatformOS.GetSerialPortName("COM4")!, device.Pins.D09, device.Pins.D11);

Resolver.Log.Debug("GNSS initialized");
Logger?.Debug("Onboard LED initialized");
}
catch (Exception e)
{
Resolver.Log.Error($"Err initializing GNSS: {e.Message}");
Logger?.Error($"Err initializing onboard LED: {e.Message}");
}

try
{
Log.Debug("Initializing BME688");
Logger?.Debug("Initializing GNSS");

AtmosphericSensor = new Bme688(I2cBus, (byte)Bme688.Addresses.Address_0x76);
GnssSensor = new NeoM8(device, device.PlatformOS.GetSerialPortName("COM4")!, device.Pins.D09, device.Pins.D11);

Log.Debug("BME688 initialized");
Logger?.Debug("GNSS initialized");
}
catch (Exception e)
{
Log.Error($"Err initializing BME688: {e.Message}");
Logger?.Error($"Err initializing GNSS: {e.Message}");
}

try
{
Resolver.Log.Debug("Initializing ePaper Display");
Logger?.Debug("Initializing ePaper Display");

var config = new SpiClockConfiguration(new Frequency(48000, Frequency.UnitType.Kilohertz), SpiClockConfiguration.Mode.Mode0);
SpiBus = device.CreateSpiBus(
Expand All @@ -197,23 +201,24 @@ public GnssTrackerHardwareBase(IF7CoreComputeMeadowDevice device, II2cBus i2cBus
busyPin: device.Pins.D05,
width: 122,
height: 250);
_display = Display;

Resolver.Log.Debug("ePaper Display initialized");
Logger?.Debug("ePaper Display initialized");
}
catch (Exception e)
{
Resolver.Log.Error($"Err initializing ePaper Display: {e.Message}");
Logger?.Error($"Err initializing ePaper Display: {e.Message}");
}

try
{
Resolver.Log.Debug("Instantiating Solar Voltage Input");
Logger?.Debug("initializing Solar Voltage Input");
SolarVoltageInput = device.Pins.A00.CreateAnalogInputPort(5);
Resolver.Log.Debug("Solar Voltage Input up");
Logger?.Debug("Solar Voltage Input initialized");
}
catch (Exception ex)
{
Resolver.Log.Error($"Unabled to create Solar Voltage Input: {ex.Message}");
Logger?.Error($"Unabled to create Solar Voltage Input: {ex.Message}");
}
}
}
Expand Down
100 changes: 93 additions & 7 deletions Source/GnssTracker/GnssTrackerHardwareV1.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,50 @@
using Meadow;
using Meadow.Foundation.Sensors.Accelerometers;
using Meadow.Foundation.Sensors.Environmental;
using Meadow.Foundation.Sensors.Atmospheric;
using Meadow.Hardware;
using Meadow.Logging;
using Meadow.Peripherals.Sensors;
using Meadow.Peripherals.Sensors.Atmospheric;
using Meadow.Peripherals.Sensors.Environmental;
using Meadow.Peripherals.Sensors.Motion;
using System;

namespace WildernessLabs.Hardware.GnssTracker
namespace Meadow.Devices
{
/// <summary>
/// Represents a Gnss Tracker Hardware V1
/// </summary>
public class GnssTrackerHardwareV1 : GnssTrackerHardwareBase
{
private ITemperatureSensor? _temperatureSensor;
private IHumiditySensor? _humiditySensor;
private IBarometricPressureSensor? _barometricPressureSensor;
private IGasResistanceSensor? _gasResistanceSensor;

/// <inheritdoc/>
protected Logger? Logger = Resolver.Log;

/// <inheritdoc/>
public override IAnalogInputPort? BatteryVoltageInput { get => null; }

/// <inheritdoc/>
public override ITemperatureSensor? TemperatureSensor => GetTemperatureSensor();

/// <inheritdoc/>
public override IHumiditySensor? HumiditySensor => GetHumiditySensor();

/// <inheritdoc/>
public override IBarometricPressureSensor? BarometricPressureSensor => GetBarometricPressureSensor();

/// <inheritdoc/>
public override Scd40? EnvironmentalSensor { get => null; protected set => throw new System.NotImplementedException(); }
public override IGasResistanceSensor? GasResistanceSensor => GetGasResistanceSensor();

/// <inheritdoc/>
public override Bmi270? MotionSensor { get => null; protected set => throw new System.NotImplementedException(); }
public override ICO2ConcentrationSensor? CO2ConcentrationSensor { get => null; }

/// <inheritdoc/>
public override IAnalogInputPort? BatteryVoltageInput { get => null; protected set => throw new System.NotImplementedException(); }
public override IGyroscope? Gyroscope { get => null; }

/// <inheritdoc/>
public override IAccelerometer? Accelerometer { get => null; }

/// <summary>
/// Create a new GnssTrackerHardwareV1 object
Expand All @@ -26,5 +53,64 @@ public class GnssTrackerHardwareV1 : GnssTrackerHardwareBase
/// <param name="i2cBus">The I2C bus</param>
public GnssTrackerHardwareV1(IF7CoreComputeMeadowDevice device, II2cBus i2cBus) : base(device, i2cBus)
{ }

private ITemperatureSensor? GetTemperatureSensor()
{
if (_temperatureSensor == null)
{
InitializeBme688();
}

return _temperatureSensor;
}

private IBarometricPressureSensor? GetBarometricPressureSensor()
{
if (_barometricPressureSensor == null)
{
InitializeBme688();
}

return _barometricPressureSensor;
}

private IHumiditySensor? GetHumiditySensor()
{
if (_humiditySensor == null)
{
InitializeBme688();
}

return _humiditySensor;
}

private IGasResistanceSensor? GetGasResistanceSensor()
{
if (_gasResistanceSensor == null)
{
InitializeBme688();
}

return _gasResistanceSensor;
}

private void InitializeBme688()
{
try
{
Logger?.Trace("Bme688: Initializing");
var bme = new Bme688(I2cBus, (byte)Bme68x.Addresses.Address_0x76);
_temperatureSensor = bme;
_humiditySensor = bme;
_barometricPressureSensor = bme;
_gasResistanceSensor = bme;
Resolver.SensorService.RegisterSensor(bme);
Logger?.Trace("Bme688: Initialized");
}
catch (Exception ex)
{
Logger?.Error($"Unable to create the BME688 atmospheric sensor: {ex.Message}");
}
}
}
}
Loading

0 comments on commit 126d0e5

Please sign in to comment.