-
Notifications
You must be signed in to change notification settings - Fork 1
/
rgb-cooling-hat-fan.py
118 lines (106 loc) · 3.58 KB
/
rgb-cooling-hat-fan.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import smbus
import time
import os
import sys
from datetime import datetime
from array import *
def setfanspeed(oVal):
try:
bus.write_byte_data(addr, fan_reg, oVal)
except:
exc_tuple = sys.exc_info()
print("setfanspeed [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])
def setRGBEffect(effect):
try:
if effect >= 0 and effect <= 4:
bus.write_byte_data(addr, rgb_effect_reg, effect&0xff)
except:
exc_tuple = sys.exc_info()
print("setRGBEffect [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])
def setRGBSpeed(speed):
try:
if speed >= 1 and speed <= 3:
bus.write_byte_data(addr, rgb_speed_reg, speed&0xff)
except:
exc_tuple = sys.exc_info()
print("setRGBSpeed [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])
def setRGBColor(color):
try:
if color >= 0 and color <= 6:
bus.write_byte_data(addr, rgb_color_reg, color&0xff)
except:
exc_tuple = sys.exc_info()
print("setRGBColor [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])
def setrgb(num,r,g,b):
try:
#turn lights off first
#bus.write_byte_data(addr, rgb_off_reg, 0x00)
if num >= max_led:
bus.write_byte_data(addr,0x00,0xff)
bus.write_byte_data(addr,0x01,r&0xff)
bus.write_byte_data(addr,0x02,g&0xff)
bus.write_byte_data(addr,0x03,b&0xff)
elif num >= 0:
bus.write_byte_data(addr,0x00,num&0xff)
bus.write_byte_data(addr,0x01,r&0xff)
bus.write_byte_data(addr,0x02,g&0xff)
bus.write_byte_data(addr,0x03,b&0xff)
except:
exc_tuple = sys.exc_info()
print("setrgb [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])
bus = smbus.SMBus(1)
addr = 0x0d
fan_reg = 0x08
state = 0
temp = 0
level_temp = 0
rgb_effect_reg = 0x04
rgb_speed_reg = 0x05
rgb_color_reg = 0x06
rgb_off_reg = 0x07
max_led = 3
rgb = [0x00, 0x00, 0x00, 0x00]
bus.write_byte_data(addr, rgb_off_reg, 0x00)
time.sleep(1)
setRGBEffect(1)
setRGBSpeed(3)
setRGBColor(4)
while True:
cmd = os.popen('vcgencmd measure_temp').readline()
CPU_TEMP = cmd.replace("temp=","").replace("'C\n","")
temp = float(CPU_TEMP)
if abs(temp - level_temp) >= 1:
if temp <= 40:
level_temp = 40
fan_speed = 0x7
rgb = [max_led, 0x00, 0x00, 0xff]
elif temp <= 45:
level_temp = 0x7
fan_speed = 0x7
rgb = [max_led, 0x1e, 0x90, 0xff]
elif temp <= 47:
level_temp = 47
fan_speed = 0x08
rgb = [max_led, 0x00, 0xbf, 0xff]
elif temp <= 49:
level_temp = 49
fan_speed = 0x06
rgb = [max_led, 0x5f, 0x9e, 0xa0]
elif temp <= 51:
level_temp = 51
fan_speed = 0x09
rgb = [max_led, 0xff, 0xff, 0x00]
elif temp > 51:
level_temp = 52
fan_speed = 0x01
rgb = [max_led, 0xff, 0x00, 0x00]
else:
level_temp = 0
fan_speed = 0x01
rgb = [max_led, 0xff, 0xff, 0xff]
print('Something wrong: choosing fan reg 0x01 instead')
setfanspeed(fan_speed)
print(CPU_TEMP + 'c: <' + str(level_temp) + ' : choosing fan reg ' + str(hex(fan_speed)))
setrgb(rgb[0],rgb[1],rgb[2],rgb[3])
sys.stdout.flush()
time.sleep(4)