-
Notifications
You must be signed in to change notification settings - Fork 1
/
XtsTinyBasicPlus.ino
3674 lines (3061 loc) · 81.5 KB
/
XtsTinyBasicPlus.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
////////////////////////////////////////////////////////////////////////////////
// TinyBasic Plus Xts
////////////////////////////////////////////////////////////////////////////////
//
// Modified by: Franck GALLIAT -- Xtase <fgalliat@gmail.com> on Nov 2016 -> va.15
//
// Authors: Mike Field <hamster@snap.net.nz>
// Scott Lawrence <yorgle@gmail.com>
// Brian O'Dell <megamemnon@megamemnon.com>
// ============================================================
// IF testing with Visual C, this needs to be the first thing in the file.
//#include "stdafx.h"
// hack to let makefiles work with this file unchanged
#ifdef FORCE_DESKTOP
#undef ARDUINO
#else
//#define ARDUINO 1
#endif
// ============================================================
// Teensy ++2.0 mode
#define teen2 1
void printXts(const unsigned char *msg, bool altSerial = false);
// ===== buttons support =====
#define USER_BTNS_SUPPORT 1
#ifdef USER_BTNS_SUPPORT
#define BTN_UP 0x01
#define BTN_DOWN 0x02
#define BTN_OK 0x04
// on a teensy ++2.0 (C5 - C7)
#define BTN_PIN_DOWN 15
#define BTN_PIN_UP 16
#define BTN_PIN_OK 17
int getBtnPressed() {
// pull-up default mode
int up = ( digitalRead( BTN_PIN_UP ) == LOW ) ? BTN_UP : 0x00;
int dn = ( digitalRead( BTN_PIN_DOWN ) == LOW ) ? BTN_DOWN : 0x00;
int ok = ( digitalRead( BTN_PIN_OK ) == LOW ) ? BTN_OK : 0x00;
return up | dn | ok;
}
#endif
// ===== String for http page serving (for ex.) ====
// redirects output to String buffer
bool bufferedOutput = false;
#define OUT_BUFF_LEN 1024
char outputBuffer[OUT_BUFF_LEN];
int outputBufferCursor = 0;
void cleanOutBuff() {
for(int i=0; i < OUT_BUFF_LEN; i++) { outputBuffer[i]=0x00; }
outputBufferCursor = 0;
}
// =================================================
#define WIFI_SUPPORT 1
#define LCD_SUPPORT 1
#define ALT_SER_PORT 1
//#undef ALT_SER_PORT
// '#1' representing ALT_SERIAL
#define ALT_SER_FILE 1
//#undef ALT_SER_FILE
#if defined(teen2)
#define __AVR_ATmega32U4__ 1
#endif
void chain(const char* fname);
#if defined(__AVR_ATmega32U4__)
#define ENABLE_FILEIO 1
#else
#undef ENABLE_FILEIO
#endif
#define kVersion "va.15"
static unsigned char *program_end;
static unsigned char *variables_begin;
// in bytes
int getFreeMem() {
return (variables_begin-program_end);
}
// ******************************************************
#if defined(LCD_SUPPORT)
// on Teensy ++2 -> SDA #1 SCL#0
#include <Wire.h>
#include <Adafruit_GFX.h>
// local version
#include "Adafruit_SSD1306.h"
// broken 64 defined as 32 pixels height
// it auto doubles the lines
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
void printmsgNoNLLCD(const unsigned char *msg) {
while( pgm_read_byte( msg ) != 0 ) {
display.print( (char)pgm_read_byte( msg++ ) );
};
}
void writeOnLCD(const char* title, const char* str, const char* str2 = NULL) {
display.clearDisplay();
display.setCursor(0,0);
if ( title == NULL ) {
display.println(" -=| Xts uBasic |=- ");
} else {
display.println(title);
}
display.println(str);
if ( str2 != NULL ) {
display.println(str2);
}
display.display();
}
// =========================
void lcd_cls() {
display.clearDisplay();
display.setCursor(0,0);
display.display();
}
/** lineNum = 1 to 4 */
void lcd_print(short lineNum, short col, char* str) {
display.setCursor( (6*(col-1)),( (lineNum-1) * 8) );
display.println(str);
display.display();
}
#else
void writeOnLCD(const char* title, const char* str, const char* str2 = NULL) {
// do nothing
}
#endif
// ******************************************************
static const unsigned char initmsg[] PROGMEM = "TinyBasic Plus " kVersion;
#if defined(teen2)
static const unsigned char cpuMsg[] PROGMEM = "on a Teensy ++2 MCU";
#elif defined(__AVR_ATmega32U4__)
static const unsigned char cpuMsg[] PROGMEM = "on a 32u4 MCU";
#elif defined(__AVR_ATmega328P__)
static const unsigned char cpuMsg[] PROGMEM = "on a 328P MCU";
#else
static const unsigned char cpuMsg[] PROGMEM = "on a ??? MCU";
#endif
#if defined (ENABLE_FILEIO)
static const unsigned char ioMsg[] PROGMEM = "SDIO enabled";
void cmd_Files(void);
#else
static const unsigned char ioMsg[] PROGMEM = "SDIO disabled";
#endif
// ******************************************************
bool WIFI_OK = false;
#if defined(WIFI_SUPPORT)
#define WIFI_BUFF_SIZE 128
char wifiBuff[WIFI_BUFF_SIZE];
void emptyWifiBuff() { for(int i=0; i < WIFI_BUFF_SIZE; i++) { wifiBuff[i] = 0x00; } }
#if defined(teen2)
// on Teensy ++2 -> RX #2 TX#3
#define HWSERIAL Serial1
#endif
#define WIFI_CMD_DEF_TIMEOUT 200
// cmd w/o cr/lf
void sendPassiveCmdToWifi(const char* cmd, bool outToScreen = true, bool outToSerial = true, bool checkWifiFlag = false) {
HWSERIAL.print( cmd );
HWSERIAL.print( "\r\n" );
// keep it 'cause it's redefined in wait text
HWSERIAL.setTimeout( WIFI_CMD_DEF_TIMEOUT );
long t0, t1;
t0 = millis();
emptyWifiBuff();
while( HWSERIAL.available() == 0 ) {
t1 = millis();
if (t1-t0 > 1000) { if (checkWifiFlag) { WIFI_OK = false; } return; }
delay(1);
}
int readed = 0;
if (checkWifiFlag) { WIFI_OK = false; }
do {
readed = HWSERIAL.readBytesUntil(0x00, wifiBuff, WIFI_BUFF_SIZE);
// cause : can pass many times in this loop (the last will always return 0)
if (checkWifiFlag && !WIFI_OK) { WIFI_OK = readed > 0; }
if ( readed <= 0 ) { break; }
if (outToScreen) { writeOnLCD(" -= ESP 8266 says =- ", (const char*)wifiBuff); }
if (outToSerial) { Serial.println(wifiBuff); }
emptyWifiBuff();
} while (true);
}
void sendTextToClient(int client, const char* str, bool toScr = false, bool toSer = false) {
char* cmd = (char*)malloc( 11 + 2 + 1 + 4 );
sprintf(cmd, "AT+CIPSEND=%d,%d", client, strlen(str) );
sendPassiveCmdToWifi((const char*)cmd, toScr, toSer); // 0 chanel -- 11 nb of bytes sent (max 2048)
HWSERIAL.print( str );
// just empty the buffer
emptyWifiBuff();
do {
int readed = HWSERIAL.readBytesUntil(0x00, wifiBuff, WIFI_BUFF_SIZE);
if ( readed <= 0 ) { break; }
emptyWifiBuff();
} while (true);
}
//#define WIFI_SERV_MODE_TELNET 1
#undef WIFI_SERV_MODE_TELNET
void wifiReset() {
sendPassiveCmdToWifi("AT+RST", true, true); // reset module
delay(500);
}
void wifiTestsFunc() {
//sendPassiveCmdToWifi("AT+RST"); // reset module
//delay(500);
bool defScr=false, defSer=false;
sendPassiveCmdToWifi("ATE0", defScr, defSer); // echo off -- ATE1 for echo on
//sendPassiveCmdToWifi("AT+CIPAP?", true, true); // display IP
#ifdef WIFI_SERV_MODE_TELNET
openTelnetServerAndWait(defScr,defSer);
#else
openWebServerAndWait(defScr,defSer);
#endif
// close conn.
sendPassiveCmdToWifi("AT+CIPCLOSE=0", defScr, defSer); // 0 chanel
// close server
sendPassiveCmdToWifi("AT+CIPSERVER=0", defScr, defSer); // close server
}
void waitFoClientConn(bool verbose = true) {
if ( verbose ) {
writeOnLCD(" -= ESP 8266 waits =- ", "Waiting conn.");
Serial.println("Waiting conn.");
}
emptyWifiBuff();
while( HWSERIAL.available() == 0 ) { delay(1); }
int readed = 0;
do {
readed = HWSERIAL.readBytesUntil(0x00, wifiBuff, WIFI_BUFF_SIZE);
if ( readed <= 0 ) { break; }
emptyWifiBuff();
} while (true);
// ===== has received something =====
}
const char* header = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\nConnection: close\r\n";
#define webContentSize 512
char content[webContentSize]; // the web page content
void openWebServerAndWait(bool defScr,bool defSer) {
// web server mode
// open server
sendPassiveCmdToWifi("AT+CIPMUX=1", defScr, defSer); // 1 -> allow multiple connections
sendPassiveCmdToWifi("AT+CIPSERVER=1,80", defScr, defSer); // 1 server ON -- 23 TCP port
//sendPassiveCmdToWifi("AT+CIPSTO=180", defScr, defSer); // server timeout in sec. -- 180sec is default
// wait for a connection
waitFoClientConn();
writeOnLCD(" -= ESP 8266 page =- ", "Preparing page");
Serial.println("Preparing page");
for(int i=0; i < webContentSize; i++) { content[i] = 0x00; }
strcat( content, "<html><head><title>ESP8266 uXtsBasic</title></head>\n");
strcat( content, "<body>\n");
strcat( content, "<h1>Hello World from my Teensy ++2</h1>\n");
strcat( content, "<table border=1><tr><td valign=top><pre>");
char b[30];
char myChar;
int slen = strlen_P((const char*)initmsg);
for (int k = 0; k < slen; k++) { myChar = pgm_read_byte_near(initmsg + k); b[k] = myChar; }
b[slen] = 0x0A; // as in a <pre>
b[slen+1] = 0x00;
strcat( content, b );
slen = strlen_P((const char*)cpuMsg);
for (int k = 0; k < slen; k++) { myChar = pgm_read_byte_near(cpuMsg + k); b[k] = myChar; }
b[slen] = 0x0A; // as in a <pre>
b[slen+1] = 0x00;
strcat( content, b );
slen = strlen_P((const char*)ioMsg);
for (int k = 0; k < slen; k++) { myChar = pgm_read_byte_near(ioMsg + k); b[k] = myChar; }
b[slen] = 0x0A; // as in a <pre>
b[slen+1] = 0x00;
strcat( content, b );
strcat( content, "WIFI enabled\n" );
sprintf( b, "%d bytes free\n", getFreeMem() );
strcat( content, b );
strcat( content, "</pre></td><td valign=top>\n" );
strcat( content, "<pre>" );
sprintf( b, "Time: %d sec\n", (millis()/1000) );
strcat( content, b );
strcat( content, "</pre>\n");
strcat( content, "</td></tr></table>\n");
// @ this time content is only 306 bytes long
// ==========================================
const char* openCode = "<hr/><pre>";
const char* closeCode = "</pre><hr/>";
cleanOutBuff();
bufferedOutput = true;
chain("web.bas");
bufferedOutput = false;
// done lower
//cleanOutBuff();
// ==========================================
const char* closeHtml = "</body></html>\n";
// can try to not set content-length if > 2048
//header += "Content-Length: ";
//header += (int)(content.length());
////header += "\r\n\r\n";
int headerlen = strlen( header ) + 4; // +4 for \r\n\r\n
int contentLen = strlen(content)+strlen(openCode)+strlen(outputBuffer)+strlen(closeCode)+strlen(closeHtml);
int ch_id = 0;
HWSERIAL.print("AT+CIPSEND=");
HWSERIAL.print(ch_id);
HWSERIAL.print(",");
HWSERIAL.println(headerlen+contentLen);
if (HWSERIAL.find(">")) {
HWSERIAL.print(header);
HWSERIAL.print("\r\n\r\n");
HWSERIAL.print(content);
HWSERIAL.print(openCode);
HWSERIAL.print(outputBuffer);
HWSERIAL.print(closeCode);
HWSERIAL.print(closeHtml);
delay(10);
}
cleanOutBuff();
writeOnLCD(" -= ESP 8266 sent =- ", "Served page");
Serial.println("Served page on TCP:80");
}
void openTelnetServerAndWait(bool defScr,bool defSer) {
int readed = 0;
// open server
sendPassiveCmdToWifi("AT+CIPMUX=1", defScr, defSer); // 1 -> allow multiple connections
sendPassiveCmdToWifi("AT+CIPSERVER=1,23", defScr, defSer); // 1 server ON -- 23 TCP port
sendPassiveCmdToWifi("AT+CIPSTO=180", defScr, defSer); // server timeout in sec.
// wait for a connection
waitFoClientConn();
sendTextToClient( 0, "Hello from my ESP8266 on uXtsBasic\r\n$>" );
// === wait for some text =========
writeOnLCD(" -= ESP 8266 waits =- ", "Waiting text");
Serial.println("Waiting text");
emptyWifiBuff();
HWSERIAL.setTimeout( 700 );
while( HWSERIAL.available() == 0 ) { delay(1); }
readed = 0;
// +IPD,0,1:c
// +IPD,0,1:o
// ...
// +IPD,0,1:
// can use scanf() to parse & separate values
do {
readed = HWSERIAL.readBytesUntil('/', wifiBuff, WIFI_BUFF_SIZE);
if ( readed <= 0 ) { break; }
writeOnLCD(" -= ESP 8266 rcv =- ", (const char*)wifiBuff);
Serial.println(wifiBuff);
emptyWifiBuff();
} while (true);
HWSERIAL.setTimeout( WIFI_CMD_DEF_TIMEOUT );
// ===== has received something =====
sendTextToClient( 0, "Bye from my ESP8266 on uXtsBasic\r\n" );
}
#endif
// ******************************************************
#if defined(__AVR_ATmega32U4__)
#if defined(teen2)
#define XTS_SD_CS_PIN 20
#else
#define XTS_SD_CS_PIN SS
#endif
#else
#define XTS_SD_CS_PIN 4
#endif
// _____________________________________
// if (true) altSerial becomes MASTER
// BEWARE : this is not the invert_logic FLAG
bool serialInverted = false;
// --> will make set the port speed (4800 or 8000) - invert logic - & set mode to 8N2
// --> else 9600 / 19200 - no invert logic - & set mode to 8N1
bool inX07mode = false;
#ifdef ALT_SER_PORT
// ADDED :
// KW_INIT, KW_INP, KW_COUT, --> CINIT / CINP / COUT / CPRINT
// KW_LLIST, KW_CPRINT,
#include "CustomSoftwareSerial_xtase.h"
// define default most-case speed
// can up du 19200 --> 115200 : garbage on port (from CustomSerial.h)
// PS. can write '115200' in tinybasic (higher than 'short int')
//#define ALT_SER_PORT_SPEED 115200 // garbage !!!
#define ALT_SER_PORT_SPEED 9600
//#define ALT_SER_PORT_SPEED 19200 // max
// X-07 works @ this speed but need a pause between chars sent
// when in 'CMD' mode (because char processing is longer than in free-hand inputs)
#define ALT_SER_PORT_SPEED_X07 8000
//#define ALT_SER_PORT_SPEED_X07 4800
#define X07_DELAY_BETWEEN_CHAR 90
// even if will be X-07 mode, set invert_logic to false
// will set it @ setup() time
#ifdef teen2
CustomSoftwareSerialXts altSerial(26,25, false);
#else
CustomSoftwareSerialXts altSerial(9,10, false);
#endif
void invertSerialPorts() {
serialInverted = !serialInverted;
if ( serialInverted ) { writeOnLCD(NULL,"Master is now", " ALTSerial"); }
else { writeOnLCD(NULL,"Master is now", " USB"); }
}
#endif
// _____________________________________
//#define XTS_CONSOLE_SPEED 115200
// in teensy ++2.0 -> speed doesn't matter : we're in CDC-mode
#define XTS_CONSOLE_SPEED 9600
char eliminateCompileErrors = 1; // fix to suppress arduino build errors
////////////////////////////////////////////////////////////////////////////////
// Feature option configuration...
// This enables LOAD, SAVE, FILES commands through the Arduino SD Library
// it adds 9k of usage as well.
//#define ENABLE_FILEIO 1
//#undef ENABLE_FILEIO
// this turns on "autorun". if there's FileIO, and a file "autorun.bas",
// then it will load it and run it when starting up
#define ENABLE_AUTORUN 1
//#undef ENABLE_AUTORUN
// and this is the file that gets run
#define kAutorunFilename "autorun.bas"
// this is the alternate autorun. Autorun the program in the eeprom.
// it will load whatever is in the EEProm and run it
//#define ENABLE_EAUTORUN 1
#undef ENABLE_EAUTORUN
// this will enable the "TONE", "NOTONE" command using a piezo
// element on the specified pin. Wire the red/positive/piezo to the kPiezoPin,
// and the black/negative/metal disc to ground.
// it adds 1.5k of usage as well.
//#define ENABLE_TONES 1
#undef ENABLE_TONES
#define kPiezoPin 5
// we can use the EEProm to store a program during powerdown. This is
// 1kbyte on the '328, and 512 bytes on the '168. Enabling this here will
// allow for this funcitonality to work. Note that this only works on AVR
// arduino. Disable it for DUE/other devices.
//#define ENABLE_EEPROM 1
#undef ENABLE_EEPROM
// Sometimes, we connect with a slower device as the console.
// Set your console D0/D1 baud rate here (9600 baud default)
//#define kConsoleBaud 9600
#define kConsoleBaud XTS_CONSOLE_SPEED
////////////////////////////////////////////////////////////////////////////////
#ifdef ARDUINO
#ifndef RAMEND
// okay, this is a hack for now
// if we're in here, we're a DUE probably (ARM instead of AVR)
#define RAMEND 4096-1
// turn off EEProm
#undef ENABLE_EEPROM
#undef ENABLE_TONES
#else
// we're an AVR!
// we're moving our data strings into progmem
#include <avr/pgmspace.h>
#endif
// includes, and settings for Arduino-specific functionality
#ifdef ENABLE_EEPROM
#include <EEPROM.h> /* NOTE: case sensitive */
int eepos = 0;
#endif
#ifdef ENABLE_FILEIO
#include <SD.h>
#include <SPI.h> /* needed as of 1.5 beta */
// Arduino-specific configuration
// set this to the card select for your SD shield
#define kSD_CS XTS_SD_CS_PIN
#define kSD_Fail 0
#define kSD_OK 1
// FOR AUTORUN / CHAIN / LOAD / SAVE commands
File fp;
// FOR CAT / WRITE commands (FILES has its own pointer)
File fpSdFiles;
#endif
// set up our RAM buffer size for program and user input
// NOTE: This number will have to change if you include other libraries.
#ifdef ARDUINO
#ifdef ENABLE_FILEIO
#define kRamFileIO (1030) /* approximate */
#else
#define kRamFileIO (0)
#endif
#ifdef ENABLE_TONES
#define kRamTones (40)
#else
#define kRamTones (0)
#endif
#endif /* ARDUINO */
#if defined(LCD_SUPPORT)
#define kRamLCD (940)
#else
#define kRamLCD (0)
#endif
#if defined(WIFI_SUPPORT)
#define kRamWIFI (WIFI_BUFF_SIZE + 1024 + webContentSize + 512)
#else
#define kRamWIFI (0)
#endif
// Xts Xts Xts Xts Xts Xts Xts Xts Xts Xts Xts Xts
// the EXPERIMENTAL String variables in this impl of TinyBasic
// str var len
#define STR_VAR_LEN 50
// nb of str var
#define STR_VAR_NB 4
char strVar[STR_VAR_NB][STR_VAR_LEN];
// vNum : 0 - 4
void cleanStrVar(int vNum) { for(int i=0; i < STR_VAR_LEN; i++) { strVar[vNum][i] = 0x00; } }
char* getStrVar(int vNum) { return strVar[vNum]; }
// Xts Xts Xts Xts Xts Xts Xts Xts Xts Xts Xts Xts
// Cf issue #18
//#define kRamSize (RAMEND - 1160 - kRamFileIO - kRamTones)
#define kRamSize (RAMEND - 1200 - (STR_VAR_LEN * STR_VAR_NB) - (kRamFileIO+50) - kRamTones - kRamLCD - kRamWIFI)
// for PC Setup (not sure that it still work....)
#ifndef ARDUINO
// Not arduino setup
#include <stdio.h>
#include <stdlib.h>
#undef ENABLE_TONES
// size of our program ram
#define kRamSize 4096 /* arbitrary */
#ifdef ENABLE_FILEIO
FILE * fp;
#endif
#endif
// for PC Setup (not sure that it still work....)
#ifdef ARDUINO
#define ECHO_CHARS 1
#else
#define ECHO_CHARS 0
#endif
#ifdef ENABLE_FILEIO
void cmd_Files( void );
#endif
////////////////////
#ifndef boolean
#define boolean int
#define true 1
#define false 0
#endif
#endif
#ifndef byte
typedef unsigned char byte;
#endif
// some catches for AVR based text string stuff...
#ifndef PROGMEM
#define PROGMEM
#endif
#ifndef pgm_read_byte
#define pgm_read_byte( A ) *(A)
#endif
////////////////////
#ifdef ENABLE_FILEIO
unsigned char * filenameWord(void);
static boolean sd_is_initialized = false;
#endif
boolean inhibitOutput = false;
static boolean runAfterLoad = false;
static boolean triggerRun = false;
// these will select, at runtime, where IO happens through for load/save
enum {
kStreamSerial = 0,
kStreamEEProm,
kStreamFile
};
static unsigned char inStream = kStreamSerial;
static unsigned char outStream = kStreamSerial;
////////////////////////////////////////////////////////////////////////////////
// ASCII Characters
#define CR '\r'
#define NL '\n'
#define LF 0x0a
#define TAB '\t'
#define BELL '\b'
#define SPACE ' '
#define SQUOTE '\''
#define DQUOTE '\"'
#define CTRLC 0x03
#define CTRLH 0x08
#define CTRLS 0x13
#define CTRLX 0x18
// Xts
#define HASH '#'
#define DOLLAR '$'
typedef short unsigned LINENUM;
static unsigned char program[kRamSize];
//static const char * sentinel = "HELLO";
static unsigned char *txtpos,*list_line, *tmptxtpos;
static unsigned char expression_error;
static unsigned char *tempsp;
/***********************************************************/
// Keyword table and constants - the last character has 0x80 added to it
const static unsigned char keywords[] PROGMEM = {
'L','I','S','T'+0x80,
'L','O','A','D'+0x80,
'N','E','W'+0x80,
'R','U','N'+0x80,
'S','A','V','E'+0x80,
'N','E','X','T'+0x80,
'L','E','T'+0x80,
'I','F'+0x80,
'G','O','T','O'+0x80,
'G','O','S','U','B'+0x80,
'R','E','T','U','R','N'+0x80,
'R','E','M'+0x80,
'F','O','R'+0x80,
// Xts Xts Xts Xts Xts Xts Xts Xts
// must be placed before 'input'
'I','N','P','U','T','S','T','R'+0x80,
// Xts Xts Xts Xts Xts Xts Xts Xts
'I','N','P','U','T'+0x80,
'P','R','I','N','T'+0x80,
'P','O','K','E'+0x80,
'S','T','O','P'+0x80,
'B','Y','E'+0x80,
'F','I','L','E','S'+0x80,
'M','E','M'+0x80,
'?'+ 0x80,
'\''+ 0x80,
'A','W','R','I','T','E'+0x80,
'D','W','R','I','T','E'+0x80,
'D','E','L','A','Y'+0x80,
'E','N','D'+0x80,
'R','S','E','E','D'+0x80,
'C','H','A','I','N'+0x80,
#ifdef ENABLE_TONES
'T','O','N','E','W'+0x80,
'T','O','N','E'+0x80,
'N','O','T','O','N','E'+0x80,
#endif
#ifdef ARDUINO
#ifdef ENABLE_EEPROM
'E','C','H','A','I','N'+0x80,
'E','L','I','S','T'+0x80,
'E','L','O','A','D'+0x80,
'E','F','O','R','M','A','T'+0x80,
'E','S','A','V','E'+0x80,
#endif
#endif
// Xtase ioFileDescriptor
#ifdef ALT_SER_FILE
'C','I','N','I','T'+0x80,
'C','O','U','T'+0x80,
'C','L','I','S','T'+0x80,
'C','P','R','I','N','T'+0x80,
'C','I','N','V'+0x80, // invert hard Vs soft serial
'O','U','T'+0x80, // as COUT but on master UART
#endif
// Xtase Esp8266 Wifi routines
#ifdef WIFI_SUPPORT
'W','T','E','S','T'+0x80,
'W','R','E','S','E','T'+0x80, // reset the 8266
#endif
// Xtase LCD routines
#ifdef LCD_SUPPORT
'L','C','C','L','S'+0x80,
'L','C','P','R','I','N','T'+0x80, // reset the 8266
#endif
// Xtase extended functions for SDCard
'D','E','L','E','T','E'+0x80,
'C','A','T'+0x80,
'W','R','I','T','E','T','X','T'+0x80, // WRITETXT <filename> ?...
'W','R','I','T','E'+0x80, // WRITE <filename>,<string\nable>
0
};
// by moving the command list to an enum, we can easily remove sections
// above and below simultaneously to selectively obliterate functionality.
enum {
KW_LIST = 0,
KW_LOAD, KW_NEW, KW_RUN, KW_SAVE,
KW_NEXT, KW_LET, KW_IF,
KW_GOTO, KW_GOSUB, KW_RETURN,
KW_REM,
KW_FOR,
// Xts Xts Xts Xts Xts Xts Xts Xts Xts
KW_INPUTSTR,
// Xts Xts Xts Xts Xts Xts Xts Xts Xts
KW_INPUT, KW_PRINT,
KW_POKE,
KW_STOP, KW_BYE,
KW_FILES,
KW_MEM,
KW_QMARK, KW_QUOTE,
KW_AWRITE, KW_DWRITE,
KW_DELAY,
KW_END,
KW_RSEED,
KW_CHAIN,
#ifdef ENABLE_TONES
KW_TONEW, KW_TONE, KW_NOTONE,
#endif
#ifdef ARDUINO
#ifdef ENABLE_EEPROM
KW_ECHAIN, KW_ELIST, KW_ELOAD, KW_EFORMAT, KW_ESAVE,
#endif
#endif
#ifdef ALT_SER_FILE
KW_INIT, KW_COUT,
KW_LLIST, KW_CPRINT,
KW_CINV,
KW_OUT, // as cout but on master
#endif
#ifdef WIFI_SUPPORT
KW_WTEST,
KW_WRESET,
#endif
#ifdef LCD_SUPPORT
KW_LCCLS,
KW_LCPRINT,
#endif
// Xtase extended functions
KW_DELETE,
KW_CAT,
KW_WRITE_TXT,
KW_WRITE,
KW_DEFAULT /* always the final one*/
};
struct stack_for_frame {
char frame_type;
char for_var;
short int terminal;
short int step;
unsigned char *current_line;
unsigned char *txtpos;
};
struct stack_gosub_frame {
char frame_type;
unsigned char *current_line;
unsigned char *txtpos;
};
const static unsigned char func_tab[] PROGMEM = {
'P','E','E','K'+0x80,
'A','B','S'+0x80,
'A','R','E','A','D'+0x80,
'D','R','E','A','D'+0x80,
'R','N','D'+0x80,
// Xtase extended functions -->
'F','R','E','E'+0x80,
// function need to be defined even if not supported because those are indexed manually !!!
'C','I','N','P'+0x80, // wait a char from SLAVE serial
'I','N','P'+0x80, // wait a char from MASTER serial
'B','T','N'+0x80, // returns the state of userBtns if defined
'S','T','R','L','E','N'+0x80, // return the length of the ONLY string var
// Xtase extended functions <--
0
};
#define FUNC_PEEK 0
#define FUNC_ABS 1
#define FUNC_AREAD 2
#define FUNC_DREAD 3
#define FUNC_RND 4
#define FUNC_FREE 5
#define FUNC_CINP 6
#define FUNC_INP 7
#define FUNC_BTN 8
#define FUNC_STRLEN 9
#define FUNC_UNKNOWN 10
const static unsigned char to_tab[] PROGMEM = {
'T','O'+0x80,
0
};
const static unsigned char step_tab[] PROGMEM = {
'S','T','E','P'+0x80,
0
};
const static unsigned char relop_tab[] PROGMEM = {
'>','='+0x80,
'<','>'+0x80,
'>'+0x80,
'='+0x80,
'<','='+0x80,
'<'+0x80,
'!','='+0x80,
0
};
#define RELOP_GE 0
#define RELOP_NE 1
#define RELOP_GT 2
#define RELOP_EQ 3
#define RELOP_LE 4
#define RELOP_LT 5
#define RELOP_NE_BANG 6
#define RELOP_UNKNOWN 7
const static unsigned char highlow_tab[] PROGMEM = {
'H','I','G','H'+0x80,
'H','I'+0x80,
'L','O','W'+0x80,
'L','O'+0x80,
0
};
#define HIGHLOW_HIGH 1
#define HIGHLOW_UNKNOWN 4
#define STACK_SIZE (sizeof(struct stack_for_frame)*5)
#define VAR_SIZE sizeof(short int) // Size of variables in bytes
static unsigned char *stack_limit;
static unsigned char *program_start;
//declared upper...
//static unsigned char *program_end;
//static unsigned char *variables_begin;
static unsigned char *current_line;
static unsigned char *sp;
#define STACK_GOSUB_FLAG 'G'
#define STACK_FOR_FLAG 'F'
static unsigned char table_index;
static LINENUM linenum;
static const unsigned char okmsg[] PROGMEM = "OK";
static const unsigned char whatmsg[] PROGMEM = "What? ";
static const unsigned char howmsg[] PROGMEM = "How?";
static const unsigned char sorrymsg[] PROGMEM = "Sorry!";
//static const unsigned char initmsg[] PROGMEM = "TinyBasic Plus " kVersion;
static const unsigned char memorymsg[] PROGMEM = " bytes free.";
#ifdef ARDUINO
#ifdef ENABLE_EEPROM
static const unsigned char eeprommsg[] PROGMEM = " EEProm bytes total.";
static const unsigned char eepromamsg[] PROGMEM = " EEProm bytes available.";
#endif
#endif
static const unsigned char breakmsg[] PROGMEM = "break!";
static const unsigned char unimplimentedmsg[] PROGMEM = "Unimplemented";
static const unsigned char backspacemsg[] PROGMEM = "\b \b";