You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
2.9 KiB
136 lines
2.9 KiB
8 years ago
|
#!/usr/bin/python
|
||
|
|
||
|
# inspired by:
|
||
|
# http://lodev.org/cgtutor/plasma.html
|
||
|
|
||
|
import sys
|
||
|
import time
|
||
|
import math
|
||
|
#import pygame
|
||
|
import colorsys
|
||
|
import blup.frame
|
||
|
import blup.output
|
||
|
|
||
|
|
||
|
class Plasma(object):
|
||
|
def __init__(self, width, height):
|
||
|
self.width = width
|
||
|
self.height = height
|
||
|
|
||
|
self.pixelvalues = [[0] * height for i in xrange(width)]
|
||
|
self.pixelsReady = False
|
||
|
self.offset = 0
|
||
|
self.update()
|
||
|
|
||
|
@property
|
||
|
def minValue(self):
|
||
|
return min(map(min, self.pixelvalues))
|
||
|
|
||
|
@property
|
||
|
def maxValue(self):
|
||
|
return max(map(max, self.pixelvalues))
|
||
|
|
||
|
def applyPalette(self, palette):
|
||
|
min = self.minValue
|
||
|
max = self.maxValue
|
||
|
norm = lambda x: ((x - min) / max)
|
||
|
|
||
|
pixeldata = [ [None] * self.height for i in xrange(self.width) ]
|
||
|
|
||
|
for x in xrange(self.width):
|
||
|
rowvalues = self.pixelvalues[x]
|
||
|
for y in xrange(self.height):
|
||
|
pixeldata[x][y] = palette.getColorValue(norm(rowvalues[y]))
|
||
|
|
||
|
return pixeldata
|
||
|
|
||
|
def update(self):
|
||
|
#if self.pixelsReady:
|
||
|
# return
|
||
|
self.offset += 1
|
||
|
|
||
|
for x in xrange(self.width):
|
||
|
for y in xrange(self.height):
|
||
|
xx = x+self.offset
|
||
|
yy = y+self.offset
|
||
|
p = 128 + 128 * math.sin(xx / 9.0)
|
||
|
p += 128 + 128 * math.sin(yy / 16.0)
|
||
|
p += 128 + 128 * math.sin((yy + xx) / 16.0)
|
||
|
p += 128 + 128 * math.sin(math.sqrt(yy*yy + xx*xx) / 16.0)
|
||
|
p += 128 + 8 * math.sin(math.sqrt(yy* xx) / 16.0)
|
||
|
self.pixelvalues[x][y] = p
|
||
|
self.pixelsReady = True
|
||
|
|
||
|
class Palette(object):
|
||
|
def __init__(self):
|
||
|
self.offset = 0
|
||
|
self.cache = {}
|
||
|
|
||
|
def getColorValue(self, x):
|
||
|
x = round(x, 3)
|
||
|
if self.cache.has_key(x):
|
||
|
return self.cache[x]
|
||
|
c = colorsys.hsv_to_rgb((x + self.offset) % 1, 1, 1)
|
||
|
c = map(lambda x: int(x * 255), c)
|
||
|
self.cache[x] = c
|
||
|
return c
|
||
|
|
||
|
def update(self):
|
||
|
self.cache = {}
|
||
|
self.offset += 0.01
|
||
|
if self.offset > 1:
|
||
|
self.offset -= 1
|
||
|
|
||
|
##############################################
|
||
|
|
||
|
w = 22
|
||
|
h = 16
|
||
|
|
||
|
#w = 80
|
||
|
#h = 18
|
||
|
|
||
|
scalex = 30
|
||
|
scaley = 60
|
||
|
|
||
|
plasma = Plasma(w,h)
|
||
|
palette= Palette()
|
||
|
|
||
|
#screen = pygame.display.set_mode((w*scalex, h*scaley))
|
||
|
#pygame.display.update()
|
||
|
|
||
|
#dim = blup.frame.FrameDimension(w, h, 8, 3)
|
||
|
dim = blup.frame.FrameDimension(w, h, 256, 3)
|
||
|
#out = blup.output.getOutput('serialblup:/dev/ttyUSB0:115200')
|
||
|
#out = blup.output.getOutput('colorfulshell')
|
||
|
#out = blup.output.getOutput('e3blp:bastel0:4242')
|
||
|
out = blup.output.getOutput('e3blp')
|
||
|
|
||
|
while True:
|
||
|
#screen.fill((0,0,0))
|
||
|
|
||
|
#print 'updating palette...'
|
||
|
palette.update()
|
||
|
#print 'updating plasma...'
|
||
|
plasma.update()
|
||
|
#print 'applying palette...'
|
||
|
pixeldata = plasma.applyPalette(palette)
|
||
|
|
||
|
#print 'drawing...'
|
||
|
f = blup.frame.Frame(dim)
|
||
|
for x in xrange(w):
|
||
|
#rowdata = pixeldata[x]
|
||
|
for y in xrange(h):
|
||
|
#pygame.draw.rect(screen, pixeldata[x][y], (x*scalex, y*scaley, scalex, scaley), 0)
|
||
|
#pygame.draw.circle(screen, rowdata[y], (x, y), 1, 1)
|
||
|
i= f.setPixel(x, y, pixeldata[x][y])
|
||
|
#print frame.channels, frame.depth
|
||
|
#print 'done drawing'
|
||
|
|
||
|
out.sendFrame(f)
|
||
|
|
||
|
#pygame.display.update()
|
||
|
#print 'done updating'
|
||
|
#time.sleep(0.05)
|
||
|
|
||
|
|