Early success followed by confusion...

As a retired electronic (hardware) design engineer who has experience of code and HDL abstraction and bloat I was delighted to discover the Propellor! I was very pleased that my first simple veroboard P8X32A-D40 ciruit worked and ran code from the Propeller Tool, with very low current consumption. Then I tried a serial LCD display and have become quite confused, mainly about how to creat strings or byte arrays in Spin that are a mixture of hex and text. I have a long-winded routine that sets every byte in an array as a number, and passing this to an assembly serial transmit routine works, but when I attempt something more elegant it seems to upset the Tool or do something strange (I can monitor the serial line on an oscilloscope). Does Spin natively support strings, or do I have to include an additional library? Are there any subtleties about how an assembly cog works its way through a string?

Comments

  • 6 Comments sorted by Date Added Votes
  • Hello and welcome. You will probably get some very quick resolution to your problem if you post your code here in the forum as an attachment.
  • Spin just support strings very minimalistic.

    in DAT sections you can use

    mystringname1 byte 1,2,3,"A","B","C",$0D,$0A,0
    mystringname2 byte "Hello World",0

    in code you can use the string function

    myaddress:=string("Hello",$0D,$0A,0)

    to do the same.

    Note: You can NOT use variables to build a string, this is compile time only. So myAddress:=string("Hello", myNumber,0) will not work.

    In both cases you basically have a byte-array terminated by a zero, also known as zero-terminated string.

    myaddress would already be the address of the array in memory, to access the string at mystringname1 you need to get the address of mystringname1 by using the @ operator giving you the hub-address of the label.

    myaddress1 := @mystringname1 to get the address of the byte-array.

    Most serial drivers have a str(address) method to output a string. That method takes a address as starting point and then sends out byte by byte over the serial line stopping at the first 0.

    You can access (and change) the content of bytes in the byte-arrays thru indexing.

    value := byte[myaddress][4] gives you the 5th byte so the "o" from "Hello" in the example string.
    byte[myaddress][0] :="J" makes "Jello" out of "Hello"

    for more complex manipulations you can use a library, there are a lot of them to use, but provided with the library folder in Proptool you have "SimpleStrings" to start with.

    Enjoy!

    Mike


    I am just another Code Monkey.

    A determined coder can write COBOL programs in any language. -- Author unknown.

    The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and "OPTIONAL" in this post are to be interpreted as described in RFC 2119.
  • Spin has very limited support for strings, but it's enough for relatively simple things. There are library routines in the Propeller Object Exchange for more complex stuff. Look at the sources for the library routines for examples. Particularly useful are BYTEMOVE and STRING which you should look up in the Propeller Manual if you haven't already. STRING takes a string literal, possibly with embedded characters written as hex values, and returns the address of the string with a zero byte terminator as it's stored in the program. BYTEMOVE can be used to copy a string to a buffer. For example:

    BYTEMOVE(@buffer[5],STRING("label xxx",CR,LF,"more yyy"),20)

    The xxx and yyy would be overlaid later with something else.
  • Ahh! Thank you both - that explains a lot. I'm working with the .pdf manual which means I tend to dip in and out rather than seeing the complete picture.

    BTW possible typo:

    myaddress:=string("Hello",$0D,$0A,0)

    gives an error message for the final zero - "only values 1 - 255 are allowed". I think the string function adds it own zero at the end.

    Kind regards,

    David
  • kwinnkwinn Posts: 7,446
    dspread wrote: »
    Ahh! Thank you both - that explains a lot. I'm working with the .pdf manual which means I tend to dip in and out rather than seeing the complete picture.

    BTW possible typo:

    myaddress:=string("Hello",$0D,$0A,0)

    gives an error message for the final zero - "only values 1 - 255 are allowed". I think the string function adds it own zero at the end.

    Kind regards,

    David

    The zero at the end is not needed as the string() function puts it there automatically.
    In science there is no authority. There is only experiment.
    Life is unpredictable. Eat dessert first.
  • tonyp12tonyp12 Posts: 1,802
    edited May 19 Vote Up0Vote Down
    Have you done any C programming?, it have hardly no string support, unless you add str.lib and it's pretty much just a few byte-by-byte functions.

    If you have a function that expect a pointer to a location in memory that for example is a block of ascII text.
    If you instead of a pointer just insert "MY TEXT" the compiler will put that text in flash and insert the pointer.
    " " adds a zero, ' ' doesn't

    if your function expect raw data and length, you could create a macro
    #define TXSTRING(pnt) (TXdata((pnt), sizeof(pnt)-1)) // macro to get string and string len to function TXdata()

    example of use:
    TXSTRING("AT&K=0\r"); // hardware flow off
Sign In or Register to comment.