Recent

Author Topic: UnoLib - library in Pascal for Arduino Uno and ATMega328p  (Read 9368 times)

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
UnoLib - library in Pascal for Arduino Uno and ATMega328p
« on: January 05, 2025, 12:40:36 pm »
I decided to open a new thread on the forum about UnoLib - mentioned already in the AVRPascal thread (https://forum.lazarus.freepascal.org/index.php/topic,68795.0.html). UnoLib is set of routines translated to Pascal from  Arduino sources dedicated for Arduino Uno (ATMega328p). Although the library is distributed with AVRPascal, it can be used independently by for example FPC IDE or Lazarus with FPC built for use with AVR microcontrollers. So please write any questions, comments and wishes to UnoLib here.

UnoLib releases are available on SourceForge: https://sourceforge.net/projects/unolib/
Current changes to UnoLib are on GitHub: https://github.com/ackarwow/unolib
« Last Edit: January 18, 2025, 05:05:30 pm by ackarwow »

AlexTP

  • Hero Member
  • *****
  • Posts: 2541
    • UVviewsoft
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #1 on: January 05, 2025, 12:43:55 pm »
I made a wiki page for it:
https://wiki.freepascal.org/UnoLib

Feel free to fill the wiki with good info.

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #2 on: January 05, 2025, 12:59:28 pm »
I made a wiki page for it:
https://wiki.freepascal.org/UnoLib

Feel free to fill the wiki with good info.

@AlexTP

Many thanks for the wiki page. I will add more detailed description when I find some free time  :)

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #3 on: January 05, 2025, 01:05:41 pm »
As was mentioned already in AVRPAscal thread - me and @Dzandaa are working on software implementation of 32-bit floating point numbers (float32.pas). Numeric routines seem to work correctly on Arduino Uno, including:
 
- Float32Add
- Float32Sub
- Float32Mul
- Float32Div
- Float32Sqrt
- Float32Sinus
- Float32Cosinus
- Float32Deg2Rad
- Float32Rad2Deg
- Float32Abs
- Float32Neg
- Float32Tan

Test program is based on excellent serial communication test written by @Dzanda, and slightly modified for testing numeric routines (attached). All functions are still being tested, so I haven't included them in UnoLib yet. If you notice any errors, please let us know.   
« Last Edit: January 05, 2025, 01:07:16 pm by ackarwow »

AlexTP

  • Hero Member
  • *****
  • Posts: 2541
    • UVviewsoft
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #4 on: January 05, 2025, 04:07:02 pm »
- Float32Sinus
- Float32Cosinus

in English it spells like Sine/Cosine, AFAIK.

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #5 on: January 05, 2025, 04:27:02 pm »
- Float32Sinus
- Float32Cosinus

in English it spells like Sine/Cosine, AFAIK.

Thanks, @AlexTP. Yes, we used Latin convention ;) We will fix it.

ccrause

  • Hero Member
  • *****
  • Posts: 1007
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #6 on: January 06, 2025, 07:42:54 am »
Comment regarding:
Code: Pascal  [Select][+][-]
  1. procedure Cli; assembler;
  2. asm
  3.   CLI
  4. end;

This code cannot be inlined even in the development version of the compiler. This means that each use of Cli will add a call and a ret instruction.

The alternative is:
Code: Pascal  [Select][+][-]
  1. uses
  2.   intrinsics;
  3. ...
  4.   avr_cli;

Here the compiler will directly insert the cli instruction, avoiding the call overhead.

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #7 on: January 06, 2025, 04:14:35 pm »
Comment regarding:
Code: Pascal  [Select][+][-]
  1. procedure Cli; assembler;
  2. asm
  3.   CLI
  4. end;

This code cannot be inlined even in the development version of the compiler. This means that each use of Cli will add a call and a ret instruction.

The alternative is:
Code: Pascal  [Select][+][-]
  1. uses
  2.   intrinsics;
  3. ...
  4.   avr_cli;

Here the compiler will directly insert the cli instruction, avoiding the call overhead.

@ccrause
Thank you for this tip. It will be very useful for optimization of UnoLib.

VisualLab

  • Hero Member
  • *****
  • Posts: 639
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #8 on: January 06, 2025, 04:28:53 pm »
- Float32Sinus
- Float32Cosinus

in English it spells like Sine/Cosine, AFAIK.

Thanks, @AlexTP. Yes, we used Latin convention ;) We will fix it.

Or maybe you will use suffixes like these functions have in the Math module? I.e.: Sin, Cos, Tan, Cot, Sec, Csc. Then the appropriate function names could be: Float32Sin, Float32Cos, Float32Tan, etc.

This would also follow the convention used in many libraries (and even some spreadsheets).

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #9 on: January 06, 2025, 04:50:06 pm »
- Float32Sinus
- Float32Cosinus

in English it spells like Sine/Cosine, AFAIK.

Thanks, @AlexTP. Yes, we used Latin convention ;) We will fix it.

Or maybe you will use suffixes like these functions have in the Math module? I.e.: Sin, Cos, Tan, Cot, Sec, Csc. Then the appropriate function names could be: Float32Sin, Float32Cos, Float32Tan, etc.

This would also follow the convention used in many libraries (and even some spreadsheets).

@VisualLab
Thank you for comment. Yes, I have modified these names using this convention. Moreover - now there is possibility to select function(s) to test.

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #10 on: January 07, 2025, 07:19:40 pm »
UnoLib repository on GitHub (https://github.com/ackarwow/unolib) has been updated. Changes:
- fixed serial buffer size in hardwareserial.pas
- added serial communication test (courtesy of @Dzandaa)
- cli/sei changed to avr_cli/avr_sei (many thanks to @ccrause)
Edit: Added comparison of sizes of test programs before and after using avr_cli/avr_sei in PDF.
« Last Edit: January 08, 2025, 07:26:29 pm by ackarwow »

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #11 on: January 08, 2025, 07:13:12 pm »
The next program for TFloat32 is for testing float-string conversion on Arduino. It requires current hardwareserial.pas (with 64-byte buffer size) from GitHub.
Functions for conversion are not perfect. String to float accept only simple notation with a dot as decimal separator. I am not sure if this function is needed at all, because is rather slow on AVRs and better use constants in UInt32(=TFloat32) format. The second function - float to string returns numbers only in scientific format, so it can be useful on devices that should display numeric values in this convention...

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #12 on: January 13, 2025, 07:08:06 pm »
There is an update for float32.pas - we added some functions:
- Float32Cotan
- Float32Mod
- Float32Log2
- Float32Ln
- Float32Log10
- Float32IntPow
- Float32Pow
- Float32Exp
Moreover, @Dzandaa changed the algorithm for trigonometric functions to a more accurate one.
Source code and test program are attached.

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #13 on: January 17, 2025, 07:25:40 pm »
I've just added the current version of float32.pas and test programs to the GitHub repository (https://github.com/ackarwow/unolib).
But most importantly - I'm happy to announce that @Dzandaa has agreed to collaborate in UnoLib development! :D

Edit New version (0.9) of UnoLib has been released. See: https://sourceforge.net/projects/unolib/.
« Last Edit: January 21, 2025, 07:53:17 am by ackarwow »

ackarwow

  • Full Member
  • ***
  • Posts: 138
    • Andrzej Karwowski's Homepage
Re: UnoLib - library in Pascal for Arduino Uno and ATMega328p
« Reply #14 on: February 21, 2025, 11:56:03 am »
@Dzandaa prepared new I2C unit and some test programs (many thanks to @ccrause). I have added them to the GitHub repository (https://github.com/ackarwow/unolib). 

 

TinyPortal © 2005-2018