Shop OBEX P1 Docs P2 Docs Learn Events
Using Arrays — Parallax Forums

Using Arrays

I want to read data from Simple Serial into an array so that I can parse it afterwards. I am spoiled with string handling facilities in other languages and Spin's string stuff reminds me of what p***ed me off with C 30 years ago. Is there any nice tutorial on SPIN's approach to strings / string arrays?

Also, what are the feelings about going the PropBasic route rather than SPIN for someone like me with a fair bit of BASIC and MicroPython exposure?

Comments

  • MJBMJB Posts: 1,235
    edited 2020-02-18 21:13
    pmulvey wrote: »
    I want to read data from Simple Serial into an array so that I can parse it afterwards. I am spoiled with string handling facilities in other languages and Spin's string stuff reminds me of what p***ed me off with C 30 years ago. Is there any nice tutorial on SPIN's approach to strings / string arrays?

    Also, what are the feelings about going the PropBasic route rather than SPIN for someone like me with a fair bit of BASIC and MicroPython exposure?

    so you are giving up on Tachyon?

    In Tachyon you declare an ARRAY of bytes just like this:
    47 bytes mybuffer
    

    and parsing commands if you allow for keywords and spaces
    (if you are free to define your own command language)
    comes free in Tachyon if you use the read-eval-print loop

    I used Tachyon as a slave to a ESP8266 by just sending commands - like you type them - that Tachyon executed for me.

  • pmulvey wrote: »
    ...Is there any nice tutorial on SPIN's approach to strings / string arrays?
    I won't discuss changing languages as all seem to have deficiencies. Moving from Spin to BASIC may give you something, but it may take something away...

    In the OBEX (http://obex.parallax.com/search?search=strings), you'll find several libraries that will help you with string handling in Spin. Look at String Library v2 in particular as it contains among many utilities for string handling, the ability to concatenate strings, which you'll find useful in building a string of text from an input source (like, serial).

    In Spin strings are at their root just arrays of bytes:
    CON
      
      STR_MAX_LENGTH = 32                                   ' limit of string size of search strings 
    
    VAR
    
      BYTE yourStr[STR_MAX_LENGTH]
    

    dgately



  • JonnyMacJonnyMac Posts: 9,161
    edited 2020-02-18 21:49
    I recently worked for at cyber security company which meant a lot of programming in Python and C, hence I got used to formatted strings. I stayed up nearly all night adding them to my version of FullDuplexSerial (attached with demo of simple string entry and display).

    The problem with Simple_Serial is that its RX is blocking -- if no character shows up, the program halts. This is where an FDS object is helpful. Lets say you want to create a method to enter a string from the terminal (PST) window. You could do it like this:
    pub get_string : len | c
    
    '' Receive string into buffer
    '' -- returns length of string
    
      bytefill(@buffer, 0, BUF_SIZE)                                ' clear buffer
      term.rxflush                                                  ' clear uart buffer
    
      repeat
        c := term.rxcheck                                           ' character ready?
        case c
          32..127 :                                                 ' printable ascii?
            if (len < BUF_SIZE)
              buffer[len++] := c
    
          term#BKSP :                                               ' backspace
            if (len > 0)
              buffer[--len] := 0                                    ' backup, erase last
    
          term#CR :
            quit
    
    You can treat an array like a string (using @), but you have to ensure that the byte following your last character is 0.
  • Cluso99Cluso99 Posts: 18,069
    Kye wrote a string handling library in spin. It may come with (or link) one of the AppNotes. Sorry don't have a link tho later I may be able to find the source (don't have time now). Try searching for StringIO ???
  • Arrays in Tachyon are "user programmable" rather than having complicated general methods that may not fit. So array entries can be any size and mixed to suit, Here are a couple of simple examples of various methods:
    : BIN>ASC ( 4bits -- ascii )  " 0123456789ABCDEF" + C@ ;
    { Usage:
    TF5> $0A BIN>ASC EMIT --- A ok
    }
    
    ( Create an array of 64 32-bit "registers" ) 
    64 longs regs
    : REG ( index -- addr )		4* regs + ;
    : REG! ( long index -- )	REG ! ;
    : REG@ ( index -- long )	REG @ ;
    { Usage:
    TF5> $12345678 48 REG! ---  ok
    TF5> 49 REG@ .L --- 0000.0000  ok
    TF5> 48 REG@ .L --- 1234.5678  ok
    TF5> 48 REG ++ ---  ok
    TF5> 48 REG@ .L --- 1234.5679  ok
    TF5> 48 REG C@ .L --- 0000.0079  ok
    }
    

    You can roll your own array methods very easily which is why Tachyon doesn't have them built-in since it easy to customize one.
  • I used Tachyon as a slave to a ESP8266 by just sending commands - like you type them - that Tachyon executed for me.

    I presume the Tachyon binary has some way of changing to 9600 to allow ESP8266 to communicate via a software serial (limited to 9600). I don't want to tie up the ESP UARTs for this. I compiled and downloaded the tachyon5v7.spin having changed baud to 9600. However I figure that I need to something else as the word "ms" is not recognised. I also ran up a simple terminal program in Python to send stuff to the P1 but this does nothing apart from lock up the serial port. It works OK with my ESP.

    Since I haven't received the EEPROMS I cannot assess if the issues I'm having are related to this.

    I might have RPN / FORTH dyslexia as I feel a headache coming on looking at Peter's arrays example ! !

  • Ah- now I know my diagnosis; RPN dyslexia
  • I came to the Propeller from C. Initially I missed the C handled strings but once I got used to the Spin does this (or doesn't do this) I came to like Spin much more than C. In Spin you have to manipulate each character by processing it a byte at a time.

    I've attached a program I use to initial PCB which communicates with both the USB and a Bluetooth module.

    One of the tasks accomplished with this program is assigning the Bluetooth module a name. The name is based partly on the date PCB is initialized. After setting this name, the program then requests the name back from the module to make sure it's correct.

    There's a lot going on in the program but the following methods might be helpful for your needs. "DateFromProgramName", "FindSmallStringInBiggerString" and the method "FindString" in the header method.

    The "FindString" method kind of allows an array of text strings. The text can't change size at run time so it's pretty much limited to text which doesn't change size. Here's the list of text I used:
    dataName                byte "FONT_8X16", 0
                            byte "FONT_FREE_DESIGN", 0
                            byte "FONT_ST_5X8", 0
                            byte "FONT_5X7", 0
                            byte "PROP_BEANIE_BITMAP_SMALL", 0
                            byte "PROP_BEANIE_BITMAP_LARGE", 0
                            byte "NETWORK_LOGO", 0
    

    I also think the method "ReadableBin" is worth using if you ever want to output binary numbers in an easier to read format. It outputs the number in groups of four bits.

    Most of the attached archive will be likely be useless since it was written to do a bunch of different things including writing fonts to a flash chip.
  • Check out my Modular Programming in Spin demo:

    http://forums.parallax.com/discussion/149189/updated-x2-with-examples-modular-programming-in-spin

    This provides a number of functions for formatting numeric output and parsing numeric strings, and the buffered version gives much of the functionality of a garbage collected string system with much better performance.
  • MJBMJB Posts: 1,235
    edited 2020-02-20 21:08
    pmulvey wrote: »
    I used Tachyon as a slave to a ESP8266 by just sending commands - like you type them - that Tachyon executed for me.

    I presume the Tachyon binary has some way of changing to 9600 to allow ESP8266 to communicate via a software serial (limited to 9600). I don't want to tie up the ESP UARTs for this. I compiled and downloaded the tachyon5v7.spin having changed baud to 9600. However I figure that I need to something else as the word "ms" is not recognised. I also ran up a simple terminal program in Python to send stuff to the P1 but this does nothing apart from lock up the serial port. It works OK with my ESP.

    Since I haven't received the EEPROMS I cannot assess if the issues I'm having are related to this.

    I might have RPN / FORTH dyslexia as I feel a headache coming on looking at Peter's arrays example ! !

    sure Tachyon runs at 9600.
    pub CONBAUD ( baud -- \ Set TACHYON's baudrate for next reboot  )
    	CLKFREQ SWAP / DUP $28 ! $28 E!
    	;
    

    9600 CONBAUD

    the best way to learn Tachyon is to load the Kernel source code plus EXTEND and EASYFILE if you use it,
    into an Editor and just search for what you guess might be there.
    This gave me lot's of insights and I also learnt PASM by just reading Peters's kernel code. - until it clicked ...

    And again: If you show an example of your commands that you think you have to parse
    we might be able to show that Tachyons built in parser will do this for you - for free.

    We had this discussion quite some time ago for parsing GPS??/navigation?? / NEMA strings.



  • You haven't been off to a good start, have you? :) First the hardware and/or loader problems, and now the lack of the essential EEPROM. Once you have the EEPROM you can download the complete binary rather than just the kernel and you can use CONBAUD to change the console baud rate and have it locked into EEPROM.
    (The complete binary is created by loading and running the kernel, and then pasting EXTEND.FTH through the terminal to be compiled on the Prop itself)

    Sorry about your dyslexia, but I think it's what you are used to, and people are used to compiler syntax and so people read Forth code as if each line needs to be parsed rather than as it is, word by word, left to right, just as it is executed. 12 34 + PRINT compiles word by word and executes in the same order.
    TF5> : DEMO 12 34 + PRINT ;
    TF5> DEMO --- 46 ok
    

    Have a look at how it compiled
    TF5> HELP DEMO
    5A3C 4D76 2 DEMO
    ADDR: DATA  NAME
    4D76: 800C  12
    4D78: 8022  $0022  34 
    4D7A: 7D01  +
    4D7C: 041B  PRINT
    4D7E: 005A  EXIT
    

Sign In or Register to comment.