Skip to content

Instantly share code, notes, and snippets.

@davegorst
Created June 4, 2017 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davegorst/713664a88a42d81c7a268d21cfbfa28e to your computer and use it in GitHub Desktop.
Save davegorst/713664a88a42d81c7a268d21cfbfa28e to your computer and use it in GitHub Desktop.
Show plasma animation on Raspberry Pi Sense HAT LED
#!/usr/bin/python
import evdev
from sense_hat import SenseHat
from math import *
from time import sleep
import colorsys
import numpy
from datetime import datetime, timedelta
"""
Before running execute the following to make sure all libraries are installed :
sudo apt-get install python-dev python-pip python-sense-hat
sudo pip install evdev
sudo pip install numpy
KEY_LEFT Faster
KEY_RIGHT Slower
"""
"""
* 'Plasma' demo effect
*
* Cycles of changing colours warped to give an illusion of liquid, organic movement.
* Colors are the sum of sine functions and various formulas.
* Based on formula by Robert Klep.
*
* http://luis.net/projects/processing/plasma/
*
"""
active_page = 1
frameCount = 0
width = 8
height = 8
pixelSize=360.0/(width*height)
fps=10
pixel_array = numpy.zeros((2,width*height,3),numpy.uint8)
sense = SenseHat ()
def setup():
global frameCount
frameCount = 24
def update():
global frameCount
global pixelSize
global width
global height
global active_page
active_page ^= 1
frameCount += 1
if frameCount >= 359:
frameCount = 1
# enable this to control the speed of animation regardless of CPU power
# int timeDisplacement = millis()/30;
# this runs plasma as fast as your computer can handle
timeDisplacement = frameCount;
# no need to do this math for every pixel
calculation1 = sin(radians(timeDisplacement * 0.61655617));
calculation2 = sin(radians(timeDisplacement * -3.6352262));
calculation3 = sin(radians(timeDisplacement * 1.6352262));
# plasma algorithm
for x in range(width):
s1 = 128.0 + (128.0 * sin(radians((x * 45)) * calculation1));
for y in range(height):
s2 = 128.0 + (128.0 * sin(radians(y * 45) * calculation2));
s3 = 128.0 + (128.0 * sin(radians(((x * 45) + (y * 45) + timeDisplacement * calculation3 * 5) / 2)));
s = (s1 + s2 + s3) / 3.0;
rgb = colorsys.hls_to_rgb(int(s) / 255.0, int(255 - s / 2.0) / 255.0, int(calculation3 * 255) / 255.0);
# sense.set_pixel(x, y, int(rgb[0] * 255.0), int(rgb[1] * 255.0), int(rgb[2] * 255.0));
r = int(abs(rgb[0] * 255.0))
g = int(abs(rgb[1] * 255.0))
b = int(abs(rgb[2] * 255.0))
#print (r,g,b)
pixel_array[active_page][(y * height) + x] = [r,g,b]
#print ((y * height) + x, pixel_array[active_page][(y * height) + x])
# sleep (0.1);
# main loop
def draw():
sense.set_pixels (pixel_array[active_page ^ 1])
def main():
setup ()
draw_period = timedelta(milliseconds=1000/fps)
next_draw_time = datetime.now() + draw_period
while True:
try:
#print ("Update")
update()
if datetime.now() > next_draw_time:
#print("Draw")
draw()
next_draw_time += draw_period
except KeyboardInterrupt:
sense.clear()
exit(0)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment