luni, 21 septembrie 2015

Lazarus / FPC: sort a StringGrid

Here it is, a nice simple procedure that sorts a StringGrid. Tested an a 5000 records StringGrid, the sorting is done in about 1-2 seconds
   sg: your stringgrid
   col: column that will be sorted
   numeric: if the values in column are numbers
   asc: ascendent or descendent
   numeric: if datas are numbers

procedure sort(sg:tstringgrid;col:integer;asc:boolean=true;numeric:boolean);
var i,c:integer; numeric:boolean; f:string; r:real; negat:boolean; x1:integer; semn,s1,s2:string;
begin
  numeric:=false;
  negat:=false;


  if asc=true then sg.SortOrder:=soascending else sg.SortOrder:=sodescending;

  if numeric=false then
     sg.SortColRow(true, col, sg.FixedRows, sg.RowCount-1)
  else begin
     f:='000000000000000.00';
     c:=sg.colcount;
     sg.ColCount:=c+1;
     for i:=1 to sg.RowCount-1 do
       begin
          try r:=strtofloat(sg.Cells[col,i]) except r:=0 end;
          if r<0 then
            begin
              negat:=true;
              sg.Cells[c,i]:='-'+formatfloat(f,r);
            end else sg.Cells[c,i]:='+'+formatfloat(f,r);
       end;
     sg.SortColRow(true, c, sg.FixedRows, sg.RowCount-1);
     if negat=true then
       begin
          x1:=0;
          semn:=sg.Cells[c,0][1];
          s1:=semn; s2:=' ';
          for i:=0 to sg.RowCount-1 do
            if sg.Cells[c,0][1]<>semn then
              begin
                semn:=sg.Cells[c,0][1];
                s2:=semn;
                x1:=i;
              end;
          if s1='-' then
            begin
               if sg.SortOrder=soascending then sg.SortOrder:=sodescending else sg.SortOrder:=soascending;
               sg.SortColRow(true, c, sg.FixedRows, x1-1);
            end;
          if s2='-' then
            begin
               if sg.SortOrder=soascending then sg.SortOrder:=sodescending else sg.SortOrder:=soascending;
               sg.SortColRow(true, c, x1, sg.RowCount-1);
            end;
       end;
     sg.ColCount:=c;
  end;
end;

duminică, 20 septembrie 2015

OpenSuSE 13.2 / Quickview plasmoid

After switching to OpenSuSE 13.2 I found that a certain plasmoid is no longer available - Quickview. Finally, I found the sources and I managed to install it on my system. I also uploaded it here:


Download the archive somewhere where you can use it (Documents, for example) and unpack it (right click > Extract here). You will find a "readme " file with full instructions of how to build and install the plasmoid. Be careful about the required extra packages (kdelibs, libxonq, - devel packages and gettext package) and don't forget to restart the system. The file also contain proper credit for those who created this plasmoid - I have no contribution

luni, 6 iulie 2015

Lazarus / FPC - run a command and read it's output, working with system tray; laptop's battery on OpenSuSE 12.x - 13.1

     For unknown reasons, KDE on OpenSuSE 12.x and 13.1 (so far) does not recognize correctly the battery of my Acer laptop. It says “Battery not present”, wich is not true. After some unsuccessful attempts to fix this, I gave up and decided to write my own program. 

     In order for my program to work, it must:
1) Run a LINUX command line to obtain the battery statistics and load the output
2) Analyse the output an identify the values that are needed
3) Create a simple interface to display the results
4) Minimise the main form to system tray (and then maximise if needed)
5) Change the system tray icon to display the level of the battery for quick view and artistic impression :-)

You need:
- a form
- a memo
- 2 buttons
- a ImageList from Common Controls
- a timer
- a TrayIcon from Additional

Doubleclick on ImageList to load the images you like for different battery states (I created my own, size 16x32)

The full program can be downloaded from here

If you are not interested in the source code, you need only the file BatteryF

Here is the full code

unit Unit1;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

  TForm1 = class(TForm)
    ImageList1: TImageList;
    Memo1: TMemo;
    Panel1: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Timer1: TTimer;
    TrayIcon1: TTrayIcon;
    procedure FormShow(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure TrayIcon1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  Bmp: tbitmap;
  Bat: integer;

function get1:tstringlist;

implementation

{$R *.lfm}

{ TForm1 }

function get1:tstringlist;
var l:tstringlist; Proc1: tprocess; t1,t2:string; i,j,k:integer;
begin
   bmp :=tbitmap.Create;
   form1.Memo1.Lines.Clear;

   /// run a command line and read the output
   /// first we must determine if a battery is present or detected

   l:=tstringlist.Create;
   proc1:=tprocess.Create(form1);

   proc1.Executable:='upower';
   proc1.Parameters.Add('-e');

   proc1.Options := proc1.Options + [poWaitOnExit, poUsePipes];
   proc1.Execute;
   l.LoadFromStream(proc1.Output);    /// load the output to stringlist
   t1:='';
   for i:=0 to l.Count-1 do
   begin
     t2:=l[i];
     if AnsiContainsText(t2,'BAT') then t1:=t2;  /// BAT is for battery
   end;

/// if a battery is detected
  if t1<>'' then
   begin
     form1.trayicon1.Hint:='';
     proc1.Parameters.Clear;
     proc1.Parameters.Add('-i');  /// for more details see upower documentation
     proc1.Parameters.Add(t1);
     proc1.Execute;
     l.LoadFromStream(proc1.Output);

     for i:=0 to l.Count-1 do
     begin
       t2:=l[i];
       if AnsiContainsText(t2,'percentage') then      /// find the battery level
        begin
          j:=length(t2);
          bat:=strtoint(midstr(t2,j-3,3));
          form1.Caption:='Battery level: '+inttostr(bat)+'%';
          form1.trayicon1.Hint:=form1.trayicon1.Hint+t2+chr(13);
        end;
       if AnsiContainsText(t2,'time to empty') then
            form1.trayicon1.Hint:=form1.trayicon1.Hint+t2+chr(13);
     end;
/// "form caption" and "trayicon hint" are for artistic impression :-)
/// if upower displays the results in a different language (I don't know if it can),
    // the program will find nothing when searching for "percentage" and "time to empty"
    // so you must modify the keyword acording to your language
   k:=0;
   if bat>20 then k:=1; /// select the image number from timagelist for system tray
   if bat>35 then k:=2;
   if bat>50 then k:=3;
   if bat>70 then k:=4;
   if bat>90 then k:=5;
   if bat=100 then k:=7;

   form1.imagelist1.GetBitmap(k,bmp);   /// get the image from timagelist
   form1.trayicon1.Icon.assign(bmp);    /// assign the image to system tray

   end

  else
      begin
        l.Add('Battery not detected or not present');
        form1.imagelist1.GetBitmap(6,bmp);
        form1.trayicon1.Icon.assign(bmp);
      end;

   proc1.Free;
   bmp.Free;
   result:=l;

end;

procedure TForm1.FormShow(Sender: TObject);
begin
    memo1.Text:=get1.Text;   /// display the full statistics
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  form1.close;    /// terminate the program
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  form1.Hide;     /// minimise to system tray
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
   memo1.Text:=get1.Text;   /// refresh the status every 60 seconds
end;

procedure TForm1.TrayIcon1Click(Sender: TObject);
begin
  form1.Show;      /// show the main form when click on tray icon
end;

end.
 
 

vineri, 17 aprilie 2015

Raspberry Pi - A (very) simple GUI for OMXPlayer

I have created a simple GUI for OMXPlayer on Raspberry Pi

It can be downloaded from here:

https://www.dropbox.com/sh/d5bw7h3j33vmu5t/AAARZeUlm4pvHHJwnqUEn4Jqa?dl=0

The only needed file is omxGUI, but I also uploaded the sources (written, of course, in Pascal)

vineri, 3 aprilie 2015

Indy 10.6 / Lazarus 1.2 / OpenSuSE 13.1

Here are the steps that I followed in order to install Indy 10.6 package on Lazarus. I don't know if all of those steps are required, but it just worked that way.


1) Download the Indy package from here:
        http://indy.fulgan.com/ZIP/
Unzip it somewhere where you can use it.

2) In order for the new Indy not to interfere with the old one, completly remove Lazarus (uninstall, delete the old directory - c:\lazarus on Windows or .lazarus on Linux home)

3) Install Lazarus

4) On Linux the package has a bug (I think): it can't correctly locate the subfolders so it will say that some files are missing. To prevent that just go to the \Lib directory and copy here the content of the following subdirectories: Lib\Core, Lib\Systems, Lib\Protocols

5) Compile the package Lib\indylaz.lpk TWICE (don't ask me why)

6) Install the package

7) Add to your projects the path to \Lib directory (Project >Project Options > Paths > Other unit files

8) Enjoy!

duminică, 16 noiembrie 2014

Lazarus / FPC - some of my work

Here are some programs that I am working with. I hope someone will find some of them useful

https://www.dropbox.com/sh/d5bw7h3j33vmu5t/AAARZeUlm4pvHHJwnqUEn4Jqa?dl=0

Raspberry Pi - Fixing some USB issues

    The USB ports appear to be somehow unstable on Raspberry Pi. A complete explanation and a solution that worked for me can be found here:

   http://ludovicrousseau.blogspot.ro/2014/04/usb-issues-with-raspberry-pi.html

In short, this is what I did:

1. Start a file manager with root privileges
        sudo thunar        (thunar is my file manager)

2. Edit the file /boot/cmdline.txt and add dwg_otg.speed=1  Your file should look like this:
   dwc_otg.lpm_enable=0 console=ttyAMA0,115200 kgdboc=ttyAMA0,115200
   dwc_otg.speed=1 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4
   elevator=deadline rootwait

3. Save and reboot

PS: I have a ASIC USB Miner for bitcoins that I'm running on Raspberry Pi just for fun (it didn't produced anything so far and I'm not expecting it to do so). The solution above appears to fix the stability of the system (it was freezing every 5-10 hours, now it's working non-stop for about 2 days)