fit-statUSB: Perl App for Displaying CPU Temp

programmable multi-color LED that plugs into a USB port
Post Reply
hassellbear
Posts: 106
Joined: Mon May 28, 2012 12:25 pm

fit-statUSB: Perl App for Displaying CPU Temp

Post by hassellbear »

Overview

For most of us, color vision is a most effective means of receiving meaningful information. We instantly understand the message being conveyed by the green, yellow, and red lights of a traffic signal. While driving, flashing red or blue lights may indicate trouble - if you are speeding.

Why then can't color be used to indicate certain operational parameters of your computer? The answer is - color can most certainly be used to provide you with a wide variety of information about your computer and the processes it is running.



An Example - CPU Temperature

What's your computer's CPU temperature?

Certainly there are applets which display your computer's CPU temperature to the tenth of a degree C in the system tray or some other such place. However, do you really want to know that temp to the tenth of a degree, or is it sufficient for your purposes to simply know in general how warm your CPU is running? Why not use a color indicator such as fit-statUSB to provide a general idea of CPU temperature.?

The perl app listed below does just that - it provides a color indication of CPU temperature beginning at blue for cold and hot pink for really warm. CPU temperature is displayed by fit-statUSB via a 20 step color continuum.

COLD------------------------WARM--------------------------HOT
######-######-######-######-######-######-######



Sample Output of Perl App Showing App Window and fit-statUSB Output

Cold CPU
Cold combo.jpeg
Cold combo.jpeg (22.81 KiB) Viewed 22305 times

Warm CPU
Warm combo.jpeg
Warm combo.jpeg (21.91 KiB) Viewed 22305 times

Hot CPU
Hot combo.jpeg
Hot combo.jpeg (21.68 KiB) Viewed 22305 times


Perl App Listing

***********************************************************************************************************************************************************
Important Note:

The following perl app has been specifically written to work with fitlet RM running Linux Mint. Without modification, it is unlikely it will work for other computers. This is because the app determines CPU temperature by processing the output of the bash sensor command, and the output format of the sensor command varies from one computer type to the next. This app has been successfully modified to work on a HP15 laptop running Linux Mint, so with proper modification it may work for your computer type as well.
***************************************************************************************************************************************************************



#!/usr/bin/perl -w
#
#fitletCPUtemp.pl

# Darryl Hassell 8/4/2018

use Tk;
use List::MoreUtils qw(first_index);
use Device::SerialPort;
use Time::HiRes ('sleep');

# Define & Initialize Variables

my $CPU_Temp = 0.0;
my $CPU_Temp_Color = '#FFFFFF';
my $Element = 0;

# Configure Serial Port
my $port = Device::SerialPort -> new("/dev/ttyACM0");
$port -> baudrate(115200);
$port -> databits(8);
$port -> parity("none");
$port -> stopbits(1);
$port -> write_settings;

# Set Sleep Delay After Writes in Seconds
our $delay = 0.1;

# Initialize fit-statUSB to Red - Green - Blue - White
$port -> write("#FF0000\n");
$port -> purge_rx;
sleep(2);

$port -> write("#00FF00\n");
$port -> purge_rx;
sleep(2);

$port -> write("#0000FF\n");
$port -> purge_rx;
sleep(2);

$port -> write("#FFFFFF\n");
$port -> purge_rx;
sleep(2);


# Create Main Window
$WinMain = MainWindow->new;
$WinMain -> resizable (0,0);

# Label Main Window
$myframe = $WinMain -> Frame ->pack();
$CPU_Temp_Label = $myframe -> Label (-text => 'fitlet CPU Temp Monitor'."\n"."$CPU_Temp".' Deg. C.', -background => "$CPU_Temp_Color", -font => "helvetica 9 bold")->pack(-ipadx => 20,-ipady => 20);

# Exit Program Button
$stopbutton = $myframe -> Button (-text => 'Exit', -command => \&Exit, -foreground => 'black', -background => 'lightblue');
$stopbutton -> pack (-side => 'bottom');

$WinMain -> repeat(2000, \&GetCPUTemp);

MainLoop();

################################ Exit Program ################################

sub Exit {

$port -> write("#000000\n");
$port -> purge_rx;
sleep($delay);
exit;
}

################################ CPU Temperature Data ################################

sub GetCPUTemp {

$CPU_Temp_Unfiltered1 = `sensors`;

# Read Unfiltered CPU Temperature Data Into Array
@CPU_Temp_Array1 = split ' ', $CPU_Temp_Unfiltered1;

# Determine Array Index of CPU Indicator k10temp-pci-00c3
my $ArrayIndex1 = first_index { $_ eq 'k10temp-pci-00c3' } @CPU_Temp_Array1;

# Determine Array Index of CPU Temperature
my $ArrayIndex2 = $ArrayIndex1 + 5;

# Read CPU Temp Out Of Array
$CPU_Temp_Unfiltered2 = $CPU_Temp_Array1[$ArrayIndex2];

# Capture Only Numbers From String
@CPU_Temp_Array2 = split '°', $CPU_Temp_Unfiltered2;

$CPU_Temp = $CPU_Temp_Array2[0];

if ($CPU_Temp <= 30){
$CPU_Temp_Color = '#0000FF';
$port -> write("#0000FF\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 30 && $CPU_Temp <= 32){
$CPU_Temp_Color = '#0079FF';
$port -> write("#0079FF\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 32 && $CPU_Temp <= 34){
$CPU_Temp_Color = '#00FFFA';
$port -> write("#00FFFA\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 34 && $CPU_Temp <= 36){
$CPU_Temp_Color = '#00FF67';
$port -> write("#00FF67\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 36 && $CPU_Temp <= 38){
$CPU_Temp_Color = '#00FF00';
$port -> write("#00FF00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 38 && $CPU_Temp <= 40){
$CPU_Temp_Color = '#37FF00';
$port -> write("#37FF00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 40 && $CPU_Temp <= 42){
$CPU_Temp_Color = '#85FF00';
$port -> write("#85FF00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 42 && $CPU_Temp <= 44){
$CPU_Temp_Color = '#BEFF00';
$port -> write("#BEFF00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 44 && $CPU_Temp <= 46){
$CPU_Temp_Color = '#FFFF00';
$port -> write("#FFFF00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 46 && $CPU_Temp <= 48){
$CPU_Temp_Color = '#FFEB00';
$port -> write("#FFEB00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 48 && $CPU_Temp <= 50){
$CPU_Temp_Color = '#FFDA00';
$port -> write("#FFDA00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 50 && $CPU_Temp <= 52){
$CPU_Temp_Color = '#FFCC00';
$port -> write("#FFCC00\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 52 && $CPU_Temp <= 54){
$CPU_Temp_Color = '#FFA500';
$port -> write("#FFA500\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 54 && $CPU_Temp <= 56){
$CPU_Temp_Color = '#FF8800';
$port -> write("#FF8800\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 56 && $CPU_Temp <= 58){
$CPU_Temp_Color = '#FF6500';
$port -> write("#FF6500\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 58 && $CPU_Temp <= 60){
$CPU_Temp_Color = '#FF3700';
$port -> write("#FF3700\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 60 && $CPU_Temp <= 62){
$CPU_Temp_Color = '#FF0000';
$port -> write("#FF0000\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 62 && $CPU_Temp <= 64){
$CPU_Temp_Color = '#FF003E';
$port -> write("#FF003E\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 64 && $CPU_Temp <= 66){
$CPU_Temp_Color = '#FF0097';
$port -> write("#FF0097\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 66 && $CPU_Temp <= 68){
$CPU_Temp_Color = '#FF00BD';
$port -> write("#FF00BD\n");
$port -> purge_rx;
sleep($delay);
}

elsif ($CPU_Temp > 68 && $CPU_Temp <= 70){
$CPU_Temp_Color = '#FF00CA';
$port -> write("#FF00CA\n");
$port -> purge_rx;
sleep($delay);
}

else {
$CPU_Temp_Color = '#FFB6F5';
$port -> write("#FFB6F5\n");
$port -> purge_rx;
sleep($delay);
}

$CPU_Temp_Label -> configure (-text => 'fitlet CPU Temp Monitor'."\n"."$CPU_Temp".' Deg. C.', -background => "$CPU_Temp_Color");

}




Conclusions

fitstat-USB is a very useful tool for displaying CPU temperature. It makes a great accessory for Compulab's fitlet computer.

Post Reply

Return to “fit-statUSB”