Recent

Author Topic: Lazarus / RaspberyPi Hardware Library  (Read 90649 times)

jwdietrich

  • Hero Member
  • *****
  • Posts: 1232
    • formatio reticularis
Re: Lazarus / RaspberyPi Hardware Library
« Reply #30 on: April 28, 2013, 11:22:37 pm »
Just keep in mind that GPIO ( working with pins) on the rasp pi is a restricted operation and often needs to be run with root privileges that usually means the lazarus IDE or the o file needs to run from root.

That is correct. For automated operation it may be advisable to write a shell script that calls the executable via sudo.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.2.6 | FPC 3.2.2 | PPC, Intel, ARM | macOS, Windows, Linux

rmudryk

  • Newbie
  • Posts: 6
Re: Lazarus / RaspberyPi Hardware Library
« Reply #31 on: May 18, 2013, 06:15:59 pm »
This works but you do not have to expose the entire OS to Root access, as well as run Lazarus as root.  But because these are dynamic devices you have to run a script in rc.local or something as root to give yourself permissions to write on every reboot or runlevel change.

Example command line:
sudo echo 4 > /sys/class/gpio/export
sudo echo out > /sys/class/gpio4/direction
sudo chmod 666 > /sys/class/gpio4/value

now because the permissions are rw rw rw  world can read/write to gpio4

you can add this kinda stuff to the rc.local in the /etc  without the sudo, as that runs as root on boot up, so your System is GPIO ready on boot for normal users to use.


i2c is a bit different, I found these instructions in a word document for settting up i2c on the raspberry pi, but I use this all the time instead of running lazarus as "Root".  I am sorry but I can't remember the link to these instructions, but thanks!  it was the key to cleaning up this debacle of having to be root for me.  I also hear there is a more formal program to do all this in unix as well. 

Once the reboot has finished you need to make the ports active by running this command:

   $sudo modprobe i2c-dev

 Now you should be able to see the i2c ports listed in your /dev/ directory

   $ls /dev/i2c*

You should see /dev/i2c-0 and /dev/i2c-1 listed. To be able to use these ports you need to give yourself permission to access them.

   $sudo chmod o+rw /dev/i2c*

Now your i2c ports will be ready for you to use. You will have to run the modprobe and chmod commands every time you login to activate the ports and apply the permissions. If you wish to automate this process so the commands are run every time you log in then you can edit /etc/rc.local and add the commands in.



Just keep in mind that GPIO ( working with pins) on the rasp pi is a restricted operation and often needs to be run with root privileges that usually means the lazarus IDE or the o file needs to run from root. Other things like I2C are less restricted

bartdereu

  • New Member
  • *
  • Posts: 17
Re: Lazarus / RaspberyPi Hardware Library
« Reply #32 on: August 10, 2013, 07:05:57 pm »
When i try the WiringPi example in Lazarus i get :

Code: [Select]
..
/usr/bin/ld:error : raspberry uses VFP register arguments, .wiringPi/wiringPi.o does not
/usr/bin/ld: failed to merge target specific data of file ./wiringPi/wiringPi.o
..

Is there someone who can help me trough getting the PiFace board working with Lazarus ?
I'm getting pretty fast grey hair the last few days!  :P


I think i tried every single piece of pascal code to make the PiFace respond to me, but nothing
has been working so far. The Piface-Emulator does work fine.
« Last Edit: August 10, 2013, 07:10:57 pm by bartdereu »

rmudryk

  • Newbie
  • Posts: 6
Re: Lazarus / RaspberyPi Hardware Library
« Reply #33 on: August 10, 2013, 09:05:10 pm »
Are you running as root or a user?   Always try as root first, and make sure the devices are already created.  I have not played with lazarus lately, but I have felt your pain before.

When i try the WiringPi example in Lazarus i get :

Code: [Select]
..
/usr/bin/ld:error : raspberry uses VFP register arguments, .wiringPi/wiringPi.o does not
/usr/bin/ld: failed to merge target specific data of file ./wiringPi/wiringPi.o
..

Is there someone who can help me trough getting the PiFace board working with Lazarus ?
I'm getting pretty fast grey hair the last few days!  :P


I think i tried every single piece of pascal code to make the PiFace respond to me, but nothing
has been working so far. The Piface-Emulator does work fine.

bartdereu

  • New Member
  • *
  • Posts: 17
Re: Lazarus / RaspberyPi Hardware Library
« Reply #34 on: August 10, 2013, 10:27:10 pm »
Yes i am running it as a root. Man, i thought the raspberry pi was a neat thing but the piface
is going to make me crazy !

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus / RaspberyPi Hardware Library
« Reply #35 on: August 11, 2013, 12:53:34 am »
And have you tried to google for "uses VFP register arguments"? The first link explains it all:
http://stackoverflow.com/questions/9753749/arm-compilation-error-vpf-registered-used-by-executable-not-object-file
Your compiled code and that lib do not match. You are trying to link code compiled with soft-float ABI with another hard-float ABI code. Search the net if you don't know what is that and why is that important with ARM. That can be fixed, but it would be easier for you to try another way to access hardware as others have already suggested. The link is still the same: http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware. It seams that rpi_hal directly supports piface as mentioned here: http://forum.lazarus.freepascal.org/index.php/topic,20991.msg126708.html#msg126708.

I would really suggest you to learn more about ARM Linux, architecture and compiling for ARM, and also about RPi from it's site and wiki and about similar RPi projects, and search this forum and wikis for anything about RPi. You can also find valuable info on PilotLogic forum. Otherwise you will run into many walls like this one.
« Last Edit: August 11, 2013, 01:05:43 am by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

picstart

  • Full Member
  • ***
  • Posts: 236
Re: Lazarus / RaspberyPi Hardware Library
« Reply #36 on: August 11, 2013, 03:36:06 am »
Let's hope most won't want to use the Raspbery pi as a slave on I2C.... just as well because the pi folks didn't bring a trace out to the GPIO to support the slave clock. A pi must always be the I2C master which can be limiting if the I2C bus already has a working master.

rmudryk

  • Newbie
  • Posts: 6
Re: Lazarus / RaspberyPi Hardware Library
« Reply #37 on: August 11, 2013, 04:20:24 pm »
They make I2C slave to SPI master chips that would solve that problem with hardware.  Or a write a software I2C slave.

Let's hope most won't want to use the Raspbery pi as a slave on I2C.... just as well because the pi folks didn't bring a trace out to the GPIO to support the slave clock. A pi must always be the I2C master which can be limiting if the I2C bus already has a working master.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus / RaspberyPi Hardware Library
« Reply #38 on: August 11, 2013, 05:12:56 pm »
Let's hope most won't want to use the Raspbery pi as a slave on I2C.... just as well because the pi folks didn't bring a trace out to the GPIO to support the slave clock. A pi must always be the I2C master which can be limiting if the I2C bus already has a working master.
A need to use RPi as a I2C slave will be very rare. People will mostly want to interface I2C slaves as a master and that's where RPi fits very well. Even if you have such a specific need when you design your own project you are aware of this problem and there are many ways to design around it.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

rmudryk

  • Newbie
  • Posts: 6
Re: Lazarus / RaspberyPi Hardware Library
« Reply #39 on: August 11, 2013, 06:51:00 pm »
Being such a complete IDE, I would really hope Lazarus would be added into the main Distribution of Raspberry PI at some point.  It is probably one of the most, if not the most complete and intuitive IDEs available, and far superior than what comes with the default Debian Squeeze distro now.  There are newer and better ways to allow users access to a virtual device in /dev that does not require having to do permissions every boot, I just forgot where. 

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: Lazarus / RaspberyPi Hardware Library
« Reply #40 on: August 12, 2013, 08:19:09 am »
Being such a complete IDE, I would really hope Lazarus would be added into the main Distribution of Raspberry PI at some point.
It will hardly happen, because foundation owners and Pi creators like Python. Even Pi's name came as an association to Python. For something simpler they chose Scratch, and for something low level they chose C.

What I would really like to see is that Debian finally moves from that old 0.9.30 to something newer. That would help a lot.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

bartdereu

  • New Member
  • *
  • Posts: 17
Re: Lazarus / RaspberyPi Hardware Library
« Reply #41 on: August 12, 2013, 03:49:34 pm »
Ofcourse i've tried different approaches to get the PiFace running, and i almost found a very good one.

You must have (ofcourse) GPIO installed correctly, then you can just execute gpio commands with the fpsystem(' ')
function. Don't forget to add 'Unix' in the uses clause of the unit.
 
Code: [Select]
Procedure TForm1.Checkbox1Change(Sender:TObject)
begin
 if Checkbox1.Checked then
  gReturncode:=fpsystem('gpio -p write 200 1')
else
  gReturncode:=fpsystem('gpio -p write 200 0');
end;

A little explanation : With the gpio installed the piface input output ports are situated on ports 200-207.
the fact that things like this are NOT explained in the PiFace manual , makes it very difficult to work with
boards like this.
This works ok, but i'm now struggling to read the state of a button (button 1 = 200)

Code: [Select]
Procedure TForm1.Timer1Timer(Sender:TObject)
begin
  gReturncode:=fpsystem('gpio -p read 200')
  if gReturncode=0 then checkbox2.checked:=true;
  else if gReturncode=1 then checkbox2.checked:=false;
end;

The command 'gpio -p read 200' in the terminal gives me a '1' when the button is not
pressed and a '0' when it does (probably bc it goes low)
So far i have been unable to get the result with this Lazarus code. The gReturncode
remains 0 all the time. Maybe pfsystem doesn't give me back a result ? is there another
way to execute the gpio line and get the result back ?

bartdereu

  • New Member
  • *
  • Posts: 17
Re: Lazarus / RaspberyPi Hardware Library
« Reply #42 on: August 12, 2013, 05:32:23 pm »
And the final solution :  ;D

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Unix,BaseUnix;

type

  { TForm1 }

  TForm1 = class(TForm)
    ApplicationProperties1: TApplicationProperties;
    Button1: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Edit1: TEdit;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
    procedure CheckBox1Change(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  gReturncode,a : longint;
  return: integer;

implementation

{$R *.lfm}

{ TForm1 }

// Set the output high for output 1:

procedure TForm1.CheckBox1Change(Sender: TObject);
begin
  if Checkbox1.Checked then
        gReturncode:=fpsystem('gpio -p write 200 1')
  else
    gReturncode:=fpsystem('gpio -p write 200 0');

  end;

// Read the state of button 1 on the PiFace :

procedure TForm1.ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
var FileDesc:Integer;
  buttonStatus:String[1] ='1';
begin
 try
 return:=fpsystem('gpio -p read 200 > inputs.dat');
 fileDesc:=fpopen('inputs.dat',O_RdOnly);
 if FileDesc > 0 Then
       begin
         gReturncode:=fpread(fileDesc,buttonStatus[1],1);
         if buttonStatus ='0' then
               checkbox2.Checked:=true
               else
                 checkbox2.Checked:=false;
       end;
  finally
  gReturncode :=fpclose(fileDesc);
end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
 fpsystem('gpio -p mode 200 up');
end;

end.

Hope this helps someone some day :-)
« Last Edit: August 12, 2013, 05:33:59 pm by bartdereu »

bartdereu

  • New Member
  • *
  • Posts: 17
Re: Lazarus / RaspberyPi Hardware Library
« Reply #43 on: August 14, 2013, 10:34:07 pm »
Apparently this approach has a nasty side -effect . It read/writes to the SD card several times a second,
and an SD card is not well suited for this kind of things. After on million writes the best is just gone. That
would be within a few days.

Now i've been trying to use a TProcess to get the output of the 'gpio -p read 200' directly into my progam,
but i'm running in more troubles then i'd like. Acces violations at the NumBytesAvailable :

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var bufstr:string;
  buflen:integer;
begin
AProcess:= TProcess.Create(nil);
AStringlist := Tstringlist.create;
AProcess.commandline := 'echo 17';
Aprocess.Execute;

try
Aprocess.Execute;
BufStr := '';
repeat
if aProcess.Output.NumBytesAvailable > 0 then
begin
BufLen := Length(BufStr);
SetLength(BufStr, BufLen + aProcess.Output.NumBytesAvailable);
aProcess.Output.Read(BufStr[BufLen+1],aProcess.Output.NumBytesAvailable);
end;
until not aProcess.Running;
finally
aProcess.Free;
edit1.text:=BufStr;

end;
end;

Did anyone succeed to get a TProcess get an output in Lazarus ?


lazaursrotton

  • Newbie
  • Posts: 3
Re: Lazarus / RaspberyPi Hardware Library
« Reply #44 on: March 14, 2015, 02:13:28 am »

it will not  require a recompile
 linklib to installed shared lib

translated more of the lib from

 2.23 wiringpi
 

 

TinyPortal © 2005-2018