-
Notifications
You must be signed in to change notification settings - Fork 3
/
functions.ino
1441 lines (1300 loc) · 48.2 KB
/
functions.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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
void calculateLST_HA() {
// HA = LST - RA
String Date_q = String(rtc.getDateStr());
String Time_q = String(rtc.getTimeStr());
int D = Date_q.substring(0, 2).toInt();
int M = Date_q.substring(3, 5).toInt();
int Y = Date_q.substring(6).toInt();
int S = Time_q.substring(6).toInt();
int H = Time_q.substring(0, 2).toInt(); // hours
if (Summer_Time == 1) {
H -= 1;
}
int MN = Time_q.substring(3, 5).toInt();
if (M < 3) {
M = M + 12;
Y = Y - 1;
}
float HH = H + ((float)MN / 60.00) + ((float)S / 3600.00);
float AA = (int)(365.25 * (Y + 4716));
float BB = (int)(30.6001 * (M + 1));
double CurrentJDN = AA + BB + D - 1537.5 + (HH - TIME_ZONE) / 24;
float current_day = CurrentJDN - 2451543.5;
//calculate terms required for LST calcuation and calculate GMST using an approximation
double MJD = CurrentJDN - 2400000.5;
int MJD0 = (int)MJD;
float ut = (MJD - MJD0) * 24.0;
double t_eph = (MJD0 - 51544.5) / 36525.0;
double GMST = 6.697374558 + 1.0027379093 * ut + (8640184.812866 + (0.093104 - 0.0000062 * t_eph) * t_eph) * t_eph / 3600.0;
LST = GMST + OBSERVATION_LONGITUDE / 15.0;
//reduce it to 24 format
int LSTint = (int)LST;
LSTint /= 24;
LST = LST - (double) LSTint * 24;
// Now I'll use the global Variables OBJECT_RA_H and OBJECT_RA_M To calculate the Hour angle of the selected object.
double dec_RA = OBJECT_RA_M / 60 + OBJECT_RA_H;
double HA_decimal = LST - dec_RA;
HAHour = int(HA_decimal);
HAMin = (HA_decimal - HAHour) * 60;
if (HAMin < 0) {
HAHour -= 1;
HAMin += 60;
}
if (HAHour < 0) {
HAHour += 24;
}
// Convert degrees into Decimal Radians
double rDEC = 0;
rDEC = OBJECT_DEC_D + (OBJECT_DEC_M / 60);
//rDEC += delta_a_DEC;
rDEC *= 0.0174532925199;
double rHA = HA_decimal * 0.26179938779915; // 0.261799.. = 15 * 3.1415/180 (to convert to Deg. and * Pi) :)
double rLAT = OBSERVATION_LATTITUDE * 0.0174532925199;
IS_OBJ_VISIBLE = true;
double sin_rDEC = sin(rDEC);
double cos_rDEC = cos(rDEC);
double sin_rLAT = sin(rLAT);
double cos_rLAT = cos(rLAT);
double cos_rHA = cos(rHA);
double sin_rHA = sin(rHA);
ALT = sin_rDEC * sin_rLAT;
ALT += (cos_rDEC * cos_rLAT * cos_rHA);
double sin_rALT = ALT;
ALT = asin(ALT);
double cos_rALT = cos(ALT);
ALT *= 57.2958;
AZ = sin_rALT * sin_rLAT;
AZ = sin_rDEC - AZ;
AZ /= (cos_rALT * cos_rLAT);
AZ = acos(AZ) * 57.2957795;
if (sin_rHA > 0) {
AZ = 360 - AZ;
}
if (ALT < 0) {
IS_OBJ_VISIBLE = false;
if ((IS_BT_MODE_ON == true) && (IS_OBJ_FOUND == false)) {
Serial3.println("Object is out of sight! Telescope not moved.");
}
IS_OBJ_FOUND = true;
IS_OBJECT_RA_FOUND = true;
IS_OBJECT_DEC_FOUND = true;
Slew_RA_timer = 0;
RA_finish_last = 0;
} else {
IS_OBJ_VISIBLE = true;
}
// Take care of the Meridian Flip coordinates
// This will make the telescope do Meridian Flip... depending on the current HA and predefined parameter: MIN_TO_MERIDIAN_FLIP
if (IS_MERIDIAN_FLIP_AUTOMATIC) {
mer_flp = HAHour + ((HAMin + MIN_TO_MERIDIAN_FLIP) / 60);
float old_HAMin = HAMin;
float old_HAHour = HAHour;
if (IS_POSIBLE_MERIDIAN_FLIP == true) {
if (mer_flp >= 24) {
HAMin = HAMin - 60;
HAHour = 0;
if (MERIDIAN_FLIP_DO == false) {
IS_TRACKING = false;
Timer3.stop();
OnScreenMsg(1);
if (IS_SOUND_ON) {
SoundOn(note_C, 32);
delay(200);
SoundOn(note_C, 32);
delay(200);
SoundOn(note_C, 32);
delay(1000);
}
IS_OBJ_FOUND = false;
IS_OBJECT_RA_FOUND = false;
IS_OBJECT_DEC_FOUND = false;
Slew_timer = millis();
Slew_RA_timer = Slew_timer + 20000; // Give 20 sec. advance to the DEC. We will revise later.
MERIDIAN_FLIP_DO = true;
drawMainScreen();
} else {
if ((old_HAHour == HAHour) && (old_HAMin == HAMin)) { // Meridian Flip is done so the code above will not execute
MERIDIAN_FLIP_DO = false;
}
}
//DEC is set as part of the SlewTo function
}
} else {
if (mer_flp >= 24) {
IS_TRACKING = false;
Timer3.stop();
}
}
}
}
void selectOBJECT_M(int index_, int objects) {
OBJECT_Index = index_;
if (objects == 0) { // I've selected a Messier Object
TRACKING_MOON = false;
int i1 = Messier_Array[index_].indexOf(';');
int i2 = Messier_Array[index_].indexOf(';', i1 + 1);
int i3 = Messier_Array[index_].indexOf(';', i2 + 1);
int i4 = Messier_Array[index_].indexOf(';', i3 + 1);
int i5 = Messier_Array[index_].indexOf(';', i4 + 1);
int i6 = Messier_Array[index_].indexOf(';', i5 + 1);
int i7 = Messier_Array[index_].indexOf(';', i6 + 1);
OBJECT_NAME = Messier_Array[index_].substring(0, i1);
OBJECT_DESCR = Messier_Array[index_].substring(i7 + 1, Messier_Array[index_].length() - 1);
String OBJ_RA = Messier_Array[index_].substring(i1, i2);
OBJECT_RA_H = OBJ_RA.substring(1, OBJ_RA.indexOf('h')).toFloat();
OBJECT_RA_M = OBJ_RA.substring(OBJ_RA.indexOf('h') + 1, OBJ_RA.length() - 1).toFloat();
String OBJ_DEC = Messier_Array[index_].substring(i2, i3);
String sign = OBJ_DEC.substring(1, 2);
OBJECT_DEC_D = OBJ_DEC.substring(2, OBJ_DEC.indexOf('°')).toFloat();
OBJECT_DEC_M = OBJ_DEC.substring(OBJ_DEC.indexOf('°') + 1, OBJ_DEC.length() - 1).toFloat();
if (sign.equals("-")) {
OBJECT_DEC_D *= -1;
OBJECT_DEC_M *= -1;
}
OBJECT_DETAILS = OBJECT_NAME + " is a ";
OBJECT_DETAILS += Messier_Array[index_].substring(i4 + 1, i5) + " in constelation ";
OBJECT_DETAILS += Messier_Array[index_].substring(i3 + 1, i4) + ", with visible magnitude of ";
OBJECT_DETAILS += Messier_Array[index_].substring(i5 + 1, i6) + " and size of ";
OBJECT_DETAILS += Messier_Array[index_].substring(i6 + 1, i7);
} else if (objects == 1) { // I've selected a Treasure Object
TRACKING_MOON = false;
int i1 = Treasure_Array[index_].indexOf(';');
int i2 = Treasure_Array[index_].indexOf(';', i1 + 1);
int i3 = Treasure_Array[index_].indexOf(';', i2 + 1);
int i4 = Treasure_Array[index_].indexOf(';', i3 + 1);
int i5 = Treasure_Array[index_].indexOf(';', i4 + 1);
int i6 = Treasure_Array[index_].indexOf(';', i5 + 1);
int i7 = Treasure_Array[index_].indexOf(';', i6 + 1);
OBJECT_NAME = Treasure_Array[index_].substring(0, i1);
OBJECT_DESCR = Treasure_Array[index_].substring(i7 + 1, Treasure_Array[index_].length() - 1);
String OBJ_RA = Treasure_Array[index_].substring(i1, i2);
OBJECT_RA_H = OBJ_RA.substring(1, OBJ_RA.indexOf('h')).toFloat();
OBJECT_RA_M = OBJ_RA.substring(OBJ_RA.indexOf('h') + 1, OBJ_RA.length() - 1).toFloat();
String OBJ_DEC = Treasure_Array[index_].substring(i2, i3);
String sign = OBJ_DEC.substring(1, 2);
OBJECT_DEC_D = OBJ_DEC.substring(2, OBJ_DEC.indexOf('°')).toFloat();
OBJECT_DEC_M = OBJ_DEC.substring(OBJ_DEC.indexOf('°') + 1, OBJ_DEC.length() - 1).toFloat();
if (sign == "-") {
OBJECT_DEC_D *= -1;
OBJECT_DEC_M *= -1;
}
OBJECT_DETAILS = OBJECT_NAME + " is a ";
OBJECT_DETAILS += Treasure_Array[index_].substring(i4 + 1, i5) + " in constelation ";
OBJECT_DETAILS += Treasure_Array[index_].substring(i3 + 1, i4) + ", with visible magnitude of ";
OBJECT_DETAILS += Treasure_Array[index_].substring(i5 + 1, i6) + " and size of ";
OBJECT_DETAILS += Treasure_Array[index_].substring(i6 + 1, i7);
} else if (objects == 2) { // I'm selecting a STAR for Synchronization - 1 Star ALLIGNMENT
TRACKING_MOON = false;
int i1 = Stars[index_].indexOf(';');
int i2 = Stars[index_].indexOf(';', i1 + 1);
int i3 = Stars[index_].indexOf(';', i2 + 1);
OBJECT_NAME = Stars[index_].substring(i1 + 1, i2) + " from " + Stars[index_].substring(0, i1);
String OBJ_RA = Stars[index_].substring(i2 + 1, i3);
OBJECT_RA_H = OBJ_RA.substring(0, OBJ_RA.indexOf('h')).toFloat();
OBJECT_RA_M = OBJ_RA.substring(OBJ_RA.indexOf('h') + 1, OBJ_RA.length() - 1).toFloat();
String OBJ_DEC = Stars[index_].substring(i3, Stars[index_].length());
String sign = OBJ_DEC.substring(0, 1);
OBJECT_DEC_D = OBJ_DEC.substring(1, OBJ_DEC.indexOf('°')).toFloat();
if (sign == "-") {
OBJECT_DEC_D *= (-1);
}
OBJECT_DEC_M = 0;
} else if (objects == 3) { // I'm selecting a STAR for Synchronization - Iterative ALLIGNMENT
TRACKING_MOON = false;
int i1 = Iter_Stars[index_].indexOf(';');
int i2 = Iter_Stars[index_].indexOf(';', i1 + 1);
int i3 = Iter_Stars[index_].indexOf(';', i2 + 1);
OBJECT_NAME = Iter_Stars[index_].substring(i1 + 1, i2) + " from " + Iter_Stars[index_].substring(0, i1);
String OBJ_RA = Iter_Stars[index_].substring(i2 + 1, i3);
OBJECT_RA_H = OBJ_RA.substring(0, OBJ_RA.indexOf('h')).toFloat();
OBJECT_RA_M = OBJ_RA.substring(OBJ_RA.indexOf('h') + 1, OBJ_RA.length() - 1).toFloat();
String OBJ_DEC = Iter_Stars[index_].substring(i3, Iter_Stars[index_].length());
String sign = OBJ_DEC.substring(0, 1);
OBJECT_DEC_D = OBJ_DEC.substring(1, OBJ_DEC.indexOf('°')).toFloat();
if (sign == "-") {
OBJECT_DEC_D *= (-1);
}
OBJECT_DEC_M = 0;
}
else if (objects == 4)
{
// I've selected a custom Object
TRACKING_MOON = false;
int i1 = custom_Array[index_].indexOf(';');
int i2 = custom_Array[index_].indexOf(';', i1 + 1);
int i3 = custom_Array[index_].indexOf(';', i2 + 1);
int i4 = custom_Array[index_].indexOf(';', i3 + 1);
int i5 = custom_Array[index_].indexOf(';', i4 + 1);
int i6 = custom_Array[index_].indexOf(';', i5 + 1);
int i7 = custom_Array[index_].indexOf(';', i6 + 1);
int i8 = custom_Array[index_].indexOf(';', i7 + 1);
OBJECT_NAME = custom_Array[index_].substring(0, i1);
OBJECT_DESCR = custom_Array[index_].substring(i7 + 1, i8);
String OBJ_RA = custom_Array[index_].substring(i1, i2);
OBJECT_RA_H = OBJ_RA.substring(1, OBJ_RA.indexOf('h')).toFloat();
OBJECT_RA_M = OBJ_RA.substring(OBJ_RA.indexOf('h') + 1, OBJ_RA.length() - 1).toFloat();
String OBJ_DEC = custom_Array[index_].substring(i2, i3);
String sign = OBJ_DEC.substring(1, 2);
OBJECT_DEC_D = OBJ_DEC.substring(2, OBJ_DEC.indexOf('°')).toFloat();
OBJECT_DEC_M = OBJ_DEC.substring(OBJ_DEC.indexOf('°') + 1, OBJ_DEC.length() - 1).toFloat();
if (sign == "-")
{
OBJECT_DEC_D *= -1;
OBJECT_DEC_M *= -1;
}
OBJECT_DETAILS = OBJECT_NAME + " is a ";
OBJECT_DETAILS += custom_Array[index_].substring(i4 + 1, i5) + " in constelation ";
OBJECT_DETAILS += custom_Array[index_].substring(i3 + 1, i4) + ", with visible magnitude of ";
OBJECT_DETAILS += custom_Array[index_].substring(i5 + 1, i6) + " and size of ";
OBJECT_DETAILS += custom_Array[index_].substring(i6 + 1, i7);
OBJECT_DETAILS += "\n" + custom_Array[index_].substring(i8 + 1, custom_Array[index_].length() - 1);
}
}
void Sidereal_rate(){
// when a manual movement of the drive happens. - This will avoid moving the stepepers with a wrong Step Mode.
if ((IS_MANUAL_MOVE == false) && (IS_TRACKING) && (IS_STEPPERS_ON)){
if (RA_mode_steps != MICROSteps){
setmStepsMode("R",MICROSteps);
}
digitalWrite(RA_DIR,STP_BACK);
PIOC->PIO_SODR=(1u<<26);
delayMicroseconds(2);
PIOC->PIO_CODR=(1u<<26);
// digitalWrite(RA_STP,HIGH);
RA_microSteps += 1;
// digitalWrite(RA_STP,LOW);
}
}
void cosiderSlewTo(){
// int RA_microSteps, DEC_microSteps;
// int SLEW_RA_microsteps, SLEW_DEC_microsteps;
// INT data type -> -2,147,483,648 to 2,147,483,647
// for more details see the XLS file with calculations
//...
float HAH;
float HAM;
float DECD;
float DECM;
double HA_decimal, DEC_decimal;
if (HAHour >= 12){
HAH = HAHour - 12;
HAM = HAMin;
IS_MERIDIAN_PASSED = false;
}else{
HAH = HAHour;
HAM = HAMin;
IS_MERIDIAN_PASSED = true;
}
// ADD Correction for RA && DEC according to the Star Alignment
HA_decimal = ((HAH+(HAM/60))*15) + delta_a_RA; // In degrees - decimal
DEC_decimal = OBJECT_DEC_D + (OBJECT_DEC_M/60) + delta_a_DEC; //I n degrees - decimal
SLEW_RA_microsteps = HA_decimal * HA_H_CONST; // Hardware Specific Code
SLEW_DEC_microsteps = DEC_90 - (DEC_decimal * DEC_D_CONST); // Hardware specific code
if(IS_MERIDIAN_PASSED == true){
SLEW_DEC_microsteps*= -1;
}
// If Home Position selected .... Make sure it goes to 0.
// DO I REALLY NEED THIS.... ????
// CONSIDER THE CODE WHEN YOU HAVE TIME!!!
int home_pos = 0;
if ((OBJECT_RA_H == 12) && (OBJECT_RA_M == 0) && (OBJECT_DEC_D == 90) && (OBJECT_DEC_M == 0)){
SLEW_RA_microsteps = RA_90;
SLEW_DEC_microsteps = 0;
home_pos = 1;
}
// Make the motors START slow and then speed-up - using the microsteps!
// Speed goes UP in 2.2 sec....then ..... FULL Speed ..... then....Speed goes Down for 3/4 Revolution of the drive
int delta_DEC_time = millis() - Slew_timer;
int delta_RA_timer = millis() - Slew_RA_timer;
if (delta_DEC_time >= 0 && delta_DEC_time < 1800){
if (DEC_mode_steps != 16){
setmStepsMode("D",16);
}
}
if (delta_DEC_time >= 1800 && delta_DEC_time < 2600){
if (DEC_mode_steps != 8){
setmStepsMode("D",8);
}
}
if (delta_DEC_time >= 2600 && delta_DEC_time < 3400){
if (DEC_mode_steps != 4){
setmStepsMode("D",4);
}
}
if (delta_DEC_time >= 3400){
if (DEC_mode_steps != 2){
setmStepsMode("D",2);
}
}
if (delta_RA_timer >= 0 && delta_RA_timer < 1800){
if (RA_mode_steps != 16){
setmStepsMode("R",16);
}
}
if (delta_RA_timer >= 1800 && delta_RA_timer < 2600){
if (RA_mode_steps != 8){
setmStepsMode("R",8);
}
}
if (delta_RA_timer >= 2600 && delta_RA_timer < 3400){
if (RA_mode_steps != 4){
setmStepsMode("R",4);
}
}
if (delta_RA_timer >= 3400){
if (RA_mode_steps != 2){
setmStepsMode("R",2);
}
}
int delta_RA_steps = SLEW_RA_microsteps - RA_microSteps;
int delta_DEC_steps = SLEW_DEC_microsteps - DEC_microSteps;
// Make the motors SLOW DOWN and then STOP - using the microsteps!
// Speed goes DOWN in 2.2 sec....then ..... FULL Speed ..... then....Speed goes Down for 3/4 Revolution of the drive
if ((abs(delta_DEC_steps) >= 1200) && (abs(delta_DEC_steps) <= 3000)){
if (DEC_mode_steps != 8){
setmStepsMode("D",8);
}
}
if ((abs(delta_DEC_steps) < 3000)){
if (DEC_mode_steps != 16){
setmStepsMode("D",16);
}
}
if ((abs(delta_RA_steps) >= 1200) && (abs(delta_RA_steps) <= 3000)){
if (RA_mode_steps != 8){
setmStepsMode("R",8);
}
}
if (abs(delta_RA_steps) < 3000){
if (RA_mode_steps != 16){
setmStepsMode("R",16);
RA_move_ending = 1;
}
}
// Taking care of the RA Slew_To.... and make sure it ends Last
// NB: This way we can jump to TRACK and be sure the RA is on target
if(abs(delta_RA_steps)>= abs(delta_DEC_steps)){
if (RA_finish_last == 0){
RA_finish_last = 1;
Slew_RA_timer = millis();
}
}
// RA_STP, HIGH - PIOC->PIO_SODR=(1u<<26)
// RA_STP, LOW - PIOC->PIO_CODR=(1u<<26)
// DEC_STP, HIGH - PIOC->PIO_SODR=(1u<<24)
// DEC_STP, LOW - PIOC->PIO_CODR=(1u<<24)
if ((IS_OBJECT_RA_FOUND == false) && (RA_finish_last == 1)){
if (SLEW_RA_microsteps >= (RA_microSteps-RA_mode_steps) && SLEW_RA_microsteps <= (RA_microSteps+RA_mode_steps)){
IS_OBJECT_RA_FOUND = true;
} else {
if (SLEW_RA_microsteps > RA_microSteps){
digitalWrite(RA_DIR,STP_BACK);
//digitalWrite(RA_STP,HIGH);
//digitalWrite(RA_STP,LOW);
PIOC->PIO_SODR=(1u<<26);
delayMicroseconds(5);
PIOC->PIO_CODR=(1u<<26);
RA_microSteps += RA_mode_steps;
}else{
digitalWrite(RA_DIR,STP_FWD);
//digitalWrite(RA_STP,HIGH);
//digitalWrite(RA_STP,LOW);
PIOC->PIO_SODR=(1u<<26);
delayMicroseconds(5);
PIOC->PIO_CODR=(1u<<26);
RA_microSteps -= RA_mode_steps;
}
}
}
// Taking care of the DEC Slew_To....
if (IS_OBJECT_DEC_FOUND == false){
if (SLEW_DEC_microsteps >= (DEC_microSteps-DEC_mode_steps) && SLEW_DEC_microsteps <= (DEC_microSteps+DEC_mode_steps)){
IS_OBJECT_DEC_FOUND = true;
} else {
if (SLEW_DEC_microsteps > DEC_microSteps){
digitalWrite(DEC_DIR,STP_BACK);
//digitalWrite(DEC_STP,HIGH);
//digitalWrite(DEC_STP,LOW);
PIOC->PIO_SODR=(1u<<24);
delayMicroseconds(5);
PIOC->PIO_CODR=(1u<<24);
DEC_microSteps += DEC_mode_steps;
}else{
digitalWrite(DEC_DIR,STP_FWD);
//digitalWrite(DEC_STP,HIGH);
//digitalWrite(DEC_STP,LOW);
PIOC->PIO_SODR=(1u<<24);
delayMicroseconds(5);
PIOC->PIO_CODR=(1u<<24);
DEC_microSteps -= DEC_mode_steps;
}
}
}
// Check if Object is found on both Axes...
if (IS_OBJECT_RA_FOUND == true && IS_OBJECT_DEC_FOUND == true){
IS_OBJ_FOUND = true;
RA_move_ending = 0;
if ((home_pos == 0 ) && (ALT > 0)){
IS_TRACKING = true;
setmStepsMode("R",MICROSteps);
if (Tracking_type == 1){ // 1: Sidereal, 2: Solar, 0: Lunar;
Timer3.start(Clock_Sidereal);
}else if (Tracking_type == 2){
Timer3.start(Clock_Solar);
}else if (Tracking_type == 0){
Timer3.start(Clock_Lunar);
}
}
if (IS_SOUND_ON){
SoundOn(note_C,64);
}
Slew_RA_timer = 0;
RA_finish_last = 0;
if (IS_BT_MODE_ON == true){
Serial3.println("Slew done! Object in scope!");
}
if (IS_IN_OPERATION == true){
drawMainScreen();
}else{
drawConstelationScreen(SELECTED_STAR);
}
}
}
void consider_Manual_Move(int xP, int yP) {
if ((xP > 0) && (xP <= 150)) {
setmStepsMode("R", 1);
digitalWrite(RA_DIR, STP_BACK);
digitalWrite(RA_STP, HIGH);
digitalWrite(RA_STP, LOW);
RA_microSteps += RA_mode_steps;
} else if ((xP > 150) && (xP <= 320)) {
setmStepsMode("R", 4);
digitalWrite(RA_DIR, STP_BACK);
digitalWrite(RA_STP, HIGH);
digitalWrite(RA_STP, LOW);
RA_microSteps += RA_mode_steps;
} else if ((xP > 320) && (xP <= 470)) {
setmStepsMode("R", 8);
digitalWrite(RA_DIR, STP_BACK);
digitalWrite(RA_STP, HIGH);
digitalWrite(RA_STP, LOW);
RA_microSteps += RA_mode_steps;
} else if ((xP > 620) && (xP <= 770)) {
setmStepsMode("R", 8);
digitalWrite(RA_DIR, STP_FWD);
digitalWrite(RA_STP, HIGH);
digitalWrite(RA_STP, LOW);
RA_microSteps -= RA_mode_steps;
} else if ((xP > 770) && (xP <= 870)) {
setmStepsMode("R", 4);
digitalWrite(RA_DIR, STP_FWD);
digitalWrite(RA_STP, HIGH);
digitalWrite(RA_STP, LOW);
RA_microSteps -= RA_mode_steps;
} else if ((xP > 870) && (xP <= 1023)) {
setmStepsMode("R", 1);
digitalWrite(RA_DIR, STP_FWD);
digitalWrite(RA_STP, HIGH);
digitalWrite(RA_STP, LOW);
RA_microSteps -= RA_mode_steps;
}
if ((yP > 0) && (yP <= 150)) {
setmStepsMode("D", 1);
digitalWrite(DEC_DIR, STP_BACK);
digitalWrite(DEC_STP, HIGH);
digitalWrite(DEC_STP, LOW);
DEC_microSteps += DEC_mode_steps;
} else if ((yP > 150) && (yP <= 320)) {
setmStepsMode("D", 4);
digitalWrite(DEC_DIR, STP_BACK);
digitalWrite(DEC_STP, HIGH);
digitalWrite(DEC_STP, LOW);
DEC_microSteps += DEC_mode_steps;
} else if ((yP > 320) && (yP <= 470)) {
setmStepsMode("D", 8);
digitalWrite(DEC_DIR, STP_BACK);
digitalWrite(DEC_STP, HIGH);
digitalWrite(DEC_STP, LOW);
DEC_microSteps += DEC_mode_steps;
} else if ((yP > 620) && (yP <= 770)) {
setmStepsMode("D", 8);
digitalWrite(DEC_DIR, STP_FWD);
digitalWrite(DEC_STP, HIGH);
digitalWrite(DEC_STP, LOW);
DEC_microSteps -= DEC_mode_steps;
} else if ((yP > 770) && (yP <= 870)) {
setmStepsMode("D", 4);
digitalWrite(DEC_DIR, STP_FWD);
digitalWrite(DEC_STP, HIGH);
digitalWrite(DEC_STP, LOW);
DEC_microSteps -= DEC_mode_steps;
} else if ((yP > 870) && (yP <= 1023)) {
setmStepsMode("D", 1);
digitalWrite(DEC_DIR, STP_FWD);
digitalWrite(DEC_STP, HIGH);
digitalWrite(DEC_STP, LOW);
DEC_microSteps -= DEC_mode_steps;
}
delayMicroseconds(1500);
}
// Keep the GPS sensor "fed" until we find the data.
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
if (IS_SOUND_ON) {
SoundOn(note_c, 8);
}
do
{
while (Serial2.available())
gps.encode(Serial2.read());
} while (millis() - start < ms);
}
void setmStepsMode(char* P, int mod) {
// P means the axis: RA or DEC; mod means MicroSteppping mode: x32, x16, x8....
// setmStepsMode(R,2) - means RA with 1/2 steps; setmStepsMode(R,4) - means RA with 1/4 steps
// PINS Mapping for fast switching
// DEC_M2 - Pin 8 UP - PC22 - PIOC->PIO_SODR=(1u<<22);
// DEC_M1 - Pin 9 UP - PC21 - PIOC->PIO_SODR=(1u<<21);
// DEC_M0 - Pin 10 UP - PC29 - PIOC->PIO_SODR=(1u<<29);
// RA_M0 - Pin 11 UP - PD7 - PIOD->PIO_SODR=(1u<<7);
// RA_M1 - Pin 12 UP - PD8 - PIOD->PIO_SODR=(1u<<8);
// RA_M2 - Pin 13 UP - PB27 - PIOB->PIO_SODR=(1u<<27);
// DEC_M2 - Pin 8 DOWN - PC22 - PIOC->PIO_CODR=(1u<<22);
// DEC_M1 - Pin 9 DOWN - PC21 - PIOC->PIO_CODR=(1u<<21);
// DEC_M0 - Pin 10 DOWN - PC29 - PIOC->PIO_CODR=(1u<<29);
// RA_M0 - Pin 11 DOWN - PD7 - PIOD->PIO_CODR=(1u<<7);
// RA_M1 - Pin 12 DOWN - PD8 - PIOD->PIO_CODR=(1u<<8);
// RA_M2 - Pin 13 DOWN - PB27 - PIOB->PIO_CODR=(1u<<27);
//
// PIOC->PIO_SODR=(1u<<25); // Set Pin High
// PIOC->PIO_CODR=(1u<<25); // Set Pin Low
if (P == "R") { // Set RA modes
if (mod == 1) { // Full Step
//digitalWrite(RA_MODE0, LOW);
//digitalWrite(RA_MODE1, LOW);
//digitalWrite(RA_MODE2, LOW);
PIOD->PIO_CODR = (1u << 7);
PIOD->PIO_CODR = (1u << 8);
PIOB->PIO_CODR = (1u << 27);
}
if (mod == 2) { // 1/2 Step
//digitalWrite(RA_MODE0, HIGH);
//digitalWrite(RA_MODE1, LOW);
//digitalWrite(RA_MODE2, LOW);
PIOD->PIO_SODR = (1u << 7);
PIOD->PIO_CODR = (1u << 8);
PIOB->PIO_CODR = (1u << 27);
}
if (mod == 4) { // 1/4 Step
//digitalWrite(RA_MODE0, LOW);
//digitalWrite(RA_MODE1, HIGH);
//digitalWrite(RA_MODE2, LOW);
PIOD->PIO_CODR = (1u << 7);
PIOD->PIO_SODR = (1u << 8);
PIOB->PIO_CODR = (1u << 27);
}
if (mod == 8) { // 1/8 Step
//digitalWrite(RA_MODE0, HIGH);
//digitalWrite(RA_MODE1, HIGH);
//digitalWrite(RA_MODE2, LOW);
PIOD->PIO_SODR = (1u << 7);
PIOD->PIO_SODR = (1u << 8);
PIOB->PIO_CODR = (1u << 27);
}
if (mod == 16) { // 1/16 Step
//digitalWrite(RA_MODE0, LOW);
//digitalWrite(RA_MODE1, LOW);
//digitalWrite(RA_MODE2, HIGH);
PIOD->PIO_CODR = (1u << 7);
PIOD->PIO_CODR = (1u << 8);
PIOB->PIO_SODR = (1u << 27);
}
if (mod == 32) { // 1/32 Step
//digitalWrite(RA_MODE0, HIGH);
//digitalWrite(RA_MODE1, LOW);
//digitalWrite(RA_MODE2, HIGH);
PIOD->PIO_SODR = (1u << 7);
PIOD->PIO_CODR = (1u << 8);
PIOB->PIO_SODR = (1u << 27);
}
RA_mode_steps = MICROSteps / mod;
}
if (P == "D") { // Set RA modes
if (mod == 1) { // Full Step
//digitalWrite(DEC_MODE0, LOW);
//digitalWrite(DEC_MODE1, LOW);
//digitalWrite(DEC_MODE2, LOW);
PIOC->PIO_CODR = (1u << 29);
PIOC->PIO_CODR = (1u << 21);
PIOC->PIO_CODR = (1u << 22);
}
if (mod == 2) { // 1/2 Step
//digitalWrite(DEC_MODE0, HIGH);
//digitalWrite(DEC_MODE1, LOW);
//digitalWrite(DEC_MODE2, LOW);
PIOC->PIO_SODR = (1u << 29);
PIOC->PIO_CODR = (1u << 21);
PIOC->PIO_CODR = (1u << 22);
}
if (mod == 4) { // 1/4 Step
//digitalWrite(DEC_MODE0, LOW);
//digitalWrite(DEC_MODE1, HIGH);
//digitalWrite(DEC_MODE2, LOW);
PIOC->PIO_CODR = (1u << 29);
PIOC->PIO_SODR = (1u << 21);
PIOC->PIO_CODR = (1u << 22);
}
if (mod == 8) { // 1/8 Step
//digitalWrite(DEC_MODE0, HIGH);
//digitalWrite(DEC_MODE1, HIGH);
//digitalWrite(DEC_MODE2, LOW);
PIOC->PIO_SODR = (1u << 29);
PIOC->PIO_SODR = (1u << 21);
PIOC->PIO_CODR = (1u << 22);
}
if (mod == 16) { // 1/16 Step
//digitalWrite(DEC_MODE0, LOW);
//digitalWrite(DEC_MODE1, LOW);
//digitalWrite(DEC_MODE2, HIGH);
PIOC->PIO_CODR = (1u << 29);
PIOC->PIO_CODR = (1u << 21);
PIOC->PIO_SODR = (1u << 22);
}
if (mod == 32) { // 1/32 Step
//digitalWrite(DEC_MODE0, HIGH);
//digitalWrite(DEC_MODE1, LOW);
//digitalWrite(DEC_MODE2, HIGH);
PIOC->PIO_SODR = (1u << 29);
PIOC->PIO_CODR = (1u << 21);
PIOC->PIO_SODR = (1u << 22);
}
DEC_mode_steps = MICROSteps / mod;
}
delayMicroseconds(5); // Makes sure the DRV8825 can follow
}
void SoundOn(int note, int duration) {
duration *= 10000;
long elapsed_time = 0;
while (elapsed_time < duration) {
digitalWrite(speakerOut, HIGH);
delayMicroseconds(note / 2);
// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(note / 2);
// Keep track of how long we pulsed
elapsed_time += (note);
}
}
void UpdateObservedObjects() {
// Write down the Observed objects information: --- USED in the STATS screen and sent to BT as status.
int Delta_Time = (((String(rtc.getTimeStr()).substring(0, 2).toInt()) * 60) + (String(rtc.getTimeStr()).substring(3, 5).toInt())) - ((Prev_Obj_Start.substring(0, 2).toInt() * 60) + Prev_Obj_Start.substring(3).toInt());
if (Delta_Time < 0) {
Delta_Time += 1440;
}
ObservedObjects[Observed_Obj_Count - 1] += ";" + String(Delta_Time);
ObservedObjects[Observed_Obj_Count] = OBJECT_NAME + ";" + OBJECT_DETAILS + ";" + String(rtc.getTimeStr()).substring(0, 5) + ";" + int(_temp) + ";" + int(_humid) + ";" + int(HAHour) + "h " + HAMin + "m;" + int(ALT);
Observed_Obj_Count += 1;
Prev_Obj_Start = String(rtc.getTimeStr()).substring(0, 5);
}
void Current_RA_DEC() {
//curr_RA_H, curr_RA_M, curr_RA_S, curr_DEC_D, curr_DEC_M, curr_DEC_S;
// curr_RA_lz, curr_DEC_lz, curr_HA_lz;
// DEC
// To ALSO correct for the Star Alignment offset
float tmp_dec = (float(DEC_90) - float(abs(DEC_microSteps))) / float(DEC_D_CONST);
tmp_dec -= delta_a_DEC;
int sDEC_tel = 0;
if (tmp_dec < 0) {
sDEC_tel = 45;
} else {
sDEC_tel = 43;
}
if (tmp_dec > 0) {
curr_DEC_D = floor(tmp_dec);
} else {
curr_DEC_D = ceil(tmp_dec);
}
curr_DEC_M = (tmp_dec - floor(curr_DEC_D)) * 60;
curr_DEC_S = (curr_DEC_M - floor(curr_DEC_M)) * 60;
sprintf(curr_DEC_lz, "%c%02d%c%02d:%02d", sDEC_tel, int(abs(curr_DEC_D)), 223, int(abs(curr_DEC_M)), int(curr_DEC_S));
// HOUR ANGLE
// To correct for the Star Alignment
double tmp_ha = double(RA_microSteps) / double(HA_H_CONST);
tmp_ha -= delta_a_RA;
if (DEC_microSteps > 0) {
tmp_ha += 180;
}
tmp_ha /= 15;
float tmp_ha_h = 0;
float tmp_ha_m = 0;
float tmp_ha_s = 0;
tmp_ha_h = floor(tmp_ha);
tmp_ha_m = (tmp_ha - floor(tmp_ha)) * 60;
tmp_ha_s = (tmp_ha_m - floor(tmp_ha_m)) * 60;
sprintf(curr_HA_lz, "%02d:%02d:%02d", int(tmp_ha_h), int(tmp_ha_m), int(tmp_ha_s));
// RIGHT ASC.
double tmp_ra = LST - tmp_ha;
if (LST < tmp_ha) {
tmp_ra += 24;
}
float tmp_ra_h = 0;
float tmp_ra_m = 0;
float tmp_ra_s = 0;
curr_RA_H = floor(tmp_ra);
curr_RA_M = (tmp_ra - curr_RA_H) * 60;
curr_RA_S = (curr_RA_M - floor(curr_RA_M)) * 60;
sprintf(curr_RA_lz, "%02d:%02d:%02d", int(curr_RA_H), int(curr_RA_M), int(curr_RA_S));
}
void DrawButton(int X, int Y, int Width, int Height, String Caption, int16_t BodyColor, int16_t BorderColor, int16_t TextColor, int tSize) {
// TYPE: 0:Solid color, no Frame; 1: Frame Only button; 2: Solid color and Frame button;
if ((BodyColor != 0) && (BorderColor == 0)) {
// Button Type = 0 ... Solid color, no Frame
tft.fillRect(X, Y, Width, Height, BodyColor);
} else if ((BodyColor == 0) && (BorderColor != 0)) {
// Button Type = 1 ... Frame Only button
tft.drawRect(X, Y, Width, Height, BorderColor);
tft.fillRect(X + 1, Y + 1, Width - 2, Height - 2, BLACK);
} else if ((BodyColor != 0) && (BorderColor != 0)) {
// Button Type = 1 ... Frame Only button
tft.drawRect(X, Y, Width, Height, BorderColor);
tft.fillRect(X + 1, Y + 1, Width - 2, Height - 2, BodyColor);
} else {
// Will not Draw Button and will return to code!
return;
}
float TX = 0;
float TY = 0;
if (tSize == 2) { // 10 x 14 px. (W x H)
TX = (X + 1 + Width / 2) - (Caption.length() * 6);
TY = Y + Height / 2 - 5;
} else if (tSize == 1) { // 5 x 7 px. (W x H)
TX = (X + 1 + Width / 2) - (Caption.length() * 3);
TY = Y + Height / 2 - 3 ;
} else if (tSize == 3) { // 15 x 21 px. (W x H)
TX = (X + 1 + Width / 2) - (Caption.length() * 8);
TY = Y + Height / 2 - 10;
}
tft.setCursor((int)TX, (int)TY);
tft.setTextSize(tSize);
tft.setTextColor(TextColor);
if (Caption == "+") {
TX -= 5;
tft.drawLine((int)TX + 10, (int)TY - 5, (int)TX + 10, (int)TY - 5, TextColor);
tft.drawLine((int)TX + 8, (int)TY - 4, (int)TX + 12, (int)TY - 4, TextColor);
tft.drawLine((int)TX + 6, (int)TY - 3, (int)TX + 14, (int)TY - 3, TextColor);
tft.drawLine((int)TX + 4, (int)TY - 2, (int)TX + 16, (int)TY - 2, TextColor);
tft.drawLine((int)TX + 2, (int)TY - 1, (int)TX + 18, (int)TY - 1, TextColor);
tft.drawLine((int)TX, (int)TY, (int)TX + 20, (int)TY, TextColor);
tft.drawLine((int)TX - 2, (int)TY + 1, (int)TX + 22, (int)TY + 1, TextColor);
tft.drawLine((int)TX - 4, (int)TY + 2, (int)TX + 24, (int)TY + 2, TextColor);
tft.drawLine((int)TX - 4, (int)TY + 8, (int)TX + 24, (int)TY + 8, TextColor);
tft.drawLine((int)TX - 2, (int)TY + 9, (int)TX + 22, (int)TY + 9, TextColor);
tft.drawLine((int)TX, (int)TY + 10, (int)TX + 20, (int)TY + 10, TextColor);
tft.drawLine((int)TX + 2, (int)TY + 11, (int)TX + 18, (int)TY + 11, TextColor);
tft.drawLine((int)TX + 4, (int)TY + 12, (int)TX + 16, (int)TY + 12, TextColor);
tft.drawLine((int)TX + 6, (int)TY + 13, (int)TX + 14, (int)TY + 13, TextColor);
tft.drawLine((int)TX + 8, (int)TY + 14, (int)TX + 12, (int)TY + 14, TextColor);
tft.drawLine((int)TX + 10, (int)TY + 15, (int)TX + 10, (int)TY + 15, TextColor);
} else {
tft.println(Caption);
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define BUFFPIXEL 80
void bmpDraw(char *filename, uint8_t x, uint16_t y) {
File bmpFile;
int bmpWidth, bmpHeight; // W+H in pixels
uint8_t bmpDepth; // Bit depth (currently must be 24)
uint32_t bmpImageoffset; // Start of image data in file
uint32_t rowSize; // Not always = bmpWidth; may have padding
uint8_t sdbuffer[3 * BUFFPIXEL]; // pixel buffer (R+G+B per pixel)
uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer
boolean goodBmp = false; // Set to true on valid header parse
boolean flip = true; // BMP is stored bottom-to-top
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if ((x >= tft.width()) || (y >= tft.height())) return;
// Open requested file on SD card
if ((bmpFile = SD.open(filename)) == NULL)
{
#ifdef serial_debug
Serial.println("file not found on the SD card");
#endif
return;
}
// Parse BMP header
if (read16(bmpFile) == 0x4D42 || read16(bmpFile) == 0x424D) // BMP signature
{
#ifdef serial_debug
Serial.print("BMP file found.\nImage offset: ");
Serial.println(read32(bmpFile), HEX);
#else
read32(bmpFile); //dummy read
#endif
(void)read32(bmpFile); // Read & ignore creator bytes
bmpImageoffset = read32(bmpFile); // Start of image data
// Read DIB header
#ifdef serial_debug
Serial.print("Image DIB header: ");
Serial.println(read32(bmpFile), HEX);
#else
read32(bmpFile); //dummy read
#endif
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if (read16(bmpFile) == 1) // # planes -- must be '1'
{
bmpDepth = read16(bmpFile); // bits per pixel
#ifdef serial_debug
Serial.println("one plane file");
Serial.print("bmpDepth: ");
Serial.println(bmpDepth);
#endif
if ((bmpDepth == 24) && (read32(bmpFile) == 0)) // 0 = uncompressed
{
goodBmp = true; // Supported BMP format -- proceed!
// BMP rows are padded (if needed) to 4-byte boundary
rowSize = (bmpWidth * 3 + 3) & ~3;
// If bmpHeight is negative, image is in top-down order.
// This is not canon but has been observed in the wild.
if (bmpHeight < 0)
{
bmpHeight = -bmpHeight;
flip = false;
}
// Crop area to be loaded
w = bmpWidth;
h = bmpHeight;
if ((x + w - 1) >= tft.width()) w = tft.width() - x;
if ((y + h - 1) >= tft.height()) h = tft.height() - y;
// Set TFT address window to clipped image bounds
tft.setAddrWindow(x, y, x + w - 1, y + h - 1);
for (row = 0; row < h; row++) { // For each scanline...
if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else // Bitmap is stored top-to-bottom
pos = bmpImageoffset + row * rowSize;
if (bmpFile.position() != pos)
{ // Need seek?
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer); // Force buffer reload
}
for (col = 0; col < w; col++) { // For each pixel...
// Time to read more pixel data?
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
}
// Convert pixel from BMP to TFT format, push to display
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft.pushColor(tft.color565(r, g, b));
} // end pixel
} // end scanline
} // end goodBmp
}
}
else
{
#ifdef serial_debug
Serial.println("invalid BMP file signature");
#endif
}
bmpFile.close();
}
// These read 16- and 32-bit types from the SD card file.
// BMP data is stored little-endian, Arduino is little-endian too.
// May need to reverse subscript order if porting elsewhere.
uint16_t read16(File &f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;