Are you interested in some code allowing for showing the CPU temperature using the fan color?
#!/usr/bin/env python3
from cm_rgb.ctrl import LedChannel, LedMode, CMRGBController
import psutil
import atexit
import time
import click
try:
import sensors
import ctypes
atexit.register(sensors.cleanup)
sensors.init()
except ImportError as e:
print(e,
"\n\nCould not import sensors.\n" +
"Cannot show temperature as fan color.\n\n" +
"To fix this, run:\npip3 install pysensors"
)
@click.command()
@click.option("--bg-color",'bgColor', default='#00FFFF', help="Background LED's color")
@click.option("--cpu-color",'cpuColor', default='#FFA500', help="Color of the cpu load LED's")
@click.option('--brightness', type=click.IntRange(1, 5, clamp=True), default=3)
@click.option('--interval', type=click.FloatRange(0.01, 60, clamp=True), default=3)
@click.option("--show_temperature", is_flag=True)
def monitor(bgColor, cpuColor, brightness, interval, show_temperature):
c = CMRGBController()
bgChannel = LedChannel.R_STATIC
cpuChannel = LedChannel.R_SWIRL
b = [0x33,0x66,0x99,0xCC,0xFF][brightness-1]
bgColor = bgColor.lstrip('#')
col = [int(bgColor[i:i+2], 16) for i in (0, 2, 4)]
c.set_channel(bgChannel, LedMode.R_DEFAULT, b, col[0], col[1], col[2])
cpuColor = cpuColor.lstrip('#')
col = [int(cpuColor[i:i+2], 16) for i in (0, 2, 4)]
c.set_channel(cpuChannel, LedMode.R_DEFAULT, b, col[0], col[1], col[2], 0x60)
c.apply()
def exit_handler():
c.restore()
atexit.register(exit_handler)
low = {"t": 45, "color": [0, 64, 16]}
high = {"t": 85, "color": [255, 0, 0]}
if show_temperature:
for chip in sensors.iter_detected_chips("k10temp-pci-00c3"):
result_p = sensors._get_features(ctypes.byref(chip), ctypes.byref(ctypes.c_int(0)))
if not result_p:
break
sensor = result_p.contents
sensor.chip = chip
while True:
if show_temperature:
print(sensor.label, sensor.get_value())
t = sensor.get_value()
interp_t = max(0, min(1, (t-low["t"])/(high["t"]-low["t"])))
color = [
int(
interp_t * high["color"][i]
+ (1 - interp_t)*low["color"][i]
)
for i in range(3)
]
print(color)
c.set_channel(LedChannel.FAN, LedMode.STATIC, b, color[0], color[1], color[2])
# gives a single float value
cpu = psutil.cpu_percent()
cpu_leds = int(round(cpu*15 / 100))
total = 15 - cpu_leds
ring_leds = ([cpuChannel]*cpu_leds)
ring_leds = ring_leds + ([bgChannel]*total)
shift = -8
ring_leds = ring_leds[-shift:]+ring_leds[:-shift]
c.assign_leds_to_channels(LedChannel.LOGO, LedChannel.FAN, *ring_leds)
c.apply()
time.sleep(interval)
if __name__ == '__main__':
monitor()
If you are interested, I will clean it up a bit & create a pull request.