Recent

Author Topic: FPC on Rasp Pi, non Lazarus, use of GPIO.  (Read 8047 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #45 on: February 20, 2021, 10:33:47 pm »
Revisiting the empty Tform ..... (see attached screen grab).

Please zip or tarball the entire project, since without- in particular- the .lfm it's meaningless.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #46 on: February 20, 2021, 10:56:18 pm »
I remember a very experienced and highly respected programmer say "Wow.  This record construct could be really useful!"
( I don't recall her ever doing anything other than assembler before that - HP-2100 ...)

I've seen "systems programmers"- basically glorified operators- boggled by assembler. Until an older colleague pulled a reference card out of her drawer.

Quote
A few years ago, I requested here, that FPC be modified to allow undeclared counter/index vars in procedures and functions as in Ada.  People beat the crap out of me - you may have been one of them!  Still seems like a good idea to me!

Probably not me. (a) I've only been using the forum for a year, before that I used the mailing lists. (b) While I don't like undeclared variables, I believe that declaring the index variable inside a for..do would be an advance since the current situation where the index value is undefined on loop termination (unless by break) is most unpleasant.

Quote
I did write some Modula-2 (Borland) back OUAT ... I seem to recall it was a university class for an easy A.  (I never went to class so I think I hit a B- on that...).  In those (DOS) days it was pretty tedious IIRC as one had to edit the unit interface file separately from the implementation.  So main, units (interface), units (implementation).  That was a loooong time ago though.  I might have preferred it over Pascal if editing wasn't so tedious.  Then TP 4 came out with Units and - wow! What an improvement.

Borland Modula-2 became TopSpeed. There were various "goings on" that I can't discuss because if I get the details wrong they'd be libellous.

Having separate definition files in M2 has the advantage that (a) they can be marked as read-only to all but senior staff and (b) they can be written as a "statement of fact" with no indication of implementation details... there's a legal storm brewing because C (etc.) specifically doesn't have that separation.

Quote
I believe, had Wirth simply extended Pascal with concepts from Modula-2, that Ada could have begun there...

Remember that Wirth was on one of the Ada teams or on the approvals committee, and eyebrows were raised since he was the only one being paid.

There was a great deal of dodginess that led to Pascal, but basically I believe that Wirth rushed it out with the specific intention of breaking compatibility with ALGOL-60. It was completely unforgivable that Pascal had the "dangling else" problem when that was fixed in ALGOL-68, Wirth obviously fixed that in Modula-2 but Pascal was a rush job... look at Wirth's early coding style and you'd see why.

Quote
I do seem to recall buying stuff from Logitech and getting 'bonus packs' of s/w of all sorts...

Although the various addon packs sounds more like a TopSpeed thing, and they were of decidedly mixed quality. Logitech had a Mouse Programmer's Toolkit which effectively gave you a large chunk of a Windows-like OS as source, and they had a damn good debugger which was subsequently spun off as Multiscope.

They also had their own share of scandals which I can't discuss. For a while I was their de-facto UK tech support.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 192
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #47 on: February 20, 2021, 11:17:04 pm »
Quote
While I don't like undeclared variables, I believe that declaring the index variable inside a for..do would be an advance since the current situation where the index value is undefined on loop termination (unless by break) is most unpleasant.

I seem to recall that everywhere (from class to code manuals) it was pretty clearly pointed out that the value of the index variable at the end of a for loop was undefined - so don't assume anything.  Further of course, if it compiles on one system with a behaviour it may do something else on another... in particular if the compiler writer is doing an efficient job by using registers for the index...

Quote
Please zip or tarball the entire project, since without- in particular- the .lfm it's meaningless.
I'll pack everything in a .zip asap - need to separate the project from all sorts of other files first.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #48 on: February 20, 2021, 11:31:34 pm »
Quote
Please zip or tarball the entire project, since without- in particular- the .lfm it's meaningless.
I'll pack everything in a .zip asap - need to separate the project from all sorts of other files first.

Background: the main unit is the .lpr file. At the bottom of that you can see where the resources in the .lfm file(s) are parsed to create components of various types on the form(s). It's likely that the project you're using has lost a button or something comparable.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 192
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #49 on: February 20, 2021, 11:44:00 pm »
Quote
Please zip or tarball the entire project, since without- in particular- the .lfm it's meaningless.

attached

Or find at: https://www.dropbox.com/s/ymtk3389e1jtr4g/unit1.pas.tar
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #50 on: February 21, 2021, 11:38:22 am »
Rule 1: don't believe everything you read on the Internet, particularly if somebody says "this is a working example" :-)

I think your example was derived from https://wiki.freepascal.org/Raspberry_Pi_:_BerryClip but it had been copied around by various people and in the process had lost the contents of its form. That not only meant that there was nothing to display any output but- crucially- it meant that noting was generating events and passing them to the event handlers in your program.

I've fixed up your example to the extent that you can single-step through it and see what's going on, I've not attempted to debug it against my own PC-based GPIO since I don't want to change your code more than necessary.

What I have done is add several calls to Application.ProcessMessages (colloquially referred to as APM) to make sure that output to the memo on the form appears as early as possible.

I've resisted the temptation of starting other methodical fixups since once one starts...

Use Lazarus to open the .lpi, compile it and set breakpoints near the start of TForm1.FormActivate() and TForm1.ApplicationProperties1Idle(), then step through line-by line paying particular attention to the result of opening or accessing your /sys/class/gpio pseudo-files.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 192
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #51 on: February 21, 2021, 01:51:51 pm »
The code I posted is that from https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi - the first two code blocks beginning at:
https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Switching_a_device_via_the_GPIO_port

The rest in the tarball was generated by the compiler.

Thanks for the patch.  As soon as the coffee kicks in I'll get to it!
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #52 on: February 21, 2021, 01:55:55 pm »
The code I posted is that from https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi - the first two code blocks beginning at:
https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Switching_a_device_via_the_GPIO_port

Yes, and that didn't include a form so was worthless. Wiki author's fault, not yours.

See the link I gave you which is rather more comprehensive.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 192
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #53 on: February 21, 2021, 02:05:01 pm »
@Mark,

I believe you mentioned Devon earlier - England?  Is that where you are now?

Closest I got to Devon was Yeovil...
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #54 on: February 21, 2021, 02:21:05 pm »
@Mark,

I believe you mentioned Devon earlier - England?  Is that where you are now?

Closest I got to Devon was Yeovil...

Currently in Sussex which is the other side of the country. I mentioned Devon because at one time there was an importer of compilers etc. in a small town there, purveyors of quality development tools and libraries to just about everybody basically but the business wilted (at least in part because it supplied tools from all suppliers impartially, and they started looking for exclusive distributors).

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 192
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #55 on: February 21, 2021, 02:29:48 pm »
OK - running.  Need to make some wiring changes to test that.

WARNING: Rant follows.

But, this illustrates exactly what I meant earlier about not wrapping a hardware demo in all the trappings of GUI.

Discovery 1.  For this to work, the main program had be .lpi, not .pas.  (How would I have known that from the Wiki page).

2. I have no idea how the form got generated.  From the source code?  From the form file?

3. What happened to the main program?

Ah well,  I'll plodge through it ...

Thanks.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #56 on: February 21, 2021, 02:50:02 pm »
OK - running.  Need to make some wiring changes to test that.

WARNING: Rant follows.

But, this illustrates exactly what I meant earlier about not wrapping a hardware demo in all the trappings of GUI.

If you don't want the GUI stuff don't use it. As I've already said, there are lots of advantages to using Lazarus for the debugger etc.

Quote
Discovery 1.  For this to work, the main program had be .lpi, not .pas.  (How would I have known that from the Wiki page).

No, the main program is the .lpr. The .lpi is the project information file that tells the development environment what units, forms etc. are in the overall scope of the project.

Quote
2. I have no idea how the form got generated.  From the source code?  From the form file?

I answered that a couple of days before you asked.

Quote
3. What happened to the main program?

Ah well,  I'll plodge through it ...

Thanks.

Stop complaining and fix the bugs.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 192
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #57 on: February 21, 2021, 03:28:27 pm »
Well.  It works.

And it's slow.

And it misses button presses.

I'll start stripping it down...

Thanks.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2696
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #58 on: February 21, 2021, 03:42:53 pm »
Well.  It works.

And it's slow.

And it misses button presses.

I'll start stripping it down...

My first suspect would be that all the processing is hung off an application idle event. I've not used it but I think that that's supposed to be the lowest priority possible, try putting a timer with a 10mSec repetition on it, or possibly an IdleTimer.

I'm currently trying to look at the file alteration monitor stuff to see if that's operational inside /sys/class/gpio but I'm not very optimistic. The new (kernel >=4.8) API appears to have its own equivalent.

Slightly later: I'm pretty sure the FAM stuff doesn't work in that context, so basically pins/bits have to be polled. I note that the driver for the CH341 chip talks about its being possible to put an interrupt on one pin at a time, but I haven't a clue how that translates to the user-accessible API.

I've not investigated the speed, but I'd be disappointed if it couldn't handle of the order of 100 events a second... https://codeandlife.com/2012/07/03/benchmarking-raspberry-pi-gpio-speed/ suggests somewhat better.

However as I've said before Linux isn't an RTOS, and this is reporting level rather than edge, so unless you're using an interlocked/acknowledged protocol (i.e. a bit like ye olde Centronics interface) you can expect dropped inputs and erratically-timed outputs on occasion.

I'll try to put a bit of time into the new GPIO API over the next few days.

MarkMLl
« Last Edit: February 21, 2021, 04:01:23 pm by MarkMLl »
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 192
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #59 on: February 21, 2021, 04:11:31 pm »
Is there a description of that API anywhere?  I've searched and come up dry.

Ah, you slipped in an edit.  Well, two can play at that!

I look forward to your API!

It occurs to me that a souped up Arduino could be my "sampler" (easy to program timer interrupts on that), compress the data and ship it to the Pi over serial (of some sort).  I'll look into that possibility too.  Not sure it could manage 1 Khz though.  I did have a main loop on the Arduino running 1300-1400 Hz (16 MHz clock) with all sorts of things going on.  Write tight and good things happen... I could also modify the Arduino to get 20 MHz ... though that's a little more complicated...
« Last Edit: February 21, 2021, 04:27:31 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

 

TinyPortal © 2005-2018