Skip to content

Commit

Permalink
adding support for more complex App types
Browse files Browse the repository at this point in the history
  • Loading branch information
ctacke committed Jun 17, 2024
1 parent 8def52d commit 5bf89d0
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
/// class for Meadow applications to get strongly-typed access to the current
/// device information.
/// </summary>
public abstract class App<D> : IApp, IAsyncDisposable
where D : class, IMeadowDevice
public abstract class AppBase : IApp
{
private ExecutionContext executionContext;

Expand All @@ -24,11 +23,10 @@ public abstract class App<D> : IApp, IAsyncDisposable
/// <summary>
/// Base constructor for the App class
/// </summary>
protected App()
protected AppBase()
{
executionContext = Thread.CurrentThread.ExecutionContext;

Device = MeadowOS.CurrentDevice as D ?? throw new ArgumentException($"Current device is not {typeof(D).Name}"); // 'D' is guaranteed to be initialized and the same type
Abort = MeadowOS.AppAbort.Token;

Resolver.Services.Add<IApp>(this);
Expand All @@ -39,20 +37,9 @@ protected App()
/// </summary>
/// <param name="action">The action to call</param>
/// <param name="state">An optional state object to pass to the Action</param>
public void InvokeOnMainThread(Action<object?> action, object? state = null)
public virtual void InvokeOnMainThread(Action<object?> action, object? state = null)
{
switch (Device.Information.Platform)
{
// ExecutionContext in Mono on the F7 isn't fully working - but we also don't worry about a MainThread there either
case Hardware.MeadowPlatform.F7FeatherV1:
case Hardware.MeadowPlatform.F7FeatherV2:
case Hardware.MeadowPlatform.F7CoreComputeV2:
action.Invoke(state);
break;
default:
ExecutionContext.Run(executionContext, new ContextCallback(action), state);
break;
}
ExecutionContext.Run(executionContext, new ContextCallback(action), state);
}

/// <summary>
Expand Down Expand Up @@ -96,11 +83,6 @@ public void OnUpdateComplete(Version oldVersion, out bool rollbackUpdate)
rollbackUpdate = false;
}

/// <summary>
/// The root Device interface
/// </summary>
public static D Device { get; protected set; } = default!;

/// <summary>
/// The app cancellation token
/// </summary>
Expand All @@ -110,4 +92,4 @@ public void OnUpdateComplete(Version oldVersion, out bool rollbackUpdate)
/// Virtual method provided for App implementations to clean up resources on Disposal
/// </summary>
public virtual ValueTask DisposeAsync() { return new ValueTask(Task.CompletedTask); }
}
}
43 changes: 43 additions & 0 deletions source/Meadow.Core/Bases/App_D.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace Meadow;

using System;


/// <summary>
/// Provides a base implementation for the Meadow App. Use this
/// class for Meadow applications to get strongly-typed access to the current
/// device information.
/// </summary>
public abstract class App<D> : AppBase
where D : class, IMeadowDevice
{
/// <summary>
/// The root Device interface
/// </summary>
public static D Device { get; protected set; } = default!;

/// <summary>
/// Base constructor for the App class
/// </summary>
public App()
{
Device = MeadowOS.CurrentDevice as D ?? throw new ArgumentException($"Current device is not {typeof(D).Name}"); // 'D' is guaranteed to be initialized and the same type
}

/// <inheritdoc/>
public override void InvokeOnMainThread(Action<object?> action, object? state = null)
{
switch (Device.Information.Platform)
{
// ExecutionContext in Mono on the F7 isn't fully working - but we also don't worry about a MainThread there either
case Hardware.MeadowPlatform.F7FeatherV1:
case Hardware.MeadowPlatform.F7FeatherV2:
case Hardware.MeadowPlatform.F7CoreComputeV2:
action.Invoke(state);
break;
default:
base.InvokeOnMainThread(action, state);
break;
}
}
}
20 changes: 20 additions & 0 deletions source/Meadow.Core/Bases/App_D_P_H.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Meadow;

/// <summary>
/// Provides a base implementation for the Meadow App. Use this
/// class for Meadow applications to get strongly-typed access to the current
/// device information.
/// </summary>
/// <typeparam name="D">The type of the IMeadowDevice this app targets</typeparam>
/// <typeparam name="P">The type of the IMeadowAppEmbeddedHardwareProvider to create</typeparam>
/// <typeparam name="H">The type of the IMeadowAppEmbeddedHardware the Provider will return</typeparam>
public abstract class App<D, P, H> : AppBase
where D : class, IMeadowDevice
where P : IMeadowAppEmbeddedHardwareProvider<H>

Check failure on line 13 in source/Meadow.Core/Bases/App_D_P_H.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IMeadowAppEmbeddedHardwareProvider<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 13 in source/Meadow.Core/Bases/App_D_P_H.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IMeadowAppEmbeddedHardwareProvider<>' could not be found (are you missing a using directive or an assembly reference?)
where H : IMeadowAppEmbeddedHardware

Check failure on line 14 in source/Meadow.Core/Bases/App_D_P_H.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IMeadowAppEmbeddedHardware' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 14 in source/Meadow.Core/Bases/App_D_P_H.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IMeadowAppEmbeddedHardware' could not be found (are you missing a using directive or an assembly reference?)
{
/// <summary>
/// The instance if the IMeadowAppEmbeddedHardware on which the stack is running
/// </summary>
public static H Hardware { get; internal set; } = default!;
}
Loading

0 comments on commit 5bf89d0

Please sign in to comment.