Skip to content

Instantly share code, notes, and snippets.

@ablakely
Last active September 3, 2022 03:01
Show Gist options
  • Save ablakely/8d8330535bddff34fbd545b7fa7090e1 to your computer and use it in GitHub Desktop.
Save ablakely/8d8330535bddff34fbd545b7fa7090e1 to your computer and use it in GitHub Desktop.
thinklight

thinklight

Controls the ThinkLight on IBM/Lenovo ThinkPads

Usage

On

thinklight on

Off

thinklight off

Blink

thinklight blink x y x is the time on y is the time off

More

See thinklight -h for more information

Installing

thinklight requires perl

chmod +x thiklight mv thinklight /usr/local/bin/

#!/usr/bin/perl -w
#
# thinklight - ThinkLight Control script
#
# Written by Aaron Blakely <aaron@ephasic.org>
#
use strict;
use warnings;
my $TL;
my $verbose = 0;
my $THINKLGHT_PATH = "/sys/class/leds/tpacpi\:\:thinklight/brightness";
sub set_lamp {
my ($state) = @_;
print "[INFO] Setting [Thinklight:$THINKLGHT_PATH] to $state\n" if ($verbose == 1);
open($TL, ">", $THINKLGHT_PATH) or die $!;
print $TL $state;
close($TL) or die $!;
}
my $idx = 0;
if (!$ARGV[$idx]) {
print "$0: [-hVvd] [on|off|blink] [on int] [off int]\n";
exit;
}
MODS:
if ($ARGV[$idx] eq "-v") {
$verbose = 1;
print "[INFO] Setting verbose.\n";
$idx++;
}
if ($ARGV[$idx] eq "-d") {
$THINKLGHT_PATH = $ARGV[++$idx];
print "[INFO] Setting device path to $THINKLGHT_PATH\n";
$idx++;
goto MODS;
}
if ($ARGV[$idx] eq "-V") {
print "thinklight: v0.5 Written by Aaron Blakely <aaron\@ephasic.org>\n";
exit;
} elsif ($ARGV[$idx] eq "-h") {
print "thinklight help:\n\n";
print "[Usage]\n";
print " -v - Enable verbose\n";
print " -h - Display this message\n";
print " -V - Display version\n";
print " -d [path] - Set device path\n\n";
print "[Commands]\n";
print " on - Turn on thinklight\n";
print " off - Turn off thinklight\n";
print " blink x y - Blink for x on and y off\n\n";
exit;
}
if ($ARGV[$idx] eq "on") {
set_lamp(1);
} elsif ($ARGV[$idx] eq "off") {
set_lamp(0);
} elsif ($ARGV[$idx] eq "blink") {
my $interval_on = $ARGV[++$idx];
my $interval_off = $ARGV[++$idx];
print "[INFO] Entering blink loop [on: $interval_on | off: $interval_off]\n" if ($verbose == 1);
while (1) {
set_lamp(1);
sleep($interval_on);
set_lamp(0);
sleep($interval_off);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment