Skip to content

Instantly share code, notes, and snippets.

@mamemomonga
Created February 21, 2024 02:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mamemomonga/533402e00dc2cffcd49f79727e718827 to your computer and use it in GitHub Desktop.
Save mamemomonga/533402e00dc2cffcd49f79727e718827 to your computer and use it in GitHub Desktop.
Raspberry Pi5のいくつかの情報をvcgencmdから取得して見やすく表示するツール
#!/usr/bin/env perl
# Raspberry Pi5のいくつかの情報をvcgencmdから取得して見やすく表示するツール
# --------------------
# 主な使い方
# $ watch ./pi5-status.pl
# --------------------
use strict;
use warnings;
use feature 'say';
# https://elinux.org/RPI_vcgencmd_usage
# man vcgencmd
sub get_soc_temp {
my $buf=`vcgencmd measure_temp`;
my $temp=0;
if($buf=~/^temp=([0-9.]+)\'C/) { $temp=$1 }
return $temp;
}
sub get_throttled {
my $buf=`vcgencmd get_throttled`;
my $state='';
if($buf=~/^throttled=0x(.+)/) { $state=$1 }
$state=unpack('N',pack('H*',sprintf("%08s",$state)));
my @res;
foreach my $bit(0..20) {
$res[$bit]=($state >> $bit) & 1;
}
return \@res;
}
sub get_arm_clock {
my $buf=`vcgencmd measure_clock arm`;
my $freq=0;
if($buf=~/^frequency\(0\)=(\d+)/) { $freq=$1 }
my $mhz=$freq/1000/1000;
return $mhz;
}
sub show_throttled {
my $throttled=shift;
my @mean=();
$mean[0] ='Under-voltage detected';
$mean[1] ='Arm frequency capped';
$mean[2] ='Currently throttled';
$mean[3] ='Soft temperature limit active';
$mean[16]='Under-voltage has occurred';
$mean[17]='Arm frequency capping has occurred';
$mean[18]='Throttling has occurred';
$mean[19]='Soft temperature limit has occurred';
for(my $i=0;$i<20;$i++) {
my $bit=$throttled->[$i];
if($bit) {
printf("[%02d] %s\n",$i,$mean[$i]);
}
}
}
sub get_pmic {
my %pmic;
my $pmic=`vcgencmd pmic_read_adc`;
foreach my $line(split(/\n/,$pmic)) {
if($line=~/^\s*(.+)_(A|V)\s(.+)=(.+)(A|V)$/) {
my ($name,$type,$val)=($1,$2,$4);
$pmic{$name}||={};
$pmic{$name}->{$type}=$val;
}
}
return \%pmic;
}
sub show_pmic {
my $pmic=shift;
my $lf=1;
foreach my $key(sort keys %{$pmic}) {
my $av=$pmic->{$key};
if(exists($av->{A})) {
printf("%10s %4.3fV %4.3fA",$key, $av->{V}, $av->{A});
} else {
printf("%10s %4.3fV ",$key, $av->{V});
}
if($lf==3) {
printf("\n");
$lf=0;
}
$lf++;
}
printf("\n");
}
{
my $pmic=get_pmic();
my $throtted=get_throttled();
my $temp_soc=get_soc_temp();
my $clock_arm=get_arm_clock();
printf("SoC Temp: %4.2f 'C ARM Clock: %6.4f MHz\n",$temp_soc, $clock_arm);
say '';
show_pmic($pmic);
say '';
show_throttled($throtted);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment