from blinkstick import blinkstick from random import randint, choice from time import sleep def set_all_leds(device, grb=[0,0,0]): """ Takes a GRB color array as input and sets the color on all LEDs. """ return device.set_led_data(0, grb*32) def morph_color(color, steps): """ Takes an int between 0-255 and either increments or decreases it by the specified steps. """ direction = choice([-1, 1]) color_candidate = color+steps*direction # if we go out of bounds reverse the direction if color_candidate < 0: return color+steps*direction*-1 elif color_candidate > 255: return color+steps*direction*-1 else: return color_candidate def morph_grb(grb, steps, mode='single'): """ Morphs a GRB array randomly by the specified steps. Available modes: single - only morph a single color all - morph all colors """ if mode == 'single': color_index = randint(0, len(grb)-1) grb[color_index] = morph_color(grb[color_index], steps) elif mode == 'all': for color_index in range(len(grb)): grb[color_index] = morph_color(grb[color_index], steps) else: raise Exception(f'Mode "{mode}" is not supported.') return grb device = blinkstick.find_first() # get a random initial color grb = [ randint(1, 255), randint(1, 255), randint(1, 255) ] while True: set_all_leds(device, grb) sleep(0.02) # minimal delay to not crash the blinkstick grb = morph_grb(grb, 2)