-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEasyCon.c
917 lines (893 loc) · 29.7 KB
/
EasyCon.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
#include "EasyCon.h"
#include "EasyCon_API.h"
// global variables
volatile uint8_t echo_ms = 0; // echo counter
// static variables
static uint8_t mem[MEM_SIZE] = {0xFF, 0xFF, VERSION}; // preallocated memory for all purposes, as well as static instruction carrier
static size_t serial_buffer_length = 0; // current length of serial buffer
static bool serial_command_ready = false; // CMD_READY acknowledged, ready to receive command byte
static uint8_t *flash_addr = 0; // start location for EEPROM flashing
static uint16_t flash_index = 0; // current buffer index
static uint16_t flash_count = 0; // number of bytes expected for this time
static uint8_t *script_addr = 0; // address of next instruction
static uint8_t *script_eof = 0; // address of EOF
static uint16_t tail_wait = 0; // insert an extra wait before next instruction (used by compressed instruction)
static uint32_t timer_elapsed = 0; // previous execution time
static bool auto_run = false;
// set led state
static volatile uint8_t _ledflag = 0;
// timers define
static volatile uint32_t timer_ms = 0; // script timer
static volatile uint32_t wait_ms = 0; // waiting counter
// some funcs only use in EasyCon
static void EasyCon_binaryop(uint8_t op, uint8_t reg, int16_t value);
static void zero_echo(void);
// Initialize script. Load static script into EEPROM if exists.
void EasyCon_script_init(void)
{
if (mem[0] != 0xFF || mem[1] != 0xFF)
{
// flash instructions from firmware
int len = mem[0] | ((mem[1] & 0b01111111) << 8);
for (int i = 0; i < len; i++)
if (EasyCon_read_byte((uint8_t *)i) != mem[i])
EasyCon_write_byte((uint8_t *)i, mem[i]);
}
memset(mem, 0, sizeof(mem));
// randomize
_seed = EasyCon_read_2byte((uint16_t *)SEED_OFFSET) + 1;
srand(_seed);
EasyCon_write_2byte((uint16_t *)SEED_OFFSET, _seed);
// calculate direction presets
for (int i = 0; i < 16; i++)
{
// part 1
int x = (Min(i, 8)) << 5;
int y = (Max(i, 8) - 8) << 5;
x = Min(x, 255);
y = Min(y, 255);
DX(i) = x;
DY(i) = y;
}
for (int i = 16; i < 32; i++)
{
// part 2
int x = (24 - Min(i, 24)) << 5;
int y = (32 - Max(i, 24)) << 5;
x = Min(x, 255);
y = Min(y, 255);
DX(i) = x;
DY(i) = y;
}
_report_echo = ECHO_TIMES;
// turn on/off led
_ledflag = EasyCon_read_byte((uint8_t *)LED_SETTING);
// only if highest bit is 0
auto_run = (EasyCon_read_byte((uint8_t *)1) >> 7) == 0;
}
void EasyCon_script_tick(void)
{
// decrement waiting counter
if (wait_ms != 0 && (_report_echo == 0 || wait_ms > 1))
wait_ms--;
}
void EasyCon_tick(void)
{
// increment timer
timer_ms++;
if (echo_ms != 0)
echo_ms--;
EasyCon_script_tick();
}
void EasyCon_decrease_report_echo(void)
{
// decrement echo counter
if (!_script_running || _report_echo > 0 || wait_ms < 2)
{
_report_echo = Max(0, _report_echo - 1);
}
}
bool EasyCon_is_script_running(void)
{
if(_script_running == 1)
{
return true;
}
else
{
return false;
}
}
// Run script on startup.
void EasyCon_script_auto_start(void)
{
if (auto_run)
EasyCon_script_start();
}
// Run script.
void EasyCon_script_start(void)
{
script_addr = (uint8_t *)2;
uint16_t eof = EasyCon_read_byte((uint8_t *)0) | (EasyCon_read_byte((uint8_t *)1) << 8);
if (eof == 0xFFFF)
eof = 0;
script_eof = (uint8_t *)(eof & 0x7FFF);
// reset variables
wait_ms = 0;
///////////////////////////
zero_echo();
///////////////////////////
timer_ms = 0;
tail_wait = 0;
memset(mem + VARSPACE_OFFSET, 0, sizeof(mem) - VARSPACE_OFFSET);
_script_running = 1;
_seed = EasyCon_read_2byte((uint16_t *)SEED_OFFSET);
if(_ledflag != 0) return;
EasyCon_runningLED_on();
}
// Stop script.
void EasyCon_script_stop(void)
{
_script_running = 0;
timer_elapsed = timer_ms;
////////////////////////////
reset_hid_report();
///////////////////////////
_report_echo = ECHO_TIMES;
EasyCon_runningLED_off();
}
// Process script instructions.
void EasyCon_script_task(void)
{
while (true)
{
// status check
if (!_script_running)
return;
// timer check
if (wait_ms > 0)
return;
// release keys
if(_ledflag == 0)
EasyCon_blink_led();
for (int i = 0; i <= KEYCODE_MAX; i++)
{
if (KEY(i) != 0)
{
KEY(i)--;
if (KEY(i) == 0)
{
if (i == 32)
{
// LS
SetLeftStick(STICK_CENTER, STICK_CENTER);
_report_echo = ECHO_TIMES;
}
else if (i == 33)
{
// RS
SetRightStick(STICK_CENTER, STICK_CENTER);
_report_echo = ECHO_TIMES;
}
else if ((i & 0x10) == 0)
{
// Button
ReleaseButtons(_BV(i));
_report_echo = ECHO_TIMES;
}
else
{
// HAT
SetHATSwitch(HAT_CENTER);
_report_echo = ECHO_TIMES;
}
}
}
}
if (tail_wait != 0)
{
// wait after compressed instruction
SETWAIT(tail_wait);
tail_wait = 0;
return;
}
if (script_addr >= script_eof)
{
// reaches EOF, end script
EasyCon_script_stop();
return;
}
_addr = (uint16_t)script_addr;
_ins0 = EasyCon_read_byte(script_addr++);
_ins1 = EasyCon_read_byte(script_addr++);
int32_t n;
int16_t reg;
if (_ins0 & 0b10000000)
{
// key/stick actions
if ((_ins0 & 0b01000000) == 0)
{
// Instruction : Key
_keycode = (_ins0 >> 1) & 0b11111;
// modify report
if ((_keycode & 0x10) == 0)
{
// Button
PressButtons(_BV(_keycode));
_report_echo = ECHO_TIMES;
}
else
{
// HAT
SetHATSwitch(_keycode & 0xF);
_report_echo = ECHO_TIMES;
}
// post effect
if (E_SET)
{
// pre-loaded duration
SETWAIT(REG(_e_val));
RESETAFTER(_keycode, 1);
}
else if ((_ins0 & 0b00000001) == 0)
{
// standard
n = _ins1;
// unscale
n *= 10;
SETWAIT(n);
RESETAFTER(_keycode, 1);
}
else if ((_ins1 & 0b10000000) == 0)
{
// compressed
tail_wait = _ins1 & 0b01111111;
// unscale
tail_wait *= 50;
SETWAIT(50);
RESETAFTER(_keycode, 1);
}
else
{
// hold
n = _ins1 & 0b01111111;
RESETAFTER(_keycode, n);
}
}
else
{
// Instruction : Stick
_lr = (_ins0 >> 5) & 1;
_keycode = 32 | _lr;
_direction = _ins0 & 0b11111;
// modify report
if (_lr)
{
// RS
SetRightStick(DX(_direction), DY(_direction));
_report_echo = ECHO_TIMES;
}
else
{
// LS
SetLeftStick( DX(_direction), DY(_direction));
_report_echo = ECHO_TIMES;
}
// post effect
if (E_SET)
{
// pre-loaded duration
SETWAIT(REG(_e_val));
RESETAFTER(_keycode, 1);
}
else if ((_ins1 & 0b10000000) == 0)
{
// standard
n = _ins1 & 0b01111111;
// unscale
n *= 50;
SETWAIT(n);
RESETAFTER(_keycode, 1);
}
else
{
// hold
n = _ins1 & 0b01111111;
RESETAFTER(_keycode, n);
}
}
}
else
{
// flow control
switch ((_ins0 >> 3) & 0b1111)
{
case 0b0000:
if ((_ins0 & 0b100) == 0)
{
// empty
}
else
{
// Instruction : SerialPrint
reg = _ins & ((1 << 9) - 1);
if ((_ins0 & 0b10) == 0)
{
EasyCon_serial_send(reg);
EasyCon_serial_send(reg >> 8);
}
else
{
EasyCon_serial_send(mem[reg]);
EasyCon_serial_send(mem[reg + 1]);
}
break;
}
break;
case 0b0001:
// Instruction : Wait
if (E_SET)
{
// pre-loaded duration
n = REG(_e_val);
}
else if ((_ins0 & 0b100) == 0)
{
// standard
n = _ins & ((1 << 10) - 1);
// unscale
n *= 10;
}
else if ((_ins0 & 0b10) == 0)
{
// extended
_ins2 = EasyCon_read_byte(script_addr++);
_ins3 = EasyCon_read_byte(script_addr++);
n = _insEx & ((1L << 25) - 1);
// unscale
n *= 10;
}
else
{
// high precision
n = _ins & ((1 << 9) - 1);
}
SETWAIT(n);
break;
case 0b0010:
// Instruction : For
if (_forstackindex == 0 || FOR_ADDR(_forstackindex - 1) != _addr)
{
// loop initialize
_forstackindex++;
FOR_I(_forstackindex - 1) = 0;
FOR_ADDR(_forstackindex - 1) = _addr;
FOR_NEXT(_forstackindex - 1) = _ins & ((1 << 11) - 1);
// pre-loaded arguments
if (E_SET)
{
// iterator
_ri0 = REG(_e_val) & 0xF;
if (_ri0 != 0)
{
// store iterator
FOR_I(_forstackindex - 1) = _ri0 | 0x80000000;
}
// count
_ri1 = (REG(_e_val) >> 4) & 0xF;
if (_ri1 != 0)
{
// write loop count
FOR_C(_forstackindex - 1) = REG(_ri1);
// Mode 2 : loop count overwritten
E(2);
// jump to next (for condition checking)
JUMP(FOR_NEXT(_forstackindex - 1));
break;
}
}
// Mode 0 : init
E(0);
// jump to next (for further initialization)
JUMP(FOR_NEXT(_forstackindex - 1));
break;
}
break;
case 0b0011:
// Instruction : Next
if (_ins0 & 0b100)
{
// extended
_ins2 = EasyCon_read_byte(script_addr++);
_ins3 = EasyCon_read_byte(script_addr++);
}
if (E_SET)
{
if (_e_val == 1)
{
// Mode 1 : break
_forstackindex--;
break;
}
else if (_e_val == 0)
{
// Mode 0 : init
if (_e_val == 0)
{
// initialize loop count
if ((_ins0 & 0b100) == 0)
{
// small number
FOR_C(_forstackindex - 1) = _ins & ((1 << 10) - 1);
if (FOR_C(_forstackindex - 1) == 0)
{
// infinite loop
FOR_C(_forstackindex - 1) = 0x80000000;
}
}
else
{
// large number
FOR_C(_forstackindex - 1) = _insEx & ((1L << 26) - 1);
}
}
}
else if (_e_val == 2)
{
// Mode 2 : loop count overwritten
// do nothing here
}
}
else
{
// normal loop step
if (FOR_I(_forstackindex - 1) & 0x80000000)
{
// iterator
REG(FOR_I(_forstackindex - 1) & 0xF) += 1;
}
else
{
// loop variable
FOR_I(_forstackindex - 1) += 1;
}
}
// check condition
if (FOR_I(_forstackindex - 1) & 0x80000000)
n = REG(FOR_I(_forstackindex - 1) & 0xF);
else
n = FOR_I(_forstackindex - 1);
if (FOR_C(_forstackindex - 1) != 0x80000000 && n >= FOR_C(_forstackindex - 1))
{
// end for
_forstackindex--;
}
else
{
// jump back
JUMP(FOR_ADDR(_forstackindex - 1));
}
break;
case 0b0100:
if (_ins0 & 0b100)
{
// comparisons
_ri0 = (_ins1 >> 3) & 0b111;
_ri1 = _ins1 & 0b111;
switch (_ins0 & 0b11)
{
case 0b00:
// Instruction : Equal
_v = REG(_ri0) == REG(_ri1);
break;
case 0b01:
// Instruction : NotEqual
_v = REG(_ri0) != REG(_ri1);
break;
case 0b10:
// Instruction : LessThan
_v = REG(_ri0) < REG(_ri1);
break;
case 0b11:
// Instruction : LessOrEqual
_v = REG(_ri0) <= REG(_ri1);
break;
}
_v = (bool)_v;
_flag = (bool)_flag;
switch (_ins1 >> 6)
{
case 0b00:
// assign
_flag = _v;
break;
case 0b01:
// and
_flag &= _v;
break;
case 0b10:
// or
_flag |= _v;
break;
case 0b11:
// xor
_flag ^= _v;
break;
}
}
else
{
switch (_ins1 >> 5)
{
case 0b000:
// Instruction : Break
if ((_ins1 & 0b10000) && !_flag)
break;
_v = _ins1 & 0b1111;
_forstackindex -= _v;
E(1);
JUMP(FOR_NEXT(_forstackindex - 1));
break;
case 0b001:
// Instruction : Continue
if ((_ins1 & 0b10000) && !_flag)
break;
_v = _ins1 & 0b1111;
_forstackindex -= _v;
JUMP(FOR_NEXT(_forstackindex - 1));
break;
case 0b111:
// Instruction : Return
if ((_ins1 & 0b10000) && !_flag)
break;
if (_callstackindex)
{
// sub function
// pop return address
JUMP(CALLSTACK(_callstackindex - 1));
_callstackindex--;
break;
}
else
{
// main function
EasyCon_script_stop();
break;
}
break;
}
}
break;
case 0b0101:
if ((_ins0 & 0b100) == 0)
{
_ri0 = (_ins >> 7) & 0b111;
if (_ri0 == 0)
{
if ((_ins1 & (1 << 6)) == 0)
{
// binary operations on instant
_ins2 = EasyCon_read_byte(script_addr++);
_ins3 = EasyCon_read_byte(script_addr++);
_v = (_ins >> 3) & 0b111;
_ri0 = _ins & 0b111;
reg = _insEx;
EasyCon_binaryop(_v, _ri0, reg);
}
else
{
// preserved
}
}
else
{
// Instruction : Mov
reg = _ins1 & 0b01111111;
// fill sign bit
reg <<= 9;
reg >>= 9;
REG(_ri0) = reg;
}
}
else if ((_ins0 & 0b110) == 0b100)
{
// binary operations on register
_v = (_ins >> 6) & 0b111;
_ri0 = (_ins >> 3) & 0b111;
_ri1 = _ins & 0b111;
EasyCon_binaryop(_v, _ri0, REG(_ri1));
}
else if ((_ins0 & 0b111) == 0b110)
{
// bitwise shift
_ri0 = (_ins1 >> 4) & 0b111;
_v = _ins1 & 0b1111;
if (_ri0 == 0)
break;
if ((_ins1 & 0b10000000) == 0)
{
// Instruction : ShL
REG(_ri0) <<= _v;
}
else
{
// Instruction : ShR
REG(_ri0) >>= _v;
}
}
else
{
// unary operations
_ri0 = _ins1 & 0b111;
switch ((_ins1 >> 3) & 0b1111)
{
case 0b0010:
// Instruction : Negative
if (_ri0 == 0)
break;
REG(_ri0) = -REG(_ri0);
break;
case 0b0011:
// Instruction : Not
if (_ri0 == 0)
break;
REG(_ri0) = ~REG(_ri0);
break;
case 0b0100:
// Instruction : Push
_stackindex++;
STACK(_stackindex - 1) = REG(_ri0);
break;
case 0b0101:
// Instruction : Pop
if (_ri0 == 0)
break;
REG(_ri0) = STACK(_stackindex - 1);
_stackindex--;
break;
case 0b0111:
// Instruction : StoreOp
E(_ri0);
break;
case 0b1000:
// Instruction : Bool
if (_ri0 == 0)
break;
REG(_ri0) = (bool)REG(_ri0);
break;
case 0b1001:
// Instruction : Rand
if (_ri0 == 0)
break;
if (!_seed)
{
_seed = timer_ms;
EasyCon_write_2byte((uint16_t *)SEED_OFFSET, _seed);
srand(_seed);
}
REG(_ri0) = rand() % REG(_ri0);
break;
}
}
break;
case 0b0110:
// branches
reg = _ins & ((1 << 9) - 1);
reg = reg << 7 >> 6;
switch ((_ins0 >> 1) & 0b11)
{
case 0b00:
// Instruction : Branch
JUMPNEAR(reg);
break;
case 0b01:
// Instruction : BranchTrue
if (_flag)
JUMPNEAR(reg);
break;
case 0b10:
// Instruction : BranchFalse
if (!_flag)
JUMPNEAR(reg);
break;
case 0b11:
// Instruction : Call
_callstackindex++;
STACK(_callstackindex - 1) = (int16_t)script_addr;
JUMPNEAR(reg);
break;
}
break;
}
}
}
}
// Perform binary operations by operator code
static void EasyCon_binaryop(uint8_t op, uint8_t reg, int16_t value)
{
if (reg == 0)
return;
switch (op)
{
case 0b000:
// Mov
REG(reg) = value;
break;
case 0b001:
// Add
REG(reg) += value;
break;
case 0b010:
// Mul
REG(reg) *= value;
break;
case 0b011:
// Div
REG(reg) /= value;
break;
case 0b100:
// Mod
REG(reg) %= value;
break;
case 0b101:
// And
REG(reg) &= value;
break;
case 0b110:
// Or
REG(reg) |= value;
break;
case 0b111:
// Xor
REG(reg) ^= value;
break;
}
}
// Process data from serial port.
void EasyCon_serial_task(int16_t byte)
{
if (byte < 0)
return;
if(_ledflag == 0)
EasyCon_blink_led();
if (flash_index < flash_count)
{
// flashing
SERIAL_BUFFER(flash_index) = byte;
flash_index++;
if (flash_index == flash_count)
{
// all bytes received
for (flash_index = 0; flash_index < flash_count; flash_index++, flash_addr++)
EasyCon_write_byte(flash_addr, SERIAL_BUFFER(flash_index));
EasyCon_serial_send(REPLY_FLASHEND);
}
}
else
{
// regular
SERIAL_BUFFER(serial_buffer_length++) = byte;
// check control byte
if ((byte & 0x80) != 0)
{
if (serial_buffer_length == 1 && !serial_command_ready && byte == CMD_READY)
{
// comand ready
serial_command_ready = true;
}
else if (serial_buffer_length == 8)
{
// report data
if (_script_running)
{
// script running, send BUSY
EasyCon_serial_send(REPLY_BUSY);
}
else
{
//memset(&next_report, 0, sizeof(USB_JoystickReport_Input_t));
SetButtons( (SERIAL_BUFFER(0) << 9) | (SERIAL_BUFFER(1) << 2) | (SERIAL_BUFFER(2) >> 5) );
SetHATSwitch( (uint8_t)((SERIAL_BUFFER(2) << 3) | (SERIAL_BUFFER(3) >> 4)) );
SetLeftStick( (uint8_t)((SERIAL_BUFFER(3) << 4) | (SERIAL_BUFFER(4) >> 3)),
(uint8_t)((SERIAL_BUFFER(4) << 5) | (SERIAL_BUFFER(5) >> 2)));
SetRightStick( (uint8_t)((SERIAL_BUFFER(5) << 6) | (SERIAL_BUFFER(6) >> 1)),
(uint8_t)((SERIAL_BUFFER(6) << 7) | (SERIAL_BUFFER(7) & 0x7f)));
// set flag
_report_echo = ECHO_TIMES;
// send ACK
EasyCon_serial_send(REPLY_ACK);
}
serial_command_ready = false;
}
else if (serial_command_ready)
{
serial_command_ready = false;
// command
switch (byte)
{
case CMD_DEBUG:;
uint32_t n;
// instruction count
//uint8_t* count = (uint8_t*)(eeprom_read_byte((uint8_t*)0) | (eeprom_read_byte((uint8_t*)1) << 8));
//for (uint8_t* i = 0; i < count; i++)
//EasyCon_serial_send(eeprom_read_byte(i));
// current loop variable
n = FOR_I(_forstackindex - 1);
for (int i = 0; i < 4; i++)
{
EasyCon_serial_send(n);
n >>= 8;
}
// time elapsed
n = timer_elapsed;
for (int i = 0; i < 4; i++)
{
EasyCon_serial_send(n);
n >>= 8;
}
// PC
n = (uint16_t)script_addr;
for (int i = 0; i < 2; i++)
{
EasyCon_serial_send(n);
n >>= 8;
}
break;
case CMD_VERSION:
EasyCon_serial_send(VERSION);
break;
case CMD_LED:
_ledflag ^= 0x8;
EasyCon_write_byte((uint8_t *)LED_SETTING, _ledflag);
EasyCon_runningLED_off();
EasyCon_serial_send(_ledflag);
break;
case CMD_READY:
serial_command_ready = true;
break;
case CMD_HELLO:
EasyCon_serial_send(REPLY_HELLO);
break;
case CMD_FLASH:
if (serial_buffer_length != 5)
{
EasyCon_serial_send(REPLY_ERROR);
break;
}
EasyCon_script_stop();
flash_addr = (uint8_t *)(SERIAL_BUFFER(0) | (SERIAL_BUFFER(1) << 7));
flash_count = (SERIAL_BUFFER(2) | (SERIAL_BUFFER(3) << 7));
flash_index = 0;
EasyCon_serial_send(REPLY_FLASHSTART);
break;
case CMD_SCRIPTSTART:
EasyCon_script_start();
EasyCon_serial_send(REPLY_SCRIPTACK);
break;
case CMD_SCRIPTSTOP:
EasyCon_script_stop();
EasyCon_serial_send(REPLY_SCRIPTACK);
break;
default:
// error
EasyCon_serial_send(REPLY_ERROR);
break;
}
}
else
{
// error
EasyCon_serial_send(serial_buffer_length);
}
// clear buffer
serial_buffer_length = 0;
}
// overflow protection
if (serial_buffer_length >= SERIAL_BUFFER_SIZE)
serial_buffer_length = 0;
}
}
void zero_echo(void)
{
echo_ms = 0;
}