-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·90 lines (75 loc) · 2.33 KB
/
app.py
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
#!/usr/bin/env python
import os
import time
import colorsys
import threading
import rainbowhat
# Touch events
@rainbowhat.touch.press()
def touch_press(channel):
global inputs
# Reset all states to false
inputs = [False, False, False]
rainbowhat.lights.rgb(0, 0, 0)
# Set the selected button status to true and turn on LED
inputs[channel] = True
rainbowhat.lights[channel].on()
class DisplayThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
set_display()
class RainbowThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
set_rainbow()
def get_ambient_temperature():
# Getting CPU temperature from Raspberry Pi
res = os.popen('vcgencmd measure_temp').readline()
cpu_temp = int(float((res.replace("temp=","").replace("'C\n",""))))
# Getting temperature from RainbowHat thermometer
rainbowhat_temp = rainbowhat.weather.temperature()
# Calculate estimated temperature
return rainbowhat_temp - (cpu_temp - rainbowhat_temp) / 2
def set_display():
while True:
rainbowhat.display.clear()
if inputs[A]:
temperature = get_ambient_temperature()
rainbowhat.display.print_float(temperature)
if inputs[B]:
pressure = rainbowhat.weather.pressure()/100
rainbowhat.display.print_float(pressure)
if inputs[C]:
current_time = time.strftime("%H%M")
rainbowhat.display.print_str(current_time)
rainbowhat.display.show()
time.sleep(0.1)
def set_rainbow():
while True:
for x in range(7):
delta = time.time() * 20
hue = delta + (x*10)
hue %= 360
hue /= 360.0
r, g, b = [int(c * 255) for c in colorsys.hsv_to_rgb(hue, 1.0, 1.0)]
rainbowhat.rainbow.set_pixel(6-x, r, g, b, brightness=0.1)
rainbowhat.rainbow.show()
time.sleep(0.05)
# Clear existing display
rainbowhat.display.clear()
rainbowhat.display.show()
# Buttons identifier
A = 0
B = 1
C = 2
# Setting up initial state
inputs = [True, False, False]
rainbowhat.lights[A].on()
# Create new threads
display_thread = DisplayThread()
rainbow_thread = RainbowThread()
# Start new Threads
display_thread.start()
rainbow_thread.start()