PINCTRL and Freepascal

PINCTRL is a simple Raspberry PI OS CLI command to let the GPIO pins act as output or input, set them high or low, or read the status of an input.
No need for SUDO, simple syntax.
Type
$ PINCTRL -h for an overview

For simple operations with the GPIOs an easy (but not superfast) I have written two procedures that call the shell with a PINCTRL command.
No extensive error checking, just a fork of a shell with a command.

procedure rpiExcecuteLocalProcessFast(const cmdToExcecute:String)
executes the command in cmdToExecute.

procedure rpiExcecuteLocalProcess(const cmdToExcecute:String; var result : shortstring);
executes the command in cmdToExecute and returns the first line of any output in result.

Here a program, kept as simple as possible that expects a LED on GPIO 4 and a button on GPIO 24 (press to ground)

The program runs without sudo!

The procedures can also be included in a Lazarus program. Also no SUDO required.


program gpioPINCTRL ;

uses

  Classes, SysUtils,   Process, Pipes,
   BaseUnix ;

var
  result : shortstring ;

procedure rpiExcecuteLocalProcess(const cmdToExcecute:String; var result : shortstring);
var
  lProcess: TProcess;
  sl : tstringlist;
begin
  {$IFDEF LINUX}
    lProcess := TProcess.Create(nil);
    lProcess.Executable := '/bin/sh';
    lProcess.Parameters.Add('-c');
    lProcess.Parameters.add(cmdToExcecute);
    lProcess.Options := lProcess.Options + [poUsePipes];
    lProcess.Execute;
    sl:=TStringList.create;
    sl.LoadFromStream(lProcess.Output); 
	if sl.count > 0
	  then 
	    result := sl[0] 
      else
        result := '' ;	  
	lProcess.Free;
	sl.free ;
  {$ENDIF}
end ;

procedure rpiExcecuteLocalProcessFast(const cmdToExcecute:String);
var
  lProcess: TProcess;
begin
  {$IFDEF LINUX}
    lProcess := TProcess.Create(nil);
    lProcess.Executable := '/bin/sh';
    lProcess.Parameters.Add('-c');
    lProcess.Parameters.add(cmdToExcecute);
    lProcess.Options := lProcess.Options + [poUsePipes];
    lProcess.Execute;
	lProcess.Free;
  {$ENDIF}
end ;

begin
  rpiExcecuteLocalProcessFast('pinctrl 24 ip pu') ;
  rpiExcecuteLocalProcessFast('pinctrl 4 op dh') ;
  readln ;
  rpiExcecuteLocalProcessFast('pinctrl 4 op dl', result) ;
  rpiExcecuteLocalProcess('pinctrl lev 24', result) ;
  if result = '1'
    then 
      writeln('GPIO 24 is high')
    else
      writeln('GPIO 24 is low') ;

end.