### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Write Part of array of char  (Read 168 times)

• Full Member
• Posts: 121
##### Write Part of array of char
« on: September 26, 2022, 02:55:13 pm »
Hallo,
it's not Freepascal, its Mikroe Pascal for AVR Controllers

I have an array of char
sign : array [0..3] OF CHAR;

And will give it out on LCD one part after another for rotating - / - \

LCD_OUT(1,16,sign[0]);

But this till make a failure because simple and complex Type:-(

Ho can I give out a part of an array of char?
Wind XP / 7 / 10

#### Zvoni

• Hero Member
• Posts: 1486
##### Re: Write Part of array of char
« Reply #1 on: September 26, 2022, 03:21:02 pm »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

#### Arioch

• Sr. Member
• Posts: 414
##### Re: Write Part of array of char
« Reply #2 on: September 26, 2022, 03:22:30 pm »
if you want to "rotate" - that is, make first numbers re-appear after last ones - then probably there is no simple way, you just can not copy a pat of array, if you need a circular queue.

you can do something like this
Code: Pascal  [Select][+][-]
1. procedure LCD_OUT_CHAR(const LCD_char_No: byte; Const ch: char);
2. begin ... output one char in a given position.... end;
3.
4. var
5.   sign : array [0..3] OF CHAR;
6.
7.   i, shift: integer;
8.
9. begin
10.    shift := 0; // 1, 2, 3
11.    for i := 0 to 3 do
12.       LCD_OUT_CHAR( i, sign[ (i + shift) mod 4 ]);
13. end;
14.

Judging by ZVONI's link, you can probably bridge the first funciton as

Code: Pascal  [Select][+][-]
1. procedure LCD_OUT_CHAR(const LCD_char_No: byte; Const ch: char);
2. begin
3.    Lcd_Chr(
4.      1,  // or whatever row you choose
5.      1 + LCD_char_No,
6.      Ord(ch)  // or byte(ch)
7.     );
8. end;
9.

But not sure what their starting coordinates are

You can also generalize it somewhat.
in Delphi/FPC it could look like this (not sure how you can adapt it to Mikroe)

Code: Pascal  [Select][+][-]
1. {\$PointerMath ON}
2.
3. procedure LCD_OUT_CHAR(const LCD_char_No: byte; Const ch: char);
4. begin ... output one char in a given position.... end;
5.
6. procedure LCD_OUT_WRAPPED(
7.    const LCD_Length: integer;  // how many letters in LCD
8.    const Array_Length: integer; // how many letters in string
9.    const Array_ptr: PChar  // first leter
10.    const Array_shift: integer;  // n-th letter of string to be put to zero-th place of LCD
11. );
12. var
13.   i: integer;
14. begin
15.    for i := 0 to LCD_Length - 1 do
16.       LCD_OUT_CHAR( i, Array_ptr[ (i + Array_shift) mod Array_Length ]);
17. end;
18.
19.
20. ....
21.
22. var
23.   sign : array [0..3] OF CHAR;
24. ...
25.
26.   LCD_OUT_WRAPPED(12, 4, @sign[0], 2 );
27.   Sleep(1000);
28.   LCD_OUT_WRAPPED(12, 4, @sign[0], 3 );
29.   Sleep(1000);
30.   LCD_OUT_WRAPPED(12, 4, @sign[0], 4 );
31.
32.

Not sure if C-style "pointer maths" is available in Mikroe, but you an replace it with explicit pointer operations.

Or, using "open array" type of procedure declaration (TurboPascal and compatibles, not sure about Mikroe)

Code: Pascal  [Select][+][-]
1. procedure LCD_OUT_CHAR(const LCD_char_No: byte; Const ch: char);
2. begin ... output one char in a given position.... end;
3.
4. procedure LCD_OUT_WRAPPED(
5.    const LCD_Length: integer;  // how many letters in LCD
6.    const Array_Letters: array of char;  // "open array", has Low(x), High(x), Length(x) implicitly passed
7.    const Array_shift: integer;  // n-th letter of string to be put to zero-th place of LCD
8. );
9. var
10.   i: integer;
11. begin
12.    for i := 0 to LCD_Length - 1 do
13.       LCD_OUT_CHAR( i, Array_Letters[ Low(Array_Letter) + (i + Array_shift) mod Length(Array_Letters) ]);
14. end;
15.
16.
17. ....
18.
19. var
20.   sign : array [0..3] OF CHAR;
21. ...
22.
23.   LCD_OUT_WRAPPED(12, sign, 2 );
24.   Sleep(1000);
25.   LCD_OUT_WRAPPED(12, sign, 3 );
26.   Sleep(1000);
27.   LCD_OUT_WRAPPED(12, sign, 4 );
28.
« Last Edit: September 26, 2022, 03:27:13 pm by Arioch »

#### Arioch

• Sr. Member
• Posts: 414
##### Re: Write Part of array of char
« Reply #3 on: September 26, 2022, 03:41:14 pm »
in TP/Delphi/FPC you also could

Code: Pascal  [Select][+][-]
1. var
2.   sign : array [0..3] OF CHAR;
3.   s, s1: string;
4.
5. ....
6. s1 := sign[0] + sign[1] + sign[2] + sign[3];
7.
8. s := s1;
9. LCD_OUT(1,16, s);
10. Sleep(1000);
11.
12. s := Copy( s1, 1+1, 4-1) + Copy(s1, 1, 1);
13. LCD_OUT(1,16, s);
14. Sleep(1000);
15.
16. s := Copy( s1, 1+2, 4-2) + Copy(s1, 1, 2);
17. LCD_OUT(1,16, s);
18.

This way you have much more memory copying but much less function calls (once per text, not once per letter);

On "big p[erating systems" this would probably be faster, but on embedded it would probably be slower