diff options
author | Thomas Lange <code@nerdmind.de> | 2017-02-15 11:40:32 +0100 |
---|---|---|
committer | Thomas Lange <code@nerdmind.de> | 2017-02-15 11:40:32 +0100 |
commit | e53682c593e9218a779c1a6bb96072c913b77f24 (patch) | |
tree | a0ac3f42152b262194703e4e66cf1b0fe9aee614 /Python/Sense HAT | |
parent | 07f918bac5beb7bc18baf6ede041e17dec85aaba (diff) | |
download | snippets-e53682c593e9218a779c1a6bb96072c913b77f24.tar.gz snippets-e53682c593e9218a779c1a6bb96072c913b77f24.tar.xz snippets-e53682c593e9218a779c1a6bb96072c913b77f24.zip |
Initial commit.
Diffstat (limited to 'Python/Sense HAT')
-rw-r--r-- | Python/Sense HAT/funny-smiley.py | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/Python/Sense HAT/funny-smiley.py b/Python/Sense HAT/funny-smiley.py new file mode 100644 index 0000000..0a0782c --- /dev/null +++ b/Python/Sense HAT/funny-smiley.py @@ -0,0 +1,101 @@ +#!/usr/bin/python + +from sense_hat import SenseHat +from time import sleep +from random import choice +import threading + +#=============================================================================== +# Define the default rotation of the pixel matrix +#=============================================================================== +rotation = 90 + +#=============================================================================== +# Define the RGB color codes for the pixel matrix +#=============================================================================== +a = [0, 0, 50] +b = [75, 25, 0] +c = [0, 50, 25] +n = [0,0,0] +r = [50, 0, 0] + +#=============================================================================== +# LED pixel matrix for first image +#=============================================================================== +image_a = [ + a,a,a,a,a,a,a,a, + a,c,a,a,a,a,c,a, + a,a,c,a,a,c,a,a, + a,a,a,a,a,a,a,a, + a,c,c,c,c,c,c,a, + a,a,c,c,c,c,a,a, + a,a,a,a,a,a,a,a, + a,a,a,a,a,a,a,a +] + +#=============================================================================== +# LED pixel matrix for second image +#=============================================================================== +image_b = [ + a,a,a,a,a,a,a,a, + a,r,r,a,a,r,r,a, + a,r,r,a,a,r,r,a, + a,a,a,a,a,a,a,a, + a,c,c,c,c,c,c,a, + a,c,n,n,n,n,c,a, + a,a,c,c,c,c,a,a, + a,a,a,a,a,a,a,a +] + +#=============================================================================== +# Update rotation when the device is moved in a certain direction +#=============================================================================== +def updateDisplayRotation(SenseHat, default=0): + last_rotation = default + + while True: + x = round(SenseHat.get_accelerometer_raw()['x'], 0) + y = round(SenseHat.get_accelerometer_raw()['y'], 0) + + if x == -1: + if last_rotation != 90: + last_rotation = 90 + SenseHat.set_rotation(90) + elif x == 1: + if last_rotation != 270: + last_rotation = 270 + SenseHat.set_rotation(270) + elif y == -1: + if last_rotation != 180: + last_rotation = 180 + SenseHat.set_rotation(180) + elif y == 1: + if last_rotation != 0: + last_rotation = 0 + SenseHat.set_rotation(0) + else: + if last_rotation != default: + last_rotation = default + SenseHat.set_rotation(default) + + sleep(0.5) + +SenseHat = SenseHat() +SenseHat.set_rotation(rotation) + +#=============================================================================== +# Run updateDisplayRotation() in the background +#=============================================================================== +BackgroundThread = threading.Thread(target=updateDisplayRotation, args=(SenseHat, rotation)) +BackgroundThread.daemon = True +BackgroundThread.start() + +#=============================================================================== +# Run updateDisplayRotation() in the background +#=============================================================================== +while True: + SenseHat.set_pixels(image_a) + sleep(choice([2,4,6,8])) + + SenseHat.set_pixels(image_b) + sleep(choice([1.0, 1.5, 2.0]))
\ No newline at end of file |