Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

A simple battery monitor code snippet.

Off-Topic discussions about science, technology, and non Debian specific topics.
Post Reply
Message
Author
User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

A simple battery monitor code snippet.

#1 Post by edbarx »

Since I know there is at least one Lazarus user frequenting these forums, I am posting the code for a simple Lazarus Pascal battery monitor.

The program when compiled displays the percentage charge and a button which shows a dialog with more details.

The Form's Code:

Code: Select all

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  StdCtrls, Process;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    Panel1: TPanel;
    Timer1: TTimer;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Label1DblClick(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
    data: string;
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i: integer;
  s1, s2, tmpS: string;
  List: TStringList;
begin
  List := TStringList.Create;
  Try
    RunCommand('/usr/bin/upower', ['-e'], s1);
    List.Text := s1;

    for i := 0 to List.Count - 1 do
    begin
      if pos('BAT', List.Strings[i]) = 0
        then continue;

      tmpS := List.Strings[i];
      break;
    end;

    RunCommand('/usr/bin/upower', ['-i', tmpS], s2);

    List.Text := s2;
    data := s2;
    for i := 0 to List.Count - 1 do
    begin
      if pos('percentage', List.Strings[i]) = 0
        then continue;

      tmpS := List.Strings[i];
      break;
    end;

    s1 := '';
    for i := length(tmpS) downto 1 do
      if (tmpS[i] = ':') or (tmpS[i] = ' ')
        then break
      else s1 := tmpS[i] + s1;

    Label1.caption := ' Battery: ' + s1 + ' ';

    if s1 <> ''
    then
      begin
        i := pos(': ', s1);
        if i > 0
        then
          begin
            i := i + 2;
            s1 := copy(s1, i, length(s1));
          end;
      end;

    Caption := s1;
  finally
    List.Free;
  end;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Timer1Timer(nil);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  MessageDlg(data, mtInformation, [mbOK], 0);
end;

procedure TForm1.Label1DblClick(Sender: TObject);
begin
  MessageDlg(data, mtInformation, [mbOK], 0);
end;

end.
The Form itself .lfm:

Code: Select all

object Form1: TForm1
  Left = 295
  Height = 260
  Top = 222
  Width = 887
  AutoSize = True
  BorderStyle = bsDialog
  BorderWidth = 15
  Caption = 'Form1'
  ClientHeight = 260
  ClientWidth = 887
  OnShow = FormShow
  LCLVersion = '1.6.2.0'
  object Panel1: TPanel
    Left = 15
    Height = 230
    Top = 15
    Width = 857
    Align = alClient
    BevelInner = bvLowered
    BevelOuter = bvLowered
    ClientHeight = 230
    ClientWidth = 857
    TabOrder = 0
    object Label1: TLabel
      Left = 2
      Height = 226
      Top = 2
      Width = 679
      Align = alClient
      Caption = 'Label1'
      Font.Height = -53
      Font.Name = 'Monospace'
      ParentColor = False
      ParentFont = False
      OnDblClick = Label1DblClick
    end
    object Button1: TButton
      Left = 681
      Height = 226
      Top = 2
      Width = 174
      Align = alRight
      Caption = 'Details...'
      OnClick = Button1Click
      TabOrder = 0
    end
  end
  object Timer1: TTimer
    Interval = 10000
    OnTimer = Timer1Timer
    left = 31
    top = 117
  end
end
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

n_hologram
Posts: 459
Joined: 2013-06-16 00:10

Re: A simple battery monitor code snippet.

#2 Post by n_hologram »

I always read your posts as if they're narrated by a majestic lion, like Aslan. Also, nice code. I've been meaning to Lazarus.
bester69 wrote:There is nothing to install in linux, from time to time i go to google searching for something fresh to install in linux, but, there is nothing
the crunkbong project: scripts, operating system, the list goes on...

User avatar
roseway
Posts: 1528
Joined: 2007-12-31 22:50
Location: Kent, UK
Has thanked: 3 times
Been thanked: 4 times

Re: A simple battery monitor code snippet.

#3 Post by roseway »

I think I'm probably the other Lazarus user you referred to, and I'll give your code a try when I can, but at the moment I don't use a laptop.
Eric

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: A simple battery monitor code snippet.

#4 Post by GarryRicketson »

Image

Thanks for sharing the code snippet, I no longer have Lazarus on my PC, because I
installed Open Bsd, but it is also not a laptop, no battery. How ever it still interests
me because I do have the "FPC" (FreePascal) compiler on OpenBsd, and on a laptop.

User avatar
edbarx
Posts: 5401
Joined: 2007-07-18 06:19
Location: 35° 50 N, 14 º 35 E
Been thanked: 2 times

Re: A simple battery monitor code snippet.

#5 Post by edbarx »

n_hologram wrote:I always read your posts as if they're narrated by a majestic lion, like Aslan.
I don't understand your post, specifically, whether it was intended to insult, but viewing your posts to other threads, does not indicate you are a person that insults others for no reason.

Regarding the lion, that is meant to represent Debian, not me. That is why I wrote "Debian is Power". Otherwise, I would have written, edbarx is power which would have been a complete lie.
n_hologram wrote:Also, nice code. I've been meaning to Lazarus.
The code was quickly written. In fact, it needs more editing to isolate some repetitive parts in separate subroutines to be more flexible to use. I cannot be flattered with such code, that would be complete narcissism. People who can be deservedly flattered are those who made breakthroughs... I didn't do any, and will definitely not do any breakthroughs in my age.

Reality does not offend me anymore. Only those who unwittingly push their head in the sand are offended when their reality is uncovered to them.
Debian == { > 30, 000 packages }; Debian != systemd
The worst infection of all, is a false sense of security!
It is hard to get away from CLI tools.

n_hologram
Posts: 459
Joined: 2013-06-16 00:10

Re: A simple battery monitor code snippet.

#6 Post by n_hologram »

Definitely wasn't meant to be an insult. I thought avatars influenced the way most people read posts. ie, the user (can't remember who) who has the photo of futurama's professor..https://cdn.meme.am/instances/500x/58202331.jpg

As for the snippet, I just wanted to offer positive support, because you periodically post practical yet simple projects. I can't imagine that a battery monitor would ever be unhelpful.
bester69 wrote:There is nothing to install in linux, from time to time i go to google searching for something fresh to install in linux, but, there is nothing
the crunkbong project: scripts, operating system, the list goes on...

Post Reply