formatting

This commit is contained in:
Joseph Poirier 2015-06-06 23:51:02 -05:00
parent a61bd97d9b
commit c5384cb514
9 changed files with 497 additions and 547 deletions

View file

@ -32,7 +32,7 @@ typedef struct StdOutStreamStruct
char *StrOutPtr; char *StrOutPtr;
int StrOutLen; int StrOutLen;
int CharCount; int CharCount;
} StdOutStream; } StdOutStream;
/* our representation of varargs within picoc */ /* our representation of varargs within picoc */
@ -54,18 +54,15 @@ void BasicIOInit(Picoc *pc)
/* output a single character to either a FILE * or a string */ /* output a single character to either a FILE * or a string */
void StdioOutPutc(int OutCh, StdOutStream *Stream) void StdioOutPutc(int OutCh, StdOutStream *Stream)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL) {
{
/* output to stdio stream */ /* output to stdio stream */
putc(OutCh, Stream->FilePtr); putc(OutCh, Stream->FilePtr);
Stream->CharCount++; Stream->CharCount++;
} } else if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1) {
else if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1)
{
/* output to a string */ /* output to a string */
*Stream->StrOutPtr = OutCh; *Stream->StrOutPtr = OutCh;
Stream->StrOutPtr++; Stream->StrOutPtr++;
if (Stream->StrOutLen > 1) if (Stream->StrOutLen > 1)
Stream->StrOutLen--; Stream->StrOutLen--;
@ -76,28 +73,23 @@ void StdioOutPutc(int OutCh, StdOutStream *Stream)
/* output a string to either a FILE * or a string */ /* output a string to either a FILE * or a string */
void StdioOutPuts(const char *Str, StdOutStream *Stream) void StdioOutPuts(const char *Str, StdOutStream *Stream)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL) {
{
/* output to stdio stream */ /* output to stdio stream */
fputs(Str, Stream->FilePtr); fputs(Str, Stream->FilePtr);
} } else {
else
{
/* output to a string */ /* output to a string */
while (*Str != '\0') while (*Str != '\0') {
{ if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1) {
if (Stream->StrOutLen < 0 || Stream->StrOutLen > 1)
{
/* output to a string */ /* output to a string */
*Stream->StrOutPtr = *Str; *Stream->StrOutPtr = *Str;
Str++; Str++;
Stream->StrOutPtr++; Stream->StrOutPtr++;
if (Stream->StrOutLen > 1) if (Stream->StrOutLen > 1)
Stream->StrOutLen--; Stream->StrOutLen--;
Stream->CharCount++; Stream->CharCount++;
} }
} }
} }
} }
@ -107,9 +99,7 @@ void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned long Va
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL)
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value); Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
else if (Stream->StrOutLen >= 0) {
else if (Stream->StrOutLen >= 0)
{
#ifndef WIN32 #ifndef WIN32
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value); int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
#else #else
@ -118,9 +108,7 @@ void StdioFprintfWord(StdOutStream *Stream, const char *Format, unsigned long Va
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
Stream->StrOutLen -= CCount; Stream->StrOutLen -= CCount;
Stream->CharCount += CCount; Stream->CharCount += CCount;
} } else {
else
{
int CCount = sprintf(Stream->StrOutPtr, Format, Value); int CCount = sprintf(Stream->StrOutPtr, Format, Value);
Stream->CharCount += CCount; Stream->CharCount += CCount;
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
@ -132,9 +120,7 @@ void StdioFprintfFP(StdOutStream *Stream, const char *Format, double Value)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL)
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value); Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
else if (Stream->StrOutLen >= 0) {
else if (Stream->StrOutLen >= 0)
{
#ifndef WIN32 #ifndef WIN32
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value); int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
#else #else
@ -143,9 +129,7 @@ void StdioFprintfFP(StdOutStream *Stream, const char *Format, double Value)
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
Stream->StrOutLen -= CCount; Stream->StrOutLen -= CCount;
Stream->CharCount += CCount; Stream->CharCount += CCount;
} } else {
else
{
int CCount = sprintf(Stream->StrOutPtr, Format, Value); int CCount = sprintf(Stream->StrOutPtr, Format, Value);
Stream->CharCount += CCount; Stream->CharCount += CCount;
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
@ -157,9 +141,7 @@ void StdioFprintfPointer(StdOutStream *Stream, const char *Format, void *Value)
{ {
if (Stream->FilePtr != NULL) if (Stream->FilePtr != NULL)
Stream->CharCount += fprintf(Stream->FilePtr, Format, Value); Stream->CharCount += fprintf(Stream->FilePtr, Format, Value);
else if (Stream->StrOutLen >= 0) {
else if (Stream->StrOutLen >= 0)
{
#ifndef WIN32 #ifndef WIN32
int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value); int CCount = snprintf(Stream->StrOutPtr, Stream->StrOutLen, Format, Value);
#else #else
@ -168,9 +150,7 @@ void StdioFprintfPointer(StdOutStream *Stream, const char *Format, void *Value)
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
Stream->StrOutLen -= CCount; Stream->StrOutLen -= CCount;
Stream->CharCount += CCount; Stream->CharCount += CCount;
} } else {
else
{
int CCount = sprintf(Stream->StrOutPtr, Format, Value); int CCount = sprintf(Stream->StrOutPtr, Format, Value);
Stream->CharCount += CCount; Stream->CharCount += CCount;
Stream->StrOutPtr += CCount; Stream->StrOutPtr += CCount;
@ -188,84 +168,75 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
struct ValueType *ShowType; struct ValueType *ShowType;
StdOutStream SOStream; StdOutStream SOStream;
Picoc *pc = Parser->pc; Picoc *pc = Parser->pc;
if (Format == NULL) if (Format == NULL)
Format = "[null format]\n"; Format = "[null format]\n";
FPos = Format; FPos = Format;
SOStream.FilePtr = Stream; SOStream.FilePtr = Stream;
SOStream.StrOutPtr = StrOut; SOStream.StrOutPtr = StrOut;
SOStream.StrOutLen = StrOutLen; SOStream.StrOutLen = StrOutLen;
SOStream.CharCount = 0; SOStream.CharCount = 0;
while (*FPos != '\0') while (*FPos != '\0') {
{ if (*FPos == '%') {
if (*FPos == '%')
{
/* work out what type we're printing */ /* work out what type we're printing */
FPos++; FPos++;
ShowType = NULL; ShowType = NULL;
OneFormatBuf[0] = '%'; OneFormatBuf[0] = '%';
OneFormatCount = 1; OneFormatCount = 1;
do do {
{ switch (*FPos) {
switch (*FPos) case 'd': case 'i': ShowType = &pc->IntType; break; /* integer decimal */
{ case 'o': case 'u': case 'x': case 'X': ShowType = &pc->IntType; break; /* integer base conversions */
case 'd': case 'i': ShowType = &pc->IntType; break; /* integer decimal */
case 'o': case 'u': case 'x': case 'X': ShowType = &pc->IntType; break; /* integer base conversions */
#ifndef NO_FP #ifndef NO_FP
case 'e': case 'E': ShowType = &pc->FPType; break; /* double, exponent form */ case 'e': case 'E': ShowType = &pc->FPType; break; /* double, exponent form */
case 'f': case 'F': ShowType = &pc->FPType; break; /* double, fixed-point */ case 'f': case 'F': ShowType = &pc->FPType; break; /* double, fixed-point */
case 'g': case 'G': ShowType = &pc->FPType; break; /* double, flexible format */ case 'g': case 'G': ShowType = &pc->FPType; break; /* double, flexible format */
#endif #endif
case 'a': case 'A': ShowType = &pc->IntType; break; /* hexadecimal, 0x- format */ case 'a': case 'A': ShowType = &pc->IntType; break; /* hexadecimal, 0x- format */
case 'c': ShowType = &pc->IntType; break; /* character */ case 'c': ShowType = &pc->IntType; break; /* character */
case 's': ShowType = pc->CharPtrType; break; /* string */ case 's': ShowType = pc->CharPtrType; break; /* string */
case 'p': ShowType = pc->VoidPtrType; break; /* pointer */ case 'p': ShowType = pc->VoidPtrType; break; /* pointer */
case 'n': ShowType = &pc->VoidType; break; /* number of characters written */ case 'n': ShowType = &pc->VoidType; break; /* number of characters written */
case 'm': ShowType = &pc->VoidType; break; /* strerror(errno) */ case 'm': ShowType = &pc->VoidType; break; /* strerror(errno) */
case '%': ShowType = &pc->VoidType; break; /* just a '%' character */ case '%': ShowType = &pc->VoidType; break; /* just a '%' character */
case '\0': ShowType = &pc->VoidType; break; /* end of format string */ case '\0': ShowType = &pc->VoidType; break; /* end of format string */
} }
/* copy one character of format across to the OneFormatBuf */ /* copy one character of format across to the OneFormatBuf */
OneFormatBuf[OneFormatCount] = *FPos; OneFormatBuf[OneFormatCount] = *FPos;
OneFormatCount++; OneFormatCount++;
/* do special actions depending on the conversion type */ /* do special actions depending on the conversion type */
if (ShowType == &pc->VoidType) if (ShowType == &pc->VoidType) {
{ switch (*FPos) {
switch (*FPos) case 'm': StdioOutPuts(strerror(errno), &SOStream); break;
{ case '%': StdioOutPutc(*FPos, &SOStream); break;
case 'm': StdioOutPuts(strerror(errno), &SOStream); break; case '\0': OneFormatBuf[OneFormatCount] = '\0'; StdioOutPutc(*FPos, &SOStream); break;
case '%': StdioOutPutc(*FPos, &SOStream); break; case 'n':
case '\0': OneFormatBuf[OneFormatCount] = '\0'; StdioOutPutc(*FPos, &SOStream); break; ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
case 'n': if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeInt)
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg))); *(int *)ThisArg->Val->Pointer = SOStream.CharCount;
if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeInt) break;
*(int *)ThisArg->Val->Pointer = SOStream.CharCount;
break;
} }
} }
FPos++; FPos++;
} while (ShowType == NULL && OneFormatCount < MAX_FORMAT); } while (ShowType == NULL && OneFormatCount < MAX_FORMAT);
if (ShowType != &pc->VoidType) if (ShowType != &pc->VoidType) {
{
if (ArgCount >= Args->NumArgs) if (ArgCount >= Args->NumArgs)
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
else else {
{
/* null-terminate the buffer */ /* null-terminate the buffer */
OneFormatBuf[OneFormatCount] = '\0'; OneFormatBuf[OneFormatCount] = '\0';
/* print this argument */ /* print this argument */
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg))); ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
if (ShowType == &pc->IntType) if (ShowType == &pc->IntType) {
{
/* show a signed integer */ /* show a signed integer */
if (IS_NUMERIC_COERCIBLE(ThisArg)) if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg)); StdioFprintfWord(&SOStream, OneFormatBuf, ExpressionCoerceUnsignedInteger(ThisArg));
@ -273,54 +244,44 @@ int StdioBasePrintf(struct ParseState *Parser, FILE *Stream, char *StrOut, int S
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} }
#ifndef NO_FP #ifndef NO_FP
else if (ShowType == &pc->FPType) else if (ShowType == &pc->FPType) {
{
/* show a floating point number */ /* show a floating point number */
if (IS_NUMERIC_COERCIBLE(ThisArg)) if (IS_NUMERIC_COERCIBLE(ThisArg))
StdioFprintfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg)); StdioFprintfFP(&SOStream, OneFormatBuf, ExpressionCoerceFP(ThisArg));
else else
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} }
#endif #endif
else if (ShowType == pc->CharPtrType) else if (ShowType == pc->CharPtrType) {
{
if (ThisArg->Typ->Base == TypePointer) if (ThisArg->Typ->Base == TypePointer)
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer); StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
else if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeChar) else if (ThisArg->Typ->Base == TypeArray && ThisArg->Typ->FromType->Base == TypeChar)
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]); StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
else else
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} } else if (ShowType == pc->VoidPtrType) {
else if (ShowType == pc->VoidPtrType)
{
if (ThisArg->Typ->Base == TypePointer) if (ThisArg->Typ->Base == TypePointer)
StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer); StdioFprintfPointer(&SOStream, OneFormatBuf, ThisArg->Val->Pointer);
else if (ThisArg->Typ->Base == TypeArray) else if (ThisArg->Typ->Base == TypeArray)
StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]); StdioFprintfPointer(&SOStream, OneFormatBuf, &ThisArg->Val->ArrayMem[0]);
else else
StdioOutPuts("XXX", &SOStream); StdioOutPuts("XXX", &SOStream);
} }
ArgCount++; ArgCount++;
} }
} }
} } else {
else
{
/* just output a normal character */ /* just output a normal character */
StdioOutPutc(*FPos, &SOStream); StdioOutPutc(*FPos, &SOStream);
FPos++; FPos++;
} }
} }
/* null-terminate */ /* null-terminate */
if (SOStream.StrOutPtr != NULL && SOStream.StrOutLen > 0) if (SOStream.StrOutPtr != NULL && SOStream.StrOutLen > 0)
*SOStream.StrOutPtr = '\0'; *SOStream.StrOutPtr = '\0';
return SOStream.CharCount; return SOStream.CharCount;
} }
@ -330,24 +291,21 @@ int StdioBaseScanf(struct ParseState *Parser, FILE *Stream, char *StrIn, char *F
struct Value *ThisArg = Args->Param[0]; struct Value *ThisArg = Args->Param[0];
int ArgCount = 0; int ArgCount = 0;
void *ScanfArg[MAX_SCANF_ARGS]; void *ScanfArg[MAX_SCANF_ARGS];
if (Args->NumArgs > MAX_SCANF_ARGS) if (Args->NumArgs > MAX_SCANF_ARGS)
ProgramFail(Parser, "too many arguments to scanf() - %d max", MAX_SCANF_ARGS); ProgramFail(Parser, "too many arguments to scanf() - %d max", MAX_SCANF_ARGS);
for (ArgCount = 0; ArgCount < Args->NumArgs; ArgCount++) for (ArgCount = 0; ArgCount < Args->NumArgs; ArgCount++) {
{
ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg))); ThisArg = (struct Value *)((char *)ThisArg + MEM_ALIGN(sizeof(struct Value) + TypeStackSizeValue(ThisArg)));
if (ThisArg->Typ->Base == TypePointer) if (ThisArg->Typ->Base == TypePointer)
ScanfArg[ArgCount] = ThisArg->Val->Pointer; ScanfArg[ArgCount] = ThisArg->Val->Pointer;
else if (ThisArg->Typ->Base == TypeArray) else if (ThisArg->Typ->Base == TypeArray)
ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0]; ScanfArg[ArgCount] = &ThisArg->Val->ArrayMem[0];
else else
ProgramFail(Parser, "non-pointer argument to scanf() - argument %d after format", ArgCount+1); ProgramFail(Parser, "non-pointer argument to scanf() - argument %d after format", ArgCount+1);
} }
if (Stream != NULL) if (Stream != NULL)
return fscanf(Stream, Format, ScanfArg[0], ScanfArg[1], ScanfArg[2], ScanfArg[3], ScanfArg[4], ScanfArg[5], ScanfArg[6], ScanfArg[7], ScanfArg[8], ScanfArg[9]); return fscanf(Stream, Format, ScanfArg[0], ScanfArg[1], ScanfArg[2], ScanfArg[3], ScanfArg[4], ScanfArg[5], ScanfArg[6], ScanfArg[7], ScanfArg[8], ScanfArg[9]);
else else
@ -355,77 +313,77 @@ int StdioBaseScanf(struct ParseState *Parser, FILE *Stream, char *StrIn, char *F
} }
/* stdio calls */ /* stdio calls */
void StdioFopen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFopen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Pointer = fopen(Param[0]->Val->Pointer, Param[1]->Val->Pointer); ReturnValue->Val->Pointer = fopen(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
} }
void StdioFreopen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFreopen(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Pointer = freopen(Param[0]->Val->Pointer, Param[1]->Val->Pointer, Param[2]->Val->Pointer); ReturnValue->Val->Pointer = freopen(Param[0]->Val->Pointer, Param[1]->Val->Pointer, Param[2]->Val->Pointer);
} }
void StdioFclose(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFclose(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fclose(Param[0]->Val->Pointer); ReturnValue->Val->Integer = fclose(Param[0]->Val->Pointer);
} }
void StdioFread(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFread(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fread(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer, Param[3]->Val->Pointer); ReturnValue->Val->Integer = fread(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer, Param[3]->Val->Pointer);
} }
void StdioFwrite(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFwrite(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fwrite(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer, Param[3]->Val->Pointer); ReturnValue->Val->Integer = fwrite(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer, Param[3]->Val->Pointer);
} }
void StdioFgetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFgetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fgetc(Param[0]->Val->Pointer); ReturnValue->Val->Integer = fgetc(Param[0]->Val->Pointer);
} }
void StdioFgets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFgets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Pointer); ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Pointer);
} }
void StdioRemove(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioRemove(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = remove(Param[0]->Val->Pointer); ReturnValue->Val->Integer = remove(Param[0]->Val->Pointer);
} }
void StdioRename(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioRename(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = rename(Param[0]->Val->Pointer, Param[1]->Val->Pointer); ReturnValue->Val->Integer = rename(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
} }
void StdioRewind(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioRewind(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
rewind(Param[0]->Val->Pointer); rewind(Param[0]->Val->Pointer);
} }
void StdioTmpfile(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioTmpfile(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Pointer = tmpfile(); ReturnValue->Val->Pointer = tmpfile();
} }
void StdioClearerr(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioClearerr(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
clearerr((FILE *)Param[0]->Val->Pointer); clearerr((FILE *)Param[0]->Val->Pointer);
} }
void StdioFeof(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFeof(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = feof((FILE *)Param[0]->Val->Pointer); ReturnValue->Val->Integer = feof((FILE *)Param[0]->Val->Pointer);
} }
void StdioFerror(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFerror(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = ferror((FILE *)Param[0]->Val->Pointer); ReturnValue->Val->Integer = ferror((FILE *)Param[0]->Val->Pointer);
} }
void StdioFileno(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFileno(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
#ifndef WIN32 #ifndef WIN32
ReturnValue->Val->Integer = fileno(Param[0]->Val->Pointer); ReturnValue->Val->Integer = fileno(Param[0]->Val->Pointer);
@ -434,88 +392,87 @@ void StdioFileno(struct ParseState *Parser, struct Value *ReturnValue, struct Va
#endif #endif
} }
void StdioFflush(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFflush(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fflush(Param[0]->Val->Pointer); ReturnValue->Val->Integer = fflush(Param[0]->Val->Pointer);
} }
void StdioFgetpos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFgetpos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fgetpos(Param[0]->Val->Pointer, Param[1]->Val->Pointer); ReturnValue->Val->Integer = fgetpos(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
} }
void StdioFsetpos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFsetpos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fsetpos(Param[0]->Val->Pointer, Param[1]->Val->Pointer); ReturnValue->Val->Integer = fsetpos(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
} }
void StdioFputc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFputc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fputc(Param[0]->Val->Integer, Param[1]->Val->Pointer); ReturnValue->Val->Integer = fputc(Param[0]->Val->Integer, Param[1]->Val->Pointer);
} }
void StdioFputs(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFputs(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fputs(Param[0]->Val->Pointer, Param[1]->Val->Pointer); ReturnValue->Val->Integer = fputs(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
} }
void StdioFtell(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFtell(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = ftell(Param[0]->Val->Pointer); ReturnValue->Val->Integer = ftell(Param[0]->Val->Pointer);
} }
void StdioFseek(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFseek(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = fseek(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer); ReturnValue->Val->Integer = fseek(Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Integer);
} }
void StdioPerror(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioPerror(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
perror(Param[0]->Val->Pointer); perror(Param[0]->Val->Pointer);
} }
void StdioPutc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioPutc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = putc(Param[0]->Val->Integer, Param[1]->Val->Pointer); ReturnValue->Val->Integer = putc(Param[0]->Val->Integer, Param[1]->Val->Pointer);
} }
void StdioPutchar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioPutchar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = putchar(Param[0]->Val->Integer); ReturnValue->Val->Integer = putchar(Param[0]->Val->Integer);
} }
void StdioSetbuf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioSetbuf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
setbuf(Param[0]->Val->Pointer, Param[1]->Val->Pointer); setbuf(Param[0]->Val->Pointer, Param[1]->Val->Pointer);
} }
void StdioSetvbuf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioSetvbuf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
setvbuf(Param[0]->Val->Pointer, Param[1]->Val->Pointer, Param[2]->Val->Integer, Param[3]->Val->Integer); setvbuf(Param[0]->Val->Pointer, Param[1]->Val->Pointer, Param[2]->Val->Integer, Param[3]->Val->Integer);
} }
void StdioUngetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioUngetc(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = ungetc(Param[0]->Val->Integer, Param[1]->Val->Pointer); ReturnValue->Val->Integer = ungetc(Param[0]->Val->Integer, Param[1]->Val->Pointer);
} }
void StdioPuts(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioPuts(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = puts(Param[0]->Val->Pointer); ReturnValue->Val->Integer = puts(Param[0]->Val->Pointer);
} }
void StdioGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioGets(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, GETS_MAXValue, stdin); ReturnValue->Val->Pointer = fgets(Param[0]->Val->Pointer, GETS_MAXValue, stdin);
if (ReturnValue->Val->Pointer != NULL) if (ReturnValue->Val->Pointer != NULL) {
{
char *EOLPos = strchr(Param[0]->Val->Pointer, '\n'); char *EOLPos = strchr(Param[0]->Val->Pointer, '\n');
if (EOLPos != NULL) if (EOLPos != NULL)
*EOLPos = '\0'; *EOLPos = '\0';
} }
} }
void StdioGetchar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioGetchar(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = getchar(); ReturnValue->Val->Integer = getchar();
} }
@ -523,7 +480,7 @@ void StdioGetchar(struct ParseState *Parser, struct Value *ReturnValue, struct V
void StdioPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioPrintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct StdVararg PrintfArgs; struct StdVararg PrintfArgs;
PrintfArgs.Param = Param; PrintfArgs.Param = Param;
PrintfArgs.NumArgs = NumArgs-1; PrintfArgs.NumArgs = NumArgs-1;
ReturnValue->Val->Integer = StdioBasePrintf(Parser, stdout, NULL, 0, Param[0]->Val->Pointer, &PrintfArgs); ReturnValue->Val->Integer = StdioBasePrintf(Parser, stdout, NULL, 0, Param[0]->Val->Pointer, &PrintfArgs);
@ -537,7 +494,7 @@ void StdioVprintf(struct ParseState *Parser, struct Value *ReturnValue, struct V
void StdioFprintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFprintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct StdVararg PrintfArgs; struct StdVararg PrintfArgs;
PrintfArgs.Param = Param + 1; PrintfArgs.Param = Param + 1;
PrintfArgs.NumArgs = NumArgs-2; PrintfArgs.NumArgs = NumArgs-2;
ReturnValue->Val->Integer = StdioBasePrintf(Parser, Param[0]->Val->Pointer, NULL, 0, Param[1]->Val->Pointer, &PrintfArgs); ReturnValue->Val->Integer = StdioBasePrintf(Parser, Param[0]->Val->Pointer, NULL, 0, Param[1]->Val->Pointer, &PrintfArgs);
@ -548,19 +505,19 @@ void StdioVfprintf(struct ParseState *Parser, struct Value *ReturnValue, struct
ReturnValue->Val->Integer = StdioBasePrintf(Parser, Param[0]->Val->Pointer, NULL, 0, Param[1]->Val->Pointer, Param[2]->Val->Pointer); ReturnValue->Val->Integer = StdioBasePrintf(Parser, Param[0]->Val->Pointer, NULL, 0, Param[1]->Val->Pointer, Param[2]->Val->Pointer);
} }
void StdioSprintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioSprintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct StdVararg PrintfArgs; struct StdVararg PrintfArgs;
PrintfArgs.Param = Param + 1; PrintfArgs.Param = Param + 1;
PrintfArgs.NumArgs = NumArgs-2; PrintfArgs.NumArgs = NumArgs-2;
ReturnValue->Val->Integer = StdioBasePrintf(Parser, NULL, Param[0]->Val->Pointer, -1, Param[1]->Val->Pointer, &PrintfArgs); ReturnValue->Val->Integer = StdioBasePrintf(Parser, NULL, Param[0]->Val->Pointer, -1, Param[1]->Val->Pointer, &PrintfArgs);
} }
void StdioSnprintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioSnprintf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct StdVararg PrintfArgs; struct StdVararg PrintfArgs;
PrintfArgs.Param = Param+2; PrintfArgs.Param = Param+2;
PrintfArgs.NumArgs = NumArgs-3; PrintfArgs.NumArgs = NumArgs-3;
ReturnValue->Val->Integer = StdioBasePrintf(Parser, NULL, Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Pointer, &PrintfArgs); ReturnValue->Val->Integer = StdioBasePrintf(Parser, NULL, Param[0]->Val->Pointer, Param[1]->Val->Integer, Param[2]->Val->Pointer, &PrintfArgs);
@ -569,7 +526,7 @@ void StdioSnprintf(struct ParseState *Parser, struct Value *ReturnValue, struct
void StdioScanf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioScanf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct StdVararg ScanfArgs; struct StdVararg ScanfArgs;
ScanfArgs.Param = Param; ScanfArgs.Param = Param;
ScanfArgs.NumArgs = NumArgs-1; ScanfArgs.NumArgs = NumArgs-1;
ReturnValue->Val->Integer = StdioBaseScanf(Parser, stdin, NULL, Param[0]->Val->Pointer, &ScanfArgs); ReturnValue->Val->Integer = StdioBaseScanf(Parser, stdin, NULL, Param[0]->Val->Pointer, &ScanfArgs);
@ -578,7 +535,7 @@ void StdioScanf(struct ParseState *Parser, struct Value *ReturnValue, struct Val
void StdioFscanf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioFscanf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct StdVararg ScanfArgs; struct StdVararg ScanfArgs;
ScanfArgs.Param = Param+1; ScanfArgs.Param = Param+1;
ScanfArgs.NumArgs = NumArgs-2; ScanfArgs.NumArgs = NumArgs-2;
ReturnValue->Val->Integer = StdioBaseScanf(Parser, Param[0]->Val->Pointer, NULL, Param[1]->Val->Pointer, &ScanfArgs); ReturnValue->Val->Integer = StdioBaseScanf(Parser, Param[0]->Val->Pointer, NULL, Param[1]->Val->Pointer, &ScanfArgs);
@ -587,7 +544,7 @@ void StdioFscanf(struct ParseState *Parser, struct Value *ReturnValue, struct Va
void StdioSscanf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void StdioSscanf(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
struct StdVararg ScanfArgs; struct StdVararg ScanfArgs;
ScanfArgs.Param = Param+1; ScanfArgs.Param = Param+1;
ScanfArgs.NumArgs = NumArgs-2; ScanfArgs.NumArgs = NumArgs-2;
ReturnValue->Val->Integer = StdioBaseScanf(Parser, NULL, Param[0]->Val->Pointer, Param[1]->Val->Pointer, &ScanfArgs); ReturnValue->Val->Integer = StdioBaseScanf(Parser, NULL, Param[0]->Val->Pointer, Param[1]->Val->Pointer, &ScanfArgs);
@ -685,13 +642,13 @@ void StdioSetupFunc(Picoc *pc)
/* make a "struct __FILEStruct" which is the same size as a native FILE structure */ /* make a "struct __FILEStruct" which is the same size as a native FILE structure */
StructFileType = TypeCreateOpaqueStruct(pc, NULL, TableStrRegister(pc, "__FILEStruct"), sizeof(FILE)); StructFileType = TypeCreateOpaqueStruct(pc, NULL, TableStrRegister(pc, "__FILEStruct"), sizeof(FILE));
/* get a FILE * type */ /* get a FILE * type */
FilePtrType = TypeGetMatching(pc, NULL, StructFileType, TypePointer, 0, pc->StrEmpty, TRUE); FilePtrType = TypeGetMatching(pc, NULL, StructFileType, TypePointer, 0, pc->StrEmpty, TRUE);
/* make a "struct __va_listStruct" which is the same size as our struct StdVararg */ /* make a "struct __va_listStruct" which is the same size as our struct StdVararg */
TypeCreateOpaqueStruct(pc, NULL, TableStrRegister(pc, "__va_listStruct"), sizeof(FILE)); TypeCreateOpaqueStruct(pc, NULL, TableStrRegister(pc, "__va_listStruct"), sizeof(FILE));
/* define EOF equal to the system EOF */ /* define EOF equal to the system EOF */
VariableDefinePlatformVar(pc, NULL, "EOF", &pc->IntType, (union AnyValue *)&EOFValue, FALSE); VariableDefinePlatformVar(pc, NULL, "EOF", &pc->IntType, (union AnyValue *)&EOFValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "SEEK_SET", &pc->IntType, (union AnyValue *)&SEEK_SETValue, FALSE); VariableDefinePlatformVar(pc, NULL, "SEEK_SET", &pc->IntType, (union AnyValue *)&SEEK_SETValue, FALSE);
@ -704,7 +661,7 @@ void StdioSetupFunc(Picoc *pc)
VariableDefinePlatformVar(pc, NULL, "_IONBF", &pc->IntType, (union AnyValue *)&_IONBFValue, FALSE); VariableDefinePlatformVar(pc, NULL, "_IONBF", &pc->IntType, (union AnyValue *)&_IONBFValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "L_tmpnam", &pc->IntType, (union AnyValue *)&L_tmpnamValue, FALSE); VariableDefinePlatformVar(pc, NULL, "L_tmpnam", &pc->IntType, (union AnyValue *)&L_tmpnamValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "GETS_MAX", &pc->IntType, (union AnyValue *)&GETS_MAXValue, FALSE); VariableDefinePlatformVar(pc, NULL, "GETS_MAX", &pc->IntType, (union AnyValue *)&GETS_MAXValue, FALSE);
/* define stdin, stdout and stderr */ /* define stdin, stdout and stderr */
VariableDefinePlatformVar(pc, NULL, "stdin", FilePtrType, (union AnyValue *)&stdinValue, FALSE); VariableDefinePlatformVar(pc, NULL, "stdin", FilePtrType, (union AnyValue *)&stdinValue, FALSE);
VariableDefinePlatformVar(pc, NULL, "stdout", FilePtrType, (union AnyValue *)&stdoutValue, FALSE); VariableDefinePlatformVar(pc, NULL, "stdout", FilePtrType, (union AnyValue *)&stdoutValue, FALSE);

View file

@ -3,7 +3,7 @@
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] = struct LibraryFunction PlatformLibrary[] =
{ {
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit() void PlatformLibraryInit()

View file

@ -1,16 +1,16 @@
#include "../interpreter.h" #include "../interpreter.h"
void MsvcSetupFunc(Picoc *pc) void MsvcSetupFunc(Picoc *pc)
{ {
} }
void CTest (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void CTest (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
printf("test(%d)\n", Param[0]->Val->Integer); printf("test(%d)\n", Param[0]->Val->Integer);
Param[0]->Val->Integer = 1234; Param[0]->Val->Integer = 1234;
} }
void CLineNo (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void CLineNo (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = Parser->Line; ReturnValue->Val->Integer = Parser->Line;
} }
@ -18,9 +18,9 @@ void CLineNo (struct ParseState *Parser, struct Value *ReturnValue, struct Value
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction MsvcFunctions[] = struct LibraryFunction MsvcFunctions[] =
{ {
{ CTest, "void Test(int);" }, {CTest, "void Test(int);"},
{ CLineNo, "int LineNo();" }, {CLineNo, "int LineNo();"},
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit(Picoc *pc) void PlatformLibraryInit(Picoc *pc)

View file

@ -5,10 +5,10 @@ static int GPSlat, GPSlon, GPSalt, GPSfix, GPSsat, GPSutc, Elcount, Ercount;
static int ScanVect[16], NNVect[NUM_OUTPUT]; static int ScanVect[16], NNVect[NUM_OUTPUT];
struct ValueType *IntArrayType; struct ValueType *IntArrayType;
void SRV1SetupFunc() void SRV1SetupFunc()
{ {
IntArrayType = TypeGetMatching(NULL, &IntType, TypeArray, 16, StrEmpty, TRUE); IntArrayType = TypeGetMatching(NULL, &IntType, TypeArray, 16, StrEmpty, TRUE);
VariableDefinePlatformVar(NULL, "scanvect", IntArrayType, (union AnyValue *)&ScanVect, FALSE); VariableDefinePlatformVar(NULL, "scanvect", IntArrayType, (union AnyValue *)&ScanVect, FALSE);
VariableDefinePlatformVar(NULL, "neuron", IntArrayType, (union AnyValue *)&NNVect, FALSE); VariableDefinePlatformVar(NULL, "neuron", IntArrayType, (union AnyValue *)&NNVect, FALSE);
@ -49,7 +49,7 @@ void Cinput(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
void Cdelay(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cdelay(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int del; int del;
del = Param[0]->Val->Integer; del = Param[0]->Val->Integer;
if ((del < 0) || (del > 1000000)) if ((del < 0) || (del > 1000000))
return; return;
@ -69,7 +69,7 @@ void Ctime(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
void Ciodir(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Ciodir(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int dir; int dir;
dir = Param[0]->Val->Integer; dir = Param[0]->Val->Integer;
*pPORTHIO_DIR = ((dir << 10) & 0xFC00) + (*pPORTHIO_DIR & 0x03FF); // H15/14/13/12/11/10 - 1=output, 0=input *pPORTHIO_DIR = ((dir << 10) & 0xFC00) + (*pPORTHIO_DIR & 0x03FF); // H15/14/13/12/11/10 - 1=output, 0=input
*pPORTHIO_INEN = (((~dir) << 10) & 0xFC00) + (*pPORTHIO_INEN & 0x03FF); // invert dir bits to enable inputs *pPORTHIO_INEN = (((~dir) << 10) & 0xFC00) + (*pPORTHIO_INEN & 0x03FF); // invert dir bits to enable inputs
@ -91,27 +91,27 @@ void Cpeek(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
unsigned char *cp; unsigned char *cp;
unsigned short *sp; unsigned short *sp;
unsigned int *ip; unsigned int *ip;
/* x = peek(addr, size); /* x = peek(addr, size);
mask ptr to align with word size */ mask ptr to align with word size */
ptr = Param[0]->Val->Integer; ptr = Param[0]->Val->Integer;
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
ReturnValue->Val->Integer = (int)((unsigned int)*cp); ReturnValue->Val->Integer = (int)((unsigned int)*cp);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary
ReturnValue->Val->Integer = (int)((unsigned short)*sp); ReturnValue->Val->Integer = (int)((unsigned short)*sp);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary
ReturnValue->Val->Integer = (int)*ip; ReturnValue->Val->Integer = (int)*ip;
break; break;
default: default:
ReturnValue->Val->Integer = 0; ReturnValue->Val->Integer = 0;
break; break;
} }
} }
@ -121,37 +121,37 @@ void Cpoke(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
unsigned char *cp; unsigned char *cp;
unsigned short *sp; unsigned short *sp;
unsigned int *ip; unsigned int *ip;
/* x = poke(addr, size, val); /* x = poke(addr, size, val);
mask ptr to align with word size */ mask ptr to align with word size */
ptr = Param[0]->Val->Integer; ptr = Param[0]->Val->Integer;
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
val = Param[2]->Val->Integer; val = Param[2]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
*cp = (unsigned char)(val & 0x000000FF); *cp = (unsigned char)(val & 0x000000FF);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); sp = (unsigned short *)(ptr & 0xFFFFFFFE);
*sp = (unsigned short)(val & 0x0000FFFF); *sp = (unsigned short)(val & 0x0000FFFF);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); ip = (unsigned int *)(ptr & 0xFFFFFFFC);
*ip = val; *ip = val;
break; break;
default: // don't bother with bad value default: // don't bother with bad value
break; break;
} }
} }
void Cencoders(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cencoders(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
unsigned int ix; unsigned int ix;
ix = encoders(); // read left and right encoders; save data to C globals lcount, rcount ix = encoders(); // read left and right encoders; save data to C globals lcount, rcount
Elcount = (ix >> 16) & 0x0000FFFF; Elcount = (ix >> 16) & 0x0000FFFF;
Ercount = ix & 0x0000FFFF; Ercount = ix & 0x0000FFFF;
} }
void Cmotors(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cmotors(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
@ -191,7 +191,7 @@ void Cmotors2(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cservos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cservos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int lspeed, rspeed; int lspeed, rspeed;
lspeed = Param[0]->Val->Integer; lspeed = Param[0]->Val->Integer;
if ((lspeed < 0) || (lspeed > 100)) if ((lspeed < 0) || (lspeed > 100))
ProgramFail(NULL, "servos(): TMR2 value out of range"); ProgramFail(NULL, "servos(): TMR2 value out of range");
@ -209,7 +209,7 @@ void Cservos(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cservos2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cservos2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int lspeed, rspeed; int lspeed, rspeed;
lspeed = Param[0]->Val->Integer; lspeed = Param[0]->Val->Integer;
if ((lspeed < 0) || (lspeed > 100)) if ((lspeed < 0) || (lspeed > 100))
ProgramFail(NULL, "servos2(): TMR6 value out of range"); ProgramFail(NULL, "servos2(): TMR6 value out of range");
@ -228,15 +228,15 @@ void Claser(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
{ {
*pPORTHIO &= 0xFD7F; // turn off both lasers *pPORTHIO &= 0xFD7F; // turn off both lasers
switch (Param[0]->Val->Integer) { switch (Param[0]->Val->Integer) {
case 1: case 1:
*pPORTHIO |= 0x0080; // turn on left laser *pPORTHIO |= 0x0080; // turn on left laser
break; break;
case 2: case 2:
*pPORTHIO |= 0x0200; // turn on right laser *pPORTHIO |= 0x0200; // turn on right laser
break; break;
case 3: case 3:
*pPORTHIO |= 0x0280; // turn on both lasers *pPORTHIO |= 0x0280; // turn on both lasers
break; break;
} }
} }
@ -264,11 +264,11 @@ void Cbattery(struct ParseState *Parser, struct Value *ReturnValue, struct Value
ReturnValue->Val->Integer = 1; // battery voltage okay ReturnValue->Val->Integer = 1; // battery voltage okay
} }
void Cvcolor(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin - void Cvcolor(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin -
// vcolor (color, ymin, ymax, umin, umax, vmin, vmax); // vcolor (color, ymin, ymax, umin, umax, vmin, vmax);
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
ymin[ix] = Param[1]->Val->Integer; ymin[ix] = Param[1]->Val->Integer;
ymax[ix] = Param[2]->Val->Integer; ymax[ix] = Param[2]->Val->Integer;
@ -278,7 +278,7 @@ void Cvcolor(struct ParseState *Parser, struct Value *ReturnValue, struct Value
vmax[ix] = Param[6]->Val->Integer; vmax[ix] = Param[6]->Val->Integer;
} }
void Cvcam(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set camera functions - void Cvcam(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set camera functions -
// enable/disable AGC(4) / AWB(2) / AEC(1) camera controls // enable/disable AGC(4) / AWB(2) / AEC(1) camera controls
// vcam(7) = AGC+AWB+AEC on vcam(0) = AGC+AWB+AEC off // vcam(7) = AGC+AWB+AEC on vcam(0) = AGC+AWB+AEC off
{ {
@ -291,11 +291,11 @@ void Cvcam(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
i2cwrite(0x21, (unsigned char *)i2c_data, 1, SCCB_ON); // OV7725 i2cwrite(0x21, (unsigned char *)i2c_data, 1, SCCB_ON); // OV7725
} }
void Cvfind(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin - void Cvfind(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin -
// vfind (color, x1, x2, y1, y2); // vfind (color, x1, x2, y1, y2);
{ {
int ix, x1, x2, y1, y2; int ix, x1, x2, y1, y2;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
x1 = Param[1]->Val->Integer; x1 = Param[1]->Val->Integer;
x2 = Param[2]->Val->Integer; x2 = Param[2]->Val->Integer;
@ -337,13 +337,13 @@ void Cvscan(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
if ((col < 1) || (col > 9)) if ((col < 1) || (col > 9))
ProgramFail(NULL, "vscan(): number of columns must be between 1 and 9"); ProgramFail(NULL, "vscan(): number of columns must be between 1 and 9");
thresh = Param[1]->Val->Integer; thresh = Param[1]->Val->Integer;
if ((thresh < 0) || (thresh > 9999)) if ((thresh < 0) || (thresh > 9999))
ProgramFail(NULL, "vscan(): threshold must be between 0 and 9999"); ProgramFail(NULL, "vscan(): threshold must be between 0 and 9999");
ix = vscan((unsigned char *)SPI_BUFFER1, (unsigned char *)FRAME_BUF, thresh, (unsigned int)col, (unsigned int *)&ScanVect[0]); ix = vscan((unsigned char *)SPI_BUFFER1, (unsigned char *)FRAME_BUF, thresh, (unsigned int)col, (unsigned int *)&ScanVect[0]);
ReturnValue->Val->Integer = ix; ReturnValue->Val->Integer = ix;
} }
void Cvmean(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cvmean(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
vmean((unsigned char *)FRAME_BUF); vmean((unsigned char *)FRAME_BUF);
Iy1 = mean[0]; Iy1 = mean[0];
@ -361,7 +361,7 @@ void Cvblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
iblob = Param[1]->Val->Integer; iblob = Param[1]->Val->Integer;
if (iblob > MAX_BLOBS) if (iblob > MAX_BLOBS)
ProgramFail(NULL, "blob(): invalid blob index"); ProgramFail(NULL, "blob(): invalid blob index");
numblob = vblob((unsigned char *)FRAME_BUF, (unsigned char *)FRAME_BUF3, ix); numblob = vblob((unsigned char *)FRAME_BUF, (unsigned char *)FRAME_BUF3, ix);
if ((blobcnt[iblob] == 0) || (numblob == -1)) { if ((blobcnt[iblob] == 0) || (numblob == -1)) {
@ -379,14 +379,14 @@ void Cvblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
void Cvjpeg (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cvjpeg (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
unsigned int image_size, qual; unsigned int image_size, qual;
unsigned char *output_start, *output_end; unsigned char *output_start, *output_end;
qual = Param[0]->Val->Integer; qual = Param[0]->Val->Integer;
if ((qual < 1) || (qual > 8)) if ((qual < 1) || (qual > 8))
ProgramFail(NULL, "vjpeg(): quality parameter out of range"); ProgramFail(NULL, "vjpeg(): quality parameter out of range");
output_start = (unsigned char *)JPEG_BUF; output_start = (unsigned char *)JPEG_BUF;
output_end = encode_image((unsigned char *)FRAME_BUF, output_start, qual, output_end = encode_image((unsigned char *)FRAME_BUF, output_start, qual,
FOUR_TWO_TWO, imgWidth, imgHeight); FOUR_TWO_TWO, imgWidth, imgHeight);
image_size = (unsigned int)(output_end - output_start); image_size = (unsigned int)(output_end - output_start);
ReturnValue->Val->Integer = image_size; ReturnValue->Val->Integer = image_size;
@ -395,15 +395,15 @@ void Cvjpeg (struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cvsend (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cvsend (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
unsigned int ix, image_size; unsigned int ix, image_size;
unsigned char *cp; unsigned char *cp;
image_size = Param[0]->Val->Integer; image_size = Param[0]->Val->Integer;
if ((image_size < 0) || (image_size > 200000)) if ((image_size < 0) || (image_size > 200000))
ProgramFail(NULL, "vsend(): image size out of range"); ProgramFail(NULL, "vsend(): image size out of range");
led1_on(); led1_on();
cp = (unsigned char *)JPEG_BUF; cp = (unsigned char *)JPEG_BUF;
for (ix=0; ix<image_size; ix++) for (ix=0; ix<image_size; ix++)
putchar(*cp++); putchar(*cp++);
led0_on(); led0_on();
@ -413,7 +413,7 @@ void Ccompass(struct ParseState *Parser, struct Value *ReturnValue, struct Value
{ {
unsigned char i2c_data[2]; unsigned char i2c_data[2];
unsigned int ix; unsigned int ix;
i2c_data[0] = 0x41; // read compass twice to clear last reading i2c_data[0] = 0x41; // read compass twice to clear last reading
i2cread(0x22, (unsigned char *)i2c_data, 2, SCCB_ON); i2cread(0x22, (unsigned char *)i2c_data, 2, SCCB_ON);
delayMS(20); delayMS(20);
@ -426,7 +426,7 @@ void Ccompass(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Ctilt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass void Ctilt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass
{ {
unsigned int ix; unsigned int ix;
ix = (unsigned int)Param[0]->Val->Integer; ix = (unsigned int)Param[0]->Val->Integer;
if ((ix<1) || (ix>3)) if ((ix<1) || (ix>3))
ProgramFail(NULL, "tilt(): invalid channel"); ProgramFail(NULL, "tilt(): invalid channel");
@ -439,33 +439,33 @@ void Canalog(struct ParseState *Parser, struct Value *ReturnValue, struct Value
unsigned int ix, channel; unsigned int ix, channel;
unsigned char mask1[] = { 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08 }; unsigned char mask1[] = { 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x04, 0x08 };
unsigned char mask2[] = { 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00 }; unsigned char mask2[] = { 0x10, 0x20, 0x40, 0x80, 0x00, 0x00, 0x00, 0x00 };
// decide which i2c device based on channel range // decide which i2c device based on channel range
ix = (unsigned char)Param[0]->Val->Integer; ix = (unsigned char)Param[0]->Val->Integer;
if ((ix<1) || (ix>28)) if ((ix<1) || (ix>28))
ProgramFail(NULL, "analog(): invalid channel"); ProgramFail(NULL, "analog(): invalid channel");
device_id = 0; device_id = 0;
switch (ix / 10) { switch (ix / 10) {
case 0: case 0:
device_id = 0x20; // channels 1-8 device_id = 0x20; // channels 1-8
break; break;
case 1: case 1:
device_id = 0x23; // channels 11-18 device_id = 0x23; // channels 11-18
break; break;
case 2: case 2:
device_id = 0x24; // channels 21-28 device_id = 0x24; // channels 21-28
break; break;
} }
channel = ix % 10; channel = ix % 10;
if ((channel<1) || (channel>8)) if ((channel<1) || (channel>8))
ProgramFail(NULL, "analog(): invalid channel"); ProgramFail(NULL, "analog(): invalid channel");
// set timer register 3 // set timer register 3
i2c_data[0] = 0x03; i2c_data[0] = 0x03;
i2c_data[1] = 0x01; i2c_data[1] = 0x01;
i2cwrite(device_id, (unsigned char *)i2c_data, 1, SCCB_ON); i2cwrite(device_id, (unsigned char *)i2c_data, 1, SCCB_ON);
// set analog channel // set analog channel
i2c_data[0] = 0x02; i2c_data[0] = 0x02;
i2c_data[1] = mask1[channel-1]; i2c_data[1] = mask1[channel-1];
i2c_data[2] = mask2[channel-1]; i2c_data[2] = mask2[channel-1];
@ -495,21 +495,21 @@ void Cgps(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Creadi2c(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax val = readi2c(device, register); void Creadi2c(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax val = readi2c(device, register);
{ {
unsigned char i2c_device, i2c_data[2]; unsigned char i2c_device, i2c_data[2];
i2c_device = (unsigned char)Param[0]->Val->Integer; i2c_device = (unsigned char)Param[0]->Val->Integer;
i2c_data[0] = (unsigned char)Param[1]->Val->Integer; i2c_data[0] = (unsigned char)Param[1]->Val->Integer;
i2cread(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF); i2cread(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF);
ReturnValue->Val->Integer = ((int)i2c_data[0] & 0x000000FF); ReturnValue->Val->Integer = ((int)i2c_data[0] & 0x000000FF);
} }
void Creadi2c2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax two_byte_val = readi2c(device, register); void Creadi2c2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax two_byte_val = readi2c(device, register);
{ {
unsigned char i2c_device, i2c_data[2]; unsigned char i2c_device, i2c_data[2];
i2c_device = (unsigned char)Param[0]->Val->Integer; i2c_device = (unsigned char)Param[0]->Val->Integer;
i2c_data[0] = (unsigned char)Param[1]->Val->Integer; i2c_data[0] = (unsigned char)Param[1]->Val->Integer;
i2cread(i2c_device, (unsigned char *)i2c_data, 2, SCCB_OFF); i2cread(i2c_device, (unsigned char *)i2c_data, 2, SCCB_OFF);
ReturnValue->Val->Integer = (((unsigned int)i2c_data[0] << 8) + i2c_data[1]); ReturnValue->Val->Integer = (((unsigned int)i2c_data[0] << 8) + i2c_data[1]);
} }
@ -521,14 +521,14 @@ void Cwritei2c(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
i2c_device = (unsigned char)Param[0]->Val->Integer; i2c_device = (unsigned char)Param[0]->Val->Integer;
i2c_data[0] = (unsigned char)Param[1]->Val->Integer; i2c_data[0] = (unsigned char)Param[1]->Val->Integer;
i2c_data[1] = (unsigned char)Param[2]->Val->Integer; i2c_data[1] = (unsigned char)Param[2]->Val->Integer;
i2cwrite(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF); i2cwrite(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF);
} }
void Csin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sin(angle) void Csin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sin(angle)
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; // input to function is angle in degrees ix = Param[0]->Val->Integer; // input to function is angle in degrees
ReturnValue->Val->Integer = sin(ix); ReturnValue->Val->Integer = sin(ix);
} }
@ -536,7 +536,7 @@ void Csin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Ccos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // cos(angle) void Ccos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // cos(angle)
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; // input to function is angle in degrees ix = Param[0]->Val->Integer; // input to function is angle in degrees
ReturnValue->Val->Integer = cos(ix); ReturnValue->Val->Integer = cos(ix);
} }
@ -544,7 +544,7 @@ void Ccos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Ctan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // tan(angle) void Ctan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // tan(angle)
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; // input to function is angle in degrees ix = Param[0]->Val->Integer; // input to function is angle in degrees
ReturnValue->Val->Integer = tan(ix); ReturnValue->Val->Integer = tan(ix);
} }
@ -575,7 +575,7 @@ void Catan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
y = Param[0]->Val->Integer; y = Param[0]->Val->Integer;
x = Param[1]->Val->Integer; x = Param[1]->Val->Integer;
ReturnValue->Val->Integer = atan(y, x); ReturnValue->Val->Integer = atan(y, x);
} }
void Cgps_head(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_head(lat1, lon1, lat2, lon2) void Cgps_head(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_head(lat1, lon1, lat2, lon2)
{ {
@ -585,7 +585,7 @@ void Cgps_head(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
lat2 = Param[2]->Val->Integer; lat2 = Param[2]->Val->Integer;
lon2 = Param[3]->Val->Integer; lon2 = Param[3]->Val->Integer;
ReturnValue->Val->Integer = gps_head(lat1, lon1, lat2, lon2); ReturnValue->Val->Integer = gps_head(lat1, lon1, lat2, lon2);
} }
void Cgps_dist(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_dist(lat1, lon1, lat2, lon2) void Cgps_dist(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_dist(lat1, lon1, lat2, lon2)
{ {
@ -595,18 +595,18 @@ void Cgps_dist(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
lat2 = Param[2]->Val->Integer; lat2 = Param[2]->Val->Integer;
lon2 = Param[3]->Val->Integer; lon2 = Param[3]->Val->Integer;
ReturnValue->Val->Integer = gps_dist(lat1, lon1, lat2, lon2); ReturnValue->Val->Integer = gps_dist(lat1, lon1, lat2, lon2);
} }
void Csqrt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sqrt(x) void Csqrt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sqrt(x)
{ {
int x; int x;
x = Param[0]->Val->Integer; x = Param[0]->Val->Integer;
ReturnValue->Val->Integer = isqrt(x); ReturnValue->Val->Integer = isqrt(x);
} }
void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1; int ix, i1;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS) if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnset(): invalid index"); ProgramFail(NULL, "nnset(): invalid index");
@ -616,7 +616,7 @@ void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
void Cnnshow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnshow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix; int ix;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS) if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnshow(): invalid index"); ProgramFail(NULL, "nnshow(): invalid index");
@ -634,7 +634,7 @@ void Cnntrain(struct ParseState *Parser, struct Value *ReturnValue, struct Value
for (ix=0; ix<NUM_NPATTERNS; ix++) { for (ix=0; ix<NUM_NPATTERNS; ix++) {
nnset_pattern(ix); nnset_pattern(ix);
nncalculate_network(); nncalculate_network();
for (i1=0; i1<NUM_OUTPUT; i1++) for (i1=0; i1<NUM_OUTPUT; i1++)
printf(" %3d", N_OUT(i1)/10); printf(" %3d", N_OUT(i1)/10);
printf("\r\n"); printf("\r\n");
} }
@ -643,7 +643,7 @@ void Cnntrain(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cnntest(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnntest(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1, i2, max; int ix, i1, i2, max;
unsigned char ch; unsigned char ch;
ix = 0; ix = 0;
for (i1=0; i1<8; i1++) { for (i1=0; i1<8; i1++) {
ch = (unsigned char)Param[i1]->Val->Integer; ch = (unsigned char)Param[i1]->Val->Integer;
@ -667,9 +667,9 @@ void Cnntest(struct ParseState *Parser, struct Value *ReturnValue, struct Value
ReturnValue->Val->Integer = ix; ReturnValue->Val->Integer = ix;
} }
void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1, max; int ix, i1, max;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > MAX_BLOBS) if (ix > MAX_BLOBS)
ProgramFail(NULL, "nnmatchblob(): invalid blob index"); ProgramFail(NULL, "nnmatchblob(): invalid blob index");
@ -679,7 +679,7 @@ void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct V
square the aspect ratio of x1, x2, y1, y2 square the aspect ratio of x1, x2, y1, y2
then subsample blob pixels to populate N_IN(0:63) with 0:1024 values then subsample blob pixels to populate N_IN(0:63) with 0:1024 values
then nncalculate_network() and display the N_OUT() results */ then nncalculate_network() and display the N_OUT() results */
nnscale8x8((unsigned char *)FRAME_BUF3, blobix[ix], blobx1[ix], blobx2[ix], nnscale8x8((unsigned char *)FRAME_BUF3, blobix[ix], blobx1[ix], blobx2[ix],
bloby1[ix], bloby2[ix], imgWidth, imgHeight); bloby1[ix], bloby2[ix], imgWidth, imgHeight);
nncalculate_network(); nncalculate_network();
ix = 0; ix = 0;
@ -696,13 +696,13 @@ void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct V
void Cnnlearnblob (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnlearnblob (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix; int ix;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS) if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnlearnblob(): invalid index"); ProgramFail(NULL, "nnlearnblob(): invalid index");
if (!blobcnt[0]) if (!blobcnt[0])
ProgramFail(NULL, "nnlearnblob(): no blob to grab"); ProgramFail(NULL, "nnlearnblob(): no blob to grab");
nnscale8x8((unsigned char *)FRAME_BUF3, blobix[0], blobx1[0], blobx2[0], nnscale8x8((unsigned char *)FRAME_BUF3, blobix[0], blobx1[0], blobx2[0],
bloby1[0], bloby2[0], imgWidth, imgHeight); bloby1[0], bloby2[0], imgWidth, imgHeight);
nnpack8x8(ix); nnpack8x8(ix);
nndisplay(ix); nndisplay(ix);
@ -711,7 +711,7 @@ void Cnnlearnblob (struct ParseState *Parser, struct Value *ReturnValue, struct
void Cautorun (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cautorun (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, t0; int ix, t0;
unsigned char ch; unsigned char ch;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
t0 = readRTC(); t0 = readRTC();
while (readRTC() < (t0 + ix*1000)) { // watch for ESC in 'ix' seconds while (readRTC() < (t0 + ix*1000)) { // watch for ESC in 'ix' seconds
@ -743,64 +743,64 @@ struct LibraryFunction PlatformLibrary[] =
/* list of all library functions included with srv1.h */ /* list of all library functions included with srv1.h */
struct LibraryFunction SRV1Functions[] = struct LibraryFunction SRV1Functions[] =
{ {
{ Csignal, "int signal();" }, {Csignal, "int signal();"},
{ Cinput, "int input();" }, {Cinput, "int input();"},
{ Cdelay, "void delay(int);" }, {Cdelay, "void delay(int);"},
{ Crand, "int rand(int);" }, {Crand, "int rand(int);"},
{ Ctime, "int time();" }, {Ctime, "int time();"},
{ Ciodir, "void iodir(int);" }, {Ciodir, "void iodir(int);"},
{ Cioread, "int ioread();" }, {Cioread, "int ioread();"},
{ Ciowrite, "void iowrite(int);" }, {Ciowrite, "void iowrite(int);"},
{ Cpeek, "int peek(int, int);" }, {Cpeek, "int peek(int, int);"},
{ Cpoke, "void poke(int, int, int);" }, {Cpoke, "void poke(int, int, int);"},
{ Cmotors, "void motors(int, int);" }, {Cmotors, "void motors(int, int);"},
{ Cmotors2, "void motors2(int, int);" }, {Cmotors2, "void motors2(int, int);"},
{ Cservos, "void servos(int, int);" }, {Cservos, "void servos(int, int);"},
{ Cservos2, "void servos2(int, int);" }, {Cservos2, "void servos2(int, int);"},
{ Cencoders, "void encoders();" }, {Cencoders, "void encoders();"},
{ Claser, "void laser(int);" }, {Claser, "void laser(int);"},
{ Csonar, "int sonar(int);" }, {Csonar, "int sonar(int);"},
{ Crange, "int range();" }, {Crange, "int range();"},
{ Cbattery, "int battery();" }, {Cbattery, "int battery();"},
{ Cvcolor, "void vcolor(int, int, int, int, int, int, int);" }, {Cvcolor, "void vcolor(int, int, int, int, int, int, int);"},
{ Cvfind, "int vfind(int, int, int, int, int);" }, {Cvfind, "int vfind(int, int, int, int, int);"},
{ Cvcam, "void vcam(int);" }, {Cvcam, "void vcam(int);"},
{ Cvcap, "void vcap();" }, {Cvcap, "void vcap();"},
{ Cvrcap, "void vrcap();" }, {Cvrcap, "void vrcap();"},
{ Cvdiff, "void vdiff(int);" }, {Cvdiff, "void vdiff(int);"},
{ Cvpix, "void vpix(int, int);" }, {Cvpix, "void vpix(int, int);"},
{ Cvscan, "int vscan(int, int);" }, {Cvscan, "int vscan(int, int);"},
{ Cvmean, "void vmean();" }, {Cvmean, "void vmean();"},
{ Cvblob, "int vblob(int, int);" }, {Cvblob, "int vblob(int, int);"},
{ Cvjpeg, "int vjpeg(int);" }, {Cvjpeg, "int vjpeg(int);"},
{ Cvsend, "void vsend(int);" }, {Cvsend, "void vsend(int);"},
{ Ccompass, "int compass();" }, {Ccompass, "int compass();"},
{ Canalog, "int analog(int);" }, {Canalog, "int analog(int);"},
{ Ctilt, "int tilt(int);" }, {Ctilt, "int tilt(int);"},
{ Cgps, "void gps();" }, {Cgps, "void gps();"},
{ Creadi2c, "int readi2c(int, int);" }, {Creadi2c, "int readi2c(int, int);"},
{ Creadi2c2, "int readi2c2(int, int);" }, {Creadi2c2, "int readi2c2(int, int);"},
{ Cwritei2c, "void writei2c(int, int, int);" }, {Cwritei2c, "void writei2c(int, int, int);"},
{ Csin, "int sin(int);" }, {Csin, "int sin(int);"},
{ Ccos, "int cos(int);" }, {Ccos, "int cos(int);"},
{ Ctan, "int tan(int);" }, {Ctan, "int tan(int);"},
{ Casin, "int asin(int, int);" }, {Casin, "int asin(int, int);"},
{ Cacos, "int acos(int, int);" }, {Cacos, "int acos(int, int);"},
{ Catan, "int atan(int, int);" }, {Catan, "int atan(int, int);"},
{ Cgps_head, "int gps_head(int, int, int, int);" }, {Cgps_head, "int gps_head(int, int, int, int);"},
{ Cgps_dist, "int gps_dist(int, int, int, int);" }, {Cgps_dist, "int gps_dist(int, int, int, int);"},
{ Csqrt, "int sqrt(int);" }, {Csqrt, "int sqrt(int);"},
{ Cnnshow, "void nnshow(int);" }, {Cnnshow, "void nnshow(int);"},
{ Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);" }, {Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);"},
{ Cnninit, "void nninit();" }, {Cnninit, "void nninit();"},
{ Cnntrain, "void nntrain();" }, {Cnntrain, "void nntrain();"},
{ Cnntest, "int nntest(int, int, int, int, int, int, int, int);" }, {Cnntest, "int nntest(int, int, int, int, int, int, int, int);"},
{ Cnnmatchblob, "int nnmatchblob(int);" }, {Cnnmatchblob, "int nnmatchblob(int);"},
{ Cnnlearnblob, "void nnlearnblob(int);" }, {Cnnlearnblob, "void nnlearnblob(int);"},
{ Cautorun, "void autorun(int);" }, {Cautorun, "void autorun(int);"},
{ Clineno, "int lineno();" }, {Clineno, "int lineno();"},
{ Cerrormsg, "void errormsg(char *);" }, {Cerrormsg, "void errormsg(char *);"},
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit() void PlatformLibraryInit()

View file

@ -9,7 +9,7 @@ static int ScanVect[16], NNVect[NUM_OUTPUT];
void PlatformLibraryInit() void PlatformLibraryInit()
{ {
struct ValueType *IntArrayType; struct ValueType *IntArrayType;
IntArrayType = TypeGetMatching(NULL, &IntType, TypeArray, 16, StrEmpty, TRUE); IntArrayType = TypeGetMatching(NULL, &IntType, TypeArray, 16, StrEmpty, TRUE);
VariableDefinePlatformVar(NULL, "scanvect", IntArrayType, (union AnyValue *)&ScanVect, FALSE); VariableDefinePlatformVar(NULL, "scanvect", IntArrayType, (union AnyValue *)&ScanVect, FALSE);
VariableDefinePlatformVar(NULL, "neuron", IntArrayType, (union AnyValue *)&NNVect, FALSE); VariableDefinePlatformVar(NULL, "neuron", IntArrayType, (union AnyValue *)&NNVect, FALSE);
@ -64,7 +64,7 @@ void Cread_int(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
{ {
int ix, sign; int ix, sign;
unsigned char ch; unsigned char ch;
ix = 0; ix = 0;
sign = 1; sign = 1;
while (1) { while (1) {
@ -85,7 +85,7 @@ void Cread_str(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
{ {
int ix; int ix;
unsigned char ch; unsigned char ch;
ix = 0; ix = 0;
char *cp = (char *)Param[0]->Val->Pointer; char *cp = (char *)Param[0]->Val->Pointer;
while (1) { while (1) {
@ -100,9 +100,9 @@ void Cread_str(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
cp[ix] = 0; cp[ix] = 0;
ix--; ix--;
break; break;
} }
} }
ReturnValue->Val->Integer = ix; ReturnValue->Val->Integer = ix;
} }
void Cinit_uart1(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return 0-9 from console input void Cinit_uart1(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return 0-9 from console input
@ -129,7 +129,7 @@ void Coutput1(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cdelay(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cdelay(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int del; int del;
del = Param[0]->Val->Integer; del = Param[0]->Val->Integer;
if ((del < 0) || (del > 1000000)) if ((del < 0) || (del > 1000000))
return; return;
@ -149,7 +149,7 @@ void Ctime(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
void Ciodir(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Ciodir(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int dir; int dir;
dir = Param[0]->Val->Integer; dir = Param[0]->Val->Integer;
*pPORTHIO_DIR = ((dir << 10) & 0xFC00) + (*pPORTHIO_DIR & 0x03FF); // H15/14/13/12/11/10 - 1=output, 0=input *pPORTHIO_DIR = ((dir << 10) & 0xFC00) + (*pPORTHIO_DIR & 0x03FF); // H15/14/13/12/11/10 - 1=output, 0=input
*pPORTHIO_INEN = (((~dir) << 10) & 0xFC00) + (*pPORTHIO_INEN & 0x03FF); // invert dir bits to enable inputs *pPORTHIO_INEN = (((~dir) << 10) & 0xFC00) + (*pPORTHIO_INEN & 0x03FF); // invert dir bits to enable inputs
@ -171,27 +171,27 @@ void Cpeek(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
unsigned char *cp; unsigned char *cp;
unsigned short *sp; unsigned short *sp;
unsigned int *ip; unsigned int *ip;
/* x = peek(addr, size); /* x = peek(addr, size);
mask ptr to align with word size */ mask ptr to align with word size */
ptr = Param[0]->Val->Integer; ptr = Param[0]->Val->Integer;
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
ReturnValue->Val->Integer = (int)((unsigned int)*cp); ReturnValue->Val->Integer = (int)((unsigned int)*cp);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary sp = (unsigned short *)(ptr & 0xFFFFFFFE); // align with even boundary
ReturnValue->Val->Integer = (int)((unsigned short)*sp); ReturnValue->Val->Integer = (int)((unsigned short)*sp);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary ip = (unsigned int *)(ptr & 0xFFFFFFFC); // aling with quad boundary
ReturnValue->Val->Integer = (int)*ip; ReturnValue->Val->Integer = (int)*ip;
break; break;
default: default:
ReturnValue->Val->Integer = 0; ReturnValue->Val->Integer = 0;
break; break;
} }
} }
@ -201,45 +201,45 @@ void Cpoke(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
unsigned char *cp; unsigned char *cp;
unsigned short *sp; unsigned short *sp;
unsigned int *ip; unsigned int *ip;
/* x = poke(addr, size, val); /* x = poke(addr, size, val);
mask ptr to align with word size */ mask ptr to align with word size */
ptr = Param[0]->Val->Integer; ptr = Param[0]->Val->Integer;
size = Param[1]->Val->Integer; size = Param[1]->Val->Integer;
val = Param[2]->Val->Integer; val = Param[2]->Val->Integer;
switch (size) { switch (size) {
case 1: // char * case 1: // char *
cp = (unsigned char *)ptr; cp = (unsigned char *)ptr;
*cp = (unsigned char)(val & 0x000000FF); *cp = (unsigned char)(val & 0x000000FF);
break; break;
case 2: // short * case 2: // short *
sp = (unsigned short *)(ptr & 0xFFFFFFFE); sp = (unsigned short *)(ptr & 0xFFFFFFFE);
*sp = (unsigned short)(val & 0x0000FFFF); *sp = (unsigned short)(val & 0x0000FFFF);
break; break;
case 4: // int * case 4: // int *
ip = (unsigned int *)(ptr & 0xFFFFFFFC); ip = (unsigned int *)(ptr & 0xFFFFFFFC);
*ip = val; *ip = val;
break; break;
default: // don't bother with bad value default: // don't bother with bad value
break; break;
} }
} }
void Cencoders(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cencoders(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
unsigned int ix; unsigned int ix;
ix = encoders(); // read left and right encoders; save data to C globals lcount, rcount ix = encoders(); // read left and right encoders; save data to C globals lcount, rcount
Elcount = (ix >> 16) & 0x0000FFFF; Elcount = (ix >> 16) & 0x0000FFFF;
Ercount = ix & 0x0000FFFF; Ercount = ix & 0x0000FFFF;
} }
void Cencoderx(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass void Cencoderx(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass
{ {
int ix; int ix;
ix = (unsigned char)Param[0]->Val->Integer; ix = (unsigned char)Param[0]->Val->Integer;
if ((ix<0) || (ix>7)) if ((ix<0) || (ix>7))
ProgramFail(NULL, "encoderx(): invalid channel"); ProgramFail(NULL, "encoderx(): invalid channel");
ReturnValue->Val->Integer = encoder_4wd(ix); ReturnValue->Val->Integer = encoder_4wd(ix);
} }
@ -283,7 +283,7 @@ void Cmotorx(struct ParseState *Parser, struct Value *ReturnValue, struct Value
{ {
unsigned char ch; unsigned char ch;
int ls, rs; int ls, rs;
ls = Param[0]->Val->Integer; ls = Param[0]->Val->Integer;
if ((ls < -100) || (ls > 100)) if ((ls < -100) || (ls > 100))
ProgramFail(NULL, "motors(): left motor value out of range"); ProgramFail(NULL, "motors(): left motor value out of range");
@ -307,7 +307,7 @@ void Cmotorx(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cservos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cservos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int lspeed, rspeed; int lspeed, rspeed;
lspeed = Param[0]->Val->Integer; lspeed = Param[0]->Val->Integer;
if ((lspeed < 0) || (lspeed > 100)) if ((lspeed < 0) || (lspeed > 100))
ProgramFail(NULL, "servos(): TMR2 value out of range"); ProgramFail(NULL, "servos(): TMR2 value out of range");
@ -325,7 +325,7 @@ void Cservos(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cservos2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cservos2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
int lspeed, rspeed; int lspeed, rspeed;
lspeed = Param[0]->Val->Integer; lspeed = Param[0]->Val->Integer;
if ((lspeed < 0) || (lspeed > 100)) if ((lspeed < 0) || (lspeed > 100))
ProgramFail(NULL, "servos2(): TMR6 value out of range"); ProgramFail(NULL, "servos2(): TMR6 value out of range");
@ -344,15 +344,15 @@ void Claser(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
{ {
*pPORTHIO &= 0xFD7F; // turn off both lasers *pPORTHIO &= 0xFD7F; // turn off both lasers
switch (Param[0]->Val->Integer) { switch (Param[0]->Val->Integer) {
case 1: case 1:
*pPORTHIO |= 0x0080; // turn on left laser *pPORTHIO |= 0x0080; // turn on left laser
break; break;
case 2: case 2:
*pPORTHIO |= 0x0200; // turn on right laser *pPORTHIO |= 0x0200; // turn on right laser
break; break;
case 3: case 3:
*pPORTHIO |= 0x0280; // turn on both lasers *pPORTHIO |= 0x0280; // turn on both lasers
break; break;
} }
} }
@ -380,11 +380,11 @@ void Cbattery(struct ParseState *Parser, struct Value *ReturnValue, struct Value
ReturnValue->Val->Integer = 1; // battery voltage okay ReturnValue->Val->Integer = 1; // battery voltage okay
} }
void Cvcolor(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin - void Cvcolor(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin -
// vcolor (color, ymin, ymax, umin, umax, vmin, vmax); // vcolor (color, ymin, ymax, umin, umax, vmin, vmax);
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
ymin[ix] = Param[1]->Val->Integer; ymin[ix] = Param[1]->Val->Integer;
ymax[ix] = Param[2]->Val->Integer; ymax[ix] = Param[2]->Val->Integer;
@ -394,7 +394,7 @@ void Cvcolor(struct ParseState *Parser, struct Value *ReturnValue, struct Value
vmax[ix] = Param[6]->Val->Integer; vmax[ix] = Param[6]->Val->Integer;
} }
void Cvcam(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set camera functions - void Cvcam(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set camera functions -
// enable/disable AGC(4) / AWB(2) / AEC(1) camera controls // enable/disable AGC(4) / AWB(2) / AEC(1) camera controls
// vcam(7) = AGC+AWB+AEC on vcam(0) = AGC+AWB+AEC off // vcam(7) = AGC+AWB+AEC on vcam(0) = AGC+AWB+AEC off
{ {
@ -407,11 +407,11 @@ void Cvcam(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
i2cwrite(0x21, (unsigned char *)i2c_data, 1, SCCB_ON); // OV7725 i2cwrite(0x21, (unsigned char *)i2c_data, 1, SCCB_ON); // OV7725
} }
void Cvfind(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin - void Cvfind(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // set color bin -
// vfind (color, x1, x2, y1, y2); // vfind (color, x1, x2, y1, y2);
{ {
int ix, x1, x2, y1, y2; int ix, x1, x2, y1, y2;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
x1 = Param[1]->Val->Integer; x1 = Param[1]->Val->Integer;
x2 = Param[2]->Val->Integer; x2 = Param[2]->Val->Integer;
@ -453,13 +453,13 @@ void Cvscan(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
if ((col < 1) || (col > 9)) if ((col < 1) || (col > 9))
ProgramFail(NULL, "vscan(): number of columns must be between 1 and 9"); ProgramFail(NULL, "vscan(): number of columns must be between 1 and 9");
thresh = Param[1]->Val->Integer; thresh = Param[1]->Val->Integer;
if ((thresh < 0) || (thresh > 9999)) if ((thresh < 0) || (thresh > 9999))
ProgramFail(NULL, "vscan(): threshold must be between 0 and 9999"); ProgramFail(NULL, "vscan(): threshold must be between 0 and 9999");
ix = vscan((unsigned char *)SPI_BUFFER1, (unsigned char *)FRAME_BUF, thresh, (unsigned int)col, (unsigned int *)&ScanVect[0]); ix = vscan((unsigned char *)SPI_BUFFER1, (unsigned char *)FRAME_BUF, thresh, (unsigned int)col, (unsigned int *)&ScanVect[0]);
ReturnValue->Val->Integer = ix; ReturnValue->Val->Integer = ix;
} }
void Cvmean(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Cvmean(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
vmean((unsigned char *)FRAME_BUF); vmean((unsigned char *)FRAME_BUF);
Iy1 = mean[0]; Iy1 = mean[0];
@ -477,7 +477,7 @@ void Cvblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
iblob = Param[1]->Val->Integer; iblob = Param[1]->Val->Integer;
if (iblob > MAX_BLOBS) if (iblob > MAX_BLOBS)
ProgramFail(NULL, "blob(): invalid blob index"); ProgramFail(NULL, "blob(): invalid blob index");
numblob = vblob((unsigned char *)FRAME_BUF, (unsigned char *)FRAME_BUF3, ix); numblob = vblob((unsigned char *)FRAME_BUF, (unsigned char *)FRAME_BUF3, ix);
if ((blobcnt[iblob] == 0) || (numblob == -1)) { if ((blobcnt[iblob] == 0) || (numblob == -1)) {
@ -495,14 +495,14 @@ void Cvblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
void Cvjpeg (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cvjpeg (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
unsigned int image_size, qual; unsigned int image_size, qual;
unsigned char *output_start, *output_end; unsigned char *output_start, *output_end;
qual = Param[0]->Val->Integer; qual = Param[0]->Val->Integer;
if ((qual < 1) || (qual > 8)) if ((qual < 1) || (qual > 8))
ProgramFail(NULL, "vjpeg(): quality parameter out of range"); ProgramFail(NULL, "vjpeg(): quality parameter out of range");
output_start = (unsigned char *)JPEG_BUF; output_start = (unsigned char *)JPEG_BUF;
output_end = encode_image((unsigned char *)FRAME_BUF, output_start, qual, output_end = encode_image((unsigned char *)FRAME_BUF, output_start, qual,
FOUR_TWO_TWO, imgWidth, imgHeight); FOUR_TWO_TWO, imgWidth, imgHeight);
image_size = (unsigned int)(output_end - output_start); image_size = (unsigned int)(output_end - output_start);
ReturnValue->Val->Integer = image_size; ReturnValue->Val->Integer = image_size;
@ -511,15 +511,15 @@ void Cvjpeg (struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cvsend (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cvsend (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
unsigned int ix, image_size; unsigned int ix, image_size;
unsigned char *cp; unsigned char *cp;
image_size = Param[0]->Val->Integer; image_size = Param[0]->Val->Integer;
if ((image_size < 0) || (image_size > 200000)) if ((image_size < 0) || (image_size > 200000))
ProgramFail(NULL, "vsend(): image size out of range"); ProgramFail(NULL, "vsend(): image size out of range");
led1_on(); led1_on();
cp = (unsigned char *)JPEG_BUF; cp = (unsigned char *)JPEG_BUF;
for (ix=0; ix<image_size; ix++) for (ix=0; ix<image_size; ix++)
putchar(*cp++); putchar(*cp++);
led0_on(); led0_on();
@ -529,7 +529,7 @@ void Ccompass(struct ParseState *Parser, struct Value *ReturnValue, struct Value
{ {
unsigned char i2c_data[2]; unsigned char i2c_data[2];
unsigned int ix; unsigned int ix;
i2c_data[0] = 0x41; // read compass twice to clear last reading i2c_data[0] = 0x41; // read compass twice to clear last reading
i2cread(0x22, (unsigned char *)i2c_data, 2, SCCB_ON); i2cread(0x22, (unsigned char *)i2c_data, 2, SCCB_ON);
delayMS(20); delayMS(20);
@ -543,7 +543,7 @@ void Ccompassx(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
{ {
short x, y, z; short x, y, z;
int ix; int ix;
ix = (int)read_compass3x(&x, &y, &z); ix = (int)read_compass3x(&x, &y, &z);
Cxmin = cxmin; Cxmin = cxmin;
Cxmax = cxmax; Cxmax = cxmax;
@ -565,7 +565,7 @@ void Ccompassxcal(struct ParseState *Parser, struct Value *ReturnValue, struct V
void Ctilt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass void Ctilt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass
{ {
unsigned int ix; unsigned int ix;
ix = (unsigned int)Param[0]->Val->Integer; ix = (unsigned int)Param[0]->Val->Integer;
if ((ix<1) || (ix>3)) if ((ix<1) || (ix>3))
ProgramFail(NULL, "tilt(): invalid channel"); ProgramFail(NULL, "tilt(): invalid channel");
@ -575,7 +575,7 @@ void Ctilt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
void Canalog(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass void Canalog(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass
{ {
unsigned int ix, channel; unsigned int ix, channel;
ix = (unsigned char)Param[0]->Val->Integer; ix = (unsigned char)Param[0]->Val->Integer;
if ((ix<1) || (ix>28)) if ((ix<1) || (ix>28))
ProgramFail(NULL, "analog(): invalid channel"); ProgramFail(NULL, "analog(): invalid channel");
@ -584,7 +584,7 @@ void Canalog(struct ParseState *Parser, struct Value *ReturnValue, struct Value
ProgramFail(NULL, "analog(): invalid channel"); ProgramFail(NULL, "analog(): invalid channel");
ReturnValue->Val->Integer = analog(ix); ReturnValue->Val->Integer = analog(ix);
} }
/* read analog channel 0-7 from SRV-4WD ( /* read analog channel 0-7 from SRV-4WD (
channel 0 = battery level channel 0 = battery level
@ -598,9 +598,9 @@ void Canalog(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Canalogx(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass void Canalogx(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // return reading from HMC6352 I2C compass
{ {
int ix; int ix;
ix = (unsigned char)Param[0]->Val->Integer; ix = (unsigned char)Param[0]->Val->Integer;
if ((ix<0) || (ix>7)) if ((ix<0) || (ix>7))
ProgramFail(NULL, "analogx(): invalid channel"); ProgramFail(NULL, "analogx(): invalid channel");
ReturnValue->Val->Integer = analog_4wd(ix); ReturnValue->Val->Integer = analog_4wd(ix);
} }
@ -619,21 +619,21 @@ void Cgps(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Creadi2c(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax val = readi2c(device, register); void Creadi2c(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax val = readi2c(device, register);
{ {
unsigned char i2c_device, i2c_data[2]; unsigned char i2c_device, i2c_data[2];
i2c_device = (unsigned char)Param[0]->Val->Integer; i2c_device = (unsigned char)Param[0]->Val->Integer;
i2c_data[0] = (unsigned char)Param[1]->Val->Integer; i2c_data[0] = (unsigned char)Param[1]->Val->Integer;
i2cread(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF); i2cread(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF);
ReturnValue->Val->Integer = ((int)i2c_data[0] & 0x000000FF); ReturnValue->Val->Integer = ((int)i2c_data[0] & 0x000000FF);
} }
void Creadi2c2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax two_byte_val = readi2c(device, register); void Creadi2c2(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // syntax two_byte_val = readi2c(device, register);
{ {
unsigned char i2c_device, i2c_data[2]; unsigned char i2c_device, i2c_data[2];
i2c_device = (unsigned char)Param[0]->Val->Integer; i2c_device = (unsigned char)Param[0]->Val->Integer;
i2c_data[0] = (unsigned char)Param[1]->Val->Integer; i2c_data[0] = (unsigned char)Param[1]->Val->Integer;
i2cread(i2c_device, (unsigned char *)i2c_data, 2, SCCB_OFF); i2cread(i2c_device, (unsigned char *)i2c_data, 2, SCCB_OFF);
ReturnValue->Val->Integer = (((unsigned int)i2c_data[0] << 8) + i2c_data[1]); ReturnValue->Val->Integer = (((unsigned int)i2c_data[0] << 8) + i2c_data[1]);
} }
@ -645,14 +645,14 @@ void Cwritei2c(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
i2c_device = (unsigned char)Param[0]->Val->Integer; i2c_device = (unsigned char)Param[0]->Val->Integer;
i2c_data[0] = (unsigned char)Param[1]->Val->Integer; i2c_data[0] = (unsigned char)Param[1]->Val->Integer;
i2c_data[1] = (unsigned char)Param[2]->Val->Integer; i2c_data[1] = (unsigned char)Param[2]->Val->Integer;
i2cwrite(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF); i2cwrite(i2c_device, (unsigned char *)i2c_data, 1, SCCB_OFF);
} }
void Cabs(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // abs(int) void Cabs(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // abs(int)
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; // return absolute value of int ix = Param[0]->Val->Integer; // return absolute value of int
if (ix < 0) if (ix < 0)
ix = -ix; ix = -ix;
@ -661,7 +661,7 @@ void Cabs(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Csin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sin(angle) void Csin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sin(angle)
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; // input to function is angle in degrees ix = Param[0]->Val->Integer; // input to function is angle in degrees
ReturnValue->Val->Integer = sin(ix); ReturnValue->Val->Integer = sin(ix);
} }
@ -669,7 +669,7 @@ void Csin(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Ccos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // cos(angle) void Ccos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // cos(angle)
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; // input to function is angle in degrees ix = Param[0]->Val->Integer; // input to function is angle in degrees
ReturnValue->Val->Integer = cos(ix); ReturnValue->Val->Integer = cos(ix);
} }
@ -677,7 +677,7 @@ void Ccos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **P
void Ctan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // tan(angle) void Ctan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // tan(angle)
{ {
int ix; int ix;
ix = Param[0]->Val->Integer; // input to function is angle in degrees ix = Param[0]->Val->Integer; // input to function is angle in degrees
ReturnValue->Val->Integer = tan(ix); ReturnValue->Val->Integer = tan(ix);
} }
@ -708,7 +708,7 @@ void Catan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **
y = Param[0]->Val->Integer; y = Param[0]->Val->Integer;
x = Param[1]->Val->Integer; x = Param[1]->Val->Integer;
ReturnValue->Val->Integer = atan(y, x); ReturnValue->Val->Integer = atan(y, x);
} }
void Cgps_head(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_head(lat1, lon1, lat2, lon2) void Cgps_head(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_head(lat1, lon1, lat2, lon2)
{ {
@ -718,7 +718,7 @@ void Cgps_head(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
lat2 = Param[2]->Val->Integer; lat2 = Param[2]->Val->Integer;
lon2 = Param[3]->Val->Integer; lon2 = Param[3]->Val->Integer;
ReturnValue->Val->Integer = gps_head(lat1, lon1, lat2, lon2); ReturnValue->Val->Integer = gps_head(lat1, lon1, lat2, lon2);
} }
void Cgps_dist(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_dist(lat1, lon1, lat2, lon2) void Cgps_dist(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // gps_dist(lat1, lon1, lat2, lon2)
{ {
@ -728,18 +728,18 @@ void Cgps_dist(struct ParseState *Parser, struct Value *ReturnValue, struct Valu
lat2 = Param[2]->Val->Integer; lat2 = Param[2]->Val->Integer;
lon2 = Param[3]->Val->Integer; lon2 = Param[3]->Val->Integer;
ReturnValue->Val->Integer = gps_dist(lat1, lon1, lat2, lon2); ReturnValue->Val->Integer = gps_dist(lat1, lon1, lat2, lon2);
} }
void Csqrt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sqrt(x) void Csqrt(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // sqrt(x)
{ {
int x; int x;
x = Param[0]->Val->Integer; x = Param[0]->Val->Integer;
ReturnValue->Val->Integer = isqrt(x); ReturnValue->Val->Integer = isqrt(x);
} }
void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1; int ix, i1;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS) if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnset(): invalid index"); ProgramFail(NULL, "nnset(): invalid index");
@ -749,7 +749,7 @@ void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value *
void Cnnshow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnshow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix; int ix;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS) if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnshow(): invalid index"); ProgramFail(NULL, "nnshow(): invalid index");
@ -767,7 +767,7 @@ void Cnntrain(struct ParseState *Parser, struct Value *ReturnValue, struct Value
for (ix=0; ix<NUM_NPATTERNS; ix++) { for (ix=0; ix<NUM_NPATTERNS; ix++) {
nnset_pattern(ix); nnset_pattern(ix);
nncalculate_network(); nncalculate_network();
for (i1=0; i1<NUM_OUTPUT; i1++) for (i1=0; i1<NUM_OUTPUT; i1++)
printf(" %3d", N_OUT(i1)/10); printf(" %3d", N_OUT(i1)/10);
printf("\r\n"); printf("\r\n");
} }
@ -776,7 +776,7 @@ void Cnntrain(struct ParseState *Parser, struct Value *ReturnValue, struct Value
void Cnntest(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnntest(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1, i2, max; int ix, i1, i2, max;
unsigned char ch; unsigned char ch;
ix = 0; ix = 0;
for (i1=0; i1<8; i1++) { for (i1=0; i1<8; i1++) {
ch = (unsigned char)Param[i1]->Val->Integer; ch = (unsigned char)Param[i1]->Val->Integer;
@ -800,9 +800,9 @@ void Cnntest(struct ParseState *Parser, struct Value *ReturnValue, struct Value
ReturnValue->Val->Integer = ix; ReturnValue->Val->Integer = ix;
} }
void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, i1, max; int ix, i1, max;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > MAX_BLOBS) if (ix > MAX_BLOBS)
ProgramFail(NULL, "nnmatchblob(): invalid blob index"); ProgramFail(NULL, "nnmatchblob(): invalid blob index");
@ -812,7 +812,7 @@ void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct V
square the aspect ratio of x1, x2, y1, y2 square the aspect ratio of x1, x2, y1, y2
then subsample blob pixels to populate N_IN(0:63) with 0:1024 values then subsample blob pixels to populate N_IN(0:63) with 0:1024 values
then nncalculate_network() and display the N_OUT() results */ then nncalculate_network() and display the N_OUT() results */
nnscale8x8((unsigned char *)FRAME_BUF3, blobix[ix], blobx1[ix], blobx2[ix], nnscale8x8((unsigned char *)FRAME_BUF3, blobix[ix], blobx1[ix], blobx2[ix],
bloby1[ix], bloby2[ix], imgWidth, imgHeight); bloby1[ix], bloby2[ix], imgWidth, imgHeight);
nncalculate_network(); nncalculate_network();
ix = 0; ix = 0;
@ -829,13 +829,13 @@ void Cnnmatchblob(struct ParseState *Parser, struct Value *ReturnValue, struct V
void Cnnlearnblob (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cnnlearnblob (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix; int ix;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
if (ix > NUM_NPATTERNS) if (ix > NUM_NPATTERNS)
ProgramFail(NULL, "nnlearnblob(): invalid index"); ProgramFail(NULL, "nnlearnblob(): invalid index");
if (!blobcnt[0]) if (!blobcnt[0])
ProgramFail(NULL, "nnlearnblob(): no blob to grab"); ProgramFail(NULL, "nnlearnblob(): no blob to grab");
nnscale8x8((unsigned char *)FRAME_BUF3, blobix[0], blobx1[0], blobx2[0], nnscale8x8((unsigned char *)FRAME_BUF3, blobix[0], blobx1[0], blobx2[0],
bloby1[0], bloby2[0], imgWidth, imgHeight); bloby1[0], bloby2[0], imgWidth, imgHeight);
nnpack8x8(ix); nnpack8x8(ix);
nndisplay(ix); nndisplay(ix);
@ -844,7 +844,7 @@ void Cnnlearnblob (struct ParseState *Parser, struct Value *ReturnValue, struct
void Cautorun (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { void Cautorun (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) {
int ix, t0; int ix, t0;
unsigned char ch; unsigned char ch;
ix = Param[0]->Val->Integer; ix = Param[0]->Val->Integer;
t0 = readRTC(); t0 = readRTC();
while (readRTC() < (t0 + ix*1000)) { // watch for ESC in 'ix' seconds while (readRTC() < (t0 + ix*1000)) { // watch for ESC in 'ix' seconds
@ -869,76 +869,76 @@ void Cerrormsg (struct ParseState *Parser, struct Value *ReturnValue, struct Val
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction PlatformLibrary[] = struct LibraryFunction PlatformLibrary[] =
{ {
{ Csignal, "int signal();" }, {Csignal, "int signal();"},
{ Csignal1, "int signal1();" }, {Csignal1, "int signal1();"},
{ Cinput, "int input();" }, {Cinput, "int input();"},
{ Cinput1, "int input1();" }, {Cinput1, "int input1();"},
{ Cinit_uart1, "void init_uart1(int);" }, {Cinit_uart1, "void init_uart1(int);"},
{ Cread_int, "int read_int();" }, {Cread_int, "int read_int();"},
{ Cread_str, "int read_str(char *);" }, {Cread_str, "int read_str(char *);"},
{ Coutput, "void output(int);" }, {Coutput, "void output(int);"},
{ Coutput1, "void output1(int);" }, {Coutput1, "void output1(int);"},
{ Cdelay, "void delay(int);" }, {Cdelay, "void delay(int);"},
{ Crand, "int rand(int);" }, {Crand, "int rand(int);"},
{ Ctime, "int time();" }, {Ctime, "int time();"},
{ Ciodir, "void iodir(int);" }, {Ciodir, "void iodir(int);"},
{ Cioread, "int ioread();" }, {Cioread, "int ioread();"},
{ Ciowrite, "void iowrite(int);" }, {Ciowrite, "void iowrite(int);"},
{ Cpeek, "int peek(int, int);" }, {Cpeek, "int peek(int, int);"},
{ Cpoke, "void poke(int, int, int);" }, {Cpoke, "void poke(int, int, int);"},
{ Cmotors, "void motors(int, int);" }, {Cmotors, "void motors(int, int);"},
{ Cmotors2, "void motors2(int, int);" }, {Cmotors2, "void motors2(int, int);"},
{ Cmotorx, "void motorx(int, int);" }, {Cmotorx, "void motorx(int, int);"},
{ Cservos, "void servos(int, int);" }, {Cservos, "void servos(int, int);"},
{ Cservos2, "void servos2(int, int);" }, {Cservos2, "void servos2(int, int);"},
{ Cencoders, "void encoders();" }, {Cencoders, "void encoders();"},
{ Cencoderx, "int encoderx(int);" }, {Cencoderx, "int encoderx(int);"},
{ Claser, "void laser(int);" }, {Claser, "void laser(int);"},
{ Csonar, "int sonar(int);" }, {Csonar, "int sonar(int);"},
{ Crange, "int range();" }, {Crange, "int range();"},
{ Cbattery, "int battery();" }, {Cbattery, "int battery();"},
{ Cvcolor, "void vcolor(int, int, int, int, int, int, int);" }, {Cvcolor, "void vcolor(int, int, int, int, int, int, int);"},
{ Cvfind, "int vfind(int, int, int, int, int);" }, {Cvfind, "int vfind(int, int, int, int, int);"},
{ Cvcam, "void vcam(int);" }, {Cvcam, "void vcam(int);"},
{ Cvcap, "void vcap();" }, {Cvcap, "void vcap();"},
{ Cvrcap, "void vrcap();" }, {Cvrcap, "void vrcap();"},
{ Cvdiff, "void vdiff(int);" }, {Cvdiff, "void vdiff(int);"},
{ Cvpix, "void vpix(int, int);" }, {Cvpix, "void vpix(int, int);"},
{ Cvscan, "int vscan(int, int);" }, {Cvscan, "int vscan(int, int);"},
{ Cvmean, "void vmean();" }, {Cvmean, "void vmean();"},
{ Cvblob, "int vblob(int, int);" }, {Cvblob, "int vblob(int, int);"},
{ Cvjpeg, "int vjpeg(int);" }, {Cvjpeg, "int vjpeg(int);"},
{ Cvsend, "void vsend(int);" }, {Cvsend, "void vsend(int);"},
{ Ccompass, "int compass();" }, {Ccompass, "int compass();"},
{ Ccompassx, "int compassx();" }, {Ccompassx, "int compassx();"},
{ Ccompassxcal, "void compassxcal(int, int, int, int, int);" }, {Ccompassxcal, "void compassxcal(int, int, int, int, int);"},
{ Canalog, "int analog(int);" }, {Canalog, "int analog(int);"},
{ Canalogx, "int analogx(int);" }, {Canalogx, "int analogx(int);"},
{ Ctilt, "int tilt(int);" }, {Ctilt, "int tilt(int);"},
{ Cgps, "void gps();" }, {Cgps, "void gps();"},
{ Creadi2c, "int readi2c(int, int);" }, {Creadi2c, "int readi2c(int, int);"},
{ Creadi2c2, "int readi2c2(int, int);" }, {Creadi2c2, "int readi2c2(int, int);"},
{ Cwritei2c, "void writei2c(int, int, int);" }, {Cwritei2c, "void writei2c(int, int, int);"},
{ Cabs, "int abs(int);" }, {Cabs, "int abs(int);"},
{ Csin, "int sin(int);" }, {Csin, "int sin(int);"},
{ Ccos, "int cos(int);" }, {Ccos, "int cos(int);"},
{ Ctan, "int tan(int);" }, {Ctan, "int tan(int);"},
{ Casin, "int asin(int, int);" }, {Casin, "int asin(int, int);"},
{ Cacos, "int acos(int, int);" }, {Cacos, "int acos(int, int);"},
{ Catan, "int atan(int, int);" }, {Catan, "int atan(int, int);"},
{ Cgps_head, "int gps_head(int, int, int, int);" }, {Cgps_head, "int gps_head(int, int, int, int);"},
{ Cgps_dist, "int gps_dist(int, int, int, int);" }, {Cgps_dist, "int gps_dist(int, int, int, int);"},
{ Csqrt, "int sqrt(int);" }, {Csqrt, "int sqrt(int);"},
{ Cnnshow, "void nnshow(int);" }, {Cnnshow, "void nnshow(int);"},
{ Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);" }, {Cnnset, "void nnset(int, int, int, int, int, int, int, int, int);"},
{ Cnninit, "void nninit();" }, {Cnninit, "void nninit();"},
{ Cnntrain, "void nntrain();" }, {Cnntrain, "void nntrain();"},
{ Cnntest, "int nntest(int, int, int, int, int, int, int, int);" }, {Cnntest, "int nntest(int, int, int, int, int, int, int, int);"},
{ Cnnmatchblob, "int nnmatchblob(int);" }, {Cnnmatchblob, "int nnmatchblob(int);"},
{ Cnnlearnblob, "void nnlearnblob(int);" }, {Cnnlearnblob, "void nnlearnblob(int);"},
{ Cautorun, "void autorun(int);" }, {Cautorun, "void autorun(int);"},
{ Clineno, "int lineno();" }, {Clineno, "int lineno();"},
{ Cerrormsg, "void errormsg(char *);" }, {Cerrormsg, "void errormsg(char *);"},
{ NULL, NULL } {NULL, NULL}
}; };

View file

@ -1,16 +1,16 @@
#include "../interpreter.h" #include "../interpreter.h"
void UnixSetupFunc() void UnixSetupFunc()
{ {
} }
void Ctest (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Ctest (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
printf("test(%d)\n", Param[0]->Val->Integer); printf("test(%d)\n", Param[0]->Val->Integer);
Param[0]->Val->Integer = 1234; Param[0]->Val->Integer = 1234;
} }
void Clineno (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) void Clineno (struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs)
{ {
ReturnValue->Val->Integer = Parser->Line; ReturnValue->Val->Integer = Parser->Line;
} }
@ -18,9 +18,9 @@ void Clineno (struct ParseState *Parser, struct Value *ReturnValue, struct Value
/* list of all library functions and their prototypes */ /* list of all library functions and their prototypes */
struct LibraryFunction UnixFunctions[] = struct LibraryFunction UnixFunctions[] =
{ {
{ Ctest, "void test(int);" }, {Ctest, "void test(int);"},
{ Clineno, "int lineno();" }, {Clineno, "int lineno();"},
{ NULL, NULL } {NULL, NULL}
}; };
void PlatformLibraryInit(Picoc *pc) void PlatformLibraryInit(Picoc *pc)

View file

@ -17,7 +17,7 @@ char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
{ {
if (Prompt != NULL) if (Prompt != NULL)
printf("%s", Prompt); printf("%s", Prompt);
fflush(stdout); fflush(stdout);
return fgets(Buf, MaxLen, stdin); return fgets(Buf, MaxLen, stdin);
} }
@ -43,34 +43,32 @@ char *PlatformReadFile(Picoc *pc, const char *FileName)
FILE *InFile; FILE *InFile;
int BytesRead; int BytesRead;
char *p; char *p;
if (stat(FileName, &FileInfo)) if (stat(FileName, &FileInfo))
ProgramFailNoParser(pc, "can't read file %s\n", FileName); ProgramFailNoParser(pc, "can't read file %s\n", FileName);
ReadText = malloc(FileInfo.st_size + 1); ReadText = malloc(FileInfo.st_size + 1);
if (ReadText == NULL) if (ReadText == NULL)
ProgramFailNoParser(pc, "out of memory\n"); ProgramFailNoParser(pc, "out of memory\n");
InFile = fopen(FileName, "r"); InFile = fopen(FileName, "r");
if (InFile == NULL) if (InFile == NULL)
ProgramFailNoParser(pc, "can't read file %s\n", FileName); ProgramFailNoParser(pc, "can't read file %s\n", FileName);
BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile); BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile);
if (BytesRead == 0) if (BytesRead == 0)
ProgramFailNoParser(pc, "can't read file %s\n", FileName); ProgramFailNoParser(pc, "can't read file %s\n", FileName);
ReadText[BytesRead] = '\0'; ReadText[BytesRead] = '\0';
fclose(InFile); fclose(InFile);
if ((ReadText[0] == '#') && (ReadText[1] == '!')) if ((ReadText[0] == '#') && (ReadText[1] == '!')) {
{ for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p) {
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p)
{
*p = ' '; *p = ' ';
} }
} }
return ReadText; return ReadText;
} }
/* read and scan a file for definitions */ /* read and scan a file for definitions */

View file

@ -14,20 +14,19 @@ char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
{ {
int ix; int ix;
char ch, *cp; char ch, *cp;
printf(Prompt); printf(Prompt);
ix = 0; ix = 0;
cp = 0; cp = 0;
// If the first character is \n or \r, eat it // If the first character is \n or \r, eat it
ch = getch(); ch = getch();
if (ch == '\n' || ch == '\r') if (ch == '\n' || ch == '\r') {
{
// And get the next character // And get the next character
ch = getch(); ch = getch();
} }
while (ix++ < MaxLen) { while (ix++ < MaxLen) {
if (ch == 0x1B || ch == 0x03) { // ESC character or ctrl-c (to avoid problem with TeraTerm) - exit if (ch == 0x1B || ch == 0x03) { // ESC character or ctrl-c (to avoid problem with TeraTerm) - exit
@ -51,7 +50,7 @@ void PlatformPutc(unsigned char OutCh, union OutputStreamInfo *Stream)
{ {
if (OutCh == '\n') if (OutCh == '\n')
putchar('\r'); putchar('\r');
putchar(OutCh); putchar(OutCh);
} }

View file

@ -39,20 +39,19 @@ void PlatformCleanup(Picoc *pc)
char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt) char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
{ {
#ifdef USE_READLINE #ifdef USE_READLINE
if (Prompt != NULL) if (Prompt != NULL) {
{
/* use GNU readline to read the line */ /* use GNU readline to read the line */
char *InLine = readline(Prompt); char *InLine = readline(Prompt);
if (InLine == NULL) if (InLine == NULL)
return NULL; return NULL;
Buf[MaxLen-1] = '\0'; Buf[MaxLen-1] = '\0';
strncpy(Buf, InLine, MaxLen-2); strncpy(Buf, InLine, MaxLen-2);
strncat(Buf, "\n", MaxLen-2); strncat(Buf, "\n", MaxLen-2);
if (InLine[0] != '\0') if (InLine[0] != '\0')
add_history(InLine); add_history(InLine);
free(InLine); free(InLine);
return Buf; return Buf;
} }
@ -60,7 +59,7 @@ char *PlatformGetLine(char *Buf, int MaxLen, const char *Prompt)
if (Prompt != NULL) if (Prompt != NULL)
printf("%s", Prompt); printf("%s", Prompt);
fflush(stdout); fflush(stdout);
return fgets(Buf, MaxLen, stdin); return fgets(Buf, MaxLen, stdin);
} }
@ -86,34 +85,32 @@ char *PlatformReadFile(Picoc *pc, const char *FileName)
FILE *InFile; FILE *InFile;
int BytesRead; int BytesRead;
char *p; char *p;
if (stat(FileName, &FileInfo)) if (stat(FileName, &FileInfo))
ProgramFailNoParser(pc, "can't read file %s\n", FileName); ProgramFailNoParser(pc, "can't read file %s\n", FileName);
ReadText = malloc(FileInfo.st_size + 1); ReadText = malloc(FileInfo.st_size + 1);
if (ReadText == NULL) if (ReadText == NULL)
ProgramFailNoParser(pc, "out of memory\n"); ProgramFailNoParser(pc, "out of memory\n");
InFile = fopen(FileName, "r"); InFile = fopen(FileName, "r");
if (InFile == NULL) if (InFile == NULL)
ProgramFailNoParser(pc, "can't read file %s\n", FileName); ProgramFailNoParser(pc, "can't read file %s\n", FileName);
BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile); BytesRead = fread(ReadText, 1, FileInfo.st_size, InFile);
if (BytesRead == 0) if (BytesRead == 0)
ProgramFailNoParser(pc, "can't read file %s\n", FileName); ProgramFailNoParser(pc, "can't read file %s\n", FileName);
ReadText[BytesRead] = '\0'; ReadText[BytesRead] = '\0';
fclose(InFile); fclose(InFile);
if ((ReadText[0] == '#') && (ReadText[1] == '!')) if ((ReadText[0] == '#') && (ReadText[1] == '!')) {
{ for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p) {
for (p = ReadText; (*p != '\r') && (*p != '\n'); ++p)
{
*p = ' '; *p = ' ';
} }
} }
return ReadText; return ReadText;
} }
/* read and scan a file for definitions */ /* read and scan a file for definitions */
@ -122,10 +119,9 @@ void PicocPlatformScanFile(Picoc *pc, const char *FileName)
char *SourceStr = PlatformReadFile(pc, FileName); char *SourceStr = PlatformReadFile(pc, FileName);
/* ignore "#!/path/to/picoc" .. by replacing the "#!" with "//" */ /* ignore "#!/path/to/picoc" .. by replacing the "#!" with "//" */
if (SourceStr != NULL && SourceStr[0] == '#' && SourceStr[1] == '!') if (SourceStr != NULL && SourceStr[0] == '#' && SourceStr[1] == '!') {
{ SourceStr[0] = '/';
SourceStr[0] = '/'; SourceStr[1] = '/';
SourceStr[1] = '/';
} }
PicocParse(pc, FileName, SourceStr, strlen(SourceStr), TRUE, FALSE, TRUE, TRUE); PicocParse(pc, FileName, SourceStr, strlen(SourceStr), TRUE, FALSE, TRUE, TRUE);