Forum > Embedded - AVR

[SOLVED] Operators overriding and use of PChars in code for AVRs

(1/11) > >>

ackarwow:
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...

Thaddy:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program silly;{$mode objfpc}{$H+}var   s:string ='hello,'#0'world';begin  writeln(pchar(s));// silly C-style  writeln(s);       // proper pascal styleend.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?


MarkMLl:

--- Quote from: Thaddy on January 16, 2025, 04:42:02 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.

--- End quote ---

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

ackarwow:

--- Quote from: Thaddy 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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program silly;{$mode objfpc}{$H+}var   s:string ='hello,'#0'world';begin  writeln(pchar(s));// silly C-style  writeln(s);       // proper pascal styleend.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.

--- End quote ---

@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:
Hi,


--- Quote from: ackarwow on January 16, 2025, 04:25:48 pm ---<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>

--- End quote ---

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,

Navigation

[0] Message Index

[#] Next page

Go to full version