-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
executable file
·2656 lines (2413 loc) · 74.4 KB
/
main.c
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
/*
* main.c
* MarcDuino Slave (HoloProjector & Lights Control)
* Created on: July 4th, 2012
* Author: Marc Verdiell
* Version 1.2r: original release 03/26/2013
* Version 1.3r: adapting for new JEDI, McWhlr Magic Panel
* Version 1.4: consolidate private and release code, HP lights from MarcDuino, PassThrough command
* Version 1.5: new buffered serial
* Version 1.7: MarcDuino v2 support
* Version 1.8: I2C command parsing support, revised serial.c
*
* Version 2.1
* Author: Neil Hutchison
* Updated: March 14th, 2020
* Setup Commands added, EEPROM Support, 13 Servo support etc
*
*/
/***********************
* Version 3.6 - Feb 22 2023
*
* Support for triggering EXT1 Pin on Client added
* Can be used to trigger a smoke machine as an example.
*
*/
/***********************
* Version 2.0
* Support for 2 Servos on the Slave
* EEPROM Support added for storing MarcDuino Settings
* Reverso Servo is now dynamic, using the Setup Commands
* Servo direction is settable per servo
* Panel Sequencer added to Slave to allow 13 panel sequences
* Panel Speed can be set per row in the Panel Sequencer
* Start and Stop Servo entries per row in the Panel Sequencer
* Maxstang's Additional Panel sequences added by default
* Panel Sequencer supports opening Panels half way
* Panel Sequencer updated to support 13 servos, and all sequences updated
* Support for Neil's Magic Panel FW
* Support for Marc's old MP code removed.
* Servo 11 on the Slave is now used for a servo, not MP
* Fix for RC Input code (could not be disabled due to missing #ifdef)
* Fix for Servo Hum in certain command cases
*
***********************/
/***********************
* Version 1.8:
* I2C command parsing support
* Updated serial.c and serial.h libraries
* Changed serial_puts so it waits for available output buffer by default
* If not characters were just dropped when sending data too fast.
* Renamed previous version serial_puts_nowait
* Updated the comments in i2c.h
*
***********************/
/**********
* v1.7
* MarcDuinoV2 support
* Bumped to version 1.7 to match the Master release
* New suart.c library with stuart2 on PC1 output instead of PC4
* Exchange suart usage to macth MarcDuino v2 board (and Master)
* - Light Output to suart2/PC1 (instead of suart)
* - Slave output on suart/PC0 (instead of suart2)
* I2C library support
* REON holoprojector support
*/
/**********
* v1.52
* Added HP test movement command
* Did not create new project
*/
/**********
* v1.51
* Corrected non centered HP servo, add extended movement option
* (all done in the header files)
* Did not create new project
* Corrected debug message version
*/
/** v1.5
*
* Up'ed the version number to match the corresponding Dome Panel Control version
* Switch to Interrupt Driven / Buffered serial implementation
* Reduce delays after JEDI commands now that suart is more reliable
*/
/** v1.4
* Add control of HP through our own board
* ON and OF commands will turn on HPs located on srv7-9 headers
* New HP lights commands
* *H1xx, *H2xx, *H3xx, and *H0xx
* Will turn on HP1, 2, 3, and all for xx seconds
* *F1xx, *F2xx, *F3xx, and *F0xx
* Will flicker HP1, 2, 3, and all for xx seconds
* New library files:
* RS232 with pgm memory for strings
* Refactor sor serial calls
* New realtime library with registered timers
* Remove sequencer files
* Added print.c and print.h (convenience, adds 2% of code) (? do I need this)
* Much smaller DRAM footprint for further expansino
* Change most strings constant to program memory strings
* Support for Alt1 (!) and Alt2 (%) expansion commands
* ! commands output on suart (with strip)
* % commands output on suart2 (with strip)
* Disabled interrupts while transmitting in suart.h -> improved reliability
*/
/** v1.3
*
* Created compile switch for 9600 baud rate for new JEDI compatibility
* Created compile switch for inverted on Magic Panel Output
* (to works with my new Holo Lights and Michael Wheeler's magic panel)
*
*/
/** Commands implemented in v1.2
*
* Hardware Setup:
* - 8 servo outputs
* Holo 1 V on pins 1 (front, holo 1)
* Holo 1 H on pins 2
* Holo 2 V on pins 3 (rear, holo 2)
* Holo 2 H on pins 4
* Holo 3 V on pins 5 (top, holo 3)
* Holo 3 H on pins 6
* Panel 12 on pin 10 (Smallest front Panel)
* Panel 13 on AUX2 (Top centre panel)
* - 4 digital outputs
* HP1 (front, holo 1) on PORT B Pin 2 (pins labeled SRV7)
* HP2 (rear, holo 2) on PORT B Pin 3 (pins labeled SRV8)
* HP3 (top, holo 3) on PORT B Pin 4 (pins labeled SRV9)
* - two suarts (software uart) output
* - suart connected to the JEDI Display light control system at 2400 bauds
* - suart2 initialized at 9600 bauds, for future use (daisy-chain expansion?)
* - one RC input
* - one main hardware UART input
* - connected to the master panel controller suart output at 9600 bauds, to receive commands
* - output sends commands acknowledgments, (not physically connected to anything at present)
*
* Valid start characters recognized in main()
* ':' panel command, ignored (see parse_panel_command). This should not be received by this slaved module anyhow
* '$' sound command, ignored (see parse_sound_command). This should not be received by this slaved module anyhow
* '@' display command, forwarded to JEDI controller on suart1 after stripping the '@' control character
* '*' hp command, acted upon here, see below
* '!' Alt1 alternate display command, passed to suart after stripping
* '%' Alt2 expansion command, passed to suart2 after stripping
* The master HP board will forward these to us
* "#" MarcDuino Setup commands used to configure various settings on the MarcDuino
*
* Commands recognized (see parse_hp_command)
* *RDxx Random Holo movement (xx=01 to 03). xx=00 and >3 all random.
* *ONxx Turns Holo Light on (xx=01 to 03). xx=00 or >3 all lights on
* *OFxx Turns Holo Lights off (xx=01 to 03). xx=00 turns all lights off
* *RCxx Holo vertical movement under RC control, horizontal centered (xx=01-03).
* 00 or >3 all RC
* *TExx Holo movement test (xx=01-03). Goes through a loop of holo movements
* to assist in adjusting holo servos mechanical setup
* 00 or >3 all HPs to test
* *STxx stop/reset Holos random movement, turns lights off, and RC off. 00=all off
* *HDxx hold: stop holo, do not change light level. 00=all stopped
* *MOxx magic panel on. xx=01 to 98, on from 1 to 98 seconds. 00=off, 99=on permanent
* *MFxx magic panel flicker xx=1 to 99 flicker for 1 to 99 seconds. 00= off.
* *H1xx, *H2xx, *H3xx, and *H0xx
* Will turn on-board HP1, 2, 3, and all (HP0xx) for xx seconds
* 99 is on permanently.
* 0 is off.
* *F1xx, *F2xx, *F3xx, and *F0xx
* Will flicker on-board HP1, 2, 3, and all (F0xx) for xx seconds
* 0 is off.
* (99 is not permanently, but 99 seconds)
*
* Setup Commands
* //// SERVO CONTROLS
* #SD00 Set Servo direction forward
* #SD01 Set servo direction reversed
* #SRxxy Set individual servo to either forward or reversed xx=servo number y=direction
* Must be a 2 digit Servo number i.e. Servo 4 is 04
* Must be either 0 or 1 to set the direction (0 normal, 1 reversed)
* Use SDxx to globally set the Servo direction, then SRxxy to change individual servos.*
*/
#include "main.h"
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h> // needed for the UART interrupt
#include <avr/pgmspace.h> // for the sequencer data arrays defined with PROGMEM
#include <avr/eeprom.h> // for state save and setup mode
#include <stdlib.h> // for itoa(), integer to string conversion
#include <errno.h> // for errors on atoi (check errno)
#include <stdio.h> // for sprintf(), don't use if you are short on memory
#include <string.h> // for strlen()
#include "toolbox.h" // map, digitalWrite, digitalMode
#include "servo.h" // servo drivers
#include "realtime.h" // real time interrupt services
#include "serial.h" // hardware serial
#include "suart.h" // software serial (write only)
#include "wmath.h" // random functions
#include "sequencer.h" // 13th Servo Sequences
#include "panel_sequences.h" // 13 servo panel sequences
#ifdef _MARCDUINOV2_
#include "i2c.h" // include I2C Master libraries for MarcDuino v2
#endif
#define HP_NUM 3 // controlling 3 HPs for RC
// command globals
char command_buffer[CMD_MAX_LENGTH]; // command string buffer
uint8_t panel_rc_control[SERVO_NUM-2]; // flag array for which panels are under RC control (Exclude the panel servos)
uint8_t panel_to_silence[SERVO_NUM]; // flag array for servos we need to turn off after a panel is closed
//setup globals
/* E2END : last valid address in Internal EEPROM */
unsigned int servo_eeprom_addr = 0; // This is a word.
// unsigned int servo_eeprom_addr2 = 1;
//unsigned int start_sound_eeprom_addr=2; // Not used in Slave
unsigned int last_servo_addr=3; // only used for CRC
unsigned int stored_crc_addr = 6; // Uses a word.
#ifdef OLD_MP_CONTROL
uint8_t mp_command_args=0;
char* mpcmd="%MP";
char* mp_cmd_val="";
#endif
//setup for ext1 control
uint8_t ext1_command_args=0;
char* ext1cmd="%EX";
char* ext1_cmd_val="";
// Controls verbosity level - affecting i2c for now
uint8_t errormessageon=_ERROR_MSG_;
uint8_t feedbackmessageon=_FEEDBACK_MSG_;
// timeout counter
rt_timer killbuzz_timer;
// flags for the HP control mode (0=nothing, 1=random, 2=RC)
static uint8_t hp1_control=0;
static uint8_t hp2_control=0;
static uint8_t hp3_control=0;
// Magic panel and holo effect depends on the value of the following global flags:
// if it's 0, it's off
// if it's 1, it's on
// if it's 2, it's on as long as rt_timeout_magic_panel is non-zero
// if it's 3, random flicker as long as rt_timeout_magic_panel is non-zero
#ifdef OLD_MP_CONTROL
static uint8_t magic_control=0;
#endif
static uint8_t hp1_light_control=0;
static uint8_t hp2_light_control=0;
static uint8_t hp3_light_control=0;
// ext1 Pin Control.
// Ext1 will be held high for the given duration
static uint8_t ext1_control=0;
// MarcDuino V2, holo states for I2C
// set them to 1 initially or they won't turn off during init...
uint8_t holo1state=1;
uint8_t holo2state=1;
uint8_t holo3state=1;
// timers
rt_timer move_timer_HP1; // HP1 random movement timer
rt_timer move_timer_HP2; // HP2 random movement timer
rt_timer move_timer_HP3; // HP3 random movement timer
#ifdef OLD_MP_CONTROL
rt_timer on_timer_magic; // magic on-time timer
rt_timer flicker_timer_magic; // magic panel flicker timer
#endif
// timer for the ext1 pin
rt_timer on_timer_ext1;
rt_timer on_timer_HP1; // HP1 on-time timer
rt_timer on_timer_HP2; // HP2 on-time timer
rt_timer on_timer_HP3; // HP3 on-time timer
rt_timer flicker_timer_HP1; // HP1 flicker timer
rt_timer flicker_timer_HP2; // HP2 flicker timer
rt_timer flicker_timer_HP3; // HP3 flicker timer
rt_timer test_timer; // For HP movement test
// string constants are in program memory to save DRAM
const char strOK[] PROGMEM="OK\n\r";
const char strWelcome[] PROGMEM="\n\rMarcDuino HP Control v3.6 \n\r";
const char strEnterPrompt[] PROGMEM="Enter HP or Display command starting with \'*\' or \'@\'\n\r";
const char strSuart1OK[] PROGMEM="\n\rsuart1 Communication OK \n\r";
const char strSuart2OK[] PROGMEM="\n\rsuart2 Communication OK \n\r";
const char strStartCharErr[] PROGMEM="**Unrecognized Command Start Character\r\n";
// utility to echo characters back cleanly
void echo(char ch)
{
// echo return and line feeds nicely on a terminal
if(ch=='\r' || ch=='\n' || ch == 0x0D )
{
serial_putc('\n');
serial_putc('\r');
}
else serial_putc(ch);
}
int main(void) {
// start hardware and software UARTs, send check string
serial_init_9600b8N1(); // 9600 bauds, 8 bits, 1 stop, no parity, use for a regular terminal console
serial_puts_p(strWelcome);
serial_puts_p(strEnterPrompt);
// initialize suart used to communicate with the JEDI Display at 2400 or 9600 bauds
uint16_t baudrate;
#ifdef _9600BAUDSJEDI_
baudrate=9600;
#else
baudrate=2400;
#endif
lightsuart_init(baudrate);
//suart_puts_p(strSuart1OK);
//_delay_ms(200);
// suart used for slave out initialized at 9600 bauds, used for Alt2 commands
slavesuart_init(9600);
#ifdef _MARCDUINOV2_
slavesuart_puts_p(strSuart1OK); // on suart1 for MarcDuino v2
#else
slavesuart_puts_p(strSuart2OK); // on suart2 for MarcDuino v1
#endif
// abort test routine, and optionally do extra Jedi programmatic setup
_delay_ms(3000); // wait that the display system is up
init_jedi();
// Received command characters from uart0 are processed asynchronously via interrupt handler
// further below. They will be used to fill the 'command_buffer' string.
// The 'command_buffer_full' flag will be raised when the full command is available.
// The buffer is then read and processed in the main loop.
serial_enable_rx_interrupt();
// Read the EEPROM to set the Servo direction
// Note we'll only read it if the EEPROM is ready.
while (!eeprom_is_ready())
{
// Just wait ... should be ready soon
// Should be ready instantly, so this will likely not be called.
_delay_ms(500);
}
// Calculate the CRC for the EEPROM to determine if it has been written or not.
uint16_t calculatedCRC = calc_crc();
// Get the current CRC from the EEPROM
uint16_t storedCRC = eeprom_read_word((uint16_t*)stored_crc_addr);
char string[30];
sprintf(string, "Calc CRC: %X StoredCRC: %X \r\n", calculatedCRC, storedCRC);
serial_puts(string);
// If we've never written data to the EEPROM, the values will be 0xFF
// Use this and the CRC to determine if we should save a set of defaults.
if (storedCRC != calculatedCRC)
{
sprintf(string, "EEPROM Corrupt or never written. Writing defaults. \r\n");
serial_puts(string);
// We have either corrupted the EEPROM, or
// we've never written to it before.
// Regardless, let's write good values!
// Set the Servo direction to "normal"
eeprom_write_word((uint16_t*)servo_eeprom_addr, (uint16_t)0x0000);
//Set the last servo number (this is just to give something else for the CRC
// it is not currently used.
eeprom_write_byte((uint8_t*)last_servo_addr, 7);
sprintf(string, "Generating new CRC. \r\n");
serial_puts(string);
// Now that we have written the new values we need to write the CRC
calculatedCRC = calc_crc();
sprintf(string, "Calc CRC: %X StoredCRC: %X \r\n", calculatedCRC, storedCRC);
serial_puts(string);
eeprom_write_word((uint16_t*)stored_crc_addr, calculatedCRC);
sprintf(string, "New Defaults and CRC Written to EEPROM \r\n");
serial_puts(string);
}
// If Servo Direction is 0, then it's "forward servos"
// If Servo Direction is 1, then it's "reverse servos"
// The Setup Mode will set this byte in the EEPROM
// Set the servos forward/reverse direction
uint16_t servo_dir = eeprom_read_word((uint16_t*)servo_eeprom_addr);
//char string[30];
for (int i=0; i<SERVO_NUM; i++)
{
uint8_t temp;
if (servo_dir & (1<<i))
{
temp = 1;
}
else
{
temp = 0;
}
//sprintf(string, "Bitmask: %4X Servo %2d, direction %2d \r\n", servo_dir, i+1, temp);
//serial_puts(string);
servo_direction[i] = temp;
}
//last_servo = eeprom_read_byte((uint8_t*)last_servo_addr);
// initialize servo and realtime units
servo_init();
realtime_init();
seq_init();
#ifdef _MARCDUINOV2_
// initialize I2C hardware on MarcDuino v2's with 10k pull-up resistors on.
i2c_init(TRUE);
#endif
// register our buzz kill timer
rt_add_timer(&killbuzz_timer);
// register all our timers and timeouts
rt_add_timer(&move_timer_HP1);
rt_add_timer(&move_timer_HP2);
rt_add_timer(&move_timer_HP3);
#ifdef OLD_MP_CONTROL
rt_add_timer(&flicker_timer_magic);
rt_add_timer(&on_timer_magic);
#endif
rt_add_timer(&on_timer_HP1);
rt_add_timer(&on_timer_HP2);
rt_add_timer(&on_timer_HP3);
rt_add_timer(&flicker_timer_HP1);
rt_add_timer(&flicker_timer_HP2);
rt_add_timer(&flicker_timer_HP3);
rt_add_timer(&test_timer);
rt_add_timer(&on_timer_ext1);
// initialize magic panel and holo lights as output
#ifdef OLD_MP_CONTROL
digitalMode(MAGIC_PORT, MAGIC_PIN, OUTPUT);
#endif
digitalMode(HP1_LIGHT_PORT, HP1_LIGHT_PIN, OUTPUT);
digitalMode(HP2_LIGHT_PORT, HP2_LIGHT_PIN, OUTPUT);
digitalMode(HP3_LIGHT_PORT, HP3_LIGHT_PIN, OUTPUT);
// initialise the ext1 as output
digitalMode(EXT1_PORT, EXT1_PIN, OUTPUT);
// turn off all lights
holo1OFF();
holo2OFF();
holo3OFF();
#ifdef OLD_MP_CONTROL
MAGIC_OFF;
#endif
// Default state for the EXT1 Pin is "off" i.e. not triggered
EXT1_OFF;
// run a close sequence on the panels to make sure they are all shut
seq_loadsequence(panel_init, SEQ_SIZE(panel_init));
seq_startsequence();
while (1)
{
/////////////////////////////////////////
// Serial Command Input
////////////////////////////////////////
char command_str[CMD_MAX_LENGTH];
bool command_available;
// check for command line input
if(serial_available())
{
char ch;
ch=serial_getc(); // get input
echo(ch); // echo back
command_available=build_command(ch, command_str); // build command line
if (command_available) dispatch_command(command_str); // send command line to dispatcher
}
///////////////////////////////////////////
// RC and random pattern of HP servos
//////////////////////////////////////////
// How the servo is control depends on hpx_control flags
// if it's 0, nothing happens
// if it's 1, random pattern
// if it's 2, RC control of vertical servo
// ********* HP RC movement ************************
if(hp1_control==2) {servo_set(FRONT_HP_SERVO_H, 1500); servo_set(FRONT_HP_SERVO_V, servo_RCread());}
if(hp2_control==2) {servo_set(REAR_HP_SERVO_H, 1500); servo_set(REAR_HP_SERVO_V, servo_RCread());}
if(hp3_control==2) {servo_set(TOP_HP_SERVO_H, 1500); servo_set(TOP_HP_SERVO_V, servo_RCread());}
// ********* HP random movement ************************
if(hp1_control==1)
{
if(move_timer_HP1==0)
{
//move_timer_HP1 = rand_howsmall_howbig(50, 500);
move_timer_HP1 = rand_howsmall_howbig(20, 200);
servo_set(FRONT_HP_SERVO_H, rand_howsmall_howbig(SERVO_HP_MIN,SERVO_HP_MAX));
servo_set(FRONT_HP_SERVO_V, rand_howsmall_howbig(SERVO_HP_MIN,SERVO_HP_MAX));
seq_setspeed(FRONT_HP_SERVO_H, random_howsmall_howbig(1,25));
seq_setspeed(FRONT_HP_SERVO_V, random_howsmall_howbig(1,25));
}
}
if(hp2_control==1)
{
if(move_timer_HP2==0)
{
//move_timer_HP2 = rand_howsmall_howbig(50, 500);
move_timer_HP2 = rand_howsmall_howbig(20, 200);
servo_set(REAR_HP_SERVO_H, rand_howsmall_howbig(SERVO_HP_MIN,SERVO_HP_MAX));
servo_set(REAR_HP_SERVO_V, rand_howsmall_howbig(SERVO_HP_MIN,SERVO_HP_MAX));
seq_setspeed(REAR_HP_SERVO_H, random_howsmall_howbig(1,25));
seq_setspeed(REAR_HP_SERVO_V, random_howsmall_howbig(1,25));
}
}
if(hp3_control==1)
{
if(move_timer_HP3==0)
{
//move_timer_HP3 = rand_howsmall_howbig(50, 500);
move_timer_HP3 = rand_howsmall_howbig(20, 200);
servo_set(TOP_HP_SERVO_H, rand_howsmall_howbig(SERVO_HP_MIN,SERVO_HP_MAX));
servo_set(TOP_HP_SERVO_V, rand_howsmall_howbig(SERVO_HP_MIN,SERVO_HP_MAX));
seq_setspeed(TOP_HP_SERVO_H, random_howsmall_howbig(1,25));
seq_setspeed(TOP_HP_SERVO_V, random_howsmall_howbig(1,25));
}
}
// ********* HP test movement ************************
// move servos to all extreme positions to adjust HP
if(hp1_control==3 || hp2_control==3 || hp3_control==3)
{
if(test_timer==0)
{
static uint8_t step;
static uint16_t a,b;
if (step>=17)step=0;
step ++;
test_timer = 200;
switch (step)
{
case 1: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 2: // up
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b= SERVO_HP_MAX;
break;
case 3: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 4: // down
a= (SERVO_HP_MIN+SERVO_HP_MAX)/2;
b= SERVO_HP_MIN;
break;
case 5: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 6: // right
a= SERVO_HP_MAX;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 7: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 8: // left
a= SERVO_HP_MIN;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 9: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 10: // up/right
a= SERVO_HP_MAX;
b= SERVO_HP_MAX;
break;
case 11: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 12: // down/right
a= SERVO_HP_MAX;
b= SERVO_HP_MIN;
break;
case 13: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 14: // down/left
a= SERVO_HP_MIN;
b= SERVO_HP_MIN;
break;
case 15: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
case 16: // up/left
a= SERVO_HP_MIN;
b= SERVO_HP_MAX;
break;
case 17: // center
a=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
b=(SERVO_HP_MIN+SERVO_HP_MAX)/2;
break;
default:
break;
}
if(hp1_control==3)
{servo_set(FRONT_HP_SERVO_H, a); servo_set(FRONT_HP_SERVO_V, b);}
if(hp2_control==3)
{servo_set(REAR_HP_SERVO_H, a); servo_set(REAR_HP_SERVO_V, b);}
if(hp3_control==3)
{servo_set(TOP_HP_SERVO_H, a); servo_set(TOP_HP_SERVO_V, b);}
}
}
#ifdef OLD_MP_CONTROL
///////////////////////////////////////////////
// Magic panel control
//////////////////////////////////////////////
// Magic panel effect depends on the 'magic_control' flag:
// Control 0-3 uses pin 3 on the MP and toggles to create effects
// Effects are generated by the MarcDuino.
// if it's 0, it's off
// if it's 1, it's on
// if it's 2, it's on as long as rt_timeout_magic_panel is non-zero
// if it's 3, random flicker as long as rt_timeout_magic_panel is non-zero
// if it's 4, Send the command via serial to the MP, and the MP manages it
switch(magic_control)
{
case 0:
MAGIC_OFF;
break;
case 1:
MAGIC_ON;
break;
case 2:
if(on_timer_magic) MAGIC_ON;
else MAGIC_OFF;
break;
case 3:
if(!on_timer_magic) MAGIC_OFF;
else
{
if(!flicker_timer_magic) // end of random flicker period
{
// Michael Wheeler magic panel has reversed polarity
#ifdef _REVERSEMAGICPANEL_
if(!digitalRead(MAGIC_PORT,MAGIC_PIN)) // panel was on
#else
if(digitalRead(MAGIC_PORT,MAGIC_PIN)) // panel was on
#endif
{
flicker_timer_magic=rand_howsmall_howbig(3,10);
MAGIC_OFF;
}
else
{
flicker_timer_magic=rand_howsmall_howbig(5,20);
MAGIC_ON;
}
}
}
break;
default:
break;
}
#endif
///////////////////////////////////////////////
// EXT1 Pin control
//////////////////////////////////////////////
// EXT1 PIN effect depends on the 'EXT1_control' flag:
// This toggles the pin high/low to enable an output on the pin to trigger
// Perfect example would be a smoke machine trigger.
// Effects are generated by the MarcDuino.
// if it's 0, it's off
// if it's 1, it's on
// if it's 2, it's on as long as rt_timeout_ext1 is non-zero
switch(ext1_control)
{
case 0:
EXT1_OFF;
break;
case 1:
EXT1_ON;
break;
case 2:
if(on_timer_ext1) EXT1_ON;
else EXT1_OFF;
break;
default:
break;
}
///////////////////////////////////////////////
// HP Light control
//////////////////////////////////////////////
// HP light mode depends on the 'hpx_light_control' flag:
// if it's 0, it's off
// if it's 1, it's on
// if it's 2, it's on as long as on_timer_HPx is non-zero
// if it's 3, random flicker as long as on_timer_HPx is non-zero
switch(hp1_light_control)
{
case 0:
holo1OFF();
break;
case 1:
holo1ON();
break;
case 2:
if(on_timer_HP1) holo1ON();
else holo1OFF();
break;
case 3:
if(!on_timer_HP1) holo1OFF();
else
{
if(!flicker_timer_HP1) // end of random flicker period
{
// check if panel was on
#ifdef _REVERSEHOLOS_
if(!digitalRead(HP1_LIGHT_PORT,HP1_LIGHT_PIN))
#else
if(digitalRead(HP1_LIGHT_PORT,HP1_LIGHT_PIN))
#endif
{
flicker_timer_HP1=rand_howsmall_howbig(3,10);
holo1OFF();
}
else
{
flicker_timer_HP1=rand_howsmall_howbig(5,20);
holo1WHITE();
}
}
}
break;
default:
break;
}
switch(hp2_light_control)
{
case 0:
holo2OFF();
break;
case 1:
holo2ON();
break;
case 2:
if(on_timer_HP2) holo2ON();
else holo2OFF();
break;
case 3:
if(!on_timer_HP2) holo2OFF();
else
{
if(!flicker_timer_HP2) // end of random flicker period
{
// check if panel was on
#ifdef _REVERSEHOLOS_
if(!digitalRead(HP2_LIGHT_PORT,HP2_LIGHT_PIN))
#else
if(digitalRead(HP2_LIGHT_PORT,HP2_LIGHT_PIN))
#endif
{
flicker_timer_HP2=rand_howsmall_howbig(3,10);
holo2OFF();
}
else
{
flicker_timer_HP2=rand_howsmall_howbig(5,20);
holo2WHITE();
}
}
}
break;
default:
break;
}
switch(hp3_light_control)
{
case 0:
holo3OFF();
break;
case 1:
holo3ON();
break;
case 2:
if(on_timer_HP3) holo3ON();
else holo3OFF();
break;
case 3:
if(!on_timer_HP3) holo3OFF();
else
{
if(!flicker_timer_HP3) // end of random flicker period
{
// check if panel was on
#ifdef _REVERSEHOLOS_
if(!digitalRead(HP3_LIGHT_PORT,HP3_LIGHT_PIN))
#else
if(digitalRead(HP3_LIGHT_PORT,HP3_LIGHT_PIN))
#endif
{
flicker_timer_HP3=rand_howsmall_howbig(3,10);
holo3OFF();
}
else
{
flicker_timer_HP3=rand_howsmall_howbig(5,20);
holo3WHITE();
}
}
}
break;
default:
break;
}
/***** simple debug RC input test: loopback input to servo */
//servo_set(2, servo_RCread());
/***** simple input RC diagnostic test: prints out the RC input value
char string[17]; // put outside while loop?
sprintf(string, "RC-in: %04d \r\n", servo_RCread());
serial_puts(string);
*/
// kill servo buzz if panel have been marked as just closed and the timeout period has expired
if(killbuzz_timer==0)
{
for(int i=1; i<=SERVO_NUM; i++)
{
if(panel_to_silence[i-1])
{
servo_set(i,_NP);
panel_to_silence[i-1]=0;
}
}
}
}
return 0;
}
/*
* Calculate the CRC based on the values we use in the EEPROM
*/
uint16_t calc_crc()
{
uint16_t calculatedCRC = 0;
// Set the Servo direction to "normal"
calculatedCRC += eeprom_read_word((uint16_t*)servo_eeprom_addr);
calculatedCRC += eeprom_read_byte((uint8_t*)last_servo_addr);
return calculatedCRC;
}
// builds the command string from the character input
uint8_t build_command(char ch, char* output_str)
{
static uint8_t pos=0;
switch(ch)
{
case CMD_END_CHAR: // end character recognized
command_buffer[pos]='\0'; // append the end of string character
pos=0; // reset buffer pointer
strcpy(output_str, (char*)command_buffer); // copy result
return TRUE; // return and signal command ready
break;
default: // regular character
command_buffer[pos]=ch; // append the character to the command string
if(pos<=CMD_MAX_LENGTH-1)pos++; // too many characters, discard them.
break;
}
return FALSE;
}
// dispatches further command processing depending on start character
void dispatch_command(char* command_str)
{
char start_char=command_str[0];
uint8_t length=strlen(command_str);
// prompt on empty command to show life at console
if(length==0)
{
serial_puts_p(strOK);
return;
}
// dispatch the command to the appropriate parser depending on start character
switch(start_char)
{
case PANEL_START_CHAR:
parse_panel_command(command_str, length);
break;
case HP_START_CHAR:
parse_hp_command(command_str, length);
break;
case DISPLAY_START_CHAR:
parse_display_command(command_str,length);
break;
case SOUND_START_CHAR:
parse_sound_command(command_str,length);
break;
case ALT1_START_CHAR:
parse_alt1_command(command_str,length);
break;
case ALT2_START_CHAR:
parse_alt2_command(command_str,length);
break;
case I2C_START_CHAR:
parse_i2c_command(command_str,length);
break;
case SETUP_START_CHAR:
parse_setup_command(command_str, length);
break;
default:
if(errormessageon) serial_puts_p(strStartCharErr);
break;
}
}
// New Setup and Configuration functions.
const char strsetupCommand[] PROGMEM="Setup command\r\n";
void parse_setup_command(char* command, uint8_t length)
{
if(feedbackmessageon) serial_puts_p(strsetupCommand);
char cmd[3];
char arg[4];
// character '*' begins command, should have been already checked if this is called
if(command[0]!=SETUP_START_CHAR)
{
if(errormessageon) serial_puts_p("Err Setup Cmd\n\r");
return;
}
cmd[0]=command[1];
cmd[1]=command[2];
cmd[2]='\0';
arg[0]=command[3];
arg[1]=command[4];
arg[2]=command[5];
arg[3]='\0';