Created
May 18, 2013 15:32
-
-
Save anonymous/5604829 to your computer and use it in GitHub Desktop.
A Simple Txtzyme Interpreter for Arduino - to play tunes through a small speaker See http://sustburbia.blogspot.co.uk/2013/05/txtzyme-minimal-interpreter-and.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Txtzyme Interpreter for Arduino - by Ward Cunningham | |
// A simple text based interpreted language | |
// With easy extensibility based on Forth Thinking | |
// Plays 6 notes A B C D E F through a small speaker on Digital 6 | |
/* | |
Commands - not all fully implemented | |
0-9 enter number | |
p print number | |
a-f select pin | |
i input | |
o output | |
m msec delay | |
u usec delay | |
{} repeat | |
k loop count | |
_ _ print words | |
s analog sample | |
v print version | |
h print help | |
t pulse width | |
*/ | |
unsigned int x = 0; | |
unsigned int y = 0; | |
char A[64] = "6d75{1o708u0o708u}"; | |
char B[64] = "6d83{1o644u0o644u}"; | |
char C[64] = "6d91{1o585u0o585u}"; | |
char D[64] = "6d100{1o532u0o532u}"; | |
char E[64] = "6d110{1o484u0o484u}"; | |
char F[64] = "6d121{1o440u0o440u}"; | |
int d = 5; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(d,OUTPUT); | |
// char A[64]; | |
} | |
void loop() { | |
char buf[64]; | |
txtRead(buf, 64); | |
txtEval(buf); | |
// txtEval(A); | |
// txtEval(B); | |
// txtEval(C); | |
// txtEval(B); | |
} | |
void txtRead (char *p, byte n) { | |
byte i = 0; | |
while (i < (n-1)) { | |
while (!Serial.available()); | |
char ch = Serial.read(); | |
if (ch == '\r' || ch == '\n') break; | |
if (ch >= ' ' && ch <= '~') { | |
*p++ = ch; | |
i++; | |
} | |
} | |
*p = 0; | |
} | |
void txtEval (char *buf) { | |
unsigned int k = 0; | |
char *loop; | |
char ch; | |
while ((ch = *buf++)) { | |
switch (ch) { | |
case '0': | |
case '1': | |
case '2': | |
case '3': | |
case '4': | |
case '5': | |
case '6': | |
case '7': | |
case '8': | |
case '9': | |
x = ch - '0'; | |
while (*buf >= '0' && *buf <= '9') { | |
x = x*10 + (*buf++ - '0'); | |
} | |
break; | |
case 'p': | |
Serial.println(x); | |
break; | |
case 'd': | |
d = x; | |
break; | |
case 'A': | |
txtEval(A); | |
break; | |
case 'B': | |
txtEval(B); | |
break; | |
case 'C': | |
txtEval(C); | |
break; | |
case 'D': | |
txtEval(D); | |
break; | |
case 'E': | |
txtEval(E); | |
break; | |
case 'F': | |
txtEval(F); | |
break; | |
case '!': // store | |
y = x; | |
break; | |
case '@': | |
x = y; | |
break; | |
case '+': | |
x = x+y; | |
break; | |
case '-': | |
x = x-y; | |
break; | |
case '*': | |
x = x*y; | |
break; | |
case '/': | |
x = x/y; | |
break; | |
case 'i': | |
x = digitalRead(d); | |
break; | |
case 'o': | |
digitalWrite(d, x%2); | |
break; | |
case 'm': | |
delay(x); | |
break; | |
case 'u': | |
delayMicroseconds(x); | |
break; | |
case '{': | |
k = x; | |
loop = buf; | |
while ((ch = *buf++) && ch != '}') { | |
} | |
case '}': | |
if (k) { | |
k--; | |
buf = loop; | |
} | |
break; | |
case 'k': | |
x = k; | |
break; | |
case '_': | |
while ((ch = *buf++) && ch != '_') { | |
Serial.print(ch); | |
} | |
Serial.println(); | |
break; | |
case 's': | |
x = analogRead(x); | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment