Browse Source

2to3 and writebml support

feature/balanceutils
Fr3deric 8 years ago committed by Frederic
parent
commit
ae63d92220
  1. 57
      staticSinePlasma.py

57
staticSinePlasma.py

@ -10,6 +10,8 @@ import math
import colorsys import colorsys
import blup.frame import blup.frame
import blup.output import blup.output
import blup.animation
import writebml
class Plasma(object): class Plasma(object):
@ -17,29 +19,29 @@ class Plasma(object):
self.width = width self.width = width
self.height = height self.height = height
self.pixelvalues = [[0] * height for i in xrange(width)] self.pixelvalues = [[0] * height for i in range(width)]
self.pixelsReady = False self.pixelsReady = False
self.offset = 0 self.offset = 0
self.update() self.update()
@property @property
def minValue(self): def minValue(self):
return min(map(min, self.pixelvalues)) return min(list(map(min, self.pixelvalues)))
@property @property
def maxValue(self): def maxValue(self):
return max(map(max, self.pixelvalues)) return max(list(map(max, self.pixelvalues)))
def applyPalette(self, palette): def applyPalette(self, palette):
min = self.minValue min = self.minValue
max = self.maxValue max = self.maxValue
norm = lambda x: ((x - min) / max) norm = lambda x: ((x - min) / max)
pixeldata = [ [None] * self.height for i in xrange(self.width) ] pixeldata = [ [None] * self.height for i in range(self.width) ]
for x in xrange(self.width): for x in range(self.width):
rowvalues = self.pixelvalues[x] rowvalues = self.pixelvalues[x]
for y in xrange(self.height): for y in range(self.height):
pixeldata[x][y] = palette.getColorValue(norm(rowvalues[y])) pixeldata[x][y] = palette.getColorValue(norm(rowvalues[y]))
return pixeldata return pixeldata
@ -49,8 +51,8 @@ class Plasma(object):
# return # return
self.offset += 1 self.offset += 1
for x in xrange(self.width): for x in range(self.width):
for y in xrange(self.height): for y in range(self.height):
xx = x+self.offset xx = x+self.offset
yy = y+self.offset yy = y+self.offset
p = 128 + 128 * math.sin(xx / 9.0) p = 128 + 128 * math.sin(xx / 9.0)
@ -68,10 +70,10 @@ class Palette(object):
def getColorValue(self, x): def getColorValue(self, x):
x = round(x, 3) x = round(x, 3)
if self.cache.has_key(x): if x in self.cache:
return self.cache[x] return self.cache[x]
c = colorsys.hsv_to_rgb((x + self.offset) % 1, 1, 1) c = colorsys.hsv_to_rgb((x + self.offset) % 1, 1, 1)
c = map(lambda x: int(x * 255), c) c = list(map(lambda x: int(x * 255), c))
self.cache[x] = c self.cache[x] = c
return c return c
@ -103,9 +105,19 @@ dim = blup.frame.FrameDimension(w, h, 256, 3)
#out = blup.output.getOutput('serialblup:/dev/ttyUSB0:115200') #out = blup.output.getOutput('serialblup:/dev/ttyUSB0:115200')
#out = blup.output.getOutput('colorfulshell') #out = blup.output.getOutput('colorfulshell')
#out = blup.output.getOutput('e3blp:bastel0:4242') #out = blup.output.getOutput('e3blp:bastel0:4242')
out = blup.output.getOutput('e3blp')
while True: if len(sys.argv) > 1:
outfile = sys.argv[1]
anim = blup.animation.Animation(dim)
else:
out = blup.output.getOutput('e3blp')
anim = None
num_frames = 200
delay = 50
i = 0
while i < num_frames:
#screen.fill((0,0,0)) #screen.fill((0,0,0))
#print 'updating palette...' #print 'updating palette...'
@ -116,20 +128,31 @@ while True:
pixeldata = plasma.applyPalette(palette) pixeldata = plasma.applyPalette(palette)
#print 'drawing...' #print 'drawing...'
f = blup.frame.Frame(dim) if anim is not None:
for x in xrange(w): frame = blup.animation.AnimationFrame(dim, delay)
else:
frame = blup.frame.Frame(dim)
for x in range(w):
#rowdata = pixeldata[x] #rowdata = pixeldata[x]
for y in xrange(h): for y in range(h):
#pygame.draw.rect(screen, pixeldata[x][y], (x*scalex, y*scaley, scalex, scaley), 0) #pygame.draw.rect(screen, pixeldata[x][y], (x*scalex, y*scaley, scalex, scaley), 0)
#pygame.draw.circle(screen, rowdata[y], (x, y), 1, 1) #pygame.draw.circle(screen, rowdata[y], (x, y), 1, 1)
i= f.setPixel(x, y, pixeldata[x][y]) frame.setPixel(x, y, pixeldata[x][y])
#print frame.channels, frame.depth #print frame.channels, frame.depth
#print 'done drawing' #print 'done drawing'
out.sendFrame(f) if anim is not None:
anim.addFrame(frame)
i += 1
else:
out.sendFrame(frame)
time.sleep(delay / 1000)
#pygame.display.update() #pygame.display.update()
#print 'done updating' #print 'done updating'
#time.sleep(0.05) #time.sleep(0.05)
if anim is not None:
writebml.writeBml(anim, outfile)

Loading…
Cancel
Save