Code for the fan and I think for the RGB
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)
and the code for the display
import Adafruit_GPIO.I2C as I2C
import time
import os
import smbus
import sys
bus = smbus.SMBus(1)
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import subprocess
hat_addr = 0x0d
rgb_effect_reg = 0x04
fan_reg = 0x08
fan_state = 2
count = 0
# Raspberry Pi pin configuration:
RST = None # on the PiOLED this pin isnt used
# 128x32 display with hardware I2C:
disp = Adafruit_SSD1306.SSD1306_128_32(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
# Draw some shapes.
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
bottom = height-padding
# Move left to right keeping track of the current x position for drawing shapes.
x = 0
# Load default font.
font = ImageFont.load_default()
# Alternatively load a TTF font. Make sure the .ttf font file is in the same directory as the python script!
# Some other nice fonts to try: http://www.dafont.com/bitmap.php
#font = ImageFont.truetype('/opt/rgb-cooling-hat/nk57.ttf', 8)
def setFanSpeed(speed):
bus.write_byte_data(hat_addr, fan_reg, speed&0xff)
def setRGBEffect(effect):
bus.write_byte_data(hat_addr, rgb_effect_reg, effect&0xff)
def getCPULoadRate():
f1 = os.popen("cat /proc/stat", 'r')
stat1 = f1.readline()
count = 10
data_1 = []
for i in range (count):
data_1.append(int(stat1.split(' ')[i+2]))
total_1 = data_1[0]+data_1[1]+data_1[2]+data_1[3]+data_1[4]+data_1[5]+data_1[6]+data_1[7]+data_1[8]+data_1[9]
idle_1 = data_1[3]
time.sleep(1)
f2 = os.popen("cat /proc/stat", 'r')
stat2 = f2.readline()
data_2 = []
for i in range (count):
data_2.append(int(stat2.split(' ')[i+2]))
total_2 = data_2[0]+data_2[1]+data_2[2]+data_2[3]+data_2[4]+data_2[5]+data_2[6]+data_2[7]+data_2[8]+data_2[9]
idle_2 = data_2[3]
total = int(total_2-total_1)
idle = int(idle_2-idle_1)
usage = int(total-idle)
print("idle:"+str(idle)+" total:"+str(total))
usageRate =int(float(usage * 100/ total))
print("usageRate:%d"%usageRate)
return "CPU:"+str(usageRate)+"%"
def setOLEDshow():
# Draw a black filled box to clear the image.
draw.rectangle((0,0,width,height), outline=0, fill=0)
#cmd = "top -bn1 | grep load | awk '{printf \"CPU:%.0f%%\", $(NF-2)*100}'"
#CPU = subprocess.check_output(cmd, shell = True)
CPU = getCPULoadRate()
cmd = os.popen('vcgencmd measure_temp').readline()
CPU_TEMP = cmd.replace("temp=","Temp:").replace("'C\n","C")
global g_temp
g_temp = float(cmd.replace("temp=","").replace("'C\n",""))
cmd = "free -m | awk 'NR==2{printf \"RAM:%s/%s MB \", $2-$3,$2}'"
MemUsage = subprocess.check_output(cmd, shell = True)
cmd = "df -h | awk '$NF==\"/\"{printf \"Disk:%d/%dMB\", ($2-$3)*1024,$2*1024}'"
Disk = subprocess.check_output(cmd, shell = True)
cmd = "hostname -I | awk '{ print $1 }'"
IP = subprocess.check_output(cmd, shell = True)
# Write two lines of text.
draw.text((x, top), str(CPU), font=font, fill=255)
draw.text((x+56, top), str(CPU_TEMP), font=font, fill=255)
draw.text((x, top+8), MemUsage.decode('utf-8'), font=font, fill=255)
draw.text((x, top+16), Disk.decode('utf-8'), font=font, fill=255)
draw.text((x, top+24), "wlan0:" + IP.decode('utf-8'), font=font, fill=255)
# Display image.
try:
disp.image(image)
disp.display()
time.sleep(.1)
except:
exc_tuple = sys.exc_info()
print("setoledshow [0]:" + exc_tuple[0] + " [1]:" + exc_tuple[1] + " [2]:" + exc_tuple[2])
#setFanSpeed(0x00)
#setRGBEffect(0x03)
while True:
setOLEDshow()
# if g_temp >= 55:
# if fan_state != 1:
# setFanSpeed(0x01)
# fan_state = 1
# elif g_temp <= 48:
# if fan_state != 0:
# setFanSpeed(0x00)
# fan_state = 0
#
# if count == 10:
# setRGBEffect(0x04)
# elif count == 20:
# setRGBEffect(0x02)
# elif count == 30:
# setRGBEffect(0x01)
# elif count == 40:
# setRGBEffect(0x03)
# count = 0
# count += 1
# was time.sleep(.5)
sys.stdout.flush()
time.sleep(5)