using System;
using System.Threading.Tasks;
using Swan;
using Swan.DependencyInjection;
using Unosquare.RaspberryIO.Abstractions;
using Unosquare.RaspberryIO.Camera;
using Unosquare.RaspberryIO.Computer;
namespace Unosquare.RaspberryIO {
///
/// Our main character. Provides access to the Raspberry Pi's GPIO, system and board information and Camera.
///
public static class Pi {
private const String MissingDependenciesMessage = "You need to load a valid assembly (WiringPi or PiGPIO).";
private static readonly Object SyncLock = new Object();
private static Boolean _isInit;
private static SystemInfo _info;
///
/// Initializes static members of the class.
///
static Pi() {
lock(SyncLock) {
Camera = CameraController.Instance;
PiDisplay = DsiDisplay.Instance;
Audio = AudioSettings.Instance;
Bluetooth = Bluetooth.Instance;
}
}
///
/// Provides information on this Raspberry Pi's CPU and form factor.
///
public static SystemInfo Info => _info ??= SystemInfo.Instance;
///
/// Provides access to the Raspberry Pi's GPIO as a collection of GPIO Pins.
///
public static IGpioController Gpio => ResolveDependency();
///
/// Provides access to the 2-channel SPI bus.
///
public static ISpiBus Spi => ResolveDependency();
///
/// Provides access to the functionality of the i2c bus.
///
public static II2CBus I2C => ResolveDependency();
///
/// Provides access to timing functionality.
///
public static ITiming Timing => ResolveDependency();
///
/// Provides access to threading functionality.
///
public static IThreading Threading => ResolveDependency();
///
/// Provides access to the official Raspberry Pi Camera.
///
public static CameraController Camera {
get;
}
///
/// Provides access to the official Raspberry Pi 7-inch DSI Display.
///
public static DsiDisplay PiDisplay {
get;
}
///
/// Provides access to Raspberry Pi ALSA sound card driver.
///
public static AudioSettings Audio {
get;
}
///
/// Provides access to Raspberry Pi Bluetooth driver.
///
public static Bluetooth Bluetooth {
get;
}
///
/// Restarts the Pi. Must be running as SU.
///
/// The process result.
public static Task RestartAsync() => ProcessRunner.GetProcessResultAsync("reboot");
///
/// Restarts the Pi. Must be running as SU.
///
/// The process result.
public static ProcessResult Restart() => RestartAsync().GetAwaiter().GetResult();
///
/// Halts the Pi. Must be running as SU.
///
/// The process result.
public static Task ShutdownAsync() => ProcessRunner.GetProcessResultAsync("halt");
///
/// Halts the Pi. Must be running as SU.
///
/// The process result.
public static ProcessResult Shutdown() => ShutdownAsync().GetAwaiter().GetResult();
///
/// Initializes an Abstractions implementation.
///
/// An implementation of .
public static void Init() where T : IBootstrap {
lock(SyncLock) {
if(_isInit) {
return;
}
Activator.CreateInstance().Bootstrap();
_isInit = true;
}
}
private static T ResolveDependency() where T : class {
if(!_isInit) {
throw new InvalidOperationException($"You must first initialize {nameof(Pi)} referencing a valid {nameof(IBootstrap)} implementation.");
}
return DependencyContainer.Current.CanResolve() ? DependencyContainer.Current.Resolve() : throw new InvalidOperationException(MissingDependenciesMessage);
}
}
}