Recent

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

ccrause

  • Hero Member
  • *****
  • Posts: 996
Re: Operators overriding and use of PChars in code for AVRs
« Reply #15 on: January 16, 2025, 06:23:25 pm »
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?
As Thaddy and other mentioned, you can use either style, but shortstrings is more convenient to work with. I would avoid ansistrings unless you also have other dynamic data allocation requirements since you have to reserve a block of SRAM for the memory manager to work with.

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #16 on: January 16, 2025, 06:25:49 pm »
I give up. This is agnostic to processors..

I have problem with compile my code for operator * for TFloat32 even on Windows. Maybe I did some mistake?
TFloat32=UInt32.

d.ioannidis

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

<snip>

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.  

What type is the TFloat32 ? If it's a record then it should work .

If it's a class, object I don't think that its supported . AFAIK, Only the enumerator operator supports classes .

EDIT: I wrote my reply while you posted :

A long shot but maybe this works ?

Code: Pascal  [Select][+][-]
  1. operator * (const f1, f2: UInt32): UInt32;

regards,
« Last Edit: January 16, 2025, 06:33:36 pm by d.ioannidis »

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #18 on: January 16, 2025, 06:31:31 pm »
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?
As Thaddy and other mentioned, you can use either style, but shortstrings is more convenient to work with. I would avoid ansistrings unless you also have other dynamic data allocation requirements since you have to reserve a block of SRAM for the memory manager to work with.
@ccrause
Thank you for comment. What about passing parameters of string type (=shortstring=255 bytes?) into procedures/functions. Isn't it better to use PChar (of any size defined earlier)?

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #19 on: January 16, 2025, 06:32:52 pm »
(...)
What type is the TFloat32 ? If it's a record then it should work .

If it's a class, object I don't think that its supported . AFAIK, Only the enumerator operator supports classes .

regards,

TFloat32=UInt32;

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #20 on: January 16, 2025, 06:44:59 pm »
(...)
Code: Pascal  [Select][+][-]
  1. operator * (const f1, f2: UInt32): UInt32;

Unfortunately, this doesn't work either. I am testing on simple unit added to project... Maybe in wrong way...  %)

d.ioannidis

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

I give up. This is agnostic to processors..

I have problem with compile my code for operator * for TFloat32 even on Windows. Maybe I did some mistake?
TFloat32=UInt32.

it doesn't work for me either . On Windows for x86 and for AVR4 ...

Maybe a bug ?

regards,

Thaddy

  • Hero Member
  • *****
  • Posts: 16516
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #22 on: January 16, 2025, 07:00:28 pm »
The bug is laziness in the programmer.I ff'ing showed you.
« Last Edit: January 16, 2025, 07:07:14 pm by Thaddy »
But I am sure they don't want the Trumps back...

ccrause

  • Hero Member
  • *****
  • Posts: 996
Re: Operators overriding and use of PChars in code for AVRs
« Reply #23 on: January 16, 2025, 07:08:05 pm »
Lets start with a simple example:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. type
  4.   TComplex = record
  5.     r, i: int8;
  6.   end;
  7.  
  8. operator + (const a, b: TComplex): TComplex;
  9. begin
  10.   Result.r := a.r + b.r;
  11.   Result.i := a.i + b.i;
  12. end;
  13.  
  14. var
  15.   x1: TComplex = (r: 1; i: 2);
  16.   x2: TComplex = (r: 1; i: -4);
  17.   y: TComplex;
  18.  
  19. begin
  20.   y := x1 + x2;
  21.   writeln('y.r = ', y.r, ' (expected: 2)');
  22.   writeln('y.i = ', y.i, ' (expected: -2)');
  23. end.

Compile (for dramatic effect I compile fro the command line):
Code: Text  [Select][+][-]
  1. $ ~/fpc/installs/lib/fpc/3.3.1/ppcrossavr -Tembedded -Wpavrsim -Mobjfpc -viwn test.pp
  2. Target OS: Embedded
  3. Compiling test.pp
  4. Assembling test
  5. Linking test
  6. 24 lines compiled, 0.2 sec, 5324 bytes code, 8593 bytes data
Now test generated bin in fp-avrsim (no need to wear out flash to do this trivial test):
Code: Text  [Select][+][-]
  1. $ ~/LazProjs/fp-avrsim-cc/avrsim test.bin
  2. y.r = 2 (expected: 2)
  3. y.i = -2 (expected: -2)

d.ioannidis

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

The bug is laziness in the programmer.I ff'ing showed you.

Oh I agree .... Did you tried now ?

Lets start with a simple example:
<snip>

I'm sorry but I never used operator overloading . Can you show a multiplication example with simple types ?

regards,

ackarwow

  • Full Member
  • ***
  • Posts: 131
    • Andrzej Karwowski's Homepage
Re: Operators overriding and use of PChars in code for AVRs
« Reply #25 on: January 16, 2025, 07:23:19 pm »
Lets start with a simple example:
Code: Pascal  [Select][+][-]
  1. program test;
  2.  
  3. type
  4.   TComplex = record
  5.     r, i: int8;
  6.   end;
  7.  
  8. operator + (const a, b: TComplex): TComplex;
  9. begin
  10.   Result.r := a.r + b.r;
  11.   Result.i := a.i + b.i;
  12. end;
  13.  
  14. var
  15.   x1: TComplex = (r: 1; i: 2);
  16.   x2: TComplex = (r: 1; i: -4);
  17.   y: TComplex;
  18.  
  19. begin
  20.   y := x1 + x2;
  21.   writeln('y.r = ', y.r, ' (expected: 2)');
  22.   writeln('y.i = ', y.i, ' (expected: -2)');
  23. end.

Compile (for dramatic effect I compile fro the command line):
Code: Text  [Select][+][-]
  1. $ ~/fpc/installs/lib/fpc/3.3.1/ppcrossavr -Tembedded -Wpavrsim -Mobjfpc -viwn test.pp
  2. Target OS: Embedded
  3. Compiling test.pp
  4. Assembling test
  5. Linking test
  6. 24 lines compiled, 0.2 sec, 5324 bytes code, 8593 bytes data
Now test generated bin in fp-avrsim (no need to wear out flash to do this trivial test):
Code: Text  [Select][+][-]
  1. $ ~/LazProjs/fp-avrsim-cc/avrsim test.bin
  2. y.r = 2 (expected: 2)
  3. y.i = -2 (expected: -2)

@ccrause
Yes, thank you. This code is compiling without problems.
Let's try with "more complex" one :)

Code: Pascal  [Select][+][-]
  1. program Operators;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.   TFloat32=UInt32;
  7.  
  8. operator * (const f1, f2: TFloat32): TFloat32;
  9. begin
  10.   Result:=f1*f2; //only for test
  11. end;
  12.  
  13. begin
  14.   //nothing to do
  15. end.
  16.  

Code: Text  [Select][+][-]
  1. Target OS: Embedded
  2. Compiling C:\Programs\avr\avrpascal\examples\Operators.pas
  3. Operators.pas(8,46) Error: Impossible operator overload
  4. Operators.pas(17) Fatal: There were 1 errors compiling module, stopping
  5. Fatal: Compilation aborted
  6.  

Where is my mistake?  %)



TRon

  • Hero Member
  • *****
  • Posts: 3925
Re: Operators overriding and use of PChars in code for AVRs
« Reply #26 on: January 16, 2025, 07:26:02 pm »
Where is my mistake?  %)
https://www.freepascal.org/docs-html/ref/refse102.html#x222-24600015.1
Quote
Free Pascal supports operator overloading. This means that it is possible to define the action of some operators on self-defined types, and thus allow the use of these types in mathematical expressions.
I do not have to remember anything anymore thanks to total-recall.

Thaddy

  • Hero Member
  • *****
  • Posts: 16516
  • Kallstadt seems a good place to evict Trump to.
Re: Operators overriding and use of PChars in code for AVRs
« Reply #27 on: January 16, 2025, 07:30:40 pm »
Tnx, Ron, saves me time to respond.
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 #28 on: January 16, 2025, 07:34:39 pm »
Where is my mistake?  %)
https://www.freepascal.org/docs-html/ref/refse102.html#x222-24600015.1
Quote
Free Pascal supports operator overloading. This means that it is possible to define the action of some operators on self-defined types, and thus allow the use of these types in mathematical expressions.

@Tron thank you, that really helped. But that's not very good information.

This code works:

Code: Pascal  [Select][+][-]
  1. program Operators;
  2.  
  3. {$mode objfpc}
  4.  
  5. type
  6.   TFloat32=array[0..3] of UInt8;// UInt32;
  7.  
  8. operator * (const f1, f2: TFloat32): TFloat32;
  9. begin
  10.   Result:=f1*f2; //only for test
  11. end;
  12.  
  13. begin
  14.   //nothing to do
  15. end.
  16.  
But array in this case is not very comfortable construction...

d.ioannidis

  • Full Member
  • ***
  • Posts: 229
    • Nephelae
Re: Operators overriding and use of PChars in code for AVRs
« Reply #29 on: January 16, 2025, 07:35:43 pm »
Hi,

Where is my mistake?  %)
https://www.freepascal.org/docs-html/ref/refse102.html#x222-24600015.1
Quote
Free Pascal supports operator overloading. This means that it is possible to define the action of some operators on self-defined types, and thus allow the use of these types in mathematical expressions.

OK got it  .. so RTFM error  :-[ and apologies  :(

Sorry for the noise ...

Regards,

 

TinyPortal © 2005-2018