-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathantenna_array_controller.ino
executable file
·1582 lines (1266 loc) · 50.8 KB
/
antenna_array_controller.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
/* Arduino Antenna Array Controller
Anthony Good
K3NG
***************************************************************************************************************
This program is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
http://creativecommons.org/licenses/by-nc-sa/3.0/
http://creativecommons.org/licenses/by-nc-sa/3.0/legalcode
***************************************************************************************************************
Revision History
1.0.2016100301
Added PIN_ACTIVE_STATE and PIN_INACTIVE_STATE settings in antenna_array_controller_settings.h
1.0.2016100601
Added optional outputs for Comtek Four Square ACB-4 series in antenna_array_controller_pins.h: comtek_45_135_225_315_bit_0 comtek_45_135_225_315_bit_1
1.0.2016101301
Fixed bugs with optional outputs for Comtek Four Square ACB-4 series
*/
#define CODE_VERSION "1.0.2016101301"
#include <avr/pgmspace.h>
#include <EEPROM.h>
#include <math.h>
#include <avr/wdt.h>
#include <LiquidCrystal.h> // required for classic 4 bit interface LCD display
//#include <Adafruit_MCP23017.h> // required for Adafruit I2C LCD display
//#include <Adafruit_RGBLCDShield.h> // required for Adafruit I2C LCD display
//#include <LiquidCrystal_I2C.h> // required for YourDuino.com or DFRobot I2C LCD display
//#include <LCD.h> // required for YourDuino.com I2C LCD display
#include "antenna_array_controller.h"
#include "antenna_array_controller_features.h"
#include "antenna_array_controller_pins.h"
#include "antenna_array_controller_settings.h"
#if !defined(FEATURE_YAESU_EMULATION) && !defined(FEATURE_EASYCOM_EMULATION)
#error "You need to activate FEATURE_YAESU_EMULATION or FEATURE_EASYCOM_EMULATION"
#endif
//#ifdef FEATURE_REMOTE_UNIT_INTERFACE
HardwareSerial * remote_unit_port;
//#endif //FEATURE_REMOTE_UNIT_INTERFACE
/* antenna and pin definitions */
const float ranges[] = {0, 180, 360}; // azimuths: 90, 270
const int pins[] = {0,0,0};
const byte number_of_positions = 2;
// const float ranges[] = {90, 270, 450}; // azimuths: 180, 0
// const int pins[] = {0,0};
// const byte number_of_positions = 2;
// const float ranges[] = {0, 90, 180, 270, 360}; // azimuths: 45, 135, 225, 315
// const int pins[] = {0,0,0,0};
// const byte number_of_positions = 4;
// const float ranges[] = {0, 45, 90, 135, 180, 215, 270, 315, 360}; // azimuths: 22.5, 67.5, 112.5, 157.5, 202.5, 247.5, 292.5, 337.5
// const int pins[] = {0,0,0,0,0,0,0,0};
// const byte number_of_positions = 8;
// const float ranges[] = {22.5, 67.5, 112.5, 157.5, 202.5, 247.5, 292.5, 337.5, 382.5}; // azimuths: 45, 90, 135, 180, 225, 270, 315, 0
// const int pins[] = {0,0,0,0,0,0,0,0};
// const byte number_of_positions = 8;
/*----------------------- variables -------------------------------------*/
int current_antenna_position = 1;
byte configuration_dirty = 0;
unsigned long last_serial_receive_time = 0;
int azimuth = 0;
byte incoming_serial_byte = 0;
byte serial0_buffer[COMMAND_BUFFER_SIZE];
int serial0_buffer_index = 0;
byte debug_mode = DEFAULT_DEBUG_STATE;
unsigned long last_debug_output_time = 0;
byte backslash_command = 0;
struct config_t {
byte magic_number;
float last_azimuth;
} configuration;
#ifdef FEATURE_LCD_DISPLAY
unsigned long last_lcd_update = 0;
String last_direction_string;
byte push_lcd_update = 0;
#define LCD_COLUMNS 16
//#define LCD_COLUMNS 20
byte lcd_state_row_0 = LCD_UNDEF;
byte lcd_state_row_1 = LCD_UNDEF;
#ifdef FEATURE_I2C_LCD
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
byte lcdcolor = GREEN; // default color of I2C LCD display
#endif //FEATURE_I2C_LCD
#endif //FEATURE_LCD_DISPLAY
#ifdef FEATURE_ROTARY_ENCODER_CONTROL
#ifdef OPTION_ENCODER_HALF_STEP_MODE // Use the half-step state table (emits a code at 00 and 11)
const unsigned char ttable[6][4] = {
{0x3 , 0x2, 0x1, 0x0}, {0x23, 0x0, 0x1, 0x0},
{0x13, 0x2, 0x0, 0x0}, {0x3 , 0x5, 0x4, 0x0},
{0x3 , 0x3, 0x4, 0x10}, {0x3 , 0x5, 0x3, 0x20}
};
#else // Use the full-step state table (emits a code at 00 only)
const unsigned char ttable[7][4] = {
{0x0, 0x2, 0x4, 0x0}, {0x3, 0x0, 0x1, 0x10},
{0x3, 0x2, 0x0, 0x0}, {0x3, 0x2, 0x1, 0x0},
{0x6, 0x0, 0x4, 0x0}, {0x6, 0x5, 0x0, 0x10},
{0x6, 0x5, 0x4, 0x0},
};
#endif //OPTION_ENCODER_HALF_STEP_MODE
#endif //FEATURE_ROTARY_ENCODER_CONTROL
/* ------------------ let's start doing some stuff now that we got the formalities out of the way --------------------*/
void setup() {
delay(1000);
initialize_serial();
read_settings_from_eeprom();
initialize_pins();
#ifdef FEATURE_LCD_DISPLAY
initialize_display();
#endif
#ifdef FEATURE_YAESU_EMULATION
report_current_azimuth(); // Yaesu - report the azimuth right off the bat without a C command; the Arduino doesn't wake up quick enough
// to get first C command from HRD and if HRD doesn't see anything it doesn't connect
#endif //FEATURE_YAESU_EMULATION
}
/*-------------------------- here's where the magic happens --------------------------------*/
void loop() {
check_serial();
#ifdef FEATURE_LCD_DISPLAY
update_display();
#endif
#ifdef FEATURE_ROTARY_ENCODER_CONTROL
check_rotary_encoder();
#endif
check_buttons();
output_debug();
check_for_dirty_configuration();
service_blink_led();
}
/* -------------------------------------- subroutines -----------------------------------------------
Where the real work happens...
*/
#ifdef FEATURE_ROTARY_ENCODER_CONTROL
void check_rotary_encoder(){
static unsigned char az_encoder_state = 0;
az_encoder_state = ttable[az_encoder_state & 0xf][((digitalRead(rotary_encoder_pin2) << 1) | digitalRead(rotary_encoder_pin1))];
unsigned char az_encoder_result = az_encoder_state & 0x30;
#ifdef DEBUG_ENCODER
static byte last_az_rotary_preset_pin1 = 0;
static byte last_az_rotary_preset_pin2 = 0;
if ((debug_mode) && (( last_az_rotary_preset_pin1 != digitalRead(rotary_encoder_pin1)) || ( last_az_rotary_preset_pin2 != digitalRead(rotary_encoder_pin2)))) {
Serial.print(F("check_preset_encoders: "));
Serial.print(last_az_rotary_preset_pin1);
Serial.print(last_az_rotary_preset_pin2);
Serial.print(digitalRead(rotary_encoder_pin1));
Serial.print(digitalRead(rotary_encoder_pin2));
Serial.print(F(" az_encoder_state: "));
Serial.print(az_encoder_state,HEX);
Serial.print(F(" encoder_result: "));
Serial.println(az_encoder_result,HEX);
}
last_az_rotary_preset_pin1 = digitalRead(rotary_encoder_pin1);
last_az_rotary_preset_pin2 = digitalRead(rotary_encoder_pin2);
#endif //DEBUG_ENCODER
if (az_encoder_result) { // If rotary encoder modified
if (az_encoder_result == DIR_CW) {
#ifdef DEBUG_ENCODER
if (debug_mode){
Serial.println(F("check_preset_encoders: az CW"));
}
#endif //DEBUG_ENCODER
submit_request(AZ,REQUEST_CW,0);
}
if (az_encoder_result == DIR_CCW) {
#ifdef DEBUG_ENCODER
if (debug_mode){
Serial.println(F("check_preset_encoders: az CCW"));
}
#endif //DEBUG_ENCODER
submit_request(AZ,REQUEST_CCW,0);
}
}
}
#endif //FEATURE_ROTARY_ENCODER_CONTROL
//--------------------------------------------------------------
void service_blink_led(){
static unsigned long last_blink_led_transition = 0;
static byte blink_led_status = 0;
#ifdef blink_led
if ((millis() - last_blink_led_transition) >= 1000){
if (blink_led_status){
digitalWriteEnhanced(blink_led,LOW);
blink_led_status = 0;
} else {
digitalWriteEnhanced(blink_led,HIGH);
blink_led_status = 1;
}
last_blink_led_transition = millis();
}
#endif //blink_led
}
//--------------------------------------------------------------
#ifdef FEATURE_YAESU_EMULATION
void yaesu_serial_command(){
if (incoming_serial_byte == 10) { return; } // ignore carriage returns
if ((incoming_serial_byte != 13) && (serial0_buffer_index < COMMAND_BUFFER_SIZE)) { // if it's not a carriage return, add it to the buffer
serial0_buffer[serial0_buffer_index] = incoming_serial_byte;
serial0_buffer_index++;
} else { // we got a carriage return, time to get to work on the command
if ((serial0_buffer[0] > 96) && (serial0_buffer[0] < 123)) {
serial0_buffer[0] = serial0_buffer[0] - 32;
}
switch (serial0_buffer[0]) { // look at the first character of the command
case 'C': // C - return current azimuth
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: C cmd"));}
#endif //DEBUG_SERIAL
#ifdef OPTION_DELAY_C_CMD_OUTPUT
delay(400);
#endif
report_current_azimuth();
break;
case 'F': // F - full scale calibration
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: F cmd"));}
#endif //DEBUG_SERIAL
yaesu_f_command();
break;
case 'H': print_help(); break; // H - print help (simulated Yaesu GS-232A help - not all commands are supported
case 'L': // L - manual left (CCW) rotation
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: L cmd"));}
#endif //DEBUG_SERIAL
submit_request(AZ,REQUEST_CCW,0);
Serial.println();
break;
case 'O': // O - offset calibration
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: O cmd"));}
#endif //DEBUG_SERIAL
yaesu_o_command();
break;
case 'R': // R - manual right (CW) rotation
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: R cmd"));}
#endif //DEBUG_SERIAL
submit_request(AZ,REQUEST_CW,0);
Serial.println();
break;
case 'A': // A - CW/CCW rotation stop
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: A cmd"));}
#endif //DEBUG_SERIAL
submit_request(AZ,REQUEST_STOP,0);
Serial.println();
break;
case 'S': // S - all stop
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: S cmd"));}
#endif //DEBUG_SERIAL
submit_request(AZ,REQUEST_STOP,0);
#ifdef FEATURE_ELEVATION_CONTROL
submit_request(EL,REQUEST_STOP,0);
#endif
#ifdef FEATURE_TIMED_BUFFER
clear_timed_buffer();
#endif //FEATURE_TIMED_BUFFER
Serial.println();
break;
case 'M': // M - auto azimuth rotation
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: M cmd"));}
#endif //DEBUG_SERIAL
yaesu_m_command();
break;
#ifdef FEATURE_TIMED_BUFFER
case 'N': // N - number of loaded timed interval entries
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: N cmd"));}
#endif //DEBUG_SERIAL
Serial.println(timed_buffer_number_entries_loaded);
break;
#endif //FEATURE_TIMED_BUFFER
#ifdef FEATURE_TIMED_BUFFER
case 'T': initiate_timed_buffer(); break; // T - initiate timed tracking
#endif //FEATURE_TIMED_BUFFER
case 'X': // X - azimuth speed change
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: X cmd"));}
#endif //DEBUG_SERIAL
yaesu_x_command();
break;
#ifdef FEATURE_ELEVATION_CONTROL
case 'U': // U - manual up rotation
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: U cmd"));}
#endif //DEBUG_SERIAL
submit_request(EL,REQUEST_UP,0);
Serial.println();
break;
case 'D': // D - manual down rotation
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: D cmd"));}
#endif //DEBUG_SERIAL
submit_request(EL,REQUEST_DOWN,0);
Serial.println();
break;
case 'E': // E - stop elevation rotation
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: E cmd"));}
#endif //DEBUG_SERIAL
submit_request(EL,REQUEST_STOP,0);
Serial.println();
break;
case 'B': report_current_elevation(); break; // B - return current elevation
#endif
case 'W': // W - auto elevation rotation
#ifdef DEBUG_SERIAL
if (debug_mode) {Serial.println(F("yaesu_serial_command: W cmd"));}
#endif //DEBUG_SERIAL
yaesu_w_command();
break;
#ifdef OPTION_GS_232B_EMULATION
case 'P': yaesu_p_command(); break; // P - switch between 360 and 450 degree mode
case 'Z': // Z - Starting point toggle
break;
#endif
default:
Serial.println(F("?>"));
#ifdef DEBUG_SERIAL
if (debug_mode) {
Serial.print(F("check_serial: serial0_buffer_index: "));
Serial.println(serial0_buffer_index);
for (int debug_x = 0; debug_x < serial0_buffer_index; debug_x++) {
Serial.print(F("check_serial: serial0_buffer["));
Serial.print(debug_x);
Serial.print(F("]: "));
Serial.print(serial0_buffer[debug_x]);
Serial.print(F(" "));
Serial.write(serial0_buffer[debug_x]);
Serial.println();
}
}
#endif //DEBUG_SERIAL
}
clear_command_buffer();
}
}
#endif //FEATURE_YAESU_EMULATION
//--------------------------------------------------------------
void clear_command_buffer(){
serial0_buffer_index = 0;
serial0_buffer[0] = 0;
}
//--------------------------------------------------------------
#ifdef FEATURE_EASYCOM_EMULATION
void easycom_serial_commmand(){
/* Easycom protocol implementation
Implemented commands:
Command Meaning Parameters
------- ------- ----------
AZ Azimuth number - 1 decimal place
EL Elevation number - 1 decimal place
ML Move Left
MR Move Right
MU Move Up
MD Move Down
SA Stop azimuth moving
SE Stop elevation moving
VE Request Version
Easycom has no way to report azimuth or elevation back to the client, or report errors
*/
float heading = -1;
if ((incoming_serial_byte != 13) && (incoming_serial_byte != 10) && (incoming_serial_byte != 32) && (serial0_buffer_index < COMMAND_BUFFER_SIZE)){ // if it's not a CR, LF, or space, add it to the buffer
if ((incoming_serial_byte > 96) && (incoming_serial_byte < 123)) {incoming_serial_byte = incoming_serial_byte - 32;} //uppercase it
serial0_buffer[serial0_buffer_index] = incoming_serial_byte;
serial0_buffer_index++;
} else { // time to get to work on the command
if (serial0_buffer_index){
switch (serial0_buffer[0]) { // look at the first character of the command
case 'A': //AZ
if (serial0_buffer[1] == 'Z'){ // format is AZx.x or AZxx.x or AZxxx.x (why didn't they make it fixed length?)
switch (serial0_buffer_index) {
#ifdef OPTION_EASYCOM_AZ_QUERY_COMMAND
case 2:
Serial.print("AZ");
Serial.println(float(azimuth*HEADING_MULTIPLIER),1);
clear_command_buffer();
return;
break;
#endif //OPTION_EASYCOM_AZ_QUERY_COMMAND
case 5: // format AZx.x
heading = (serial0_buffer[2]-48) + ((serial0_buffer[4]-48)/10);
break;
case 6: // format AZxx.x
heading = ((serial0_buffer[2]-48)*10) + (serial0_buffer[3]-48) + ((serial0_buffer[5]-48)/10);
break;
case 7: // format AZxxx.x
heading = ((serial0_buffer[2]-48)*100) + ((serial0_buffer[3]-48)*10) + (serial0_buffer[4]-48) + ((serial0_buffer[6]-48)/10);
break;
//default: Serial.println("?"); break;
}
if (((heading >= 0) && (heading < 451)) && (serial0_buffer[serial0_buffer_index-2] == '.')){
submit_request(AZ,REQUEST_AZIMUTH,(heading*HEADING_MULTIPLIER));
} else {
Serial.println("?");
}
} else {
Serial.println("?");
}
break;
case 'S': // SA or SE - stop azimuth, stop elevation
switch (serial0_buffer[1]) {
case 'A':
submit_request(AZ,REQUEST_STOP,0);
break;
#ifdef FEATURE_ELEVATION_CONTROL
case 'E':
submit_request(EL,REQUEST_STOP,0);
break;
#endif //FEATURE_ELEVATION_CONTROL
default: Serial.println("?"); break;
}
break;
case 'M': // ML, MR, MU, MD - move left, right, up, down
switch (serial0_buffer[1]){
case 'L': // ML - move left
submit_request(AZ,REQUEST_CCW,0);
break;
case 'R': // MR - move right
submit_request(AZ,REQUEST_CW,0);
break;
default: Serial.println(F("?")); break;
}
break;
case 'V': // VE - version query
if (serial0_buffer[1] == 'E') {Serial.println(F("VE002"));} // not sure what to send back, sending 002 because this is easycom version 2?
break;
default: Serial.println("?"); break;
}
}
clear_command_buffer();
}
}
#endif //FEATURE_EASYCOM_EMULATION
//--------------------------------------------------------------
void check_serial(){
if (Serial.available()) {
if (serial_led) {
digitalWriteEnhanced(serial_led, HIGH); // blink the LED just to say we got something
}
incoming_serial_byte = Serial.read();
last_serial_receive_time = millis();
if ((incoming_serial_byte == 92) && (serial0_buffer_index == 0)) { // do we have a backslash command?
serial0_buffer[serial0_buffer_index] = incoming_serial_byte;
serial0_buffer_index++;
backslash_command = 1;
return;
}
if (backslash_command) {
if (incoming_serial_byte == 13) { // do we have a carriage return?
switch(serial0_buffer[1]){
case 'D': if (debug_mode) {debug_mode = 0;} else {debug_mode = 1;} break; // D - Debug
case 'E' : // E - Initialize eeprom
initialize_eeprom_with_defaults();
Serial.println(F("Initialized eeprom, please reset..."));
break;
case 'L': // L - rotate to long path
if (azimuth < (180)){
submit_request(AZ,REQUEST_AZIMUTH,(azimuth+180));
} else {
submit_request(AZ,REQUEST_AZIMUTH,(azimuth-180));
}
break;
#ifdef FEATURE_ANCILLARY_PIN_CONTROL
case 'N' : // \Nxx - turn pin on; xx = pin number
if ((((serial0_buffer[2] > 47) && (serial0_buffer[2] < 58)) || (toupper(serial0_buffer[2]) == 'A')) && (serial0_buffer[3] > 47) && (serial0_buffer[3] < 58) && (serial0_buffer_index == 4)){
byte pin_value = 0;
if (toupper(serial0_buffer[2]) == 'A'){
pin_value = get_analog_pin(serial0_buffer[3]-48);
} else {
pin_value = ((serial0_buffer[2]-48)*10) + (serial0_buffer[3]-48);
}
pinModeEnhanced(pin_value,OUTPUT);
digitalWriteEnhanced(pin_value,PIN_ACTIVE_STATE);
Serial.println("OK");
} else {
Serial.println(F("Error"));
}
break;
case 'F' : // \Fxx - turn pin off; xx = pin number
if ((((serial0_buffer[2] > 47) && (serial0_buffer[2] < 58)) || (toupper(serial0_buffer[2]) == 'A')) && (serial0_buffer[3] > 47) && (serial0_buffer[3] < 58) && (serial0_buffer_index == 4)){
byte pin_value = 0;
if (toupper(serial0_buffer[2]) == 'A'){
pin_value = get_analog_pin(serial0_buffer[3]-48);
} else {
pin_value = ((serial0_buffer[2]-48)*10) + (serial0_buffer[3]-48);
}
pinModeEnhanced(pin_value,OUTPUT);
digitalWriteEnhanced(pin_value,PIN_INACTIVE_STATE);
Serial.println("OK");
} else {
Serial.println(F("Error"));
}
break;
case 'W' : // \Wxxyyy - turn on pin PWM; xx = pin number, yyy = PWM value (0-255)
if (((serial0_buffer[2] > 47) && (serial0_buffer[2] < 58)) && (serial0_buffer[3] > 47) && (serial0_buffer[3] < 58) && (serial0_buffer_index == 7)){
byte pin_value = 0;
if (toupper(serial0_buffer[2]) == 'A'){
pin_value = get_analog_pin(serial0_buffer[3]-48);
} else {
pin_value = ((serial0_buffer[2]-48)*10) + (serial0_buffer[3]-48);
}
int write_value = ((serial0_buffer[4]-48)*100) + ((serial0_buffer[5]-48)*10) + (serial0_buffer[6]-48);
if ((write_value >= 0) && (write_value < 256)){
pinModeEnhanced(pin_value,OUTPUT);
analogWrite(pin_value,write_value);
Serial.println("OK");
} else {
Serial.println(F("Error"));
}
} else {
Serial.println(F("Error"));
}
break;
#endif //FEATURE_ANCILLARY_PIN_CONTROL
default: Serial.println(F("error"));
}
clear_command_buffer();
backslash_command = 0;
} else { // no, add the character to the buffer
if ((incoming_serial_byte > 96) && (incoming_serial_byte < 123)) {incoming_serial_byte = incoming_serial_byte - 32;} //uppercase it
if (incoming_serial_byte != 10) { // add it to the buffer if it's not a line feed
serial0_buffer[serial0_buffer_index] = incoming_serial_byte;
serial0_buffer_index++;
}
}
} else {
#ifdef FEATURE_YAESU_EMULATION
yaesu_serial_command();
#endif //FEATURE_YAESU_EMULATION
#ifdef FEATURE_EASYCOM_EMULATION
easycom_serial_commmand();
#endif //FEATURE_EASYCOM_EMULATION
}
if (serial_led) {
digitalWriteEnhanced(serial_led, LOW);
}
} //if (Serial.available())
}
//--------------------------------------------------------------
#ifdef FEATURE_LCD_DISPLAY
char *azimuth_direction(int azimuth_in){
//azimuth_in = azimuth_in / HEADING_MULTIPLIER;
if (azimuth_in > 348) {return "N";}
if (azimuth_in > 326) {return "NNW";}
if (azimuth_in > 303) {return "NW";}
if (azimuth_in > 281) {return "WNW";}
if (azimuth_in > 258) {return "W";}
if (azimuth_in > 236) {return "WSW";}
if (azimuth_in > 213) {return "SW";}
if (azimuth_in > 191) {return "SSW";}
if (azimuth_in > 168) {return "S";}
if (azimuth_in > 146) {return "SSE";}
if (azimuth_in > 123) {return "SE";}
if (azimuth_in > 101) {return "ESE";}
if (azimuth_in > 78) {return "E";}
if (azimuth_in > 56) {return "ENE";}
if (azimuth_in > 33) {return "NE";}
if (azimuth_in > 11) {return "NNE";}
return "N";
}
#endif
//--------------------------------------------------------------
#ifdef FEATURE_LCD_DISPLAY
void update_display()
{
// update the LCD display
String direction_string;
static int last_azimuth = -1;
char workstring[7];
if (lcd_state_row_0 == LCD_SPLASH){
if ((millis()-last_lcd_update) < 2000){
return;
} else {
lcd_state_row_0 = LCD_UNDEF;
lcd_state_row_1 = LCD_UNDEF;
}
}
// row 0 ------------------------------------------------------------
if (((millis() - last_lcd_update) > LCD_UPDATE_TIME) || (push_lcd_update)){
if ((lcd_state_row_0 == LCD_UNDEF) && (lcd_state_row_1 == LCD_UNDEF)){
lcd.clear();
lcd.setCursor(((LCD_COLUMNS - direction_string.length())/2),0);
lcd.print(direction_string);
lcd_state_row_0 = LCD_DIRECTION;
}
if ((last_azimuth != azimuth) || (lcd_state_row_0 != LCD_DIRECTION)){
direction_string = azimuth_direction(azimuth);
if ((last_direction_string == direction_string) || (lcd_state_row_0 != LCD_DIRECTION)) {
clear_display_row(0);
lcd.setCursor(((LCD_COLUMNS - direction_string.length())/2),0);
lcd.print(direction_string);
lcd_state_row_0 = LCD_DIRECTION;
#ifdef DEBUG_DISPLAY
if (debug_mode) {
Serial.print(F("update_display: "));
Serial.println(direction_string);
}
#endif //DEBUG_DISPLAY
} else {
lcd.setCursor(((LCD_COLUMNS - direction_string.length())/2)-1,0);
lcd.print(" ");
lcd.print(direction_string);
lcd.print(" ");
#ifdef DEBUG_DISPLAY
if (debug_mode) {
Serial.print(F("update_display: row 0: "));
Serial.println(direction_string);
}
#endif //DEBUG_DISPLAY
}
}
push_lcd_update = 0;
}
// row 1 --------------------------------------------
if ((millis()-last_lcd_update) > LCD_UPDATE_TIME) {
if (last_azimuth != azimuth) {
clear_display_row(1);
direction_string = "Azimuth ";
dtostrf(azimuth,1,0,workstring);
direction_string.concat(workstring);
direction_string.concat(char(223));
lcd.setCursor(((LCD_COLUMNS - direction_string.length())/2),1);
//lcd.setCursor(0,2);
//lcd.print("test123");
lcd.print(direction_string);
#ifdef DEBUG_DISPLAY
if (debug_mode) {
Serial.print(F("update_display: row 1: "));
Serial.println(direction_string);
}
#endif //DEBUG_DISPLAY
last_azimuth = azimuth;
lcd_state_row_1 = LCD_HEADING;
}
}
if ((millis() - last_lcd_update) > LCD_UPDATE_TIME) {last_lcd_update = millis();}
last_direction_string = direction_string;
}
#endif
//--------------------------------------------------------------
#ifdef FEATURE_LCD_DISPLAY
void clear_display_row(byte row_number)
{
lcd.setCursor(0,row_number);
for (byte x = 0; x < LCD_COLUMNS; x++) {
lcd.print(" ");
}
}
#endif
//--------------------------------------------------------------
void get_keystroke()
{
while (Serial.available() == 0) {}
while (Serial.available() > 0) {
incoming_serial_byte = Serial.read();
}
}
//--------------------------------------------------------------
#ifdef FEATURE_YAESU_EMULATION
void yaesu_x_command() {
if (serial0_buffer_index > 1) {
switch (serial0_buffer[1]) {
case '4':
case '3':
case '2':
case '1':
Serial.println(F("Speed commands unsupported..."));
break;
default: Serial.println(F("?>")); break;
}
} else {
Serial.println(F("?>"));
}
}
#endif //FEATURE_YAESU_EMULATION
//--------------------------------------------------------------
#ifdef FEATURE_YAESU_EMULATION
#ifdef OPTION_GS_232B_EMULATION
void yaesu_p_command()
{
if ((serial0_buffer[1] == '3') && (serial0_buffer_index > 2)) { // P36 command
// configuration.azimuth_rotation_capability = 360;
// Serial.print(F("Mode 360 degree\r\n"));
// write_settings_to_eeprom();
} else {
if ((serial0_buffer[1] == '4') && (serial0_buffer_index > 2)) { // P45 command
// configuration.azimuth_rotation_capability = 450;
// Serial.print(F("Mode 450 degree\r\n"));
// write_settings_to_eeprom();
} else {
Serial.println(F("?>"));
}
}
}
#endif //OPTION_GS_232B_EMULATION
#endif //FEATURE_YAESU_EMULATION
//--------------------------------------------------------------
#ifdef FEATURE_YAESU_EMULATION
void yaesu_o_command()
{
clear_serial_buffer();
}
#endif //FEATURE_YAESU_EMULATION
//--------------------------------------------------------------
#ifdef FEATURE_YAESU_EMULATION
void print_wrote_to_memory(){
Serial.println(F("Wrote to memory"));
}
#endif //FEATURE_YAESU_EMULATION
//--------------------------------------------------------------
#ifdef FEATURE_YAESU_EMULATION
void clear_serial_buffer(){
delay(200);
while (Serial.available()) {incoming_serial_byte = Serial.read();}
}
#endif //FEATURE_YAESU_EMULATION
//--------------------------------------------------------------
#ifdef FEATURE_YAESU_EMULATION
void yaesu_f_command()
{
clear_serial_buffer();
}
#endif //FEATURE_YAESU_EMULATION
//--------------------------------------------------------------
void read_settings_from_eeprom()
{
//EEPROM_readAnything(0,configuration);
byte* p = (byte*)(void*)&configuration;
unsigned int i;
int ee = 0;
for (i = 0; i < sizeof(configuration); i++){
*p++ = EEPROM.read(ee++);
}
if (configuration.magic_number == EEPROM_MAGIC_NUMBER) {
#ifdef DEBUG_EEPROM
//if (debug_mode) {
Serial.print(F("read_settings_from_eeprom: reading settings from eeprom: "));
Serial.print("last_azimuth:");
Serial.println(configuration.last_azimuth,1);
//}
#endif //DEBUG_EEPROM
} else { // initialize eeprom with default values
#ifdef DEBUG_EEPROM
//if (debug_mode) {
Serial.println(F("read_settings_from_eeprom: uninitialized eeprom, calling initialize_eeprom_with_defaults()"));
//}
#endif //DEBUG_EEPROM
initialize_eeprom_with_defaults();
}
}
//--------------------------------------------------------------
void initialize_eeprom_with_defaults(){
#ifdef DEBUG_EEPROM
if (debug_mode) {
Serial.println(F("initialize_eeprom_with_defaults: writing eeprom"));
}
#endif //DEBUG_EEPROM
configuration.last_azimuth = azimuth;
write_settings_to_eeprom();
}
//--------------------------------------------------------------
void write_settings_to_eeprom()
{
#ifdef DEBUG_EEPROM
if (debug_mode) {
Serial.print(F("write_settings_to_eeprom: writing settings to eeprom\n"));
}
#endif //DEBUG_EEPROM
configuration.magic_number = EEPROM_MAGIC_NUMBER;
const byte* p = (const byte*)(const void*)&configuration;
unsigned int i;
int ee = 0;
for (i = 0; i < sizeof(configuration); i++){
EEPROM.write(ee++, *p++);
}
configuration_dirty = 0;
}
//--------------------------------------------------------------
void output_debug()
{
if (((millis() - last_debug_output_time) >= 3000) && (debug_mode)) {
Serial.flush();
Serial.print("debug: ");
Serial.print(CODE_VERSION);
Serial.print(" uptime: ");
Serial.print(millis()/1000);
#ifdef DEBUG_MEMORY
void* HP = malloc(4);
if (HP) {
free (HP);
}
unsigned long free = (unsigned long)SP - (unsigned long)HP;
// if (free > 2048) {
// free = 0;
// }
Serial.print((unsigned long)free,DEC);
Serial.print(F("b free"));
#endif
#ifdef FEATURE_YAESU_EMULATION
Serial.print(F(" GS-232"));
#ifdef OPTION_GS_232B_EMULATION
Serial.print(F("B"));
#endif
#ifndef OPTION_GS_232B_EMULATION
Serial.print(F("A"));
#endif
#endif //FEATURE_YAESU_EMULATIO