#!/usr/bin/env python # coding: latin-1 # Import libary functions we need import time # Make a function to set the LedBorg colour def SetColour(colour): LedBorg=open('/dev/ledborg','w') LedBorg.write(colour) LedBorg.close() # Set up our temperature chart, from cool to hot colours = ['002', '012', '022', '021', '020', '120', '220', '210', '200'] # Setup for processor monitor pathSensor = '/sys/class/thermal/thermal_zone0/temp' # File path used to read the temperature readingPrintMultiplier = 0.001 # Value to multiply the reading by for user display tempHigh = 40000 # Highest scale reading tempLow = 30000 # Lowest scale reading interval = 1 # Time between readings in seconds filterSize = 10 # Number of samples for the filter to cover (larger is smoother but slower) # Box filter class class BoxFilter: # Filter storage oldValues = [] lastIndex = 0 seeded = False window = 0 average = 0.0 # Filter initialisation def __init__(self, window): # Make sure we have at least 1 sample if window < 1: self.window = 1 else: self.window = int(window) # Make our storage the right size for i in range(self.window): self.oldValues.append(0.0) # Run a new value through the filter def run(self, newValue): # Ensure newValue is floating point newValue = float(newValue) # Check if we need to 'seed' the filter if self.seeded: # Find the next value to replace (the list wraps at the end) self.lastIndex += 1 if self.lastIndex >= self.window: self.lastIndex = 0 # Fill in the next value and re-calculate the average self.average -= self.oldValues[self.lastIndex] / float(self.window) self.oldValues[self.lastIndex] = newValue self.average += self.oldValues[self.lastIndex] / float(self.window) else: # We have not, fill in all fields for i in range(self.window): self.oldValues[i] = newValue self.seeded = True # Use the new value as the average self.average = newValue # Return the average return self.average try: # Make sure we are using floats tempHigh = float(tempHigh) tempLow = float(tempLow) # Make a new filter tempFilter = BoxFilter(filterSize) while True: # Read the temperature in from the file system fSensor = open(pathSensor, 'r') rawReading = float(fSensor.read()) fSensor.close() # Pass the reading through the filter reading = tempFilter.run(rawReading) # Pick the relevant colour position = (reading - tempLow) / (tempHigh - tempLow) position = int(position * len(colours)) if position < 0: position = 0 elif position >= len(colours): position = len(colours) - 1 # Set the relevant colour SetColour(colours[position]) # Print the latest reading, both filtered and raw print '%02.3f (raw %02.3f)' % (reading * readingPrintMultiplier, rawReading * readingPrintMultiplier) # Wait a while time.sleep(interval) except KeyboardInterrupt: # CTRL+C exit, turn off the LedBorg print 'Terminated' SetColour('000')