question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Feature request: Show CPU temperature as Fan color (contains prototype)

See original GitHub issue

Are you interested in some code allowing for showing the CPU temperature using the fan color?

I coded a proof of concept version using pysensor for temperature value access, which is a wrapper for lm-sensors (additional requirements).

#!/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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
mpsdskdcommented, May 5, 2020

Nice 👍

Your code is a lot less prototype-y^^

Works on my machine. Thanks for including my suggestions into this project.

1reaction
gfduszynskicommented, May 3, 2020

I’ve updated the code with slightly simplified temp source selection (Single argument for source) There is also a possibility to list temp sources in format that is accepted by the argument.

Warning for optional dependency is being triggered only when dependency is needed. I’ve removed dependency for ctypes, best part is no part 😃

Give it a try if you have time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sensors Monitor - Applets - Cinnamon Spices - Linux Mint
This applet displays and monitors the values ​​of many computer sensors concerning Temperatures (from CPU, GPU, Power Supply), Fan Speed, ...
Read more >
Feature request: monitor CPU temperature #5454 - GitHub
I would like to monitor the temperature on my embedded devices. It'd be good if it is displayed as a graph, just like...
Read more >
How To Easily Monitor Your PC's Temperature
The fans turn yellow as the temperature increases. Red fans indicate the CPU is too hot. Incidentally, the same mechanism that adjusts the...
Read more >
How to Monitor CPU and GPU Temperatures on Any Computer
How to Monitor CPU and GPU Temperatures on Any ComputerOpen Hardware Monitor is a free open source software that monitors temperature ...
Read more >
How to monitor your PC's CPU temperature
Rig running hot? Here's how to monitor your PC's CPU temperature using free software utilities, as well as some tips on keeping the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found