Last active
November 28, 2018 20:20
-
-
Save kangasta/97b6bfae1ff8090bc8ca93904b4241ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pigpio | |
| from math import pi, sin | |
| from time import sleep | |
| class RGBLed(object): | |
| def __init__(self, r_pin=22, g_pin=23, b_pin=24): | |
| self.__pins = (r_pin, g_pin, b_pin) | |
| self.__pi = pigpio.pi() | |
| def __call__(self, r, g, b): | |
| rgb = (min(255, max(0, int(i))) for i in (r, g, b)) | |
| for val, pin in zip(rgb, self.__pins): | |
| self.__pi.set_PWM_dutycycle(pin, val) | |
| def police(self, T, n=40, cycles=None, offset=0, brightness=255): | |
| while cycles is None or cycles > 0: | |
| for i in range(n): | |
| value = sin(float(i)/n*2*pi + offset)*brightness | |
| blue = value if value > 0 else 0 | |
| red = -value if value < 0 else 0 | |
| self(red, 0, blue) | |
| sleep(float(T)/n) | |
| if cycles is not None: | |
| cycles -= 1 | |
| def strobe(self, f, cycles=None, r=255, g=255, b=255): | |
| while cycles is None or cycles > 0: | |
| T=1.0/f; | |
| for vals in [(r, g, b,), (0, 0, 0,)]: | |
| self(*vals) | |
| sleep(float(T)/2) | |
| if cycles is not None: | |
| cycles -= 1 | |
| if __name__ == "__main__": | |
| from argparse import ArgumentParser | |
| parser = ArgumentParser(description="Control RGB led with Raspberry Pi") | |
| for i in ["Red", "Green", "Blue"]: | |
| parser.add_argument(i, type=int, nargs='?', default=0, help="Value for " + i[0] + ", should be 0 <= " + i[0] + " <= 255") | |
| parser.add_argument("--pins", type=int, default=(22,23,24), nargs=3, metavar=("R","G","B"), help="Pin numbers for R, G, and B") | |
| parser.add_argument("--police", action="store_true", help="Flash alternatively red and blue") | |
| parser.add_argument("--strobe", action="store_true", help="Flash white repetitively") | |
| args = parser.parse_args() | |
| set_rgb = RGBLed(*args.pins) | |
| try: | |
| if args.police: | |
| set_rgb.police(2) | |
| if args.strobe: | |
| set_rgb.strobe(20) | |
| except KeyboardInterrupt: | |
| pass | |
| set_rgb(args.Red, args.Green, args.Blue) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from argparse import ArgumentParser | |
| from threading import Thread | |
| from time import sleep | |
| from PiRGBLed import RGBLed | |
| def police_cycle(led, T=2, n=20, b=255): | |
| while True: | |
| led.police(T=T, n=n, cycles=3, brightness=b) | |
| led.strobe(f=1/(T/32), cycles=8, r=b, g=b, b=b) | |
| parser = ArgumentParser() | |
| parser.add_argument("--cycle-time", "-T", type=float, default=2.0) | |
| parser.add_argument("--num-steps", "-n", type=int, default=20) | |
| parser.add_argument("--brightness", "-b", type=int, default=255) | |
| args = parser.parse_args() | |
| threads = [] | |
| leds = [RGBLed(17,18,19), RGBLed(22,23,24)] | |
| for led in leds: | |
| threads.append(Thread(target=police_cycle, args=[led, args.cycle_time, args.num_steps, args.brightness])) | |
| threads[-1].start() | |
| sleep(args.cycle_time/4) | |
| try: | |
| for thread in threads: | |
| thread.join() | |
| except KeyboardInterrupt: | |
| pass | |
| for led in leds: | |
| led(0,0,0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment