Recent

Author Topic: [SOLVED] Operators overriding and use of PChars in code for AVRs  (Read 2774 times)

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
I have two questions for Pascal (FPC) programmers who have more experience in AVR programming than me:

1. Is it possible to override operators such as '+', '-', '/', '*' using dedicated functions? As mentioned elsewhere we (me and @Dzandaa) are working on a software implementation of floating point numbers (as type TFloat32=UInt32). In this case using functions as equivalent for operators (for example Float32Mul for multiplication) is not very comfortable, simple operators could be much easier...

2. What is your opinion about PChars (=arrays[0..n] of char) in code for AVRs? Is this feature fully supported by the compiler? In the wiki page https://wiki.freepascal.org/AVR_Programming there is no mention about PChars, but I think it was written at  one point that PChars work. If yes - what is better to use in practice: strings[n] or arrays[0..n] of char?

Do you have any thoughts or opinions on these topics? Maybe @ccrause has the answers...
« Last Edit: January 16, 2025, 09:11:25 pm by ackarwow »

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #1 on: January 16, 2025, 04:42:02 pm »
ad 1) yes, of course
ad 2) if you expect and can live with all the pitfalls of C-style "strings"  plz do so, but that is ill-adviced if you have the luxury of using Pascal strings.
Code: Pascal  [Select][+][-]
  1. program silly;
  2. {$mode objfpc}{$H+}
  3. var
  4.   s:string ='hello,'#0'world';
  5. begin
  6.   writeln(pchar(s));// silly C-style
  7.   writeln(s);       // proper pascal style
  8. end.
Also note the difference between strlen() and length().
Good luck with your curly bracket strings  8) :D ;) :)

FreePascal fully supports C-style strings, but really, you do not want to use them unless you need to interface with C-style code.
In the case of AVR I would advise to use the Pascal shortstring format. (like I do)
I guess most will agree about that, it is hardly worth a discussion.
(note that in the pool of forum users, the sub-pool of avr users is quite small and there is a very big risk that users w/o avr experience will respond with utter nonsense)

The implicit point you make is about floats: depending on the avr type that depends.
I would use a scaled integer type for hardware starved avr's, but simply floats(single or double) on more modern avr's.
Can you mention a bottom line model?


« Last Edit: January 16, 2025, 05:00:01 pm by Thaddy »
But I am sure they don't want the Trumps back...

MarkMLl

  • Hero Member
  • *****
  • Posts: 8181
Re: Operators overriding and use of PChars in code for AVRs
« Reply #2 on: January 16, 2025, 04:54:12 pm »
ad 2) if you expect and can live with all the pitfalls of C-style "strings"  plz do so, but that is ill-adviced if you have the luxury of using Pascal strings.

Although small-system programming implies being very careful with heap usage: AVR's don't usually have virtual memory to an external swap device.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #3 on: January 16, 2025, 04:54:37 pm »
ad 1) yes, of course
ad 2) if you expect and can live with all the pitfalls of C-style "strings"  plz do so, but that is ill-adviced if you have the luxury of using Pascal strings.
Code: Pascal  [Select][+][-]
  1. program silly;
  2. {$mode objfpc}{$H+}
  3. var
  4.   s:string ='hello,'#0'world';
  5. begin
  6.   writeln(pchar(s));// silly C-style
  7.   writeln(s);       // proper pascal style
  8. end.
Also note the difference between strlen() and length().
Good luck with your curly bracket strings  8) :D ;) :)

FreePascal fully supports C-style strings, but really, you do not want to use them unless you need to interface with C-style code.

@Thaddy
Thanks for your comment.
ad 1) Could you show a simple working example of operator/s overriding for AVRs?
ad 2) So far I've been using PChars in my AVR code without any problems, I'm also using ShortStrings but I'm thinking about choosing one convention. What about passing ShortStrings (=255 chars) as parameters of procedures/routines? Wouldn't it be better to use PChar? 

d.ioannidis

  • Full Member
  • ***
  • Posts: 229
    • Nephelae
Re: Operators overriding and use of PChars in code for AVRs
« Reply #4 on: January 16, 2025, 04:54:59 pm »
Hi,

<snip>
As mentioned elsewhere we (me and @Dzandaa) are working on a software implementation of floating point numbers (as type TFloat32=UInt32). In this case using functions as equivalent for operators (for example Float32Mul for multiplication) is not very comfortable, simple operators could be much easier...
<snip>

IIRC, at least one time in the past, the floating point numbers subject was discussed in the past .

https://forum.lazarus.freepascal.org/index.php/topic,48135.0.html

regards,

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #5 on: January 16, 2025, 05:01:11 pm »
I added to my comment about the use of floats.
Note that to implement floats like you propose, you simply use shifts, not a cast to integer, but shifted integers, a.k.a. fixed point floating point arithmatic.
« Last Edit: January 16, 2025, 05:05:27 pm by Thaddy »
But I am sure they don't want the Trumps back...

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #6 on: January 16, 2025, 05:07:04 pm »
Hi,

<snip>
As mentioned elsewhere we (me and @Dzandaa) are working on a software implementation of floating point numbers (as type TFloat32=UInt32). In this case using functions as equivalent for operators (for example Float32Mul for multiplication) is not very comfortable, simple operators could be much easier...
<snip>

IIRC, at least one time in the past, the floating point numbers subject was discussed in the past .

https://forum.lazarus.freepascal.org/index.php/topic,48135.0.html

regards,

@d.ioannidis
Thanks for your comment and the link. I see that work is in progress, I hope it will be successful soon. As far as I know FPC 3.3.1 for AVR does not support floating point numbers.
We are writing our own implementation, because we need the code now...

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #7 on: January 16, 2025, 05:18:45 pm »
I added to my comment about the use of floats.
Note that to implement floats like you propose, you simply use shifts, not a cast to integer, but shifted integers, a.k.a. fixed point floating point arithmatic.
Our module has a lot of functions (Float32Add, Float32Sub, Float32Mul, Float32Div, Float32Sqrt, Float32Sin, Float32Cos, Float32Deg2Rad, Float32Rad2Deg, Float32Abs, Float32Neg, Float32Tan, Float32Cotan, Float32Mod, Float32Log2, Float32Ln, Float32Log10, Float32IntPow, Float32Pow, Float32Exp). All of them are in tests on Arduino Uno (ATmega328p), and seem to work OK. Of course for slower AVRs they cannot be very useful. Yes, we are using byte shifting etc. And yes I know about fixed point arithmetic. It is also implemented in UnoLib (https://github.com/ackarwow/unolib).

d.ioannidis

  • Full Member
  • ***
  • Posts: 229
    • Nephelae
Re: Operators overriding and use of PChars in code for AVRs
« Reply #8 on: January 16, 2025, 05:19:02 pm »
Hi,

Hi,

<snip>
As mentioned elsewhere we (me and @Dzandaa) are working on a software implementation of floating point numbers (as type TFloat32=UInt32). In this case using functions as equivalent for operators (for example Float32Mul for multiplication) is not very comfortable, simple operators could be much easier...
<snip>

IIRC, at least one time in the past, the floating point numbers subject was discussed in the past .

https://forum.lazarus.freepascal.org/index.php/topic,48135.0.html

regards,

@d.ioannidis
Thanks for your comment and the link. I see that work is in progress, I hope it will be successful soon. As far as I know FPC 3.3.1 for AVR does not support floating point numbers.
We are writing our own implementation, because we need the code now...

unfortunately, the last two or so years, I'm not doing anything "serious" with FPC and AVR but I have my eye on FPC for AVR ;). So far I didn't notice any work in progress regarding floating point for that arch ...

OT, as I'm quite familiar with the USBasp, libusb and hidapi projects, if you need any help don't hesitate to ask . If I've time I'll gladly help ...

regards,

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #9 on: January 16, 2025, 05:23:04 pm »
I added to my comment about the use of floats.
Note that to implement floats like you propose, you simply use shifts, not a cast to integer, but shifted integers, a.k.a. fixed point floating point arithmatic.
@Thaddy
Thanks for your comments. Floating points are not my main question. I am mainly interested how can I use operators overriding on AVRs... Is it possible at all?
A

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #10 on: January 16, 2025, 05:33:50 pm »
(...)
unfortunately, the last two or so years, I'm not doing anything "serious" with FPC and AVR but I have my eye on FPC for AVR ;). So far I didn't notice any work in progress regarding floating point for that arch ...

OT, as I'm quite familiar with the USBasp, libusb and hidapi projects, if you need any help don't hesitate to ask . If I've time I'll gladly help ...

regards,
Thank you. I will ask.

d.ioannidis

  • Full Member
  • ***
  • Posts: 229
    • Nephelae
Re: Operators overriding and use of PChars in code for AVRs
« Reply #11 on: January 16, 2025, 05:45:06 pm »
Hi,

<snip>
... I am mainly interested how can I use operators overriding on AVRs... Is it possible at all?
A

I never tried operator overloading in AVR but if you tried and didn't work, perhaps the reason is that as the AVR RTL is configured with minimum functions for almost all the supported AVRs except the AVR6 family, this functionality is left out. AFAIR, in the <fpc source>/rtl/embedded/system.cfg is the RTL configuration, try to enable the rest and see if it works ...

EDIT: From my FPC 3.2.3 rtl/embedded/system.cfg :
Quote
# does not require extra memory, neither code nor data
# in programs not using e. g. writeln based I/O which is the common case for AVR
#ifdef CPUAVR
-SfOBJECTS
-SfEXCEPTIONS
-SfCLASSES
-SfRTTI
# AVR6 has normally more memory, so enable more functions
#ifdef CPUAVR6
-SfANSISTRINGS
-SfWIDESTRINGS
-SfDYNARRAYS
-SfTHREADING
-SfVARIANTS
-SfOBJECTS
-SfCOMMANDARGS
-SfRANDOM
-SfRESOURCES
#endif
#endif
 
regards,
« Last Edit: January 16, 2025, 05:47:05 pm by d.ioannidis »

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #12 on: January 16, 2025, 05:54:56 pm »
I am mainly interested how can I use operators overriding on AVRs... Is it possible at all?
Of course. It is part of the Freepascal dialect of Pascal.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. operator / (const a,b:string):string;
  3. begin
  4.   result := 'Hello, world';
  5. end;
  6. var
  7.   a:string ='n' ;
  8.   b:string = 'm';
  9. begin
  10.   writeln (a/b);
  11. end.
This is absolutely irrespective of any cpu or mcu.
« Last Edit: January 16, 2025, 05:59:05 pm by Thaddy »
But I am sure they don't want the Trumps back...

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #13 on: January 16, 2025, 06:12:49 pm »
I am mainly interested how can I use operators overriding on AVRs... Is it possible at all?
Of course. It is part of the Freepascal dialect of Pascal.
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. operator / (const a,b:string):string;
  3. begin
  4.   result := 'Hello, world';
  5. end;
  6. var
  7.   a:string ='n' ;
  8.   b:string = 'm';
  9. begin
  10.   writeln (a/b);
  11. end.
This is absolutely irrespective of any cpu or mcu.

Thanks, your code for operator is compiling, but there is unable to check it via write/writeln on AVRs  :)
I tried following code:

Code: Pascal  [Select][+][-]
  1. uses
  2.   float32;
  3.  
  4. {$mode objfpc}
  5. operator * (const f1, f2: TFloat32): TFloat32; <-- HERE
  6. begin
  7.   Result:=Float32Mul(f1, f2);
  8. end;
  9.  

and FPC throws an error:

Code: Text  [Select][+][-]
  1. Target OS: Embedded
  2. Compiling C:\Programs\avr\avrpascal\examples\Operators.pas
  3. Operators.pas(12,46) Error: Impossible operator overload
  4. Operators.pas(17,11) Fatal: Syntax error, "identifier" expected but "OPERATOR" found
  5. Fatal: Compilation aborted
  6.  

Thaddy

  • Hero Member
  • *****
  • Posts: 16520
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #14 on: January 16, 2025, 06:15:08 pm »
I give up. This is agnostic to processors..
But I am sure they don't want the Trumps back...

 

TinyPortal © 2005-2018