Skip to content

Commit

Permalink
Merge pull request #290 from WildernessLabs/develop
Browse files Browse the repository at this point in the history
Meadow.Core to RC3
  • Loading branch information
jorgedevs authored Apr 2, 2023
2 parents c49fe3a + 5542c24 commit 853d9a3
Show file tree
Hide file tree
Showing 23 changed files with 1,398 additions and 903 deletions.
2 changes: 1 addition & 1 deletion source/Meadow.Core/Hardware/AnalogInputPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ public override void StartUpdating(TimeSpan? updateInterval = null)
if (ct.IsCancellationRequested)
{
// do task clean up here
observers.ForEach(x => x.OnCompleted());
Observers.ForEach(x => x.OnCompleted());
break;
}

Expand Down
4 changes: 3 additions & 1 deletion source/Meadow.Core/Hardware/BiDirectionalPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Meadow.Hardware
public class BiDirectionalPort : BiDirectionalPortBase
{
private PortDirectionType _currentDirection;
private TimeSpan _debounceDuration;
private TimeSpan _glitchDuration;
protected IMeadowIOController IOController { get; }
protected DateTime LastEventTime { get; set; } = DateTime.MinValue;

Expand Down Expand Up @@ -208,7 +210,7 @@ public override TimeSpan GlitchDuration
}
}

void OnInterrupt(IPin pin, bool state)
private void OnInterrupt(IPin pin, bool state)
{
if (pin == this.Pin)
{
Expand Down
30 changes: 6 additions & 24 deletions source/Meadow.Core/Hardware/Communications/SerialPortBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Meadow.Devices;
using System;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
Expand All @@ -15,7 +14,6 @@ namespace Meadow.Hardware
public abstract class SerialPortBase : IDisposable, ISerialPort
{
private IntPtr _driverHandle = IntPtr.Zero;
protected bool _showSerialDebug = false;
private CircularBuffer<byte>? _readBuffer;
protected Thread? _readThread;
protected int _baudRate;
Expand Down Expand Up @@ -56,16 +54,12 @@ protected SerialPortBase(
StopBits stopBits = StopBits.One,
int readBufferSize = 4096)
{
#if !DEBUG
// ensure this is off in release (in case a dev sets it to true and fogets during check-in
_showSerialDebug = false;
#endif
if (baudRate <= 0) { throw new ArgumentOutOfRangeException("Invalid baud rate"); }
if (dataBits < 5 || dataBits > 8) { throw new ArgumentOutOfRangeException("Invalid dataBits"); }

PortName = portName.SystemName;
BaudRate = baudRate;
Parity = Parity;
Parity = parity;
DataBits = dataBits;
StopBits = stopBits;
ReadTimeout = TimeSpan.FromMilliseconds(-1);
Expand Down Expand Up @@ -329,9 +323,6 @@ public int Write(byte[] buffer, int index, int count)
int bytesToWriteThisLoop = maxCount;
int bytesLeft = count;

Output.WriteLineIf(_showSerialDebug, $"count:{count}, maxCount:{maxCount}");
Output.WriteLineIf(_showSerialDebug, $"Starting with: {(BitConverter.ToString(buffer, 0, (buffer.Length > 6) ? 6 : buffer.Length))}");

Timer? writeTimeoutTimer = null;

if (WriteTimeout.TotalMilliseconds > 0)
Expand All @@ -350,7 +341,6 @@ public int Write(byte[] buffer, int index, int count)
// if there's an offset, we want to slice
if (currentIndex > 0)
{
Output.WriteLineIf(_showSerialDebug, $"Slicing. currentIndex:{currentIndex}, bytesLeft:{bytesLeft}, bytesToWriteThisLoop:{bytesToWriteThisLoop}");
Span<byte> data = buffer.AsSpan<byte>().Slice(currentIndex, bytesToWriteThisLoop);
result = WriteHardwarePort(_driverHandle, data.ToArray(), count);
}
Expand All @@ -362,13 +352,10 @@ public int Write(byte[] buffer, int index, int count)
// otherwise,
totalBytesWritten += result;

Output.WriteLineIf(_showSerialDebug, $"bytesActallyWrittenThisLoop: {result} totalBytesWritten: {totalBytesWritten}");

// recalculate the current index, including the original offset
currentIndex = totalBytesWritten + index;
bytesLeft = count - totalBytesWritten;
bytesToWriteThisLoop = bytesLeft > systemBufferMax ? systemBufferMax : bytesLeft;
Output.WriteLineIf(_showSerialDebug, $"currentIndex: {currentIndex}, bytesLeft: {bytesLeft}, bytesToWriteThisLoop:{bytesToWriteThisLoop}");
}
}
finally
Expand All @@ -388,7 +375,7 @@ private void ReadThreadProc()
{
var readBuffer = new byte[4096];

Output.WriteLineIf(_showSerialDebug, $"ReadThreadProc: {IsOpen}");
Resolver.Log.Debug($"ReadThreadProc: {IsOpen}");

while (IsOpen)
{
Expand All @@ -398,9 +385,6 @@ private void ReadThreadProc()

if (result > 0)
{
Output.WriteLineIf(_showSerialDebug, $"data read");
Output.WriteLineIf(_showSerialDebug, $"Enqueuing {result} bytes");

_readBuffer?.Append(readBuffer, 0, result);

if (DataReceived != null)
Expand All @@ -410,14 +394,12 @@ private void ReadThreadProc()
{
try
{
Output.WriteLineIf(_showSerialDebug, $"Calling event handlers");
DataReceived?.Invoke(this, new SerialDataReceivedEventArgs(SerialDataType.Chars));
Output.WriteLineIf(_showSerialDebug, $"event handler complete");
}
catch (Exception ex)
{
// if the event handler throws, we don't want this to die
Output.WriteLine($"Serial event handler threw: {ex.Message}");
Resolver.Log.Error($"Serial event handler threw: {ex.Message}");
}
});
}
Expand All @@ -432,11 +414,11 @@ private void ReadThreadProc()
}
catch (Exception ex)
{
Output.WriteLine($"ReadThreadProc error: {ex.Message}");
Resolver.Log.Error($"ReadThreadProc error: {ex.Message}");
}
}

Output.WriteLineIf(_showSerialDebug, $"ReadThreadProc: port closed");
Resolver.Log.Debug($"ReadThreadProc: port closed");
}

/// <summary>
Expand Down
53 changes: 25 additions & 28 deletions source/Meadow.Core/Meadow.Core.csproj
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Exe</OutputType>
<AssemblyName>Meadow</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>http://beta-developer.wildernesslabs.co/Meadow/</PackageProjectUrl>
<PackageId>Meadow</PackageId>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Core</RepositoryUrl>
<PackageTags>Meadow</PackageTags>
<Version>0.23.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>Meadow</RootNamespace>
<Nullable>enable</Nullable>
<Description>Core .NET libraries for Wilderness Labs Meadow</Description>
</PropertyGroup>
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>9.0</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Include="..\icon.png" Link="icon.png" Pack="true" PackagePath="" />
<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Exe</OutputType>
<AssemblyName>Meadow</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>http://beta-developer.wildernesslabs.co/Meadow/</PackageProjectUrl>
<PackageId>Meadow</PackageId>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Core</RepositoryUrl>
<PackageTags>Meadow</PackageTags>
<Version>0.23.0</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>Meadow</RootNamespace>
<Nullable>enable</Nullable>
<Description>Core .NET libraries for Wilderness Labs Meadow</Description>
<LangVersion>10</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<ItemGroup>
<None Include="..\icon.png" Link="icon.png" Pack="true" PackagePath="" />
<PackageReference Include="Meadow.Contracts" Version="0.*" />
<PackageReference Include="Meadow.MQTT" Version="0.*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="2.2.0" />
</ItemGroup>
<PackageReference Include="Meadow.MQTT" Version="0.*" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="7.0.0" />
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="2.2.0" />
</ItemGroup>
</Project>
19 changes: 12 additions & 7 deletions source/Meadow.Core/MeadowOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,29 @@ public static partial class MeadowOS
private static IApp App { get; set; }
private static ILifecycleSettings LifecycleSettings { get; set; }
private static IUpdateSettings UpdateSettings { get; set; }

internal static CancellationTokenSource AppAbort = new();

/// <summary>
/// The entry point for Meadow applications
/// </summary>
/// <param name="_"></param>
/// <returns></returns>
public async static Task Main(string[] _)
public async static Task Main(string[] args)
{
_startedDirectly = true;
await Start();
await Start(args);
}

/// <summary>
/// Initializes and starts up the Meadow Core software stack
/// </summary>
/// <returns></returns>
public async static Task Start()
public async static Task Start(string[]? args)
{
bool systemInitialized = false;
try
{
systemInitialized = Initialize();
systemInitialized = Initialize(args);

if (!systemInitialized)
{
Expand Down Expand Up @@ -276,7 +275,7 @@ private static Type FindAppType()
return appType;
}

private static bool Initialize()
private static bool Initialize(string[]? args)
{
try
{
Expand Down Expand Up @@ -328,7 +327,7 @@ private static bool Initialize()
Resolver.Log.Trace($"Device Initialize starting...");
CurrentDevice.Initialize();
Resolver.Log.Trace($"PlatformOS Initialize starting...");
CurrentDevice.PlatformOS.Initialize(CurrentDevice.Capabilities); // initialize the devices' platform OS
CurrentDevice.PlatformOS.Initialize(CurrentDevice.Capabilities, args); // initialize the devices' platform OS

// initialize file system folders and such
// TODO: move this to platformOS
Expand Down Expand Up @@ -374,6 +373,12 @@ private static bool Initialize()

private static void Shutdown()
{
// stop the update service
if (Resolver.Services.Get<IUpdateService>() is { } updateService)
{
updateService.Shutdown();
}

// schedule a device restart if possible and if the user hasn't disabled it
ScheduleRestart();

Expand Down
Loading

0 comments on commit 853d9a3

Please sign in to comment.