0

I wrote a program using Lazarus and it checks for GPIO2 pin's status every 3 seconds using TProcess component to run a terminal command for retrieving GPIO pin status as follows...

 procedure TForm1.Timer1Timer(Sender: TObject);
 begin
    ...
    Process1.CommandLine:='gpio -g read 2';
    Process1.Execute;
    Memo1.Lines.LoadFromStream(Process1.Output); 
    ...
 end;

This works great with no issue. However, when I let my program run for several hours or all day, my program starts to freeze or become unresponsive. Probably due to repeatedly calling TProcess over and over again every 3 seconds. I have looked up on the Internet about this and found many options available but a lot of them are too complicated or the libraries just won't work or outdated. So, what is the simplest and not over complicated ways of reading GPIO pin status? fpSystem seems promising but I can't find any code where you capture the return values for the terminal command.

Milliways
  • 59,890
  • 31
  • 101
  • 209
ThN
  • 1,063
  • 6
  • 22
  • 35
  • Maybe see https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware – Bodo Aug 29 '23 at 15:30

1 Answers1

1

Using a WiringPi utility to poll a pin seems inefficient but there are alternatives.

Some years ago I did a survey of libraries and the available Command line tools.

I use raspi-gpio and pigs
e.g. raspi-gpio get 2 returns GPIO 2: level=1 fsel=4 alt=0 func=SDA1
pigs r 2 returns 1

The gpiochip libgpiod interface has a couple of command line tools.
gpioget 0 2 returns 1

The rgpiod daemon should allow your pascal program to access GPIO using the socket interface.

Milliways
  • 59,890
  • 31
  • 101
  • 209