Skip to content

Instantly share code, notes, and snippets.

@SamWhited
Created May 30, 2015 22:38
Show Gist options
  • Save SamWhited/88b2479f439573270a6f to your computer and use it in GitHub Desktop.
Save SamWhited/88b2479f439573270a6f to your computer and use it in GitHub Desktop.
Turn on or off the ThinkLight (or keyboard backlight) on various ThinkPad laptops
package main
import (
"flag"
"log"
"os"
)
func btoi(n bool) byte {
if n {
return 1
} else {
return 0
}
}
func main() {
const (
off byte = 0x03
low byte = 0x43
high byte = 0x83
)
flag_off := flag.Bool("off", false, "turns off the keyboard backlight")
flag_low := flag.Bool("low", false, "turns the keyboard backlight on low")
flag_high := flag.Bool("high", false, "turns the keyboard backlight on high")
flag.Parse()
if !(*flag_off || *flag_low || *flag_high) {
flag.Usage()
os.Exit(1)
} else if btoi(*flag_off)+btoi(*flag_low)+btoi(*flag_high) > 1 {
log.Println("You must specify only one level.")
flag.Usage()
os.Exit(1)
}
f, err := os.OpenFile("/sys/kernel/debug/ec/ec0/io", os.O_RDWR, os.ModeDevice)
if err != nil {
log.Fatal(err)
}
defer f.Close()
var level byte
if *flag_off {
level = off
} else if *flag_low {
level = low
} else if *flag_high {
level = high
}
_, err = f.WriteAt([]byte{level}, 0x0d)
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment