Apology in advance for wall of text, but here’s the current progress on the code.
I use a program called OpenHardwareMonitor to dump gpu temperature to a log file located in a ramdisk and then read by python.
It’s definitely not the most efficient way of doing things, but for now it’s functional.
And I’m slowly incorporating more things that allow it to be easily customized, with colour and pattern speeds
[PULSE] is a small wave of lights that travel along the led strip
[HEARTBEAT] was originally just the entire strip in sync and brightness changed, but was altered to have multiple starting points and the pulse goes out and back in from each point (try it out to see what I mean, it’s 1.30am here :P)
[MAIN] the temperature recorder
Starts with 2x pulses, then colour fades in and changes live depending on GPU temp (might need to adjust depending on your log files layout if you’re doing it the same way)
If the temp passes critical (set at 80 here) it begins heartbeat, getting faster with each degree until 90C where it’s constant flashing
import time
import math
import colorsys
import os
from datetime import date, timedelta
from random import randint
from blinkstick import blinkstick
class Main(blinkstick.BlinkStickPro):
def pulse(self,red,green,blue,numtimes):
self.numtimes = numtimes
self.red = red
self.green = green
self.blue = blue
print "-Pulse- x%d" %(numtimes)
startingpoint=-10
br = 0
ud = 1
x = 0
maxbrightness=255
brightnessskip=43
minbrightness=maxbrightness-(29*brightnessskip)
numofblinks=90
currentblinks=0
numled=startingpoint
curtime=0
try:
while curtime<numtimes:
curtime=curtime+1
while numled<59-startingpoint:
currentblinks = currentblinks + 1
x=maxbrightness
if numled>=0:
if numled <=59:
self.set_color(0,numled,red,green,blue)
numledleft=numled
numledright=numled
while x-brightnessskip>=0:
if numledleft+1>=0:
if numledleft+1<=59:
self.set_color(0,numledleft+1,(red*(x-brightnessskip))/255,(green*(x-brightnessskip))/255,(blue*(x-brightnessskip))/255)
if numledright-1>=0:
if numledright-1<=59:
self.set_color(0,numledright-1,(red*(x-brightnessskip))/255,(green*(x-brightnessskip))/255,(blue*(x-brightnessskip))/255)
numledleft=numledleft+1
numledright=numledright-1
x=x-brightnessskip
for i in range(0,2):
if x-brightnessskip<0:
if numledleft+1>=0:
if numledleft+1<=59:
self.set_color(0,numledleft+1,0,0,0)
if numledright-1>=0:
if numledright-1<=59:
self.set_color(0,numledright-1,0,0,0)
numledleft=numledleft+1
numledright=numledright-1
numled = numled + 1
self.send_data_all()
time.sleep(.01)
numled=startingpoint
except KeyboardInterrupt:
self.off()
return
#################################################################################
def beat(self,speed):
print "-Heartbeat-"
self.speed = speed
if speed < 0:
speed = 0
elif speed >10:
speed = 10
red = 255
green = 0
blue = 0
numled = 29
x = 0
maxbrightness=255
numofsegments=10
brightnessskip=maxbrightness/(numofsegments)
beatdist=20
numofblinks=2
currentblinks=0
numofbeats=1
currentbeats=0
y = 0
blinkstate=[]
try:
while currentbeats<numofbeats:
currentbeats=currentbeats+1
while currentblinks<numofblinks:
currentblinks = currentblinks + 1
x=maxbrightness
self.set_color(0,numled,red,green,blue)
self.set_color(0,numled+beatdist,red,green,blue)
self.set_color(0,numled-beatdist,red,green,blue)
for i in range(2,numofsegments):
y=x-brightnessskip
self.set_color(0,numled+i,y,0,0)
self.set_color(0,numled-i,y,0,0)
self.set_color(0,numled+beatdist+i,y,0,0)
self.set_color(0,numled+beatdist-i,y,0,0)
self.set_color(0,numled-beatdist+i,y,0,0)
self.set_color(0,numled-beatdist-i,y,0,0)
x=x-brightnessskip
self.send_data_all()
time.sleep((speed)*.001)
for j in range(2,numofsegments):
k=numofsegments-j+1
self.set_color(0,numled+j,0,0,0)
self.set_color(0,numled-j,0,0,0)
self.set_color(0,numled+beatdist+j,0,0,0)
self.set_color(0,numled+beatdist-j,0,0,0)
self.set_color(0,numled-beatdist+j,0,0,0)
self.set_color(0,numled-beatdist-j,0,0,0)
self.send_data_all()
time.sleep((speed)*.001)
currentblinks=0
time.sleep((speed)*.1)
except KeyboardInterrupt:
self.off()
return
#########################################################################
def run(self):
print "Starting..."
self.send_data_all()
main.pulse(255,0,0,2)
prevtime = time.time()
prevtemp = 0
temperature = 0
count = 0
fadetime = 0.0
r0 = 0
g0 = 0
b0 = 0
try:
while True:
try:
with open(time.strftime("OpenHardwareMonitorLog-%Y-%m-%d.csv"), "rb") as f:
first = f.readline() # Read the first line.
f.seek(-2, 2) # Jump to the second last byte.
while f.read(1) != "\n": # Until EOL is found...
f.seek(-2, 1) # ...jump back the read byte plus one more.
log = f.readline() # Read last line.
temperature = int(log[20] + log[21])
except:
pass
count = count + 1
if (time.time() - prevtime) > 600: # Truncate file down to 1 line to save space
f = open(time.strftime("OpenHardwareMonitorLog-%Y-%m-%d.csv"), "r+")
f.seek(0)
f.truncate()
f.write(log)
f.close
prevtime = time.time()
yesterday = date.today() - timedelta(1)
print "Current file truncated"
try:
f = yesterday.strftime("OpenHardwareMonitorLog-%Y-%m-%d.csv") # If previous day's file found, delete
os.remove(f)
print "Previous file deleted"
except:
print "Error deleting previous file: File not found"
count = 0
if temperature < 30:
temperature = 30
if temperature <= 50:
x = (temperature - 30.0)/20.0
red = 255 * x
green = 0
blue = 255
elif temperature <= 80:
x = 1-((temperature - 50.0)/30.0)
red = 255
green = 0
blue = 255 * x
else:
main.beat(90-temperature)
if temperature <= 80 and temperature != prevtemp:
fadetime = fadetime + 1.0
if fadetime>=20.0:
fadetime = 0.0
r0 = red
g0 = green
b0 = blue
prevtemp = temperature
delay = .5
else:
delay=0.02
step=fadetime/20.0
for i in range(0,60):
self.set_color(0,i,r0+((red-r0)*step),g0+((green-g0)*step),b0+((blue-b0)*step))
self.send_data_all()
time.sleep(delay)
print "R: %3.2f G: %3.2f B %3.2f " % ((r0+((red-r0)*step)), (g0+((green-g0)*step)), (b0+((blue-b0)*step)))
else:
time.sleep(1)
print "sleep"
except KeyboardInterrupt:
self.off()
return
#########################################################################
# Change the number of LEDs for r_led_count
main = Main(r_led_count=60, max_rgb_value=255)
if main.connect():
main.run()
else:
print "No BlinkSticks found"