#!/usr/bin/env python import getopt, sys, os, struct, time; iBLmax = 255; iBLmin = 12; # Below 12 it won't turn back on. # Path to the PCI adapter. sPCIBus = "/sys/bus/pci/devices/0000:00:02.0/config"; def setBL(param): fBacklight.seek(iBLO); fBacklight.write(struct.pack("B", param)); fBacklight.flush(); def printUsage(): print "Usage: %s [options]" % (sys.argv[0],) print """Where options are: -h, --help Display this usage message. -s, --set Set the backlight at a specific percentage. -i, --inc Increase backlight from current setting. -d, --dec Decrease backlight from current setting. -g, --get Get the current backlight in percent. -t, --time Specifies the time in millisecond for the fade in/out. --gethex Gets the backlight setting in hex. --sethex Sets the backlight setting in hex. All percentage are float numbers and may contain decimals.""" sys.exit(2); if len(sys.argv) < 2: printUsage(); sLongOpts = [ "help", "set=", "inc=", "dec=", "get", "time=", "gethex", "sethex=", ]; try: sArgs = getopt.getopt(sys.argv[1:], "hs:i:d:gt:", sLongOpts)[0]; except: printUsage(); if os.geteuid() != 0: args = [ "/usr/bin/sudo" ]; args.extend(sys.argv); os.execv(args[0], args); iBLO = 0xF4; # Backlight Offset fBacklight = file(sPCIBus, 'r+'); fBacklight.seek(iBLO); iCurBL = struct.unpack("B", fBacklight.read(1))[0]; iCurBLP = iCurBL * 100 / 255; iSetting = 100; iFade = 0; for (arg, param) in sArgs: arg = arg.lower(); if arg == "--help" or arg == "-h": printUsage(); if arg == "--get" or arg == "-g": print "%d" % (iCurBLP,) sys.exit(0); if arg == "--set" or arg == "-s": iSetting = float(param); if arg == "--inc" or arg == "-i": iSetting = float(param) + iCurBLP if arg == "--dec" or arg == "-d": iSetting = -float(param) + iCurBLP if arg == "--time" or arg == "-t": iFade = float(param)/1000; if arg == "--sethex": setBL(int(param)); sys.exit(0); if arg == "--gethex": print "%d" % (iCurBL,); sys.exit(0); iBL = int(iSetting * 255 / 100); if iBL > iBLmax: iBL = iBLmax; if iBL < iBLmin: iBL = iBLmin; iUD = 1; if iCurBL > iBL: iUD = -1; try: iTime = iFade / abs(iCurBL - iBL); except ZeroDivisionError: iFade = 0; try: if iFade > 0: for i in range(iCurBL, iBL, iUD): setBL(i); time.sleep(iTime); except: pass; setBL(iBL); fBacklight.close();