Skip to content

Instantly share code, notes, and snippets.

@quantum-x
Created December 15, 2014 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quantum-x/df69a3ddabf33f43c0cb to your computer and use it in GitHub Desktop.
Save quantum-x/df69a3ddabf33f43c0cb to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import sys,struct, argparse, time, os, signal
import tempfile
#Setup Variables
#Setup RGB Pins
pins = (27, 23, 18)
#Setup lock file for potential background work
lockfile = 'rgbled.lock'
temp_dir = tempfile.gettempdir()
if not temp_dir.endswith(os.sep):
temp_dir = ''.join([temp_dir, os.sep])
lockfile = ''.join([temp_dir, lockfile])
description='Raspberry Pi RGB LED tool'
parser = argparse.ArgumentParser(description=description,
epilog='--blink, --pulse, --keep and --delay are mutually exclusive')
parser.add_argument('rgbstr',action='store',nargs=1,
help='Desired LED Color in HEX format, ie: FFcc00',
metavar="RGBString")
parser.add_argument('-v','--verbose',action='store_true',help='Output verbose information')
group = parser.add_mutually_exclusive_group(required=False)
group.add_argument('-b','--blink',action='store',nargs=2,dest='blink',help='Blink the LED n times, d delay',metavar=("n","d"))
group.add_argument('-d','--delay',action='store',nargs=1,dest='delay',help='Show and hold the LED for a delay',metavar='d')
group.add_argument('-p','--pulse',action='store',nargs=1, dest='pulse',help='Pulse the led with a speed 0 - 5',metavar='s')
group.add_argument('-k','--keep',action='store_true',help='Keep the LED lit')
args = parser.parse_args()
def check_lock():
if args.verbose is not False: print('[>] Checking for lockfile')
if os.path.isfile(lockfile):
if args.verbose is not False: print(' [+] Lockfile found')
with open(lockfile, "rt") as f:
if args.verbose is not False: print(' [+] Killing existing PID')
try:
os.kill(int(f.read().rstrip()), signal.SIGKILL)
except:
if args.verbose is not False: print(' [!] Already terminated')
if args.verbose is not False: print(' [+] Deleting lockfile')
os.remove(lockfile)
else:
if args.verbose is not False: print(' [-] No lockfile')
def set_lock():
if args.verbose is not False: print('[>] Creating lockfile')
with open(lockfile, "wt") as f:
f.write(str(os.getpid()))
def setLED(rgbstr):
for (i, value) in enumerate(struct.unpack('BBB',bytes.fromhex(rgbstr))):
os.system('/bin/echo "{}={}" > /dev/pi-blaster'.format(pins[i],value/255))
def pulseLED(rgbstr, speed):
rgb = ('r','g','b')
orig_str = struct.unpack('BBB',bytes.fromhex(rgbstr))
#max_step = int(max(orig_str)/step)
speed = 1 if speed == 0 else speed
max_step = int(100/speed) #For consistant timing
lower_bound = .2 #Lower bound for LED pulsing
try:
while 1:
for s in range(max_step):
for (i, value) in enumerate(struct.unpack('BBB',bytes.fromhex(rgbstr))):
if (value-((value/max_step))) >= lower_bound:
os.system('/bin/echo "{}={}" > /dev/pi-blaster'.format(pins[i], ((value-((value/max_step)*s))/255)))
else:
#os.system('/bin/echo "{}={}" > /dev/pi-blaster'.format(pins[i],0))
pass #Turning off completely is jarring
for s in reversed(range(max_step)):
for (i, value) in enumerate(struct.unpack('BBB',bytes.fromhex(rgbstr))):
os.system('/bin/echo "{}={}" > /dev/pi-blaster'.format(pins[i], ((value-((value/max_step)*s))/255)))
except (KeyboardInterrupt, SystemExit):
setLED('000000')
os.remove(lockfile)
check_lock()
#If we're going to be pulsing, we're going to write a lock file, with our PID in it
#If when the app launches, and we see a lockfile, we'll nuke the PID inside, and delete the file
if args.pulse is not None:
set_lock()
pulseLED(args.rgbstr[0], int(args.pulse[0]))
elif args.keep is not False:
setLED(args.rgbstr[0])
elif args.blink is not None:
#If 'blink' is not set, we're going to be blinking 1 time anyhow
numFlashes = 1 if args.blink is None else int(args.blink[0])
delay = args.delay[0] if args.blink is None else args.blink[1]
for i in range(0,numFlashes):
setLED(args.rgbstr[0])
time.sleep(float(int(delay)/1000))
setLED('000000')
if i+1 != numFlashes:
time.sleep(float(int(delay)/1000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment