RaspberryIO/Unosquare.RaspberryIO/Pi.cs
2019-12-03 18:43:54 +01:00

121 lines
3.3 KiB
C#

using Unosquare.RaspberryIO.Camera;
using Unosquare.RaspberryIO.Computer;
using Unosquare.RaspberryIO.Gpio;
using Unosquare.RaspberryIO.Native;
using System.Threading.Tasks;
using Unosquare.Swan.Components;
using System;
namespace Unosquare.RaspberryIO {
/// <summary>
/// Our main character. Provides access to the Raspberry Pi's GPIO, system and board information and Camera
/// </summary>
public static class Pi {
private static readonly Object SyncLock = new Object();
/// <summary>
/// Initializes static members of the <see cref="Pi" /> class.
/// </summary>
static Pi() {
lock(SyncLock) {
// Extraction of embedded resources
Resources.EmbeddedResources.ExtractAll();
// Instance assignments
Gpio = GpioController.Instance;
Info = SystemInfo.Instance;
Timing = Timing.Instance;
Spi = SpiBus.Instance;
I2C = I2CBus.Instance;
Camera = CameraController.Instance;
PiDisplay = DsiDisplay.Instance;
}
}
#region Components
/// <summary>
/// Provides access to the Raspberry Pi's GPIO as a collection of GPIO Pins.
/// </summary>
public static GpioController Gpio {
get;
}
/// <summary>
/// Provides information on this Raspberry Pi's CPU and form factor.
/// </summary>
public static SystemInfo Info {
get;
}
/// <summary>
/// Provides access to The PI's Timing and threading API
/// </summary>
public static Timing Timing {
get;
}
/// <summary>
/// Provides access to the 2-channel SPI bus
/// </summary>
public static SpiBus Spi {
get;
}
/// <summary>
/// Provides access to the functionality of the i2c bus.
/// </summary>
public static I2CBus I2C {
get;
}
/// <summary>
/// Provides access to the official Raspberry Pi Camera
/// </summary>
public static CameraController Camera {
get;
}
/// <summary>
/// Provides access to the official Raspberry Pi 7-inch DSI Display
/// </summary>
public static DsiDisplay PiDisplay {
get;
}
/// <summary>
/// Gets the logger source name.
/// </summary>
internal static String LoggerSource => typeof(Pi).Namespace;
#endregion
#region Methods
/// <summary>
/// Restarts the Pi. Must be running as SU
/// </summary>
/// <returns>The process result</returns>
public static async Task<ProcessResult> RestartAsync() => await ProcessRunner.GetProcessResultAsync("reboot", null, null);
/// <summary>
/// Restarts the Pi. Must be running as SU
/// </summary>
/// <returns>The process result</returns>
public static ProcessResult Restart() => RestartAsync().GetAwaiter().GetResult();
/// <summary>
/// Halts the Pi. Must be running as SU
/// </summary>
/// <returns>The process result</returns>
public static async Task<ProcessResult> ShutdownAsync() => await ProcessRunner.GetProcessResultAsync("halt", null, null);
/// <summary>
/// Halts the Pi. Must be running as SU
/// </summary>
/// <returns>The process result</returns>
public static ProcessResult Shutdown() => ShutdownAsync().GetAwaiter().GetResult();
#endregion
}
}