-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtsk_data.ks
162 lines (139 loc) · 3.08 KB
/
rtsk_data.ks
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
////////////////////////////////////////////////////////////////////////////////
// RTSK DATA SUBSYSTEM
////////////////////////////////////////////////////////////////////////////////
// PERSISTENT VARIABLES
GLOBAL PVARS TO LEXICON().
// FILE HOLDING PERSISTENT VARIABLES
IF NOT EXISTS("0:/PVARS") {
GLOBAL PFILE TO CREATE("0:/PVARS").
} ELSE {
GLOBAL PFILE TO OPEN("0:/PVARS").
}
// TELEMETRY VARIABLES
GLOBAL TVARS TO LEXICON().
GLOBAL TVARS_CHANGED TO LEXICON().
////////////////////////////////////////////////////////////////////////////////
//
// SET TELEMETRY VALUE
//
FUNCTION DOWNLINK {
PARAMETER NAME, VALUE.
IF NOT TVARS:HASKEY(NAME) {
TVARS:ADD(NAME, VALUE - 1).
TVARS_CHANGED:ADD(NAME, FALSE).
}
IF TVARS[NAME] <> VALUE {
SET TVARS[NAME] TO VALUE.
SET TVARS_CHANGED[NAME] TO TRUE.
}
}
////////////////////////////////////////////////////////////////////////////////
//
// PVAR SAVING TASK
//
FUNCTION TASK_PVAR_SAVE { PARAMETER DT.
PVARS_SAVE().
}
//
// SET PERSISTENT VALUE
//
FUNCTION SET_PVAR {
PARAMETER NAME, VALUE.
IF NOT PVARS:HASKEY(NAME) {
PVARS:ADD(NAME, VALUE).
} ELSE {
SET PVARS[NAME] TO VALUE.
}
// SAVE PVARS AFTER SOME DELAY
TASK_SCHEDULE(6, TASK_PVAR_SAVE@).
}
//
// SET PERSISTENT VALUE (FASTER)
//
FUNCTION SET_PVAR_FAST {
PARAMETER NAME, VALUE.
IF NOT PVARS:HASKEY(NAME) {
PVARS:ADD(NAME, VALUE).
} ELSE {
SET PVARS[NAME] TO VALUE.
}
}
//
// GET PERSISTENT VALUE
//
FUNCTION GET_PVAR {
PARAMETER NAME, DEFAULT.
IF NOT PVARS:HASKEY(NAME) {
PVARS:ADD(NAME, DEFAULT).
}
RETURN PVARS[NAME].
}
//
// SAVE PVARS
//
FUNCTION PVARS_SAVE {
PFILE:CLEAR().
FOR NAME IN PVARS:KEYS {
PFILE:WRITELN(NAME + "|" + PVARS[NAME]).
}
}
//
// LOAD PVARS
//
FUNCTION PVARS_LOAD {
LOCAL PVARS_ITERATOR IS PFILE:READALL:ITERATOR.
UNTIL NOT PVARS_ITERATOR:NEXT {
LOCAL PAIR IS PVARS_ITERATOR:VALUE:SPLIT("|").
IF PAIR:LENGTH = 2 {
LOCAL NAME IS PAIR[0].
LOCAL VALUE IS PAIR[1].
IF NOT NAME:CONTAINS("$") {
SET VALUE TO PVAR_S2N(PAIR[1]).
}
IF NOT PVARS:HASKEY(NAME) {
PVARS:ADD(NAME, VALUE).
} ELSE {
SET PVARS[NAME] TO VALUE.
}
}
}
}
//
// HELPER FUNCTION: GET NUMBER FROM STRING
//
LOCAL NUMBERS IS LEXICON().
NUMBERS:ADD("0", 0).
NUMBERS:ADD("1", 1).
NUMBERS:ADD("2", 2).
NUMBERS:ADD("3", 3).
NUMBERS:ADD("4", 4).
NUMBERS:ADD("5", 5).
NUMBERS:ADD("6", 6).
NUMBERS:ADD("7", 7).
NUMBERS:ADD("8", 8).
NUMBERS:ADD("9", 9).
FUNCTION PVAR_S2N {
PARAMETER STR.
IF STR:STARTSWITH("-") {
RETURN -PVAR_S2N(STR:SUBSTRING(1, STR:LENGTH-1)).
}
IF STR:CONTAINS(".") {
LOCAL PARTS IS STR:SPLIT(".").
IF PARTS:LENGTH <> 2 {
RETURN 0.
}
LOCAL PART0 IS PVAR_S2N(PARTS[0]).
LOCAL PART1 IS PVAR_S2N(PARTS[1]).
RETURN PART0 + (PART1 / (10^PARTS[1]:LENGTH)).
}
LOCAL VALUE IS 0.
FOR I IN STR:SPLIT(""):SUBLIST(1, STR:LENGTH) {
IF NUMBERS:HASKEY(I) {
SET VALUE TO VALUE + NUMBERS[I].
} ELSE {
RETURN 0.
}
SET VALUE TO VALUE * 10.
}
RETURN VALUE / 10.
}