using System;
using System.Globalization;
using System.IO;
using Swan;
namespace Unosquare.RaspberryIO.Computer {
///
/// 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 Boolean IsPresent => File.Exists(BrightnessFilename);
///
/// Gets or sets the brightness of the DSI display via filesystem.
///
///
/// The brightness.
///
public Byte Brightness {
get => this.IsPresent ? Byte.TryParse(File.ReadAllText(BrightnessFilename).Trim(), out Byte brightness) ? brightness : (Byte)0 : (Byte)0;
set {
if(this.IsPresent) {
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 Boolean IsBacklightOn {
get => this.IsPresent && Int32.TryParse(File.ReadAllText(BacklightFilename).Trim(), out Int32 value) && value == 0;
set {
if(this.IsPresent) {
File.WriteAllText(BacklightFilename, value ? "0" : "1");
}
}
}
}
}