diff --git a/picoc.c b/picoc.c index fd2d73a..5095e5f 100644 --- a/picoc.c +++ b/picoc.c @@ -46,19 +46,23 @@ int picoc(char *SourceStr) int ix; Initialise(); - for (ix=0; ixVal->Integer; - hyp = Param[1]->Val->Integer; + y = Param[0]->Val->Integer; + hyp = Param[1]->Val->Integer; if (y > hyp) ProgramFail(NULL, "asin(): opposite greater than hypotenuse"); - if (y == 0) { + if (y == 0) { ReturnValue->Val->Integer = 0; - return; - } - sgn = hyp * y; - if (hyp < 0) - hyp = -hyp; - if (y < 0) - y = -y; - quot = (y * 10000) / hyp; - if (quot > 9999) - quot = 9999; - for (ix=0; ix<90; ix++) - if ((quot < cosine[ix]) && (quot >= cosine[ix+1])) - break; - if (sgn < 0) - ReturnValue->Val->Integer = -(90-ix); - else - ReturnValue->Val->Integer = 90-ix; + return; + } + sgn = hyp * y; + if (hyp < 0) + hyp = -hyp; + if (y < 0) + y = -y; + quot = (y * 10000) / hyp; + if (quot > 9999) + quot = 9999; + for (ix=0; ix<90; ix++) + if ((quot < cosine[ix]) && (quot >= cosine[ix+1])) + break; + if (sgn < 0) + ReturnValue->Val->Integer = -(90-ix); + else + ReturnValue->Val->Integer = 90-ix; } void Cacos(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // acos(x,hyp) { int x, hyp, quot, sgn, ix; - x = Param[0]->Val->Integer; - hyp = Param[1]->Val->Integer; + x = Param[0]->Val->Integer; + hyp = Param[1]->Val->Integer; if (x > hyp) ProgramFail(NULL, "acos(): adjacent greater than hypotenuse"); - if (x == 0) { - if (hyp < 0) + if (x == 0) { + if (hyp < 0) ReturnValue->Val->Integer = -90; else ReturnValue->Val->Integer = 90; - return; - } - sgn = hyp * x; - if (hyp < 0) - hyp = -hyp; - if (x < 0) - x = -x; - quot = (x * 10000) / hyp; - if (quot > 9999) - quot = 9999; - for (ix=0; ix<90; ix++) - if ((quot < cosine[ix]) && (quot >= cosine[ix+1])) - break; - if (sgn < 0) - ReturnValue->Val->Integer = -ix; - else - ReturnValue->Val->Integer = ix; + return; + } + sgn = hyp * x; + if (hyp < 0) + hyp = -hyp; + if (x < 0) + x = -x; + quot = (x * 10000) / hyp; + if (quot > 9999) + quot = 9999; + for (ix=0; ix<90; ix++) + if ((quot < cosine[ix]) && (quot >= cosine[ix+1])) + break; + if (sgn < 0) + ReturnValue->Val->Integer = -ix; + else + ReturnValue->Val->Integer = ix; } void Catan(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) // atan(y,x) { int x,y, angle, coeff_1, coeff_2, r; - y = Param[0]->Val->Integer; - x = Param[1]->Val->Integer; - if (x == 0) { - if (y >= 0) - ReturnValue->Val->Integer = 90; - else - ReturnValue->Val->Integer = -90; - return; - } - coeff_1 = 3141/4; - coeff_2 = coeff_1*3; + y = Param[0]->Val->Integer; + x = Param[1]->Val->Integer; + if (x == 0) { + if (y >= 0) + ReturnValue->Val->Integer = 90; + else + ReturnValue->Val->Integer = -90; + return; + } + coeff_1 = 3141/4; + coeff_2 = coeff_1*3; if (y < 0) y = -y; - if (x >= 0) { - r = (x - y)*1000 / (x + y); - angle = (coeff_1*1000 - coeff_1 * r); - } else { - r = (x + y)*1000 / (y - x); - angle = (coeff_2*1000 - coeff_1 * r); - } - angle = angle*57/1000000; - if (y < 0) - ReturnValue->Val->Integer = -angle; - else - ReturnValue->Val->Integer = angle; + if (x >= 0) { + r = (x - y)*1000 / (x + y); + angle = (coeff_1*1000 - coeff_1 * r); + } else { + r = (x + y)*1000 / (y - x); + angle = (coeff_2*1000 - coeff_1 * r); + } + angle = angle*57/1000000; + if (y < 0) + ReturnValue->Val->Integer = -angle; + else + ReturnValue->Val->Integer = angle; } +void Cnnset(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { + int ix, i1; + + ix = Param[0]->Val->Integer; + if (ix > NUM_NPATTERNS) + ProgramFail(NULL, "nnset(): invalid index"); + for (i1=0; i1<8; i1++) + npattern[ix*8 + i1] = (unsigned char)Param[i1+1]->Val->Integer; +} + +void Cnnshow(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { + int ix; + + ix = Param[0]->Val->Integer; + if (ix > NUM_NPATTERNS) + ProgramFail(NULL, "nnshow(): invalid index"); + nndisplay(ix); +} + +void Cnninit(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { + nninit_network(); +} + +void Cnntrain(struct ParseState *Parser, struct Value *ReturnValue, struct Value **Param, int NumArgs) { + int ix, i1; + + nntrain_network(10000); + for (ix=0; ixVal->Integer; + for (i2=0; i2<8; i2++) { + if (ch & nmask[i2]) + N_IN(ix++) = 1024; + else + N_IN(ix++) = 0; + } + } + nncalculate_network(); + for (i1=0; i1Val->Integer; + if (ix > MAX_BLOBS) + ProgramFail(NULL, "nnmatchblob(): invalid blob index"); + if (!blobcnt[ix]) + ProgramFail(NULL, "nnmatchblob(): not a valid blob"); + /* use data still in blob_buf[] (FRAME_BUF3) + square the aspect ratio of x1, x2, y1, y2 + then subsample blob pixels to populate N_IN(0:63) with 0:1024 values + then nncalculate_network() and display the N_OUT() results */ + nnscale8x8((unsigned char *)FRAME_BUF3, blobix[ix], blobx1[ix], blobx2[ix], + bloby1[ix], bloby2[ix], imgWidth, imgHeight); + nncalculate_network(); + for (i1=0; i1Val->Integer; + if (ix > NUM_NPATTERNS) + ProgramFail(NULL, "nnlearnblob(): invalid index"); + if (!blobcnt[0]) + ProgramFail(NULL, "nnlearnblob(): no blob to grab"); + nnscale8x8((unsigned char *)FRAME_BUF3, blobix[0], blobx1[0], blobx2[0], + bloby1[0], bloby2[0], imgWidth, imgHeight); + nnpack8x8(ix); + nndisplay(ix); +} + /* list of all library functions and their prototypes */ struct LibraryFunction PlatformLibrary[] = { @@ -458,7 +548,7 @@ struct LibraryFunction PlatformLibrary[] = { Cvpix, "void vpix(int, int)" }, { Cvmean, "void vmean()" }, { Cvblob, "int vblob(int, int)" }, - { Ccompass, "int compass(int)" }, + { Ccompass, "int compass()" }, { Creadi2c, "int readi2c(int, int)" }, { Creadi2c2, "int readi2c2(int, int)" }, { Cwritei2c, "void writei2c(int, int, int)" }, @@ -468,6 +558,13 @@ struct LibraryFunction PlatformLibrary[] = { Casin, "int asin(int, int)" }, { Cacos, "int acos(int, int)" }, { Catan, "int atan(int, int)" }, + { Cnnshow, "void nnshow(int)" }, + { Cnnset, "void nnset(int, int, int, int, int, int, int, int, int)" }, + { Cnninit, "void nninit()" }, + { Cnntrain, "void nntrain()" }, + { Cnntest, "void nntest(int, int, int, int, int, int, int, int)" }, + { Cnnmatchblob, "void nnmatchblob(int)" }, + { Cnnlearnblob, "void nnlearnblob(int)" }, { NULL, NULL } }; #endif diff --git a/platform_support.c b/platform_support.c index 721832e..745cbe6 100644 --- a/platform_support.c +++ b/platform_support.c @@ -68,6 +68,25 @@ void PlatformExit() /* get a line of interactive input */ char *PlatformGetLine(char *Buf, int MaxLen) { + int ix; + char ch, *cp; + + ix = 0; + cp = Buf; + while (ix++ < MaxLen) { + ch = getch(); + if (ch == 0x1B) { // ESC character - exit + printf("leaving picoC\n\r"); + return NULL; + } + if (ch == '\n') { + *cp++ = '\n'; // if newline, send newline character followed by null + *cp = 0; + return Buf; + } + *cp++ = ch; + ix++; + } return NULL; }