-
Notifications
You must be signed in to change notification settings - Fork 5
/
c3.ino
117 lines (104 loc) · 3.71 KB
/
c3.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
// Support for development boards
#include "c3.h"
#define mySerial Serial // Teensy and Pico
enum { xFOPEN=101, xFCLOSE, xFREAD, xFWRITE, xFGETS, FLOAD, BLOAD,
OPEN_INPUT=110, OPEN_OUTPUT, OPEN_PULLUP,
PIN_READ, PIN_READA, PIN_WRITE, PIN_WRITEA,
EDIT_BLK
};
#ifdef mySerial
void serialInit() { while (!mySerial) ; }
void printChar(char c) { mySerial.print(c); }
void printString(const char *s) { mySerial.print(s); }
int qKey() { return mySerial.available(); }
int key() {
while (!qKey()) {}
return mySerial.read();
}
#else
void serialInit() { }
void printChar(char c) {}
void printString(char *s) {}
int qKey() { return 0; }
int key() { return 0; }
#endif
cell_t sysTime() { return micros(); }
// Cells are always 32-bit on dev boards (no 64-bit)
#define S1(x, y) (*(x)=((y)&0xFF))
#define G1(x, y) (*(x)<<y)
void Store(char *l, cell_t v) { S1(l,v); S1(l+1,v>>8); S1(l+2,v>>16); S1(l+3,v>>24); }
cell_t Fetch(const char *l) { return (*l)|G1(l+1,8)|G1(l+2,16)|G1(l+3,24); }
void loadUserWords() {
parseF("-ML- PIN-INPUT %d 3 -MLX- inline", OPEN_INPUT);
parseF("-ML- PIN-OUTPUT %d 3 -MLX- inline", OPEN_OUTPUT);
parseF("-ML- PIN-PULLUP %d 3 -MLX- inline", OPEN_PULLUP);
parseF("-ML- DPIN@ %d 3 -MLX- inline", PIN_READ);
parseF("-ML- APIN@ %d 3 -MLX- inline", PIN_READA);
parseF("-ML- DPIN! %d 3 -MLX- inline", PIN_WRITE);
parseF("-ML- APIN! %d 3 -MLX- inline", PIN_WRITEA);
parseF("-ML- FOPEN %d 3 -MLX- inline", xFOPEN);
parseF("-ML- FCLOSE %d 3 -MLX- inline", xFCLOSE);
parseF("-ML- FREAD %d 3 -MLX- inline", xFREAD);
parseF("-ML- FWRITE %d 3 -MLX- inline", xFWRITE);
parseF("-ML- FGETS %d 3 -MLX- inline", xFGETS);
parseF("-ML- (LOAD) %d 3 -MLX- inline", FLOAD);
parseF("-ML- LOAD %d 3 -MLX- inline", BLOAD);
parseF("-ML- EDIT %d 3 -MLX- inline", EDIT_BLK);
parseF(": isPC 0 ;");
}
char *doUser(char *pc, int ir) {
cell_t t, n, x;
switch (ir) {
case OPEN_INPUT: t=pop(); pinMode(t, INPUT);
RCASE OPEN_OUTPUT: t=pop(); pinMode(t, OUTPUT);
RCASE OPEN_PULLUP: t=pop(); pinMode(t, INPUT_PULLUP);
RCASE PIN_READ: t=pop(); push(digitalRead(t));
RCASE PIN_READA: t=pop(); push(analogRead(t));
RCASE PIN_WRITE: t=pop(); n=pop(); digitalWrite(t, n);
RCASE PIN_WRITEA: t=pop(); n=pop(); analogWrite(t, n);
RCASE xFOPEN: t=pop(); n=pop(); push(fOpen((char*)n, (char*)t));
RCASE xFCLOSE: t=pop(); fClose(t);
RCASE xFREAD: t=pop(); n=pop(); x=pop(); push(fRead((char*)x, 1, (int)n, t));
RCASE xFWRITE: t=pop(); n=pop(); x=pop(); push(fWrite((char*)x, 1, (int)n, t));
RCASE xFGETS: t=pop(); n=pop(); x=pop(); push(fGets(t, (char*)x, (int)n));
RCASE FLOAD: n=pop(); t=fOpen((char*)n, "rt");
if (t && input_fp) { ipush(input_fp); }
if (t) { input_fp = t; *in = 0; in = (char*)0; }
else { printStringF("-noFile[%s]-", (char*)n); }
RCASE BLOAD: t=pop(); blockLoad((int)t);
RCASE EDIT_BLK: t=pop(); editBlock(t);
return pc; default: return 0;
}
}
void setup() {
serialInit();
// printString("Hello.");
fileInit();
c3Init();
printString(" ok\r\n");
in = (char*)0;
}
void idle() {
// Fill this in as desired
}
void loop() {
if (qKey() == 0) { idle(); return; }
int c = key();
if (!in) {
in = tib;
fill(tib, 0, sizeof(tib));
}
if (c==9) { c = 32; }
if (c==13) {
*(in) = 0;
PC(32);
ParseLine(tib);
printString(" ok\r\n");
in = 0;
} else if ((c==8) || (c==127)) {
if ((--in) < tib) { in = tib; }
else { PC(8); PC(32); PC(8); }
} else {
if (BTW(c,32,126)) { *(in++) = c; PC(c); }
}
}