-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPicoMite.c
4458 lines (4225 loc) · 188 KB
/
PicoMite.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
/***********************************************************************************************************************
PicoMite MMBasic
Picomite.c
<COPYRIGHT HOLDERS> Geoff Graham, Peter Mather
Copyright (c) 2021, <COPYRIGHT HOLDERS> All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. The name MMBasic be used when referring to the interpreter in any documentation and promotional material and the original copyright message be displayed
on the console at startup (additional copyright messages may be added).
4. All advertising materials mentioning features or use of this software must display the following acknowledgement: This product includes software developed
by the <copyright holder>.
5. Neither the name of the <copyright holder> nor the names of its contributors may be used to endorse or promote products derived from this software
without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDERS> AS IS AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDERS> BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
************************************************************************************************************************/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdbool.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "pico/binary_info.h"
#include "configuration.h"
#include "hardware/watchdog.h"
#include "hardware/clocks.h"
#include "hardware/flash.h"
#include "hardware/adc.h"
#include "hardware/exception.h"
#include "MMBasic_Includes.h"
#include "Hardware_Includes.h"
#include "hardware/structs/systick.h"
#include "hardware/structs/timer.h"
#include "hardware/vreg.h"
#include "hardware/structs/pads_qspi.h"
#include "pico/unique_id.h"
#include "hardware/pwm.h"
#ifdef rp2350
#include "hardware/structs/qmi.h"
extern void start_i2s(int pio, int sm);
#endif
#define COPYRIGHT "Copyright " YEAR " Geoff Graham\r\n"\
"Copyright " YEAR2 " Peter Mather\r\n\r\n"
#ifdef USBKEYBOARD
#include "tusb.h"
#include "host/hcd.h"
#include "usb_host_files/tusb_config.h"
#else
#include "pico/unique_id.h"
#include "class/cdc/cdc_device.h"
#endif
#ifndef rp2350
#include "hardware/structs/ssi.h"
#else
#ifdef HDMI
#include "hardware/structs/hstx_ctrl.h"
#include "hardware/structs/hstx_fifo.h"
#endif
#include "hardware/dma.h"
#include "hardware/gpio.h"
#include "hardware/irq.h"
#include "hardware/structs/bus_ctrl.h"
#include "hardware/structs/xip_ctrl.h"
#include "hardware/structs/sio.h"
#include "hardware/vreg.h"
#include "pico/multicore.h"
#include "pico/sem.h"
#include <stdio.h>
#include <stdlib.h>
#include "pico/stdlib.h"
#include "hardware/clocks.h"
#include <string.h>
#include "hardware/regs/sysinfo.h"
bool rp2350a=true;
uint32_t PSRAMsize=0;
#endif
#include "hardware/structs/bus_ctrl.h"
#include <pico/bootrom.h>
#include "hardware/irq.h"
#include "hardware/pio.h"
#include "hardware/pio_instructions.h"
#ifdef PICOMITEWEB
#include "lwipopts.h"
#include "pico/cyw43_arch.h"
#include "lwip/pbuf.h"
#include "lwip/tcp.h"
#include "lwip/dns.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#endif
#ifdef PICOMITEVGA
uint16_t map16[16]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
uint32_t map16pairs[16];
volatile uint8_t transparent=0;
volatile uint8_t transparents=0;
volatile int RGBtransparent=0;
int MODE1SIZE=MODE1SIZE_S, MODE2SIZE=MODE2SIZE_S, MODE3SIZE=MODE3SIZE_S, MODE4SIZE=MODE4SIZE_S, MODE5SIZE=MODE5SIZE_S;
#ifdef HDMI
uint16_t map16d[16];
uint32_t map16q[16];
#endif
// 126 MHz timings
int QVGA_TOTAL;// total clock ticks (= QVGA_HSYNC + QVGA_BP + WIDTH*QVGA_CPP[1600] + QVGA_FP)
int QVGA_HSYNC; // horizontal sync clock ticks
int QVGA_BP ; // back porch clock ticks
int QVGA_FP; // front porch clock ticks
// QVGA vertical timings
int QVGA_VACT; // V active scanlines (= 2*HEIGHT)
int QVGA_VFRONT; // V front porch
int QVGA_VSYNC; // length of V sync (number of scanlines)
int QVGA_VBACK; // V back porch
int QVGA_VTOT; // total scanlines (= QVGA_VSYNC + QVGA_VBACK + QVGA_VACT + QVGA_VFRONT)
#ifndef HDMI
#include "Include.h"
#endif
#ifdef USBKEYBOARD
#ifdef HDMI
#define MES_SIGNON "\rPicoMiteHDMI MMBasic USB " CHIP " Edition V"VERSION "\r\n"
#else
#define MES_SIGNON "\rPicoMiteVGA MMBasic USB " CHIP " Edition V"VERSION "\r\n"
#endif
extern void hid_app_task(void);
volatile int keytimer=0;
extern void USB_bus_reset(void);
bool USBenabled=false;
#else
#ifdef HDMI
#define MES_SIGNON "\rPicoMiteHDMI MMBasic " CHIP " Edition V"VERSION "\r\n"
#else
#define MES_SIGNON "\rPicoMiteVGA MMBasic " CHIP " Edition V"VERSION "\r\n"
#endif
#endif
#endif
#ifdef PICOMITEWEB
#define MES_SIGNON "\rWebMite MMBasic " CHIP " Edition V"VERSION "\r\n"
volatile int WIFIconnected=0;
int startupcomplete=0;
void ProcessWeb(int mode);
char LCDAttrib=0;
#endif
#ifdef PICOMITE
#ifdef USBKEYBOARD
#include "tusb.h"
#include "host/hcd.h"
#define MES_SIGNON "\rPicoMite MMBasic USB " CHIP " Edition V"VERSION "\r\n"
extern void hid_app_task(void);
volatile int keytimer=0;
extern void USB_bus_reset(void);
bool USBenabled=false;
#include "pico/multicore.h"
mutex_t frameBufferMutex; // mutex to lock frame buffer
#else
#define MES_SIGNON "\rPicoMite MMBasic " CHIP " Edition V"VERSION "\r\n"
#include "pico/multicore.h"
mutex_t frameBufferMutex; // mutex to lock frame buffer
#endif
char LCDAttrib=0;
#endif
#define KEYCHECKTIME 16
int ListCnt;
int MMCharPos;
int MMPromptPos;
int busfault=0;
int ExitMMBasicFlag = false;
volatile int MMAbort = false;
unsigned int _excep_peek;
void CheckAbort(void);
void TryLoadProgram(void);
unsigned char lastchar=0;
int adc_clk_div;
unsigned char BreakKey = BREAK_KEY; // defaults to CTRL-C. Set to zero to disable the break function
volatile char ConsoleRxBuf[CONSOLE_RX_BUF_SIZE]={0};
volatile int ConsoleRxBufHead = 0;
volatile int ConsoleRxBufTail = 0;
volatile char ConsoleTxBuf[CONSOLE_TX_BUF_SIZE]={0};
volatile int ConsoleTxBufHead = 0;
volatile int ConsoleTxBufTail = 0;
#ifndef USBKEYBOARD
extern void initMouse0(int sensitivity);
volatile unsigned int MouseTimer = 0;
#endif
volatile unsigned int AHRSTimer = 0;
volatile unsigned int InkeyTimer = 0;
volatile long long int mSecTimer = 0; // this is used to count mSec
volatile unsigned int WDTimer = 0;
volatile unsigned int diskchecktimer = DISKCHECKRATE;
volatile unsigned int clocktimer=60*60*1000;
volatile unsigned int PauseTimer = 0;
volatile unsigned int ClassicTimer = 0;
volatile unsigned int NunchuckTimer = 0;
volatile unsigned int IntPauseTimer = 0;
volatile unsigned int Timer1=0, Timer2=0, Timer3=0, Timer4=0, Timer5=0; //1000Hz decrement timer
volatile unsigned int KeyCheck=2000;
volatile int ds18b20Timer = -1;
volatile unsigned int ScrewUpTimer = 0;
//volatile int second = 0; // date/time counters
//volatile int minute = 0;
//volatile int hour = 0;
//volatile int day = 1;
//volatile int month = 1;
//volatile int year = 2000;
volatile unsigned int GPSTimer = 0;
volatile unsigned int SecondsTimer = 0;
volatile unsigned int I2CTimer = 0;
volatile int day_of_week=1;
unsigned char PulsePin[NBR_PULSE_SLOTS];
unsigned char PulseDirection[NBR_PULSE_SLOTS];
int PulseCnt[NBR_PULSE_SLOTS];
int PulseActive;
const uint8_t *flash_option_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET);
const uint8_t *SavedVarsFlash = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET + FLASH_ERASE_SIZE);
const uint8_t *flash_target_contents = (const uint8_t *) (XIP_BASE + FLASH_TARGET_OFFSET + FLASH_ERASE_SIZE + SAVEDVARS_FLASH_SIZE);
const uint8_t *flash_progmemory = (const uint8_t *) (XIP_BASE + PROGSTART);
const uint8_t *flash_libmemory = (const uint8_t *) (XIP_BASE + PROGSTART - MAX_PROG_SIZE);
int ticks_per_second;
int InterruptUsed;
int calibrate=0;
char id_out[12];
MMFLOAT VCC=3.3;
int PromptFont, PromptFC=0xFFFFFF, PromptBC=0; // the font and colours selected at the prompt
volatile int DISPLAY_TYPE;
volatile bool processtick = true;
unsigned char WatchdogSet = false;
unsigned char IgnorePIN = false;
unsigned char SPIatRisk = false;
uint32_t __uninitialized_ram(_excep_code);
uint64_t __uninitialized_ram(_persistent);
unsigned char lastcmd[STRINGSIZE*2]; // used to store the last command in case it is needed by the EDIT command
FATFS fs; // Work area (file system object) for logical drive
bool timer_callback(repeating_timer_t *rt);
static uint64_t __not_in_flash_func(uSecFunc)(uint64_t a){
uint64_t b=time_us_64()+a;
while(time_us_64()<b){}
return b;
}
extern void MX470Display(int fn);
//Vector to CFunction routine called every command (ie, from the BASIC interrupt checker)
extern unsigned int CFuncInt1;
//Vector to CFunction routine called by the interrupt 2 handler
extern unsigned int CFuncInt2;
extern unsigned int CFuncmSec;
extern void CallCFuncInt1(void);
extern void CallCFuncInt2(void);
extern volatile bool CSubComplete;
static uint64_t __not_in_flash_func(uSecTimer)(void){ return time_us_64();}
static int64_t PinReadFunc(int a){return gpio_get(PinDef[a].GPno);}
extern void CallExecuteProgram(char *p);
extern void CallCFuncmSec(void);
extern volatile uint32_t irqs;
#define CFUNCRAM_SIZE 256
int CFuncRam[CFUNCRAM_SIZE/sizeof(int)];
repeating_timer_t timer;
MMFLOAT IntToFloat(long long int a){ return a; }
MMFLOAT FMul(MMFLOAT a, MMFLOAT b){ return a * b; }
MMFLOAT FAdd(MMFLOAT a, MMFLOAT b){ return a + b; }
MMFLOAT FSub(MMFLOAT a, MMFLOAT b){ return a - b; }
MMFLOAT FDiv(MMFLOAT a, MMFLOAT b){ return a / b; }
uint32_t CFunc_delay_us;
#ifndef HDMI
int QVGA_CLKDIV; // SM divide clock ticks
#endif
void PIOExecute(int pion, int sm, uint32_t ins){
PIO pio = (pion ? pio1: pio0);
pio_sm_exec(pio, sm, ins);
}
int IDiv(int a, int b){return a/b;}
int FCmp(MMFLOAT a,MMFLOAT b){if(a>b) return 1;else if(a<b)return -1; else return 0;}
MMFLOAT LoadFloat(unsigned long long c){union ftype{ unsigned long long a; MMFLOAT b;}f;f.a=c;return f.b; }
const void * const CallTable[] __attribute__((section(".text"))) = { (void *)uSecFunc, //0x00
(void *)putConsole, //0x04
(void *)getConsole, //0x08
(void *)ExtCfg, //0x0c
(void *)ExtSet, //0x10
(void *)ExtInp, //0x14
(void *)PinSetBit, //0x18
(void *)PinReadFunc, //0x1c
(void *)MMPrintString, //0x20
(void *)IntToStr, //0x24
(void *)CheckAbort, //0x28
(void *)GetMemory, //0x2c
(void *)GetTempMemory, //0x30
(void *)FreeMemory, //0x34
(void *)&DrawRectangle, //0x38
(void *)&DrawBitmap, //0x3c
(void *)DrawLine, //0x40
(void *)FontTable, //0x44
(void *)&ExtCurrentConfig, //0x48
(void *)&HRes, //0x4C
(void *)&VRes, //0x50
(void *)SoftReset, //0x54
(void *)error, //0x58
(void *)&ProgMemory, //0x5c
(void *)&g_vartbl, //0x60
(void *)&g_varcnt, //0x64
(void *)&DrawBuffer, //0x68
(void *)&ReadBuffer, //0x6c
(void *)&FloatToStr, //0x70
(void *)CallExecuteProgram, //0x74
(void *)&CFuncmSec, //0x78
(void *)CFuncRam, //0x7c
(void *)&ScrollLCD, //0x80
(void *)IntToFloat, //0x84
(void *)FloatToInt64, //0x88
(void *)&Option, //0x8c
(void *)sin, //0x90
(void *)DrawCircle, //0x94
(void *)DrawTriangle, //0x98
(void *)uSecTimer, //0x9c
(void *)FMul,//0xa0
(void *)FAdd,//0xa4
(void *)FSub,//0xa8
(void *)FDiv,//0xac
(void *)FCmp,//0xb0
(void *)&LoadFloat,//0xb4
(void *)&CFuncInt1, //0xb8
(void *)&CFuncInt2, //0xbc
(void *)&CSubComplete, //0xc0
(void *)&AudioOutput, //0xc4
(void *)IDiv,//0x0xc8
(void *)&AUDIO_WRAP,//0x0xcc
(void *)&CFuncInt3, //0xb8
(void *)&CFuncInt4, //0xbc
(void *)PIOExecute,
};
const struct s_PinDef PinDef[]={
{ 0, 99, "NULL", UNUSED ,99, 99},
{ 1, 0, "GP0", DIGITAL_IN | DIGITAL_OUT | SPI0RX | UART0TX | I2C0SDA | PWM0A,99,0}, // pin 1
{ 2, 1, "GP1", DIGITAL_IN | DIGITAL_OUT | UART0RX | I2C0SCL | PWM0B ,99,128}, // pin 2
{ 3, 99, "GND", UNUSED ,99,99}, // pin 3
{ 4, 2, "GP2", DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA | PWM1A ,99,1}, // pin 4
{ 5, 3, "GP3", DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL | PWM1B ,99,129}, // pin 5
{ 6, 4, "GP4", DIGITAL_IN | DIGITAL_OUT | SPI0RX| UART1TX | I2C0SDA | PWM2A ,99,2}, // pin 6
{ 7, 5, "GP5", DIGITAL_IN | DIGITAL_OUT | UART1RX | I2C0SCL | PWM2B ,99,130}, // pin 7
{ 8, 99, "GND", UNUSED ,99, 99}, // pin 8
{ 9, 6, "GP6", DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA | PWM3A ,99, 3}, // pin 9
{ 10, 7, "GP7", DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL | PWM3B ,99, 131}, // pin 10
{ 11, 8, "GP8", DIGITAL_IN | DIGITAL_OUT | SPI1RX | UART1TX | I2C0SDA | PWM4A ,99, 4}, // pin 11
{ 12, 9, "GP9", DIGITAL_IN | DIGITAL_OUT | UART1RX | I2C0SCL | PWM4B ,99, 132}, // pin 12
{ 13, 99, "GND", UNUSED ,99, 99}, // pin 13
{ 14, 10, "GP10", DIGITAL_IN | DIGITAL_OUT | SPI1SCK | I2C1SDA | PWM5A ,99, 5}, // pin 14
{ 15, 11, "GP11", DIGITAL_IN | DIGITAL_OUT | SPI1TX | I2C1SCL | PWM5B ,99, 133}, // pin 15
#ifdef HDMI
{ 16, 12, "HDMI", UNUSED ,99, 99}, // pin 16
{ 17, 13, "HDMI", UNUSED ,99, 99}, // pin 17
{ 18, 99, "GND", UNUSED ,99, 99}, // pin 18
{ 19, 14, "HDMI", UNUSED ,99, 99}, // pin 19
{ 20, 15, "HDMI", UNUSED ,99, 99}, // pin 20
{ 21, 16, "HDMI", UNUSED ,99, 99}, // pin 21
{ 22, 17, "HDMI", UNUSED ,99, 99}, // pin 22
{ 23, 99, "GND", UNUSED ,99, 99}, // pin 23
{ 24, 18, "HDMI", UNUSED ,99, 99}, // pin 24
{ 25, 19, "HDMI", UNUSED ,99, 99}, // pin 25
#else
{ 16, 12, "GP12", DIGITAL_IN | DIGITAL_OUT | SPI1RX | UART0TX | I2C0SDA | PWM6A ,99, 6}, // pin 16
{ 17, 13, "GP13", DIGITAL_IN | DIGITAL_OUT | UART0RX | I2C0SCL | PWM6B ,99, 134}, // pin 17
{ 18, 99, "GND", UNUSED ,99, 99}, // pin 18
{ 19, 14, "GP14", DIGITAL_IN | DIGITAL_OUT | SPI1SCK | I2C1SDA | PWM7A ,99, 7}, // pin 19
{ 20, 15, "GP15", DIGITAL_IN | DIGITAL_OUT | SPI1TX | I2C1SCL | PWM7B ,99, 135}, // pin 20
{ 21, 16, "GP16", DIGITAL_IN | DIGITAL_OUT | SPI0RX | UART0TX | I2C0SDA | PWM0A ,99, 0}, // pin 21
{ 22, 17, "GP17", DIGITAL_IN | DIGITAL_OUT | UART0RX | I2C0SCL | PWM0B ,99, 128}, // pin 22
{ 23, 99, "GND", UNUSED ,99, 99}, // pin 23
{ 24, 18, "GP18", DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA | PWM1A ,99, 1}, // pin 24
{ 25, 19, "GP19", DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL | PWM1B ,99, 129}, // pin 25
#endif
{ 26, 20, "GP20", DIGITAL_IN | DIGITAL_OUT | SPI0RX | UART1TX| I2C0SDA | PWM2A ,99, 2}, // pin 26
{ 27, 21, "GP21", DIGITAL_IN | DIGITAL_OUT | UART1RX| I2C0SCL | PWM2B ,99, 130}, // pin 27
{ 28, 99, "GND", UNUSED ,99, 99}, // pin 28
{ 29, 22, "GP22", DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA| PWM3A ,99, 3}, // pin 29
{ 30, 99, "RUN", UNUSED ,99, 99}, // pin 30
{ 31, 26, "GP26", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | SPI1SCK| I2C1SDA | PWM5A , 0 , 5},// pin 31
{ 32, 27, "GP27", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | SPI1TX| I2C1SCL | PWM5B , 1, 133},// pin 32
{ 33, 99, "AGND", UNUSED ,99, 99}, // pin 33
{ 34, 28, "GP28", DIGITAL_IN |DIGITAL_OUT| ANALOG_IN| SPI1RX| UART0TX|I2C0SDA| PWM6A, 2, 6},// pin 34
{ 35, 99, "VREF", UNUSED ,99, 99}, // pin 35
{ 36, 99, "3V3", UNUSED ,99, 99}, // pin 36
{ 37, 99, "3V3E", UNUSED ,99, 99}, // pin 37
{ 38, 99, "GND", UNUSED ,99, 99}, // pin 38
{ 39, 99, "VSYS", UNUSED ,99, 99}, // pin 39
{ 40, 99, "VBUS", UNUSED ,99, 99}, // pin 40
#ifndef PICOMITEWEB
{ 41, 23, "GP23", DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL| PWM3B ,99 , 131}, // pseudo pin 41
{ 42, 24, "GP24", DIGITAL_IN | DIGITAL_OUT | SPI1RX | UART1TX | I2C0SDA| PWM4A ,99 , 4}, // pseudo pin 42
{ 43, 25, "GP25", DIGITAL_IN | DIGITAL_OUT | UART1RX | I2C0SCL| PWM4B ,99 , 132}, // pseudo pin 43
{ 44, 29, "GP29", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | UART0RX | I2C0SCL | PWM6B, 3, 134},// pseudo pin 44
#endif
#ifdef rp2350
#ifndef PICOMITEWEB
{ 45, 30, "GP30", DIGITAL_IN | DIGITAL_OUT | SPI1SCK | I2C1SDA | PWM7A ,99 , 7}, // pseudo pin 45
{ 46, 31, "GP31", DIGITAL_IN | DIGITAL_OUT | SPI1TX | I2C1SCL| PWM7B ,99 , 135}, // pseudo pin 46
{ 47, 32, "GP32", DIGITAL_IN | DIGITAL_OUT | UART0TX | SPI0RX | I2C0SDA| PWM8A ,99 , 8}, // pseudo pin 47
{ 48, 33, "GP33", DIGITAL_IN | DIGITAL_OUT | UART0RX | I2C0SCL| PWM8B ,99 , 136}, // pseudo pin 48
{ 49, 34, "GP34", DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA| PWM9A ,99 , 9}, // pseudo pin 49
{ 50, 35, "GP35", DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL| PWM9B ,99 , 137}, // pseudo pin 50
{ 51, 36, "GP36", DIGITAL_IN | DIGITAL_OUT | UART1TX | SPI0RX | I2C0SDA| PWM10A ,99 , 10}, // pseudo pin 51
{ 52, 37, "GP37", DIGITAL_IN | DIGITAL_OUT | UART1RX | I2C0SCL| PWM10B ,99 , 138}, // pseudo pin 52
{ 53, 38, "GP38", DIGITAL_IN | DIGITAL_OUT | SPI0SCK | I2C1SDA| PWM11A ,99 , 11}, // pseudo pin 53
{ 54, 39, "GP39", DIGITAL_IN | DIGITAL_OUT | SPI0TX | I2C1SCL| PWM11B ,99 , 139}, // pseudo pin 54
{ 55, 40, "GP40", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN| UART1TX | SPI1RX | I2C0SDA| PWM8A ,0 , 8}, // pseudo pin 55
{ 56, 41, "GP41", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN| UART1RX | I2C0SCL| PWM8B ,1 , 136}, // pseudo pin 56
{ 57, 42, "GP42", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN| SPI1SCK | I2C1SDA| PWM9A ,2 , 9}, // pseudo pin 57
{ 58, 43, "GP43", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN| SPI1TX | I2C1SCL| PWM9B ,3 , 137}, // pseudo pin 58
{ 59, 44, "GP44", DIGITAL_IN | DIGITAL_OUT | UART0TX | ANALOG_IN | SPI1RX | I2C0SDA| PWM10A ,4 , 10}, // pseudo pin 59
{ 60, 45, "GP45", DIGITAL_IN | DIGITAL_OUT | UART0RX | ANALOG_IN | I2C0SCL| PWM10B ,5 , 138}, // pseudo pin 60
{ 61, 46, "GP46", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | SPI1SCK | I2C1SDA| PWM11A ,6 , 11}, // pseudo pin 61
{ 62, 47, "GP47", DIGITAL_IN | DIGITAL_OUT | ANALOG_IN | SPI1TX | I2C1SCL| PWM11B ,7 , 139}, // pseudo pin 62
#endif
#endif
};
char alive[]="\033[?25h";
const char DaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
static inline CommandToken commandtbl_decode(const unsigned char *p){
return ((CommandToken)(p[0] & 0x7f)) | ((CommandToken)(p[1] & 0x7f)<<7);
}
char banner[64];
void __not_in_flash_func(routinechecks)(void){
static int when=0;
if(abs((time_us_64()-mSecTimer*1000))> 5000){
cancel_repeating_timer(&timer);
add_repeating_timer_us(-1000, timer_callback, NULL, &timer);
mSecTimer=time_us_64()/1000;
}
if (CurrentlyPlaying == P_WAV || CurrentlyPlaying == P_FLAC || CurrentlyPlaying==P_MP3 || CurrentlyPlaying==P_MIDI ){
#ifdef PICOMITE
if(SPIatRisk)mutex_enter_blocking(&frameBufferMutex); // lock the frame buffer
#endif
checkWAVinput();
#ifdef PICOMITE
if(SPIatRisk)mutex_exit(&frameBufferMutex);
#endif
}
if(CurrentlyPlaying == P_MOD) checkWAVinput();
if(++when & 7 && CurrentLinePtr) return;
#ifdef USBKEYBOARD
if(USBenabled){
if(mSecTimer>2000){
tuh_task();
hid_app_task();
}
}
#else
static int c, read=0;
if(tud_cdc_connected() && (Option.SerialConsole==0 || Option.SerialConsole>4) && Option.Telnet!=-1){
while(( c=tud_cdc_read_char())!=-1){
ConsoleRxBuf[ConsoleRxBufHead] = c;
if(BreakKey && ConsoleRxBuf[ConsoleRxBufHead] == BreakKey) {// if the user wants to stop the progran
MMAbort = true; // set the flag for the interpreter to see
ConsoleRxBufHead = ConsoleRxBufTail; // empty the buffer
} else if(ConsoleRxBuf[ConsoleRxBufHead] == keyselect && KeyInterrupt!=NULL){
Keycomplete=true;
} else {
ConsoleRxBufHead = (ConsoleRxBufHead + 1) % CONSOLE_RX_BUF_SIZE; // advance the head of the queue
if(ConsoleRxBufHead == ConsoleRxBufTail) { // if the buffer has overflowed
ConsoleRxBufTail = (ConsoleRxBufTail + 1) % CONSOLE_RX_BUF_SIZE; // throw away the oldest char
}
}
}
}
#endif
if(GPSchannel)processgps();
if(diskchecktimer == 0)CheckSDCard();
#ifdef GUICONTROLS
if(Ctrl)ProcessTouch();
#endif
// if(tud_cdc_connected() && KeyCheck==0){
// SSPrintString(alive);
// }
if(clocktimer==0 && Option.RTC){
RtcGetTime(0);
clocktimer=(1000*60*60);
}
#ifndef USBKEYBOARD
if(Option.KeyboardConfig==CONFIG_I2C && KeyCheck==0){
if(read==0){
CheckI2CKeyboard(0,0);
read=1;
} else {
CheckI2CKeyboard(0,1);
read=0;
}
KeyCheck=KEYCHECKTIME;
}
#endif
if(classic1 && ClassicTimer>=10){
if(classicread==false){
WiiSend(sizeof(readcontroller),(char *)readcontroller);
classicread=true;
} else {
classicread=false;
classic1=2;
WiiReceive(6, (char *)nunbuff);
classicproc();
}
ClassicTimer=0;
}
if(nunchuck1 && NunchuckTimer>=10){
if(nunchuckread==false){
WiiSend(sizeof(readcontroller),(char *)readcontroller);
nunchuckread=true;
} else {
nunchuckread=false;
nunchuck1=2;
WiiReceive(6, (char *)nunbuff);
nunproc();
}
NunchuckTimer=0;
}
}
int __not_in_flash_func(getConsole)(void) {
int c=-1;
#ifdef PICOMITEWEB
ProcessWeb(1);
#endif
CheckAbort();
if(ConsoleRxBufHead != ConsoleRxBufTail) { // if the queue has something in it
c = ConsoleRxBuf[ConsoleRxBufTail];
ConsoleRxBufTail = (ConsoleRxBufTail + 1) % CONSOLE_RX_BUF_SIZE; // advance the head of the queue
}
return c;
}
void putConsole(int c, int flush) {
if(OptionConsole & 2)DisplayPutC(c);
if(OptionConsole & 1)SerialConsolePutC(c, flush);
}
// put a character out to the serial console
char SerialConsolePutC(char c, int flush) {
if(c == '\b') {
if (MMCharPos!=1){
MMCharPos -= 1;
}
}
#ifdef PICOMITEWEB
if(Option.Telnet!=-1){
#endif
#ifndef USBKEYBOARD
if(Option.SerialConsole==0 || Option.SerialConsole>4){
if(tud_cdc_connected()){
putc(c,stdout);
if(flush){
fflush(stdout);
}
}
}
#endif
if(Option.SerialConsole){
int empty=uart_is_writable((Option.SerialConsole & 3)==1 ? uart0 : uart1);
while(ConsoleTxBufTail == ((ConsoleTxBufHead + 1) % CONSOLE_TX_BUF_SIZE)); //wait if buffer full
ConsoleTxBuf[ConsoleTxBufHead] = c; // add the char
ConsoleTxBufHead = (ConsoleTxBufHead + 1) % CONSOLE_TX_BUF_SIZE; // advance the head of the queue
if(empty){
while(irqs){}
uart_set_irq_enables((Option.SerialConsole & 3)==1 ? uart0 : uart1, true, true);
irq_set_pending((Option.SerialConsole & 3)==1 ? UART0_IRQ : UART1_IRQ);
}
}
#ifdef PICOMITEWEB
}
TelnetPutC(c,flush);
ProcessWeb(1);
#endif
return c;
}
char MMputchar(char c, int flush) {
putConsole(c, flush);
if(isprint(c)) MMCharPos++;
if(c == '\r') {
MMCharPos = 1;
}
return c;
}
// returns the number of character waiting in the console input queue
int kbhitConsole(void) {
int i;
i = ConsoleRxBufHead - ConsoleRxBufTail;
if(i < 0) i += CONSOLE_RX_BUF_SIZE;
return i;
}
// check if there is a keystroke waiting in the buffer and, if so, return with the char
// returns -1 if no char waiting
// the main work is to check for vt100 escape code sequences and map to Maximite codes
#if (defined(PICOMITEVGA) || defined(PICOMITEWEB)) && !defined(rp2350)
int MMInkey(void) {
#else
int __not_in_flash_func(MMInkey)(void) {
#endif
unsigned int c = -1; // default no character
unsigned int tc = -1; // default no character
unsigned int ttc = -1; // default no character
static unsigned int c1 = -1;
static unsigned int c2 = -1;
static unsigned int c3 = -1;
static unsigned int c4 = -1;
// static int crseen = 0;
if(c1 != -1) { // check if there are discarded chars from a previous sequence
c = c1; c1 = c2; c2 = c3; c3 = c4; c4 = -1; // shuffle the queue down
return c; // and return the head of the queue
}
c = getConsole(); // do discarded chars so get the char
#ifndef USBKEYBOARD
if(c==-1)CheckKeyboard();
#endif
if(!(c==0x1b))return c;
InkeyTimer = 0; // start the timer
while((c = getConsole()) == -1 && InkeyTimer < 30); // get the second char with a delay of 30mS to allow the next char to arrive
if(c == 'O'){ //support for many linux terminal emulators
while((c = getConsole()) == -1 && InkeyTimer < 50); // delay some more to allow the final chars to arrive, even at 1200 baud
if(c == 'P') return F1;
if(c == 'Q') return F2;
if(c == 'R') return F3;
if(c == 'S') return F4;
if(c == 'T') return F5;
if(c == '2'){
while((tc = getConsole()) == -1 && InkeyTimer < 70); // delay some more to allow the final chars to arrive, even at 1200 baud
if(tc == 'R') return F3 + 0x20;
c1 = 'O'; c2 = c; c3 = tc; return 0x1b; // not a valid 4 char code
}
c1 = 'O'; c2 = c; return 0x1b; // not a valid 4 char code
}
if(c != '[') { c1 = c; return 0x1b; } // must be a square bracket
while((c = getConsole()) == -1 && InkeyTimer < 50); // get the third char with delay
if(c == 'A') return UP; // the arrow keys are three chars
if(c == 'B') return DOWN;
if(c == 'C') return RIGHT;
if(c == 'D') return LEFT;
if(c < '1' && c > '6') { c1 = '['; c2 = c; return 0x1b; } // the 3rd char must be in this range
while((tc = getConsole()) == -1 && InkeyTimer < 70); // delay some more to allow the final chars to arrive, even at 1200 baud
if(tc == '~') { // all 4 char codes must be terminated with ~
if(c == '1') return HOME;
if(c == '2') return INSERT;
if(c == '3') return DEL;
if(c == '4') return END;
if(c == '5') return PUP;
if(c == '6') return PDOWN;
c1 = '['; c2 = c; c3 = tc; return 0x1b; // not a valid 4 char code
}
while((ttc = getConsole()) == -1 && InkeyTimer < 90); // get the 5th char with delay
if(ttc == '~') { // must be a ~
if(c == '1') {
if(tc >='1' && tc <= '5') return F1 + (tc - '1'); // F1 to F5
if(tc >='7' && tc <= '9') return F6 + (tc - '7'); // F6 to F8
}
if(c == '2') {
if(tc =='0' || tc == '1') return F9 + (tc - '0'); // F9 and F10
if(tc =='3' || tc == '4') return F11 + (tc - '3'); // F11 and F12
if(tc =='5' || tc=='6') return F3 + 0x20 + tc-'5'; // SHIFT-F3 and F4
if(tc =='8' || tc=='9') return F5 + 0x20 + tc-'8'; // SHIFT-F5 and F6
}
if(c == '3') {
if(tc >='1' && tc <= '4') return F7 + 0x20 + (tc - '1'); // SHIFT-F7 to F10
}
//NB: SHIFT F1, F2, F11, and F12 don't appear to generate anything
}
// nothing worked so bomb out
c1 = '['; c2 = c; c3 = tc; c4 = ttc;
return 0x1b;
}
// get a line from the keyboard or a serial file handle
// filenbr == 0 means the console input
void MMgetline(int filenbr, char *p) {
int c, nbrchars = 0;
char *tp;
while(1) {
CheckAbort();
if(FileTable[filenbr].com > MAXCOMPORTS && FileEOF(filenbr)) break;
c = MMfgetc(filenbr);
if(c <= 0) continue; // keep looping if there are no chars
// if this is the console, check for a programmed function key and insert the text
if(filenbr == 0) {
tp = NULL;
if(c == F2) tp = "RUN";
if(c == F3) tp = "LIST";
if(c == F4) tp = "EDIT";
if(c == F10) tp = "AUTOSAVE";
if(c == F11) tp = "XMODEM RECEIVE";
if(c == F12) tp = "XMODEM SEND";
if(c == F1) tp = (char *)Option.F1key;
if(c == F5) tp = (char *)Option.F5key;
if(c == F6) tp = (char *)Option.F6key;
if(c == F7) tp = (char *)Option.F7key;
if(c == F8) tp = (char *)Option.F8key;
if(c == F9) tp = (char *)Option.F9key;
if(tp) {
strcpy(p, tp);
if(EchoOption) { MMPrintString(tp); MMPrintString("\r\n"); }
return;
}
}
if(c == '\t') { // expand tabs to spaces
do {
if(++nbrchars > MAXSTRLEN) error("Line is too long");
*p++ = ' ';
if(filenbr == 0 && EchoOption) MMputchar(' ',1);
} while(nbrchars % 4);
continue;
}
if(c == '\b') { // handle the backspace
if(nbrchars) {
if(filenbr == 0 && EchoOption) MMPrintString("\b \b");
nbrchars--;
p--;
}
continue;
}
if(c == '\n') { // what to do with a newline
break; // a newline terminates a line (for a file)
}
if(c == '\r') {
if(filenbr == 0 && EchoOption) {
MMPrintString("\r\n");
break; // on the console this means the end of the line - stop collecting
} else
continue ; // for files loop around looking for the following newline
}
if(isprint(c)) {
if(filenbr == 0 && EchoOption) MMputchar(c,1); // The console requires that chars be echoed
}
if(++nbrchars > MAXSTRLEN) error("Line is too long"); // stop collecting if maximum length
*p++ = c; // save our char
}
*p = 0;
}
// insert a string into the start of the lastcmd buffer.
// the buffer is a sequence of strings separated by a zero byte.
// using the up arrow usere can call up the last few commands executed.
void MIPS16 InsertLastcmd(unsigned char *s) {
int i, slen;
if(strcmp((const char *)lastcmd, (const char *)s) == 0) return; // don't duplicate
slen = strlen((const char *)s);
if(slen < 1 || slen > sizeof(lastcmd) - 1) return;
slen++;
for(i = sizeof(lastcmd) - 1; i >= slen ; i--)
lastcmd[i] = lastcmd[i - slen]; // shift the contents of the buffer up
strcpy((char *)lastcmd, (char *)s); // and insert the new string in the beginning
for(i = sizeof(lastcmd) - 1; lastcmd[i]; i--) lastcmd[i] = 0; // zero the end of the buffer
}
void MIPS16 EditInputLine(void) {
char *p = NULL;
char buf[MAXKEYLEN + 3];
char goend[10];
int lastcmd_idx, lastcmd_edit;
int insert, /*startline,*/ maxchars;
int CharIndex, BufEdited;
int c, i, j;
int l4,l3,l2;
maxchars=255;
if(Option.DISPLAY_CONSOLE && Option.Width<=SCREENWIDTH){ //We will always assume the Vt100 is 80 colums if LCD is the console <=80.
l2=SCREENWIDTH+1-MMPromptPos;
l3=2*SCREENWIDTH+2-MMPromptPos;
l4=3*SCREENWIDTH+3-MMPromptPos;
}else{ // otherwise assume the VT100 matches Option.Width
l2=Option.Width +1-MMPromptPos;
l3=2*Option.Width+2-MMPromptPos;
l4=3*Option.Width+3-MMPromptPos;
}
// Build "\e[80C" equivalent string for the line length
//strcpy(goend,"\e[");IntToStr(linelen,l2+MMPromptPos, 10);strcat(goend,linelen); strcat(goend, "C");
strcpy(goend,"\e[");IntToStr(&goend[strlen(goend)],l2+MMPromptPos, 10);strcat(goend, "C");
MMPrintString((char *)inpbuf); // display the contents of the input buffer (if any)
CharIndex = strlen((const char *)inpbuf); // get the current cursor position in the line
insert = false;
// Cursor = C_STANDARD;
lastcmd_edit = lastcmd_idx = 0;
BufEdited = false; //(CharIndex != 0);
while(1) {
c = MMgetchar();
if(c == TAB) {
strcpy(buf, " ");
switch (Option.Tab) {
case 2:
buf[2 - (CharIndex % 2)] = 0; break;
case 3:
buf[3 - (CharIndex % 3)] = 0; break;
case 4:
buf[4 - (CharIndex % 4)] = 0; break;
case 8:
buf[8 - (CharIndex % 8)] = 0; break;
}
} else {
buf[0] = c;
buf[1] = 0;
}
do {
switch(buf[0]) {
case '\r':
case '\n': //if(autoOn && atoi(inpbuf) > 0) autoNext = atoi(inpbuf) + autoIncr;
//if(autoOn && !BufEdited) *inpbuf = 0;
goto saveline;
break;
case '\b':
if(CharIndex > 0) {
BufEdited = true;
i = CharIndex - 1;
j= CharIndex;
for(p = (char *)inpbuf + i; *p; p++) *p = *(p + 1); // remove the char from inpbuf
// Lets put the cursor at the beginning of where the command is displayed.
// backspace to the beginning of line
#define USEBACKSPACE
#ifdef USEBACKSPACE
while(j) {
if (j==l4 || j==l3 ||j==l2 ){DisplayPutC('\b');SSPrintString("\e[1A");SSPrintString(goend);}else{ MMputchar('\b',0);}
j--;
}
fflush(stdout);
MX470Display(CLEAR_TO_EOS);SSPrintString("\033[0J"); //Clear to End Of Screen
#else
CurrentX=0;CurrentY=CurrentY-((CharIndex+1)/Option.Width * gui_font_height);
if (CharIndex>l4-1)SSPrintString("\e[3A");
else if (CharIndex>l3-1)SSPrintString("\e[2A");
else if(CharIndex>l2-1)SSPrintString("\e[1A");
SSPrintString("\r");
//CurrentX=0;SerUSBPutS("\r");
MX470Display(CLEAR_TO_EOS);SSPrintString("\033[0J");
MMPrintString("> ");
fflush(stdout);
#endif
j=0;
while(j < strlen((const char *)inpbuf)) {
MMputchar(inpbuf[j],0);
if((j==l4-1 || j==l3-1 || j==l2-1 ) && j == strlen((const char *)inpbuf)-1 ){SSPrintString(" ");SSPrintString("\b");}
if((j==l4-1 || j==l3-1 || j==l2-1 ) && j < strlen((const char *)inpbuf)-1 ){SerialConsolePutC(inpbuf[j+1],0);SSPrintString("\b");}
j++;
}
fflush(stdout);
// return the cursor to the right position
for(j = strlen((const char *)inpbuf); j > i; j--){
if (j==l4 || j==l3 || j==l2) {DisplayPutC('\b');SSPrintString("\e[1A");SSPrintString(goend);}else{MMputchar('\b',0);}
}
CharIndex--;
fflush(stdout);
if(strlen((const char *)inpbuf)==0)BufEdited = false;
}
break;
case CTRLKEY('S'):
case LEFT:
BufEdited = true;
insert=false; //left at first char will turn OVR on
if(CharIndex > 0) {
// if(CharIndex == strlen((const char *)inpbuf)) {
//insert = true;
// }
if (CharIndex==l4 || CharIndex==l3 || CharIndex==l2 ){DisplayPutC('\b');SSPrintString("\e[1A");SSPrintString(goend);}else{MMputchar('\b',1);}
insert=true; //Any left turns on INS
CharIndex--;
}
break;
case CTRLKEY('D'):
case RIGHT:
if(CharIndex < strlen((const char *)inpbuf)) {
BufEdited = true;
MMputchar(inpbuf[CharIndex],1);
if((CharIndex==l4-1 || CharIndex==l3-1|| CharIndex==l2-1 ) && CharIndex == strlen((const char *)inpbuf)-1 ){SSPrintString(" ");SSPrintString("\b");}
if((CharIndex==l4-1 || CharIndex==l3-1|| CharIndex==l2-1 ) && CharIndex < strlen((const char *)inpbuf)-1 ){SerialConsolePutC(inpbuf[CharIndex+1],0);SSPrintString("\b");}
CharIndex++;
}
// insert=false; //right always switches to OVER
break;
case CTRLKEY(']'):
case DEL:
if(CharIndex < strlen((const char *)inpbuf)) {
BufEdited = true;
i = CharIndex;
for(p = (char *)inpbuf + i; *p; p++) *p = *(p + 1); // remove the char from inpbuf
j = strlen((const char *)inpbuf);
// Lets put the cursor at the beginning of where the command is displayed.
// backspace to the beginning of line
j=CharIndex;
while(j) {
if (j==l4 || j==l3 ||j==l2 ){DisplayPutC('\b');SSPrintString("\e[1A");SSPrintString(goend);}else{ MMputchar('\b',0);}
j--;
}
fflush(stdout);
MX470Display(CLEAR_TO_EOS);SSPrintString("\033[0J"); //Clear to End Of Screen
j=0;
while(j < strlen((const char *)inpbuf)) {
MMputchar(inpbuf[j],0);
if((j==l4-1 || j==l3-1 || j==l2-1 ) && j == strlen((const char *)inpbuf)-1 ){SSPrintString(" ");SSPrintString("\b");}
if((j==l4-1 || j==l3-1 || j==l2-1 ) && j < strlen((const char *)inpbuf)-1 ){SerialConsolePutC(inpbuf[j+1],0);SSPrintString("\b");}
j++;
}
fflush(stdout);
// return the cursor to the right position
for(j = strlen((const char *)inpbuf); j > i; j--){
if (j==l4 || j==l3 || j==l2) {DisplayPutC('\b');SSPrintString("\e[1A");SSPrintString(goend);}else{ MMputchar('\b',0);}
}
fflush(stdout);
}
break;
case CTRLKEY('N'):
case INSERT:insert = !insert;
// Cursor = C_STANDARD + insert;
break;
case CTRLKEY('U'):
case HOME:
BufEdited = true;
if(CharIndex > 0) {
if(CharIndex == strlen((const char *)inpbuf)) {
insert = true;
// Cursor = C_INSERT;
}
// backspace to the beginning of line
while(CharIndex) {
if (CharIndex==l4 || CharIndex==l3 || CharIndex==l2 ){DisplayPutC('\b');SSPrintString("\e[1A");SSPrintString(goend);}else{MMputchar('\b',0);}
CharIndex--;
}
fflush(stdout);
}else{ //HOME @ home turns off edit mode
BufEdited = false;
insert=false; //home at first char will turn OVR on
}
break;
case CTRLKEY('K'):
case END:
BufEdited = true;
while(CharIndex < strlen((const char *)inpbuf)){
MMputchar(inpbuf[CharIndex++],0);
}
fflush(stdout);
break;
/* if(c == F2) tp = "RUN";
if(c == F3) tp = "LIST";
if(c == F4) tp = "EDIT";
if(c == F10) tp = "AUTOSAVE";
if(c == F11) tp = "XMODEM RECEIVE";
if(c == F12) tp = "XMODEM SEND";
if(c == F5) tp = Option.F5key;
if(c == F6) tp = Option.F6key;
if(c == F7) tp = Option.F7key;
if(c == F8) tp = Option.F8key;
if(c == F9) tp = Option.F9key;
*/
case 0x91:
if(*Option.F1key)strcpy(&buf[1],(char *)Option.F1key);
break;
case 0x92:
strcpy(&buf[1],"RUN\r\n");
break;
case 0x93:
strcpy(&buf[1],"LIST\r\n");
break;
case 0x94:
strcpy(&buf[1],"EDIT\r\n");
break;
case 0x95:
if(*Option.F5key){
strcpy(&buf[1],(char *)Option.F5key);
}else{
/*** F5 will clear the VT100 ***/
SSPrintString("\e[2J\e[H");
fflush(stdout);
if(Option.DISPLAY_CONSOLE){ClearScreen(gui_bcolour);CurrentX=0;CurrentY=0;}
if(FindSubFun((unsigned char *)"MM.PROMPT", 0) >= 0) {
ExecuteProgram((unsigned char *)"MM.PROMPT\0");