Írtam Pascalban egy GPIO interface-t:unit gpio_sysfs;
{$mode objfpc}{$H+}
interface
function open_gpio(pin: string): boolean;
function close_gpio(pin: string): boolean;
function set_gpio_dir(pin: string; d_out: boolean): boolean;
function set_gpio_val(pin: string; state: boolean): boolean;
function get_gpio_val(pin: string; out state: boolean): boolean;
implementation
uses linehnd, sysutils;
const sysfs = '/sys/class/gpio/';
function open_gpio(pin: string): boolean;
var
tc: integer;
gpio: string;
begin
if (not put_line(sysfs + 'export', pin)) then
begin
result := false;
exit;
end;
gpio := sysfs + 'gpio' + pin + '/';
tc := 0;
repeat
sleep(10);
inc(tc);
result := directoryexists(gpio);
until (result) or (tc = 100);
end;
function close_gpio(pin: string): boolean;
begin
if (not put_line(sysfs + 'unexport', pin)) then
begin
result := false;
exit;
end;
end;
function set_gpio_dir(pin: string; d_out: boolean): boolean;
var
gpio: string;
dir: string;
begin
gpio := sysfs + 'gpio' + pin + '/';
result := directoryexists(gpio);
if (not result) then
begin
exit;
end;
if (d_out) then dir := 'out' else dir := 'in';
result := put_line(gpio + 'direction', dir);
end;
function set_gpio_val(pin: string; state: boolean): boolean;
var
gpio: string;
val: string;
begin
gpio := sysfs + 'gpio' + pin + '/';
result := directoryexists(gpio);
if (not result) then
begin
exit;
end;
if (state) then val := '1' else val := '0';
result := put_line(gpio + 'value', val);
end;
function get_gpio_val(pin: string; out state: boolean): boolean;
var
gpio: string;
val: string;
begin
gpio := sysfs + 'gpio' + pin + '/';
result := directoryexists(gpio);
if (not result) then
begin
exit;
end;
result := get_line(gpio + 'value', val);
state := val <> '0';
end;
end. A linehnd unit meg itt van hozzá:unit linehnd;
{$mode objfpc}{$H+}
interface
function put_line(filename: string; line: string): boolean;
function get_line(filename: string; out line: string): boolean;
implementation
function put_line(filename: string; line: string): boolean;
var tf: textfile;
begin
result := false;
assignfile(tf, filename);
try
rewrite(tf);
except
exit;
end;
writeln(tf, line);
closefile(tf);
result := true;
end;
function get_line(filename: string; out line: string): boolean;
var tf: textfile;
begin
result := false;
assignfile(tf, filename);
try
reset(tf);
except
exit;
end;
readln(tf, line);
closefile(tf);
result := true;
end;
end. |