-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_system.nxc
145 lines (136 loc) · 3.85 KB
/
file_system.nxc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// ex_file_system.nxc
// This program is intended to serve as an introduction to data files on the
// NXT. It focuses on handling the codes returned by the file system's API
// calls, which is an important aspect of the API that is all too often
// neglected by programmers.
//
// The program deals with a data file describing circles. On each run, it adds
// a new circle record to the data file. Then it reads in the whole data file
// and displays all the circles on NXT screen. It creates the data file if
// doesn't already exist. If you run it several times in seccession, you will
// fill the data file and get a file-is-full exception.
//
// The data flie created by this program is not visible on the NXT. To delete
// the file, circles.dat, you can use the NeXT Explorer or the example program
// ex_delete_data_file.nxc.
byte handle = 0; // file handle
struct movement
{
int stepA;
int stepC;
};
void update_movement(movement & m)
{
m.stepA = MotorRotationCount(OUT_A);
m.stepC = MotorRotationCount(OUT_C);
}
// Make sure file is closed whether or not file operations succeed or fail.
void shutdown(const int delay)
{
if (handle) CloseFile(handle);
// Get user's attention.
PlayTone(TONE_C5, SEC_1);
// Give the user time to read screen messages.
Wait(delay);
Stop(true);
}
// Display a return code from a file sytem API call on the NXT screen.
void rtn_code_out(const unsigned int code)
{
/*TextOut(0, LCD_LINE2, "code ");*/
// This line was causing issues on non-enhance NXT firmware
// feel free to uncomment it if you are running the enhanced firmware
/*TextOut(50, LCD_LINE2, FormatNum("%04x", code));*/
}
// Open the data file for writing.
void open_for_write()
{
unsigned int file_size = FILE_SIZE;
handle = 0;
// Start with the assumptions the file doesn't exist and needs to be created.
unsigned int rtn_code = CreateFile(FILE_NAME, file_size, handle);
// If the file already exists, open it with the intent of adding to the data
// that is already there.
if (rtn_code == LDR_FILEEXISTS)
rtn_code = OpenFileAppend(FILE_NAME, file_size, handle);
// Return code handling
switch (rtn_code)
{
case LDR_SUCCESS:
return;
case LDR_FILEISFULL:
TextOut(0, LCD_LINE1, "file is full ");
break;
default:
// Unanticipated exception.
TextOut(0, LCD_LINE1, "write open ");
rtn_code_out(rtn_code);
break;
}
shutdown(SEC_8);
}
// Open the data file for reading.
void open_for_read()
{
unsigned int file_size = FILE_SIZE;
handle = 0;
unsigned int rtn_code = OpenFileRead(FILE_NAME, file_size, handle);
// Return code handling
if (rtn_code != LDR_SUCCESS)
{
// Unanticipated exception.
TextOut(0, LCD_LINE1, "read open ");
rtn_code_out(rtn_code);
shutdown(SEC_8);
}
}
// Write one circle record to the data file.
void write_recd(const movement recd)
{
unsigned int rtn_code = Write(handle, recd);
// Return code handling
if (rtn_code != LDR_SUCCESS)
{
switch (rtn_code)
{
case LDR_EOFEXPECTED:
TextOut(0, LCD_LINE1, "no more space ");
/*break;*/
default:
// Unanticipated exception.
TextOut(0, LCD_LINE1, "Unknown Error");
rtn_code_out(rtn_code);
/*break;*/
}
shutdown(SEC_8);
}
}
// Read all the circle records from the data file. Display each circle as it is
// read.
void play_all(const movement recd, movement ¤t)
{
unsigned int rtn_code = Read(handle, recd);
// rtn_code_out(rtn_code);
// Return code handling
switch (rtn_code)
{
while(true)
{
case LDR_SUCCESS:
current.stepA = recd.stepA;
current.stepC = recd.stepC;
RotateMotor(OUT_A,75,recd.stepA);
RotateMotor(OUT_C,75,recd.stepC);
/*Wait(SEC_2);*/
break;
case LDR_ENDOFFILE:
// No more data to read.
return;
default:
// Unanticipated exception.
TextOut(0, LCD_LINE1, "read failed ");
rtn_code_out(rtn_code);
shutdown(SEC_8);
}
}
}