18 December 2015
■ FIGURE 7. Bluetooth
coms with logger.
■ FIGURE 6. Data logger.
We can also read strings from a
file with the Propeller. In fact, my
data logger has a command called
VIEW that will open and display the
contents of the text file to the
terminal. The FSRW object doesn’t
have a method for reading strings, so we’re going to do
this manually:
\sd.pputs(p_str)
if (usecr)
\sd.pputc(13)
if (uself)
\sd.pputc(10)
pub rd_line(p_str, n) | c, len
len := 0
I found through experimenting that using the seek()
method after opening a file seemed to improve initial read
speed. If we want to create a new file or replace one that
exists, the mode would be “w.” We can add to an existing
file with “a” (append), or delete a file with “d.”
This method requires a pointer to a string buffer and a
maximum length. It will read the string from the SD card
one character at a time until it encounters a line feed ( 10).
When we’re done, we return the length of the string just
read in. If the result is -1, we have reached the end of the
file. Here’s an example of that method being used to read
and display a text file from the SD card:
For the data logger application, I
want to write values to a file in CSV
format; this will allow me to open the
file as a standard spreadsheet. To
simplify writing a line of text to a file,
I have this method:
pub show_file(p_file) | len
ifnot (mounted)
term.str(string(“SD not
mounted.”))
term.x(term#CR)
pub wr_line(p_str, usecr,
uself)
This takes a pointer to my text
buffer and flags for appending CR
(carriage return) and LF (line feed)
characters to the string. As I’m a
Windows user, I usually set both flags
to true to match common Windows
text files.
if (open_file(p_file,
“r”))
repeat
bytefill(@linebuf,
0, 81)
len := rd_line(@line
buf, 80)
if (len > 0)
term.str(@linebuf)
time.pause(5)
else
close_file
quit
As you can see, this method
opens the file (defined by pointer
p_file) for read mode, then iterates
through the file reading and displaying
one line at a time. There is a short
delay after each line to prevent the
terminal receive buffer from being
overrun. When the resulting line
length is zero or smaller, we can close
the file and exit.
Figure 6 shows my little battery-