PropWare
C++ objects and CMake build system for Parallax Propeller
Home
Download
Getting Started
Documentation
Reference
About
Related Links
Namespace List
Class List
File List
Examples
Build System
File List
File Members
propsideworkspace
Learn
Examples
Devices
Memory
SD for Text Files and Back to Variables.c
1
/*
2
SD for Text Files and Back to Variables.c
3
4
Save variable values to a text file that can be viewed with Windows
5
Notepad and Apple TextEdit. Retrieve those values and convert them from
6
text representation back to variables for use by the program. This fixed
7
line width approach with sprint, fwrite, fread, and sscan is recommended
8
because the program size will not increase too much if float types are used
9
in place of int.
10
11
http://learn.parallax.com/propeller-c-simple-devices/
12
*/
13
14
#include "simpletools.h"
// Include simpletools header
15
16
int
DO = 22, CLK = 23, DI = 24,
CS
= 25;
// SD card pins on Propeller BOE
17
18
int
val;
// Declare value variable
19
char
s[32];
// 32 byte array for strings
20
21
int
main
(
void
)
// main function
22
{
23
sd_mount(DO, CLK, DI,
CS
);
// Mount SD card
24
25
FILE* fp = fopen(
"test.txt"
,
"w"
);
// Open a file for writing
26
27
// Store values as text in test.txt.
28
for
(
int
i = -5; i < 15; i++)
// Repeat 20x (-5...14)
29
{
30
memset(s,
' '
, 30);
// 30 spaces into string
31
s[30] =
'\r'
;
// Newline for Windows Notepad
32
s[31] =
'\n'
;
33
val = i * 10;
// Emulate sensor reading
34
sprint(s,
"i = %d , val = %d "
, i, val);
// Data to s array as characters
35
fwrite(s, 1, 32, fp);
// Write line to file
36
}
37
38
fclose(fp);
// Close file
39
40
// Retrieve values from file and store in
41
// variables for program use.
42
fp = fopen(
"test.txt"
,
"r"
);
// Reopen file for reading
43
44
int
idx;
45
for
(
int
i = -5; i < 15; i++)
// go back through
46
{
47
memset(s,
'\0'
, 32);
// All characters to zero
48
fread(s, 1, 32, fp);
// Read the string
49
sscan(s,
"i = %d , val = %d "
, &idx, &val);
// String values -> variables
50
print(
"idx = %d, val = %d \n"
, idx, val);
// Print variables
51
}
52
53
fclose(fp);
// Close the file
54
}
main
int main(void)
Definition:
GraphicsTest.c:20
CS
const PropWare::Port::Mask CS
Definition:
L3G_Demo.cpp:38
Generated on Sat Aug 27 2016 00:58:03 for PropWare by
1.8.12