Shop OBEX P1 Docs P2 Docs Learn Events
Need some help with code. Trying to store data in buffer. — Parallax Forums

Need some help with code. Trying to store data in buffer.

Don MDon M Posts: 1,652
edited 2016-06-02 02:00 in Propeller 1
I have this method where I'm trying to enter numbers into a buffer. They can be stored as characters and then be read out like a string.

Here's a video of what I'm trying to do:

https://youtu.be/736Rfc8orn0

Here's my method. I get mixed up trying to write characters to a buffer. Don'y know whether I need to add "0" or not. Tried several different ways. The SerNum buffer is a byte buffer declared as byte SerNum[20]
pub EnterSerial  | idx, c, e, x, y

  oled.cmd(oled#CLS)
  oled.moveto(1, 1)
  oled.str(string("Enter Serial Number:"))
  oled.moveto(1, 2)
  oled.str(string("SIM"))

  byte[SerNum][0] := "S"
  byte[SerNum][1] := "I"
  byte[SerNum][2] := "M"

  idx := 0

  x := 4
  y := 2
  
  repeat 13
    oled.moveto(x++, y)                  ' X, Y
    oled.out($FF)                        ' Show placeholders
    
  x := 4
  idx := 3
  
  repeat
    e := encdr.read // 10

    oled.moveto(x, y)
    oled.dec(e)
    if encdr.ispushed == 1
      release
      byte[SerNum][idx++] := (e + "0")    
      x++
    
    if x == 17                           ' If end of spaces then quit
      byte[SerNum][idx] := 0             ' Store end of string
      quit
      
  oled.cmd(oled#CLS)

When I go to read it out such as term.str(@SerNum) I get nothing.

Thanks.

Comments

  • Don MDon M Posts: 1,652
    edited 2016-06-02 02:07
    Sorry.. had a few remnants I forgot to take out before posting..
    pub EnterSerial  | idx, c, e, x, y
    
      oled.cmd(oled#CLS)
      oled.moveto(1, 1)
      oled.str(string("Enter Serial Number:"))
      oled.moveto(1, 2)
      oled.str(string("SIM"))
    
      byte[SerNum][0] := "S"
      byte[SerNum][1] := "I"
      byte[SerNum][2] := "M"
    
      x := 4
      y := 2
      
      repeat 13
        oled.moveto(x++, y)                  ' X, Y
        oled.out($FF)                        ' Show placeholders
        
      x := 4
      idx := 3
      
      repeat
        e := encdr.read // 10
    
        oled.moveto(x, y)
        oled.dec(e)
        if encdr.ispushed == 1
          release
          byte[SerNum][idx++] := (e + "0")   ' Store character in buffer
        
        if x == 17                           ' If end of spaces then quit
          byte[SerNum][idx] := 0             ' Store end of string
          quit
          
      oled.cmd(oled#CLS)                     ' Clear Display
            
    
  • You need to use "@SerNum" everywhere you have just "SerNum" (except for the declaration of "SerNum"). What's happening is that the contents of the first byte of SerNum is being used as the address of the buffer. This is probably zero until the first BYTE[SerNum][0] := "S" is executed.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2016-06-02 02:43
    As long as "SerNum" has been initialized as a byte array, you could assign the individual elements of the array directly like this:
      SerNum[0] := "S"
      SerNum[1] := "I"
      SerNum[2] := "M"
    

    You don't need to use the "byte" operator designator.

    As Mike Green mentioned, if you do use the byte operator designator you need to include the "@" symbol.
      byte[@SerNum][0] := "S"
      byte[@SerNum][1] := "I"
      byte[@SerNum][2] := "M"
    

    Edit: "byte" is a designator not an operator.
  • Don MDon M Posts: 1,652
    Thanks guys. Got it working.
Sign In or Register to comment.