class Pascal(threading.Thread): def run(self): """Run the thang""" global addr self.terminate = False self.holiday = HolidaySecretAPI(addr=addr) self.reset() while True: if self.terminate: return for i in range(self.holiday.NUM_GLOBES): col = self.wheel[self.values[i] % self.mod] self.holiday.setglobe(i, *col) if i == 0: self.values[i] = 1 else: self.values[i] = self.values[i] + self.values[i - 1] self.holiday.render() # Send the colours out time.sleep(SLEEP) self.tick += 1 if self.tick > ITERS: self.reset() def reset(self): self.values = [1 for i in range(self.holiday.NUM_GLOBES)] self.mod = random.randint(2, 36) s = float(self.mod) / 36.0 self.wheel = colourwheel(self.mod, s) self.tick = 0 print "Pascal mod " + str(self.mod)
class Dropsapp(threading.Thread): def run(self): """Go""" global addr self.terminate = False self.holiday = HolidaySecretAPI(addr=addr) self.n = 8 self.setup(self.n) while True: if self.terminate: return for j in range(self.n): self.drops[j].move() for i in range(self.holiday.NUM_GLOBES): r = 0.0 b = 0.0 g = 0.0 for j in range(self.n): ( r1, g1, b1 ) = self.drops[j].brightness(i) r += r1 g += g1 b += b1 self.holiday.setglobe(i, toholiday(r), toholiday(g), toholiday(b)) self.holiday.render() # Send the colours out time.sleep(.01) # And finally, wait. def setup(self, n): """Make some things""" self.drops = [] for i in range(n): d = Drop() d.show() self.drops.append(d)
class Timecolourapp(threading.Thread): def run(self): """Go""" global addr self.terminate = False self.holiday = HolidaySecretAPI(addr=addr) self.values = [] for i in range(self.holiday.NUM_GLOBES): self.values.append((0, 0, 0)) self.render() while True: if self.terminate: return nt = datetime.datetime.now() col = timecolour(nt) self.values.append(col) del(self.values[0]) self.render() # Send the colours out time.sleep(1) # And finally, wait. def render(self): for i, (r, g, b) in enumerate(self.values): self.holiday.setglobe(i, r, g, b) self.holiday.render()
class CARunner(threading.Thread): def setup(self, ca): self.ca = ca def run(self): """Run an CellularAutomaton""" global addr self.terminate = False self.holiday = HolidaySecretAPI(addr=addr) self.ca.start(self.holiday.NUM_GLOBES) while True: if self.terminate: return self.ca.step() for i in range(self.holiday.NUM_GLOBES): self.holiday.setglobe(i, *self.ca.cell(i)) self.holiday.render() time.sleep(SLEEP) def reset(self): self.values = [1 for i in range(self.holiday.NUM_GLOBES)] self.mod = random.randint(2, 36) s = float(self.mod) / 36.0 self.wheel = colourwheel(self.mod, s) self.tick = 0 print "Pascal mod " + str(self.mod)
class Blinkyapp(threading.Thread): def run(self): """Gonna make some pretty pretty colours here""" global addr self.terminate = False self.holiday = HolidaySecretAPI(addr=addr) while True: if self.terminate: return ln = 0 while (ln < self.holiday.NUM_GLOBES): r = random.randint(0, 63) g = random.randint(0, 63) b = random.randint(0, 63) # Now based on another random value, squash one of these values cn = random.randint(0,2) if (cn == 0): r = 0 elif (cn == 1): g = 0 else: b = 0 self.holiday.setglobe(ln, r, g, b) ln = ln+1 self.holiday.render() # Send the colours out time.sleep(0.2) # And finally, wait.
class Sineapp(threading.Thread): def run(self): """Run the """ global addr self.terminate = False self.holiday = HolidaySecretAPI(addr=addr) self.init() self.tick = 0 while True: if self.terminate: return roff = int(self.tick * RV) goff = int(self.tick * GV) boff = int(self.tick * BV) for i in range(self.holiday.NUM_GLOBES): ri = (roff + RMULT * i * self.atog) % SINELENGTH gi = (goff + GMULT * i * self.atog) % SINELENGTH bi = (boff + BMULT * i * self.atog) % SINELENGTH self.holiday.setglobe(i, self.sine[ri], self.sine[gi], self.sine[bi]) self.holiday.render() # Send the colours out time.sleep(SLEEP) self.tick += 1 def init(self): """Builds a shape based on a sinewave""" self.sine = [] for i in range(SINELENGTH): theta = 2.0 * math.pi * i / SINELENGTH self.sine.append(int(32 + 31 * math.sin(theta))) self.atog = SINELENGTH / self.holiday.NUM_GLOBES
class Sorterapp(threading.Thread): def run(self): """Go""" global addr self.terminate = False self.holiday = HolidaySecretAPI(addr=addr) self.s = 0 self.sorters = [Bubblesorter, Insertsorter, Mergesorter, Quicksorter ] while True: if self.terminate: return self.runsort() time.sleep(SLEEP_BETWEEN) def runsort(self): list = range(self.holiday.NUM_GLOBES) shuffle(list) self.makegradient() sorter = self.makesorter(list) sorter.sort() self.render(list, -1) def makesorter(self, list): sorterc = self.sorters[self.s] self.s += 1 if self.s == len(self.sorters): self.s = 0 print "Algorithm: ", sorterc sorter = sorterc(list, lambda l, c: self.render(l, c)) return sorter def render(self, list, cursor): i = 0 for l in list: ( r, g, b ) = self.gradient(l) self.holiday.setglobe(i, r, g, b) i += 1 if cursor > -1: self.holiday.setglobe(cursor, *CURSOR) self.holiday.render() time.sleep(WAIT) def makegradient(self): #c = random.sample(COLOURS, 2) self.grvalues = [] h1 = random.random() h2 = h1 + .5 + random.uniform(-.25, .25) if h2 > 1: h2 -= 1 s1 = random.uniform(.5, 1) s2 = random.uniform(.5, 1) n = self.holiday.NUM_GLOBES fn = float(n) for i in range(n): k = i / fn h = lerp(h1, h2, k) s = lerp(s1, s2, k) ( r, g, b ) = colorsys.hsv_to_rgb(h, s, 1) self.grvalues.append(rgbtoholiday(r, g, b)) self.gradient = lambda i: self.grvalues[i]
class HCMapp(threading.Thread): def run(self): global holiday_address self.terminate = False self.hol = HolidaySecretAPI(addr=holiday_address) while True: if self.terminate: self.reset_globes() return cpus = self.fetch_cpu_vals() self.my_render(cpus) def reset_globes(self): for globeindex in range(self.hol.NUM_GLOBES): self.hol.setglobe(globeindex, 0x00, 0x00, 0x00) self.hol.render() def my_render(self, cpus): """ Renders a list of CPU usage values to the Holiday """ led_on = 0xFF led_off = 0x00 globes = self.hol.NUM_GLOBES green = [] blue = [] red = [] cpu_count = len(cpus) globes_per_fragment_base = globes // cpu_count remainder = globes % cpu_count stride = cpu_count // remainder fragment_counts = [] for cpu in range(cpu_count): if (cpu % stride == 0): fragment_counts.append(globes_per_fragment_base + 1) else: fragment_counts.append(globes_per_fragment_base) for cpu in range(cpu_count): fragment_count = fragment_counts[cpu] greencount = (cpus[cpu] * fragment_count) / 100 # Fill the arrays of colours for globe in range(fragment_count): if globe < greencount: if globe >= fragment_count - ((fragment_count * 25) / 100): red.append(led_on) green.append(led_off) else: red.append(led_off) green.append(led_on) blue.append(led_off) else: green.append(led_off) blue.append(led_off) red.append(led_off) # Set the globe values and render for globeindex in range(globes): self.hol.setglobe(globeindex, red[globeindex], green[globeindex], blue[globeindex]) self.hol.render() def fetch_cpu_vals(self): """ Get the CPU use values from psutil """ return psutil.cpu_percent(interval=0.075, percpu=True)