| TEA Syntax Last Modified: 2006-10-25 | | |
| Acroname Robotics | PDF webpage version | ||
| The code below has examples of most of the syntax that might appear in a TEA program. It includes a variety of loops, conditional statements, and expressions. When compiled, this program is 973 bytes. Depending which BrainStem module you are using, 973 bytes may be near the file slot size limitation, depending which BrainStem module you are using Code size may vary significantly depending on what subroutines and libraries are used, but this program gives a rough idea of how big one TEA program can be. /*
This is a comment.
The slash-asterisk begins the comment.
The asterisk-slash ends the comment.
This type of comment can span many lines.
*/
// This is an alternate comment style.
// The slash-slash comments out everything after it.
// It only affects one line.
// these statements link files
// that contain useful subroutines
// use <> for files in the aSystem folder
// use "" for files in the aUser folder
#include <aCore.tea> // for aCore_Sleep
#include <aPrint.tea> // for print statements
// defining some constants
#define FIVE 5
#define ONESEC 10000
/*
example of function
uses a switch statement
returns 10 raised to 0,1,2,3,4
any other input value will return 1
*/
int myFunc(char a)
{
int power;
switch(a)
{
case 0:
power=1;
break;
case 1:
power=10;
break;
case 2:
power=100;
break;
case 3:
power=1000;
break;
case 4:
power=10000;
break;
default:
power=1;
break;
}
return power;
}
// example of a subroutine with several input parameters
void mySub(int a, int b, int c)
{
int k;
k=a+b+c;
aPrint_String("k=");
aPrint_IntDec(k);
aPrint_Char('\n');
}
void main()
{
// variable declarations come first in
// any routine, subroutine, or function
// it is good practice to include initializers
char c=0;
int i=0;
int j=0;
// example for printing text
// backslash-n is carriage return
aPrint_String("Hello world.\n");
// sleep routine is in 0.1ms units
// parameter of 10000 will cause delay of 1 second
aCore_Sleep(ONESEC);
// example of subroutine call
j=10;
mySub(30,i,j);
aCore_Sleep(ONESEC);
// example of for loop and printing a variable
// BASIC equivalent: for i=0 to 2 step 1
// the loop starts at 0 and counts while i<3
// the ++ is an increment operator
for (i=0; i<3; i++)
{
aPrint_IntDec(i);
aPrint_Char('\n');
}
aCore_Sleep(ONESEC);
// example of while loop and printing a variable
j=3;
while (j>0)
{
j=j-1;
aPrint_IntDec(j);
aPrint_Char('\n');
}
aCore_Sleep(ONESEC);
// example of do-while loop
// do-while guarantees at least one iteration
c=0;
do
{
i=myFunc(c);
aPrint_IntDec(i);
aPrint_Char('\n');
c++;
} while (c<FIVE);
aCore_Sleep(ONESEC);
// example of break statement
// forces a loop to exit
for (i=0; i<10; i++)
{
if (i>4) break;
aPrint_IntDec(i);
aPrint_Char('\n');
}
aCore_Sleep(ONESEC);
// example of if
j=0;
if (j==0)
{
aPrint_String("j equals 0\n");
j=3;
}
aCore_Sleep(ONESEC);
// example of if-else
if (j!=3)
{
aPrint_String("j does not equal 3\n");
}
else
{
aPrint_String("j equals 3\n");
j=1;
}
aCore_Sleep(ONESEC);
// loops and if statements can be nested
if (j>0)
{
aPrint_String("j greater than 0\n");
if (j!=3)
{
aPrint_String("j not equal to 3\n");
for (j=0; j<5; j++)
{
i=0;
while (i<=j)
{
aPrint_IntDec(i);
aPrint_Char(',');
aPrint_IntDec(j);
aPrint_Char('\n');
aCore_Sleep(ONESEC);
i=i+1;
}
}
}
}
// integer math
j=0; // assignment, j will be 0
j=j+9; // addition, j will be 9
j=j-8; // subtraction, j will be 1
j=j*16; // multiplication, j will be 16
j=j/3; // integer division, j will be 5
j=j|27; // bitwise OR, j will be 31
j=j&7; // bitwise AND, j will be 7
j=j^1; // bitwise XOR, j will be 6
j=j%5; // modulo (remainder) operation, j will be 1
aPrint_IntDec(j);
aPrint_Char('\n');
aCore_Sleep(ONESEC);
}
|
| |||||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved. |