namespace Unosquare.RaspberryIO.Computer { using Swan.Abstractions; using System.Globalization; using System.IO; /// /// The Official Raspberry Pi 7-inch touch display from the foundation /// Some docs available here: /// http://forums.pimoroni.com/t/official-7-raspberry-pi-touch-screen-faq/959 /// public class DsiDisplay : SingletonBase { private const string BacklightFilename = "/sys/class/backlight/rpi_backlight/bl_power"; private const string BrightnessFilename = "/sys/class/backlight/rpi_backlight/brightness"; /// /// Prevents a default instance of the class from being created. /// private DsiDisplay() { // placeholder } /// /// Gets a value indicating whether the Pi Foundation Display files are present. /// /// /// true if this instance is present; otherwise, false. /// public bool IsPresent => File.Exists(BrightnessFilename); /// /// Gets or sets the brightness of the DSI display via filesystem. /// /// /// The brightness. /// public byte Brightness { get { if (IsPresent == false) return 0; return byte.TryParse(File.ReadAllText(BrightnessFilename).Trim(), out var brightness) ? brightness : (byte)0; } set { if (IsPresent == false) return; File.WriteAllText(BrightnessFilename, value.ToString(CultureInfo.InvariantCulture)); } } /// /// Gets or sets a value indicating whether the backlight of the DSI display on. /// This operation is performed via the file system /// /// /// true if this instance is backlight on; otherwise, false. /// public bool IsBacklightOn { get { if (IsPresent == false) return false; if (int.TryParse(File.ReadAllText(BacklightFilename).Trim(), out var backlight)) return backlight == 0; return false; } set { if (IsPresent == false) return; File.WriteAllText(BacklightFilename, value ? "0" : "1"); } } } }