BYTECOPY - copying bits in byte with specified length
Herdy
Posts: 6
Hi,
I am desperately to work out a function called BYTECOPY, which copies a value from a BYTE/String with a specified length. The aim is to cut a text stored in a BYTE or a string to a specified length in order to display the value on a LCD. The variable len defines the length of the string to be displayed - if a BYTE/String is longer the result is cut after the set lenght. In case the BYTE/String is shorter than the target length, the remaining bits are set $00.
Coding example:
Command/expected result:
PUB output(text,col,row,len) | str2
· init(BACKLIGHT) ' start lcd
· clrstr(@text, len) ' clear output string
· str2 := bytecopy(text, len)
· lcd.gotoxy(col,row)
· lcd.str(str2)
· lcd.cursor(0) ' cursor off
However, the string displayed on the LCD does not look as expected. The strings are cut in the middle.
Does anybody see the·mistake I am making?
Cheers,
Herdy
I am desperately to work out a function called BYTECOPY, which copies a value from a BYTE/String with a specified length. The aim is to cut a text stored in a BYTE or a string to a specified length in order to display the value on a LCD. The variable len defines the length of the string to be displayed - if a BYTE/String is longer the result is cut after the set lenght. In case the BYTE/String is shorter than the target length, the remaining bits are set $00.
Coding example:
Command/expected result:
VAR @myStr BYTE "123456789" output(string("Hello World"),0,0,6) ' := "Hello " output(string("123"),0,0,6) ' := "123 " output(@myStr,0,0,6) ' := "123456"
PUB output(text,col,row,len) | str2
· init(BACKLIGHT) ' start lcd
· clrstr(@text, len) ' clear output string
· str2 := bytecopy(text, len)
· lcd.gotoxy(col,row)
· lcd.str(str2)
· lcd.cursor(0) ' cursor off
[b]PRI[/b] [b]bytecopy[/b](strAddr, len) repeat len ' for each character in string byte[noparse][[/noparse]strAddr] := byte[noparse][[/noparse]strAddr++] ' write the character byte[noparse][[/noparse]strAddr++] := $00 ' add a zero bit result := strAddr ' return value address in memory strAddr~ ' reset pointer
[b]PRI clrstr[/b](strAddr, size) bytefill(strAddr, 0, size) ' clear string to zeros ndx~
However, the string displayed on the LCD does not look as expected. The strings are cut in the middle.
Does anybody see the·mistake I am making?
Cheers,
Herdy
Comments
Also you can replace the copy loop with bytemove.
I redid the code using standard SPIN command in order to set a string to zero bit as follows:
With the command bytefill I would like to set all bytes of str to $00.
Then the text variable is copied and cut into the str byte. This works fine. However, the all empty space in the str byte are not cleared/set to zero bit. Why? (Error in bytefill?)
Any hint?
Function calls:
The result is not as expected.
a) if size is set bytemove(outp,input,@size) (do not know why), at least a text is shown at the lcd display
b) with the bytemove(outp,input,size) without the @ not text is displayed.
The result must have to do with the pointers. However, I do not find the correct result.
lcd.str sends text onto the LCD with the SPIN class lcd_debug.
add to your var section at the top of the file
byte outp[noparse][[/noparse]80]
then change all 4 uses of outp to @outp i.e. the address of the array outp you declared in the var section