Verificar status de carga da bateria do notebook

Publicado por Sandro Marcell (última atualização em 07/03/2014)

[ Hits: 14.469 ]

Homepage: http://sanmarcell.wordpress.com

Download battery_status

Download 1381087011.battery_status (versão 2)

Download 1394198886.battery_status (versão 3)




De férias do trampo e sem ter muito o que fazer por enquanto :), criei esse simples Shell Script que mostra o status da carga da bateria do notebook, em forma de barra, porcentagem e tempo estimado restante para o fim total de carga da mesma. Não é lá essas coisas, mas funciona! Sugestões e melhorias serão bem-vindas. ;)

  



Versões atualizadas deste script

Versão 2 - Enviado por Sandro Marcell em 06/10/2013

Changelog: Remoção do POG! ;)

Download 1381087011.battery_status


Versão 3 - Enviado por Sandro Marcell em 07/03/2014

Changelog: Remoção da POG e algumas melhorias sutis... =)

Download 1394198886.battery_status


Esconder código-fonte

#!/bin/bash
#
#  Copyright 2013 Sandro Marcell <smarcell@myopera.com>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
#  MA 02110-1301, USA.
#
PATH='/bin:/usr/bin:/usr/local/bin'

bateria='BAT0'

# Codigo UTF-8 para 'seta para cima' e 'seta para baixo'
seta_cima='\u2191'
seta_baixo='\u2193'

exec 3< /sys/class/power_supply/${bateria}/energy_now; read -u3 capacidade_restante; exec 3<&-
exec 3< /sys/class/power_supply/${bateria}/energy_full; read -u3 capacidade_total; exec 3<&-
exec 3< /sys/class/power_supply/${bateria}/status; read -u3 estado_bateria; exec 3<&-
exec 3< /sys/class/power_supply/${bateria}/power_now; read -u3 taxa_bateria; exec 3<&-

((carga = capacidade_restante * 100 / capacidade_total))

# Tempo restante estimado de carga
((taxa_bateria == 0)) && taxa_bateria=1 # POG!!! :P 100% de carga
horas=$(bc <<< "(1.0 * $capacidade_restante) / (1.0 * $taxa_bateria)")
minutos=$(bc <<< "(60.0 * $capacidade_restante) / $taxa_bateria - ($horas * 60)")

case "$estado_bateria" in
   'Full')
      status_bateria="{FONTE}33[1m=={FONTE}33[0m"
      info='Bateria recarregada';;
   'Charging')
      status_bateria="{FONTE}33[1m${seta_cima}${seta_cima}{FONTE}33[0m"
      info='Bateria carregando...';;
   'Discharging')
      status_bateria="{FONTE}33[1m${seta_baixo}${seta_baixo}{FONTE}33[0m"
      info="Tempo restante estimado = ${horas}h:${minutos}m"
esac

cor=32 # Codigo ANSI para cor verde
if ((carga < 15)); then
   cor=31 # Vermelho
elif ((carga <= 50)); then
   cor=33 # Amarelo
fi

# Mostra a barra
while ((i++ < carga)); do
   (($i % 2 == 0)) && echo -ne "{FONTE}33[7;${cor}m {FONTE}33[0m"
done

echo -e " {FONTE}33[1;${cor}m${carga}%{FONTE}33[0m\n$status_bateria $info"

Scripts recomendados

Atualizando o sistema no Debian

Prova 1.0

pc-qemu.sh - Script configurador para QEMU

Pegando seu IP e Hostname com um unico comando.

Shell Script para instalação do software FTOOL


  

Comentários
[1] Comentário enviado por rrafael em 07/10/2013 - 15:23h

Show.. funciona 100%

[2] Comentário enviado por smallboy em 07/10/2013 - 15:40h

Para executar preciso dar permissão chmod +x nome_arquivo ? no meu não fungo, abriu executou rapidamente e fechou.

[3] Comentário enviado por rrafael em 07/10/2013 - 16:11h

No meu tive que mudar no script

touch bateria.sh
chmof 744 bateria.sh
vi bateria.sh
alterei a linha;
bateria='BAT0'
para
bateria='BAT1'

savei;

para ver se tem mais algum erro use;
sh -x bateria.sh

Abraço.

[4] Comentário enviado por /bin/laden em 13/03/2014 - 09:56h

Legal! =)


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts