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

Const DrawTimeInterval=5;
Const DrawStartDelay=10;
Const VirtualStackSize=65536;
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}




procedure gpiod_ctxless_set_value(s_DEVICE:PChar; _OFFSET:NativeInt ; _VALUE:NativeInt ; _ACTIVE_LOW:NativeInt ; s_CONSUMER:PChar; _CB:NativeInt ; s_DATA:PChar)cdecl;  external 'libgpiod.so' name 'gpiod_ctxless_set_value';
function gpiod_ctxless_event_monitor(s_DEVICE:PChar; _EVENT_TYPE:NativeInt ; _OFFSET:NativeInt ; _ACTIVE_LOW:NativeInt ; s_CONSUMER:PChar; s_TIMEOUT:PChar; _POLL_CB:NativeInt ; _EVENT_CB:NativeInt ; s_DATA:PChar):NativeInt;cdecl;  external 'libgpiod.so' name 'gpiod_ctxless_event_monitor';
{$static on}
type t_GPIO=object
Public
 procedure init;
 procedure done;
function _CB(_EVTYPE:double; _OFFSET:double; s_TS:Ansistring; s_DATA:Ansistring):double; 
procedure _GPIOD_CTXLESS_SET_VALUE(var s_DEVICE:Ansistring; var _OFFSET:double; var _VALUE:double; var _ACTIVE_LOW:double; var s_CONSUMER:Ansistring; var _CB:double; var s_DATA:Ansistring); 
function _GPIOD_CTXLESS_EVENT_MONITOR(s_DEVICE:Ansistring; _EVENT_TYPE:double; _OFFSET:double; _ACTIVE_LOW:double; s_CONSUMER:Ansistring; s_TIMEOUT:Ansistring; _POLL_CB:double; _EVENT_CB:double; s_DATA:Ansistring):double; 
private
__COUNT:double;
 ModuleChannelList:TChannelList;
end;
var _GPIO: t_GPIO;
function t_GPIO._CB(_EVTYPE:double; _OFFSET:double; s_TS:Ansistring; s_DATA:Ansistring):double; 
var arg_n00001:double;
var arg_n00002:double;
var arg_n00003:double;
var arg_n00004:double;
var arg_s00001:Ansistring;
var arg_s00002:Ansistring;
var arg_s00003:Ansistring;
begin
{$MAXFPUREGISTERS 0}
result:=0;
try
//  1190    LET cb=0
result := 0;
//  1200    PRINT evtype
console.PRINT([],rsNone, false ,[_EVTYPE, TNewLine.create ]);
//  1210    IF evtype=2 THEN
if _EVTYPE=2 then
 begin
//  1220       LET count=count+1
__COUNT := __COUNT+1;
//  1230       CALL gpiod_ctxless_set_value("gpiochip4", 4, MOD(count,2), 0, "LED", 0, "")
_GPIOD_CTXLESS_SET_VALUE(StringVariable(arg_s00001,'gpiochip4')^,NumericVariable(arg_n00001,4)^,NumericVariable(arg_n00002,BMOD(__COUNT , 2))^,NumericVariable(arg_n00003,0)^,StringVariable(arg_s00002,'LED')^,NumericVariable(arg_n00004,0)^,StringVariable(arg_s00003,'')^);
//  1240       IF count=10 THEN LET cb=1
if __COUNT=10 then
 begin
result := 1;
 end
 else
 begin
 end;
//  1250    ELSEIF evtype=3 then
 end
 else
 begin
if _EVTYPE=3 then
 begin
//  1260       WAIT DELAY 0.5
wait(0.5);
//  1270    END IF
 end
 else
 begin
 end;
 end;
//  1280 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 CallBack4(a0:NativeInt; a1:NativeInt; a2:PChar; a3:PChar):NativeInt;
begin
Result:=Trunc(_GPIO._CB(a0, a1, a2, a3));
end;
procedure t_GPIO._GPIOD_CTXLESS_SET_VALUE(var s_DEVICE:Ansistring; var _OFFSET:double; var _VALUE:double; var _ACTIVE_LOW:double; var s_CONSUMER:Ansistring; var _CB:double; var s_DATA:Ansistring); 
begin
{$MAXFPUREGISTERS 0}
try
//  1310    ASSIGN "libgpiod.so","gpiod_ctxless_set_value"
gpiod_ctxless_set_value(PChar(s_DEVICE), trunc(_OFFSET), trunc(_VALUE), trunc(_ACTIVE_LOW), PChar(s_CONSUMER), trunc(_CB), PChar(s_DATA));
//  1320 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 t_GPIO._GPIOD_CTXLESS_EVENT_MONITOR(s_DEVICE:Ansistring; _EVENT_TYPE:double; _OFFSET:double; _ACTIVE_LOW:double; s_CONSUMER:Ansistring; s_TIMEOUT:Ansistring; _POLL_CB:double; _EVENT_CB:double; s_DATA:Ansistring):double; 
begin
{$MAXFPUREGISTERS 0}
result:=0;
try
//  1460    ASSIGN "libgpiod.so","gpiod_ctxless_event_monitor"
result:=gpiod_ctxless_event_monitor(PChar(s_DEVICE), trunc(_EVENT_TYPE), trunc(_OFFSET), trunc(_ACTIVE_LOW), PChar(s_CONSUMER), PChar(s_TIMEOUT), trunc(_POLL_CB), trunc(_EVENT_CB), PChar(s_DATA));
//  1470 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 t_GPIO.init;
begin
ModuleChannelList:=TChannelList.create;
try
//  1010 MODULE OPTION ARITHMETIC NATIVE

//  1020 PUBLIC FUNCTION gpiod_ctxless_event_monitor

//  1030 SHARE SUB gpiod_ctxless_set_value

//  1050 SHARE NUMERIC count

//  1060 LET count=0
__COUNT := 0;
//  1170 !

//  1290 !

//  1480 END MODULE

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  t_GPIO.done;
begin
ModuleChannelList.free;
end;

(* Modules initialize and dispose *)
Procedure ModulesInit;
begin
_GPIO.init;
end;
Procedure ModulesDone;
begin
_GPIO.done;
end;

(* Main Program *)
procedure BASICmain;
var s_TIMEOUT:Ansistring;
var _RET:double=0;
begin
//  100 OPTION ARITHMETIC NATIVE

//  110 DECLARE EXTERNAL FUNCTION GPIO.gpiod_ctxless_event_monitor

//  120 LET timeout$=DWORD$(2)&DWORD$(0)&DWORD$(0)&DWORD$(0) ! Time out 2秒
LETS([TStrvar.create(@s_TIMEOUT)],DWord_s(2)+DWord_s(0)+DWord_s(0)+DWord_s(0));
//  130 LET ret=GPIO.gpiod_ctxless_event_monitor("gpiochip4", 3, 5, 1, "callback test", timeout$, 0, callbackadr(4), "")
_RET := _GPIO._GPIOD_CTXLESS_EVENT_MONITOR('gpiochip4',3,5,1,'callback test',s_TIMEOUT,0,NativeInt(@CallBack4),'');
//  140 PRINT ret
console.PRINT([],rsNone, false ,[_RET, TNewLine.create ]);
//  150 END

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



Finalization



end.
