summaryrefslogtreecommitdiffstats
path: root/Python/Sense HAT/funny-smiley.py
blob: bd8e0b45b45f89c165ba48f7f2f2dca67f724c9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
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()

#===============================================================================
# Change the smileys face after a random period of time
#===============================================================================
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]))