Using Arrays
pmulvey
Posts: 24
in Propeller 1
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?
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
so you are giving up on Tachyon?
In Tachyon you declare an ARRAY of bytes just like this:
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.
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:
dgately
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:
You can treat an array like a string (using @), but you have to ensure that the byte following your last character is 0.
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 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 ! !
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:
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.
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.
sure Tachyon runs at 9600.
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.
(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.
Have a look at how it compiled