Shop OBEX P1 Docs P2 Docs Learn Events
BYTECOPY - copying bits in byte with specified length — Parallax Forums

BYTECOPY - copying bits in byte with specified length

HerdyHerdy Posts: 6
edited 2008-06-29 21:43 in Propeller 1
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:
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

  • TimmooreTimmoore Posts: 1,031
    edited 2008-06-24 22:57
    one problem is the use of clrstr, when calling output you pass the address of the string, you then @ the address, I think you get the address of the text ptr variable rather then the address of the text string. Though why you want that I also see a problem because if you remove the @, then clrstr sets the string passed into outstr to 0's overwriting the "hello world' for example, because I dont see a 2nd buffer that the code assumes.
    Also you can replace the copy loop with bytemove.
  • HerdyHerdy Posts: 6
    edited 2008-06-25 21:50
    I appresciate your help. You have brought me nearer to the solution.
    I redid the code using standard SPIN command in order to set a string to zero bit as follows:
    [b]PUB output[/b](text,col,row,len) 
      init(BACKLIGHT)             ' start lcd
      bytefill(@str, 0, len)                            ' clear string to zeros
      bytemove(@str,@text,len)
      byte[noparse][[/noparse]str+len] := $00
      lcd.gotoxy(col,row)
      lcd.str(str)
      lcd.cursor(0)                                       ' cursor off
    
    

    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?
  • TimmooreTimmoore Posts: 1,031
    edited 2008-06-25 22:01
    Whats the definition of str? Assuming its a byte array, then @text doesn't look correct, you already have done that passing the string to outstr so just text is enough. Also byte[noparse][[/noparse]str+len] should be byte[noparse][[/noparse]@str+len] since you need the address of str. Also if str is a byte array then lcd.str should be lcd.str(@str) since I expect lcd.str needs a string pointer
  • HerdyHerdy Posts: 6
    edited 2008-06-29 21:05
    It really seems that I am confused with pointers (address in memory )and variable. It may be the cause, why the code does not work correctly.

    [b]PUB output[/b](input,col,row,size) | outp
      init(BACKLIGHT)                                     ' start lcd
      bytefill(outp, $20, size)                           ' clear string to zeros
      bytemove(outp,input, size)                          ' copies input into outp
      byte[noparse][[/noparse]outp+size] := $00                              ' adds a zero-byte at the end of outp
      lcd.gotoxy(col,row)                                 ' set position to xy on LCD
      lcd.str(outp)                                       ' output onto serial lcd
      lcd.cursor(0)                                       ' cursor off
    
    

    Function calls:
      output(string("123456789012345678"),2,1, 10)          ' expected result: "1234567890"
      output(string("888"),2,2,8)                           ' expected result: "888       "
    
    

    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.
  • TimmooreTimmoore Posts: 1,031
    edited 2008-06-29 21:43
    outp is a long variable, not an array. So you are taking the content of it and using it as an address and then using that address as an array.
    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
Sign In or Register to comment.