unit basicunit;
{$mode delphi} {$H+}
interface
uses  Classes, SysUtils;
Procedure ModulesInit;
Procedure ModulesDone;
procedure BASICmain;

Const DrawTimeInterval=5;
Const DrawStartDelay=10;

implementation
uses Forms, math, graphics,
 Unix,dl,
  base,base2,textfrm,textfile,arrays,affine,graphsys,
  math2sub,datalist,baslib,graphlib,debugdg,paintfrm,
  MyThread,assignlib, mathlib0;
{$MAXFPUREGISTERS 0}




function gpiod_chip_open_lookup(s_S:PChar):NativeInt; external 'libgpiod.so' name 'gpiod_chip_open_lookup';
function gpiod_chip_get_line(_N:NativeInt ; _T:NativeInt ):NativeInt; external 'libgpiod.so' name 'gpiod_chip_get_line';
procedure gpiod_line_request_output(_N:NativeInt ; s_S:PChar; _T:NativeInt ) external 'libgpiod.so' name 'gpiod_line_request_output';
procedure gpiod_line_request_input(_N:NativeInt ; s_S:PChar) external 'libgpiod.so' name 'gpiod_line_request_input';
procedure gpiod_line_set_value(_GPIO:NativeInt ; _X:NativeInt ) external 'libgpiod.so' name 'gpiod_line_set_value';
function gpiod_line_get_value(_GPIO:NativeInt ):NativeInt; external 'libgpiod.so' name 'gpiod_line_get_value';
procedure gpiod_chip_close(_GCHIP:NativeInt ) external 'libgpiod.so' name 'gpiod_chip_close';

(* Modules initialize and dispose *)
Procedure ModulesInit;
begin
end;
Procedure ModulesDone;
begin
end;

(* Main Program *)
procedure BASICmain;
var _GCHIP:double=0;
var _GPIO4:double=0;
var _GPIO5:double=0;
var _I:double=0;
var __own1_00000:double=0;
var __own2_00001:double=0;
var arg_n00001:double;
var arg_s00001:Ansistring;
function _0GPIOD_CHIP_OPEN_LOOKUP(s_S:Ansistring):double; overload; forward;
function _0GPIOD_CHIP_GET_LINE(_N:double; _T:double):double; overload; forward;
procedure _0GPIOD_LINE_REQUEST_OUTPUT(var _N:double; var s_S:Ansistring; var _T:double); overload; forward;
procedure _0GPIOD_LINE_REQUEST_INPUT(var _N:double; var s_S:Ansistring); overload; forward;
procedure _0GPIOD_LINE_SET_VALUE(var _GPIO:double; var _X:double); overload; forward;
function _0GPIOD_LINE_GET_VALUE(_GPIO:double):double; overload; forward;
procedure _0PPIOD_CHIP_CLOSE(var _GCHIP:double); overload; forward;
function _0GPIOD_CHIP_OPEN_LOOKUP(s_S:Ansistring):double; overload;
begin
{$MAXFPUREGISTERS 0}
result:=0;
try
//  140    ASSIGN "libgpiod.so","gpiod_chip_open_lookup"
result:=gpiod_chip_open_lookup(PChar(s_S));
//  150 END FUNCTION

except
on E:EMathError do begin
  RecoverFloatException; 
  raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EDivByZero do begin
  RecoverFloatException; 
   raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EExType do begin
   E.extype:=PropagatedExtype(E.Extype); raise end;
end;
end;
function _0GPIOD_CHIP_GET_LINE(_N:double; _T:double):double; overload;
begin
{$MAXFPUREGISTERS 0}
result:=0;
try
//  170    ASSIGN "libgpiod.so","gpiod_chip_get_line"
result:=gpiod_chip_get_line(trunc(_N), trunc(_T));
//  180 END FUNCTION

except
on E:EMathError do begin
  RecoverFloatException; 
  raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EDivByZero do begin
  RecoverFloatException; 
   raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EExType do begin
   E.extype:=PropagatedExtype(E.Extype); raise end;
end;
end;
procedure _0GPIOD_LINE_REQUEST_OUTPUT(var _N:double; var s_S:Ansistring; var _T:double); overload;
begin
{$MAXFPUREGISTERS 0}
try
//  200    ASSIGN "libgpiod.so","gpiod_line_request_output"
gpiod_line_request_output(trunc(_N), PChar(s_S), trunc(_T));
//  210 END SUB

except
on E:EMathError do begin
  RecoverFloatException; 
  raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EDivByZero do begin
  RecoverFloatException; 
   raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EExType do begin
   E.extype:=PropagatedExtype(E.Extype); raise end;
end;
end;
procedure _0GPIOD_LINE_REQUEST_INPUT(var _N:double; var s_S:Ansistring); overload;
begin
{$MAXFPUREGISTERS 0}
try
//  230    ASSIGN "libgpiod.so","gpiod_line_request_input"
gpiod_line_request_input(trunc(_N), PChar(s_S));
//  240 END SUB

except
on E:EMathError do begin
  RecoverFloatException; 
  raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EDivByZero do begin
  RecoverFloatException; 
   raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EExType do begin
   E.extype:=PropagatedExtype(E.Extype); raise end;
end;
end;
procedure _0GPIOD_LINE_SET_VALUE(var _GPIO:double; var _X:double); overload;
begin
{$MAXFPUREGISTERS 0}
try
//  260    ASSIGN "libgpiod.so","gpiod_line_set_value"
gpiod_line_set_value(trunc(_GPIO), trunc(_X));
//  270 END SUB

except
on E:EMathError do begin
  RecoverFloatException; 
  raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EDivByZero do begin
  RecoverFloatException; 
   raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EExType do begin
   E.extype:=PropagatedExtype(E.Extype); raise end;
end;
end;
function _0GPIOD_LINE_GET_VALUE(_GPIO:double):double; overload;
begin
{$MAXFPUREGISTERS 0}
result:=0;
try
//  290    ASSIGN "libgpiod.so","gpiod_line_get_value"
result:=gpiod_line_get_value(trunc(_GPIO));
//  300 END FUNCTION

except
on E:EMathError do begin
  RecoverFloatException; 
  raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EDivByZero do begin
  RecoverFloatException; 
   raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EExType do begin
   E.extype:=PropagatedExtype(E.Extype); raise end;
end;
end;
procedure _0PPIOD_CHIP_CLOSE(var _GCHIP:double); overload;
begin
{$MAXFPUREGISTERS 0}
try
//  320    ASSIGN "libgpiod.so","gpiod_chip_close"
gpiod_chip_close(trunc(_GCHIP));
//  330 END SUB

except
on E:EMathError do begin
  RecoverFloatException; 
  raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EDivByZero do begin
  RecoverFloatException; 
   raise EExtype.create(PropagatedExtype( EXTYPE(E,ExCode))) end;
on E:EExType do begin
   E.extype:=PropagatedExtype(E.Extype); raise end;
end;
end;
begin
//  100 ! sudo apt install libgpiod2 libgpiod-dev libgpiod-doc

//  110 ! /usr/share/doc/libgpiod-dev/html

//  120 OPTION ARITHMETIC NATIVE

//  130 FUNCTION GPIOD_CHIP_OPEN_LOOKUP(s$)

//  160 FUNCTION gpiod_chip_get_line(n,t)

//  190 SUB gpiod_line_request_output(n,s$,t)

//  220 SUB gpiod_line_request_input(n,s$)

//  250 SUB gpiod_line_set_value(gpio, x)

//  280 FUNCTION gpiod_line_get_value(gpio)

//  310 SUB  ppiod_chip_close(gchip)

//  340 LET GChip=GPIOD_CHIP_OPEN_LOOKUP(STR$(0))
_GCHIP := _0GPIOD_CHIP_OPEN_LOOKUP(STR_s(0));
//  350 ! PRINT GChip

//  360 LET gpio4 = gpiod_chip_get_line(GChip, 4)
_GPIO4 := _0GPIOD_CHIP_GET_LINE(_GCHIP,4);
//  370 ! PRINT gpio4

//  380 LET gpio5 = gpiod_chip_get_line(GChip, 5)
_GPIO5 := _0GPIOD_CHIP_GET_LINE(_GCHIP,5);
//  390 ! PRINT gpio5

//  400 CALL gpiod_line_request_output(gpio4, "LED", 0)
_0GPIOD_LINE_REQUEST_OUTPUT(_GPIO4,StringVariable(arg_s00001,'LED')^,NumericVariable(arg_n00001,0)^);
//  410 CALL gpiod_line_request_input(gpio5,"Switch")
_0GPIOD_LINE_REQUEST_INPUT(_GPIO5,StringVariable(arg_s00001,'Switch')^);
//  420 FOR i=1 TO 20
__own1_00000 := 20 ;
_I := 1 ;
while (_I <= __own1_00000) do begin
//  430    CALL gpiod_line_set_value(gpio4, MOD(i,2))
_0GPIOD_LINE_SET_VALUE(_GPIO4,NumericVariable(arg_n00001,BMOD(_I , 2))^);
//  440    WAIT DELAY 0.25
wait(0.25);
//  450    PRINT gpiod_line_get_value(gpio5)
console.PRINT([],rsNone, false ,[_0GPIOD_LINE_GET_VALUE(_GPIO5), TNewLine.create ]);
//  460    WAIT DELAY 0.25
wait(0.25);
//  470 NEXT i
_I := _I + 1 ;
end;
//  480 CALL ppiod_chip_close(GChip)
_0PPIOD_CHIP_CLOSE(_GCHIP);
//  490 END

end;{end main}
initialization
  AppName:='/home/shiraishi/ParactBASIC2.1x/ASSIGN_GPIOD_1';
  SystemStackSize := 16711680;
  signiwidth0:=15;
  TextMode := true ;
  GraphMode:= false ;
  BMPSize:=BMP401;
  InitialBeamMode:= false ;
  UseCharInput:= false ;
  FontCharSet:=1;
  FontSize:=0;
  FontName:='default';
  FontStyle:=[];
  FirstThreadPriority:=tpNormal;



Finalization



end.
