forked from hjd1964/OnStep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.ino
273 lines (208 loc) · 5.23 KB
/
Library.ino
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
// -----------------------------------------------------------------------------------
// Object libraries
Library::Library()
{
catalog=0;
byteMin=200+PECBufferSize;
#if defined(TEENSYDUINO) && !defined(E2END)
#define E2END 2047
#elif defined(__TM4C123GH6PM__) || defined(__LM4F120H5QR__)
#define E2END 2047
#elif defined(__TM4C1294NCPDT__) || defined(__TM4C1294XNCZAD__)
#define E2END 6143
#endif
byteMax=E2END;
byteCount=(byteMax-byteMin)+1;
bytePos=byteMin;
recMax=byteCount/rec_size; // maximum number of records
firstRec();
}
Library::~Library()
{
}
boolean Library::setCatalog(int num)
{
if ((num<0) || (num>14)) return false;
catalog=num;
return firstRec();
}
void Library::writeVars(char* name, int code, double RA, double Dec)
{
libRec_t work;
for (int l=0; l<11; l++) work.libRec.name[l] = name[l];
work.libRec.code = (code | (catalog<<4));
// convert into ulong, RA=0..360
RA=degRange(RA)/360.0;
// convert into ulong, Dec=0..180
if (Dec>90.0) Dec=90.0; if (Dec<-90.0) Dec=-90.0; Dec=Dec+90.0; Dec=Dec/180.0;
uint16_t r=round(RA*65536.0);
uint16_t d=round(Dec*65536.0);
work.libRec.RA = r;
work.libRec.Dec = d;
writeRec(recPos,work);
}
void Library::readVars(char* name, int* code, double* RA, double* Dec)
{
libRec_t work;
work=readRec(recPos);
int cat = work.libRec.code>>4;
// empty? or not found
if ((cat==15) || (cat!=catalog)) { name[0]=0; *code=0; *RA=0.0; *Dec=0.0; return; }
for (int l=0; l<11; l++) name[l]=work.libRec.name[l]; name[11]=0;
*code = work.libRec.code & 15;
uint16_t r = work.libRec.RA;
uint16_t d = work.libRec.Dec;
// convert from ulong
*RA=(double)r;
*RA=(*RA/65536.0)*360.0;
*Dec=(double)d;
*Dec=((*Dec/65536.0)*180.0)-90.0;
}
libRec_t Library::readRec(int address)
{
libRec_t work;
int l=address*rec_size+byteMin;
for (int m=0;m<16;m++) work.libRecBytes[m]=EEPROM.read(l+m);
return work;
}
void Library::writeRec(int address, libRec_t data)
{
if ((address>=0) && (address<recMax)) {
int l=address*rec_size+byteMin;
for (int m=0;m<16;m++) EEPROM.write(l+m,data.libRecBytes[m]);
}
}
void Library::clearRec(int address)
{
if ((address>=0) && (address<recMax)) {
int l=address*rec_size+byteMin;
int code=15<<4;
EEPROM.write(l+11,(byte)code); // catalog code 15 = deleted
}
}
boolean Library::firstRec()
{
libRec_t work;
// see if first record is for the currentLib
recPos=0;
work=readRec(recPos);
int cat=(int)work.libRec.code>>4;
if ((work.libRec.name[0]!='$') && (cat==catalog)) return true;
// otherwise find the first one, if it exists
return nextRec();
}
// move to the catalog name rec
boolean Library::nameRec()
{
libRec_t work;
int cat;
recPos=-1;
do
{
recPos++; if (recPos>=recMax) { break; }
work=readRec(recPos);
cat=(int)work.libRec.code>>4;
if ((work.libRec.name[0]=='$') && (cat==catalog)) break;
} while (recPos<recMax);
if (recPos>=recMax) { recPos=recMax-1; return false; }
return true;
}
// move to first unused record for this catalog
boolean Library::firstFreeRec()
{
libRec_t work;
int cat;
recPos=-1;
do
{
recPos++; if (recPos>=recMax) { break; }
work=readRec(recPos);
cat=(int)work.libRec.code>>4;
if (cat==15) break; // unused?
} while (recPos<recMax);
if (recPos>=recMax) { recPos=recMax-1; return false; }
return true;
}
// read the previous record, if it exists
boolean Library::prevRec()
{
libRec_t work;
int cat;
do
{
recPos--; if (recPos<0) break;
work=readRec(recPos);
cat=(int)work.libRec.code>>4;
if ((work.libRec.name[0]!='$') && (cat==catalog)) break;
} while (recPos>=0);
if (recPos<0) { recPos=0; return false; }
return true;
}
// read the next record, if it exists
boolean Library::nextRec()
{
libRec_t work;
int cat;
do
{
recPos++; if (recPos>=recMax) break;
work=readRec(recPos);
cat=(int)work.libRec.code>>4;
if ((work.libRec.name[0]!='$') && (cat==catalog)) break;
} while (recPos<recMax);
if (recPos>=recMax) { recPos=recMax-1; return false; }
return true;
}
// read the specified record (of this catalog), if it exists
boolean Library::gotoRec(int num)
{
libRec_t work;
int cat;
int l,r=0;
int c=0;
for (l=0;l<recMax;l++) {
work=readRec(l); r=l;
cat=(int)work.libRec.code>>4;
if ((work.libRec.name[0]!='$') && (cat==catalog)) c++;
if (c==num) break;
}
if (c==num) { recPos=r; return true; } else return false;
}
// count all catalog records
int Library::recCount()
{
libRec_t work;
int cat;
int c=0;
for (int l=0;l<recMax;l++) {
work=readRec(l);
cat=(int)work.libRec.code>>4;
if ((work.libRec.name[0]!='$') && (cat==catalog)) c++;
}
return c;
}
// mark this catalog record as empty
void Library::clearCurrentRec()
{
libRec_t work;
int cat;
work=readRec(recPos);
cat=(int)work.libRec.code>>4;
if (cat==catalog) clearRec(recPos);
}
// mark all catalog records as empty
void Library::clearLib()
{
libRec_t work;
int cat;
for (int l=0;l<recMax;l++) {
work=readRec(l);
cat=(int)work.libRec.code>>4;
if (cat==catalog) clearRec(l);
}
}
// mark all records as empty
void Library::clearAll()
{
for (int l=0;l<recMax;l++) clearRec(l);
}