raspberrypi-brightness.php

#!/usr/bin/env php
<?php
// Raspberry Pi TFT module brightness control
// Erik Andersson 2018
// http://packy.se/

$brightFile = '/sys/class/backlight/rpi_backlight/brightness';
$brightMin  = 20;
$brightMax = 255;

if(!is_readable($brightFile)) {
        die($argv[0] . ': Error - unable to read the brightness setting. Does it exist and have permissions to read?' . PHP_EOL);
}

if($argc <> 2) {
        $brightness = trim(file_get_contents($brightFile));
        die('Current brightness: ' . $brightness . PHP_EOL);
}

if(!is_numeric($argv[1])) {
        die('Usage: ' . $argv[0] . ' (<new brightness>)' . PHP_EOL);
}
if(!is_writable($brightFile)) {
        die($argv[0] . ': Error - unable to write the brightness setting. Does it exist and have permissioin to write?' . PHP_EOL);
}
$brightness = min($brightMax, max($brightMin, $argv[1]));
file_put_contents($brightFile, $brightness);
echo 'Brightness: ' . $brightness . PHP_EOL;