Shop OBEX P1 Docs P2 Docs Learn Events
Help with strings — Parallax Forums

Help with strings

joeldjoeld Posts: 49
edited 2008-02-20 16:38 in Propeller 1
I've been reading the Prop manual and am kind of stumped on the correct way to implement a string function in Spin. I'm new at Spin and haven't programmed much recently so as simple as this may sound I still need some help.

I am taking a floating point value (temp from sensirion), using the FloatToFormat command out of the Float object to convert the number into a string of fixed length.·My goal is to write a method that will take the string length and variable as inputs and output characters one at a time to a uOLED display. I was thinking a conditional loop that would run the number of iteration as characters in the screen. I'm not finding much info on working with strings in the prop manual. I see how to get the starting address. Can I somehow reference the following addresses somewhat like an array?

Any help getting pointed in the right direction would be appreciated.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-19 20:49
    Yes, you can reference the string as an array. For example, if you have a method parameter called "strAddr" which contains the address of the start of the string, you can refer to the bytes of the string with "byte[noparse][[/noparse] strAddr ] [noparse][[/noparse] i ]" where "i" is the index into the array starting at zero. Look at the "str" method in any of the display objects or in FullDuplexSerial for an example of what you want.
  • joeldjoeld Posts: 49
    edited 2008-02-19 22:03
    Thanks for the start. Here's what I've tried so far:

    PUB displayString(row, col, length, s) : Index
    repeat Index from 0 to length -1
    oled.putChar(byte[noparse][[/noparse]@s][noparse][[/noparse]Index++],col,row,1, 255,255,255)
    col++

    This method is passed the starting row, col, string length and string (s). Right now it's just writing jibberish to the screen. I'm assuming now I will have to find a way to convert that byte to the ascii number for the char I want displayed. I'll have to spend some thought on that. Am I off base or at least starting down the right path?
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-19 23:18
    No, that is not the problem... your routine is nearly correct.

    However "s" IS THE ADDRESS of the string, so adding @ before it in this routine is "doppelt gemoppelt" as we say in German smile.gif

    As you are using a "for"-loop INDEX is incremented automatically, so incrementing it between the brackets is again "doppelt gemoppelt"

    You can also avoid a further "doppel moppel" by callint StrSize, that will determine the length of the string , so you need not pass it as a parameter.

    Last... well, it is possible to use the return value as variable (especially when you want to use the fact that it is zero preset), but it is cleaner to declare a local variable after the | symbol...

    Post Edited (deSilva) : 2/19/2008 11:24:01 PM GMT
  • joeldjoeld Posts: 49
    edited 2008-02-20 00:46
    Thanks deSilva

    The loop issue explains why I was only getting half the number of characters. I'm still getting jibberish on the screen. I think it's probably the way I'm assigning the value to the varaible before I pass it to the method. Time to study some more then maybe I can ask a more intelligent question. I think I'm getting close. I don't have any other display to hook up for debugging where I'm at, so I'm not totaly sure the SHT1x is putting out good info either. I thought I might just work with a constant till I get the display method working.
  • Mike GreenMike Green Posts: 23,101
    edited 2008-02-20 01:03
    You can use the programming port for debugging information.· There are some debug routines in the BS2 function library or you can use FullDuplexSerial even if you just use it for transmitting and there are other DEBUG output objects in the Propeller Object Exchange.· There's a program called PropTerm that you can use on the PC end (search the forum)·or you can use HyperTerm or any other serial terminal emulator.
  • deSilvadeSilva Posts: 2,967
    edited 2008-02-20 01:18
    There could be the suspicion that you do not pass the memory address where the "string" is allocated. This is the place where you need @s (or not - depending on whether "s" is the label or already CONTAINS the ADDRESS...)

    Note that I do not consider it helpful to talk of "arrays" or "strings" when using SPIN. SPIN does not know of such "data structures". You are always on your own with the most basic referencing and dereferencing techniquies as in machine code.
  • joeldjoeld Posts: 49
    edited 2008-02-20 16:38
    Thanks for the help. Things are starting to make a little sense to me. Here's the code I wound up with for the display.

    PUB displayString(row, col, s) | Index
    ··· repeat Index from 0 to strSize(s) - 1
    ····· oled.putChar(byte[noparse][[/noparse]s][noparse][[/noparse]Index],col,row,1, 0,0,255)
    ····· col++

    I finally got the correct statements up in the main method to send the addresses of the formated data. At first I wasn't getting good info out of the sensirion module but I lowered the clock from 16x to 8x and it sprung to life as well. I've tried to attach a picture.

    Joel
    319 x 239 - 46K
Sign In or Register to comment.