forked from jraller/Crestron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings-from-file.usp
334 lines (280 loc) · 7.9 KB
/
settings-from-file.usp
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*******************************************************************************************
SIMPL+ Module Information
*******************************************************************************************/
/*
Dealer Name: UC Davis School of Law
System Name: Settings File
System Number:
Programmer:Jason Aller
Comments:
*/
/*******************************************************************************************
Compiler Directives
*******************************************************************************************/
#ENABLE_DYNAMIC
#SYMBOL_NAME "Settings"
#HINT "Loads variables from text file"
#CATEGORY "9" // Memory
#PRINT_TO_TRACE
#DIGITAL_EXPAND OutputsWithParams
#ANALOG_SERIAL_EXPAND OutputsWithParams
#DEFAULT_VOLATILE
#ENABLE_STACK_CHECKING
// #ENABLE_TRACE // comment out before release
#ENCODING_INHERIT_FROM_PROGRAM
#HELP_BEGIN
MODULE NAME: Settings
CATEGORY: Memory
VERSION: 1.1 (2016/04/04)
SUMMARY: Load variables from a text file, allow defaults if variables not found
MODULE DESCRIPTION:
When the program starts, looks for a file matching the supplied name.
Loads file and parses it for variables matching output names.
For each matched name it populates the provided value.
For each non-matched name use either supplied default or 0/""
If the file is in the format:
foo:1
bar:Two people walk into a...
line mentioning baz:1
baz:32000
then foo will be set to 1
bar to "Two people walk into a..."
and baz to 32000 because line 3 will be treated like a comment.
INPUT DESCRIPTIONS:
load can be tied to program start, or triggered as a means of reseting values.
Module defaults are only loaded once.
OUTPUT DESCRIPTIONS:
loaded is set to true when processing is complete.
load_os is pulsed as a one shot when processing is complete.
PARAMETER DESCRIPTIONS:
Parameters need to match the variable name in the file and can be 20 characters long.
Parameters are tied to either digital or serial outputs.
If a separator is provided it is possible to specify both a variable name and a default value.
REVISION HISTORY:
====1.0====
Initial release
====1.1====
Add default parameter values
#HELP_END
/*******************************************************************************************
DIGITAL, ANALOG and SERIAL INPUTS and OUTPUTS
*******************************************************************************************/
DIGITAL_INPUT load, _SKIP_, _SKIP_;
DIGITAL_OUTPUT loaded, load_os, _SKIP_;
DIGITAL_OUTPUT d_const[100,1];
STRING_OUTPUT s_const[200,1];
/*******************************************************************************************
Parameters
*******************************************************************************************/
STRING_PARAMETER path[60];
#BEGIN_PARAMETER_PROPERTIES path
propDefaultValue = "\\User\settings.txt";
propShortDescription = "The full path to the settings file separated by \\";
#END_PARAMETER_PROPERTIES
STRING_PARAMETER separator[1], _SKIP_;
#BEGIN_PARAMETER_PROPERTIES separator
propDefaultValue = "|";
propShortDescription = "The separator used in parameters if both variable name and default value are used";
#END_PARAMETER_PROPERTIES
STRING_PARAMETER const_name[300,2][276]; // string_output of 255 + sep + 20
/*******************************************************************************************
Global Variables
*******************************************************************************************/
DYNAMIC STRING sBuf[1];
INTEGER dCount;
INTEGER sCount;
INTEGER index;
// Digitals 20 char id + : + 1 + EOL * 100 = 2300
// Analogs 20 char id + : + 5 + EOL * 100 = 2700
// Strings 20 char id + : + 60 + EOL * 100 = 8200
// 13200 total rounded up is 14000
/*******************************************************************************************
Functions
*******************************************************************************************/
Function LoadSettings()
{
SIGNED_INTEGER fileOps; // are we clear to access files
SIGNED_INTEGER nFileHandle; // the file handler
INTEGER fileSize;
STRING rBuf[14000];
rBuf = "";
fileOps = StartFileOperations();
IF (fileOps = 0 )
{
nFileHandle = FileOpenShared(path, _O_RDONLY | _O_TEXT);
IF (nFileHandle = 0 && FileEOF(nFileHandle) = 0)
{
Print("We have a non-empty file %d\n", FileEOF(nFileHandle));
fileSize = FileRead(nFileHandle, rBuf, 14000);
ResizeString(sBuf, fileSize + 2);
sBuf = "\n" + LEFT(rBuf, fileSize) + "\n"; // pre and postpend \n
FileClose(nFileHandle);
}
ELSE
{
Print("File not found:(%s) %d \n", path, nFileHandle);
}
EndFileOperations();
}
ELSE
{
Print("File error %d \n", fileOps);
}
}
// get the variable portion of a parameter
String_Function getVariable(STRING param)
{
STRING value[20];
INTEGER sepPos;
value = "";
sepPos = Find(separator, param);
IF (sepPos <> 0)
{
value = Left(param, sepPos - 1);
}
ELSE
{
value = param;
}
RETURN(value);
}
// get the default value portion of a parameter if it is present
String_Function getDefault(STRING param)
{
STRING value[255];
INTEGER sepPos;
value = "";
sepPos = Find(separator, param);
IF (sepPos <> 0)
{
value = right(param, Len(param) - sepPos);
}
ELSE
{
value = param;
}
RETURN(value);
}
// match a parameter variable from within sbuf and set the matching output
Function FindParam(INTEGER offset, STRING source)
{
STRING target[22]; // holds wrapped version of source
INTEGER startPos;
STRING line[1];
INTEGER endPos;
INTEGER colPos;
STRING val[1];
target = "\n" + getVariable(source) + ":";
startPos = FIND(target, sBuf); // \n before and : after
endPos = FIND("\n", sBuf, startPos + 1);
reSizeString(line, endPos - startPos);
line = MID(sBuf, startPos, endPos - startPos);
IF (LEN(line) <> 0)
{
colPos = FIND(":", line) + 1;
reSizeString(val, LEN(line) + 1 - colPos);
val = MID(line, colPos, LEN(line) + 1 - colPos);
PRINT("Setting %s to: [%s]\n", source, val);
if (offset <= dCount)
{
if (val = "1" || lower(val) = "on")
{
d_const[offset] = on;
}
ELSE
{
d_const[offset] = off;
}
}
ELSE
{
s_const[offset - dCount] = val;
}
}
}
// loop through the parameters
Function VarLoop()
{
INTEGER X;
FOR (X = 1 TO (dCount + sCount))
{
IF (const_name[X] <> "")
{
FindParam(X, const_name[X]);
}
}
}
Function loadConstants()
{
loaded = off;
LoadSettings();
VarLoop();
ProcessLogic();
loaded = on;
Pulse(10, load_os);
}
/*******************************************************************************************
Event Handlers
*******************************************************************************************/
// this will only load from file, not reload from module parameter defaults
THREADSAFE PUSH load
{
loadConstants();
}
/*******************************************************************************************
Main()
Uncomment and place one-time startup code here
(This code will get called when the system starts up)
*******************************************************************************************/
Function Main()
{
INTEGER sepPos;
loaded = off;
load_os = off;
dCount = 0;
sCount = 0;
sBuf = "";
// Count up defined digital signals
For (index = 1 to GetNumArrayCols(d_const))
{
if (isSignalDefined(d_const[index]))
{
dCount = dCount + 1;
}
}
// Count up defined serial signals
For (index = 1 to GetNumArrayRows(s_const))
{
if (isSignalDefined(s_const[index]))
{
sCount = sCount + 1;
}
}
//interate const_names and set parameter default values or 0/""
For (index = 1 to (dCount + sCount))
{
if (Find(separator, const_name[index]) <> 0)
{
if (index <= dCount)
{
d_const[index] = Atoi(getDefault(const_name[index]));
}
ELSE
{
s_const[index - dCount] = getDefault(const_name[index]);
}
}
ELSE
{
if (index <= dCount)
{
d_const[index] = 0;
}
ELSE
{
s_const[index + dCount] = "";
}
}
}
loadConstants();
}