From dace593f3b5e35f2021d8e09265020e3b8f008d0 Mon Sep 17 00:00:00 2001 From: Jakub Zika Date: Wed, 21 Sep 2011 23:38:20 +0200 Subject: [PATCH] Added class simplifiing ledbar output * also added rainbow.py, which uses this class to create rainbow effect --- host_python/README | 26 ++++++++++++++++++++++ host_python/ledbar.py | 50 ++++++++++++++++++++++++++++++++++++++++++ host_python/rainbow.py | 33 ++++++++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 host_python/README create mode 100644 host_python/ledbar.py create mode 100755 host_python/rainbow.py diff --git a/host_python/README b/host_python/README new file mode 100644 index 0000000..ff3639b --- /dev/null +++ b/host_python/README @@ -0,0 +1,26 @@ +INTRODUCTION: + +The scripts in this directory try to work according to the KISS principle. It +contains a script which redirects its stdin to serial device +(send_to_serial.py), one that simulates the ledbar and sends the data unaltered +from its stdin to its stdout, a module that helps programmers to program +various graphical effects and some example effect generators using this module. + + +EXAMPLES: + +./rainbow.py | ./demo.py >/dev/null + - only simulate the rainbow effect, do not send it anywhere + +./rainbow.py | ./send_to_serial.py + - only send the data to the serial device, do not show the simulation + +./rainbow.py | ./demo.py | ./send_to_serial.py + - combine both options, show a simulation and send the data to the serial + device + + +DEPENDENCIES: + +demo.py requires PyGame, Python wrapper around SDL +send_to_serial.py requires PySerial, Python library for work with serial ports diff --git a/host_python/ledbar.py b/host_python/ledbar.py new file mode 100644 index 0000000..72fed7d --- /dev/null +++ b/host_python/ledbar.py @@ -0,0 +1,50 @@ +#!/usr/bin/python +# vim:et:sw=4:ts=4:sts=4 + +import sys +import time + +class Ledbar: + + def __init__(self, boxes=10, secs_per_frame=0.025): + self.boxes = boxes + self.secs_per_frame = secs_per_frame + self.last_update = time.time() + self.pixels = [] + for i in xrange(boxes): + self.pixels.append([0, 0, 0]) + + def set_pixel(self, pixel, red, green, blue): + self.set_red(pixel, red) + self.set_green(pixel, green) + self.set_blue(pixel, blue) + + def set_red(self, pixel, red): + if red < 0.0 or red > 1.0: raise ValueError('red has to be between 0.0 and 1.0') + self.pixels[pixel][0] = int(red*255.99) + + def set_green(self, pixel, green): + if green < 0.0 or green > 1.0: raise ValueError('green has to be between 0.0 and 1.0') + self.pixels[pixel][1] = int(green*255.99) + + def set_blue(self, pixel, blue): + if blue < 0.0 or blue > 1.0: raise ValueError('blue has to be between 0.0 and 1.0') + self.pixels[pixel][2] = int(blue*255.99) + + def echo(self, s, no_newline=False): + sys.stderr.write(str(s) + ('' if no_newline else '\n')) + + def update(self): + now = time.time() + delta = now - self.last_update + if delta < self.secs_per_frame: + time.sleep(self.secs_per_frame - delta) + try: + for p in self.pixels: + for c in p: + sys.stdout.write(chr(c)) + sys.stdout.flush() + except IOError: + return False + self.last_update += self.secs_per_frame + return True diff --git a/host_python/rainbow.py b/host_python/rainbow.py new file mode 100755 index 0000000..36aa39c --- /dev/null +++ b/host_python/rainbow.py @@ -0,0 +1,33 @@ +#!/usr/bin/python +# vim:et:sw=4:ts=4:sts=4 + +import sys + +from ledbar import Ledbar + +PIXELS = 10 + +def update(t, i): + offset = float(i)/PIXELS + time = 0.005*t + phi = 6*offset+time + phase = int(phi%6) + part = phi % 1.0 + inc = part + dec = 1-part + if phase == 0: return ( 1, inc, 0) + elif phase == 1: return (dec, 1, 0) + elif phase == 2: return ( 0, 1, inc) + elif phase == 3: return ( 0, dec, 1) + elif phase == 4: return (inc, 0, 1) + elif phase == 5: return ( 1, 0, dec) + +l = Ledbar(PIXELS) +t = 0 +work = True +while work: + for i in xrange(PIXELS): + c = update(t, i) + l.set_pixel(i, c[0], c[1], c[2]) + work = l.update() + t += 1