void SG(int samples){ PlayTone(300,10); //De quantos em quantos valores ele ira ler if(samples<=100){ SG_jump=1; }else{ SG_jump=2; } //Configs: Jump, yScale, EXIT eraseDisplay(); SG_ySpace=0; SG_yScale=2; SG_update=0; SG_exit=0; SG_pos=1; SG_xSpace=1; while(SG_exit==0){ SG_Button(); if(SG_update==0){ eraseDisplay(); for(int i=1;i<100 && i<samples/SG_jump;i++){ nxtDrawRect(i+SG_xSpace, SG_ySpace+AUlt[(i-1)*SG_jump]/SG_yScale,i+1+SG_xSpace,SG_ySpace+AUlt[i*SG_jump]/SG_yScale); } SG_Config(0); SG_update=1; } } }
//Retrieve data from the sound sensor void i2c_read_registers_text(ubyte register_2_read, int message_size, int return_size) { memset(I2Creply, 0, sizeof(I2Creply)); message_size = message_size+3; I2Cmessage[0] = message_size; // Messsage Size I2Cmessage[1] = ARDUINO_ADDRESS; I2Cmessage[2] = register_2_read; // Register sendI2CMsg(S1, &I2Cmessage[0], return_size); wait1Msec(20); readI2CReply(ARDUINO_PORT, &I2Creply[0], return_size); int i = 0,top=0; //Pitch if(cmd==1) writeDebugStreamLine("%d", (int)I2Creply[0]+(int)I2Creply[1]*256); //Sound Power Level else if(cmd==2) //For detecting clap //if((int)I2Creply>85) writeDebugStreamLine("%i", (int)I2Creply[0]); //FFT else if(cmd==3) { for(int x = 0; x <return_size; x++) { writeDebugStream("%i", I2Creply[x]); writeDebugStream(" "); } writeDebugStreamLine(" "); //Graphic Equalizer nxtEraseRect(6,62,99,6); nxtDrawLine(5,2,5,61); nxtDrawLine(2,5,95,5); for(int j=0;j<return_size;j++) { top=I2Creply[j]+5; if(top>61) top=61; nxtDrawRect(5+6*j,top,5+6*(j+1),5); } } }
task updateHUD () { int x = 0; int y = 0; while (true) { nxtEraseRect(4,50, 44,10); nxtDisplayTextLine(2, " H: %3d", angleI/100); nxtDisplayTextLine(3, " X: %3d", x_accel/100); nxtDisplayTextLine(4, " Y: %3d", y_accel/100); nxtDisplayTextLine(5, " Z: %3d", z_accel/100); nxtDrawCircle(84, 50, 4); nxtDrawCircle(4, 50, 40); x = (cosDegrees(-1 * (angleI/100 - 90)) * 20) + 24; y = (sinDegrees(-1 * (angleI/100 - 90)) * 20) + 30; nxtDrawLine(24, 30, x, y); nxtEraseRect(0,0, 99, 8); nxtDrawRect(0,0, 99, 8); nxtFillRect(50,0, (float)(rotI / 150)/100.0 *50 + 50, 8); wait1Msec(100); } }
/* Main task */ task main () { blob_array _blobs; memset(_blobs, 0, sizeof(blob_array)); // combine all colliding blobs into one bool _condensed = true; //blob_array _blobs; int _l, _t, _r, _b; int _x, _y; int _nblobs; eraseDisplay(); // Initialise the camera NXTCAMinit(cam); while(true) { eraseDisplay(); // Fetch all the blobs, have the driver combine all // the colliding blobs. _nblobs = NXTCAMgetBlobs(cam, _blobs, _condensed); for (int i = 0; i < _nblobs; i++) { // Draw the scaled blobs _l = xscale(_blobs[i].x1); _t = yscale(_blobs[i].y1); _r = xscale(_blobs[i].x2); _b = yscale(_blobs[i].y2); nxtDrawRect(_l, _t, _r, _b); } NXTCAMgetAverageCenter(_blobs, 3, 0, _x, _y); _x = xscale(_x); _y = yscale(_y); nxtEraseRect(_x-4, _y-4, _x+4, _y+4); nxtDrawLine(_x, _y+2, _x, _y-2); nxtDrawLine(_x+2, _y, _x-2, _y); nxtDisplayTextLine(1, "%d", _nblobs); wait1Msec(100); } }
task main () { bool selected_50hz = true; nxtDisplayCenteredTextLine(0, "HiTechnic"); nxtDisplayCenteredBigTextLine(1, "Color V2"); nxtDisplayCenteredTextLine(4, "Config operating"); nxtDisplayCenteredTextLine(5, "frequency to"); nxtDisplayCenteredTextLine(6, "50 or 60 Hz"); wait1Msec(2000); eraseDisplay(); nxtDisplayCenteredTextLine(0, "Use arrow keys"); nxtDisplayCenteredTextLine(1, "to select a"); nxtDisplayCenteredTextLine(2, "frequency"); nxtDisplayCenteredBigTextLine(4, "50 60"); nxtDisplayCenteredTextLine(6, "[enter] to set"); nxtDisplayCenteredTextLine(7, "[exit] to cancel"); nxtDrawRect(19, 34, 44, 16); while (true) { // Do nothing while no buttons are pressed while (nNxtButtonPressed == kNoButton) { wait1Msec(1); } switch (nNxtButtonPressed) { // if the left button is pressed, set the sensor for 50Hz case kLeftButton: if (selected_50hz) { PlaySound(soundBlip); while(bSoundActive) {wait1Msec(1);} } else { selected_50hz = true; nxtEraseRect(55, 34, 80, 16); nxtDisplayCenteredBigTextLine(4, "50 60"); nxtDrawRect(19, 34, 44, 16); } break; // if the right button is pressed, set the sensor for 60Hz case kRightButton: if (!selected_50hz) { PlaySound(soundBlip); while(bSoundActive) {wait1Msec(1);} } else { selected_50hz = false; nxtEraseRect(19, 34, 44, 16); nxtDisplayCenteredBigTextLine(4, "50 60"); nxtDrawRect(55, 34, 80, 16); } break; // Make the setting permanent by saving it to the sensor case kEnterButton: eraseDisplay(); nxtDisplayCenteredTextLine(2, "The Sensor is"); nxtDisplayCenteredTextLine(3, "configured for"); if (selected_50hz) { _HTCSsendCommand(HTCS2, 0x35); nxtDisplayCenteredTextLine(4, "50 Hz operating"); } else { _HTCSsendCommand(HTCS2, 0x36); nxtDisplayCenteredTextLine(4, "60 Hz operating"); } nxtDisplayCenteredTextLine(5, "frequency"); for (int i = 5; i > 0; i--) { nxtDisplayCenteredTextLine(7, "Exiting in %d sec", i); wait1Msec(1000); } StopAllTasks(); break; } // Debounce the button while (nNxtButtonPressed != kNoButton) { wait1Msec(1); } } }
void measureTime(const TInstructions nInstructionType) { TSensorTypes nSensorType = testSensorType; int i; int j; int k; #if defined(useLongs) long l1; long l2; #endif #if defined(useFloats) float idleLoopTime; float loopTime; float elapsed; #else int idleLoopTime; int loopTime; int elapsed; #endif float fTemp; short iArray[10]; long lArray[10]; float fArray[10]; const int kInitialValues = 511; // an arbitrary value testType = nInstructionType; wait1Msec(200); // Some variable initialization. //i = random(10); i = 0; j = kInitialValues; k = kInitialValues; #if defined(useLongs) l1 = kInitialValues * 256; l2 = kInitialValues; #endif #if defined(useFloats) fTemp = 0.999999999; #endif idleLoopTime = nElapsedTime[typeIdleLoop]; clearTimer(executionTimer); for (index = 1; index <= kNumberOfLoops; ++index) { // // to measure a different instruction type, simply change the // definition of the following constant. // // // Since 'nInstructionType' is a constant, code optimizer will eliminate // the 'switch' instruction and the 'dead code' for the unreachable // cases. // int nTemp; switch (nInstructionType) { case typeIdleLoop: { nTemp = typeIdleLoop; // So that branch does not get optimized out break; } case typeAlive: { alive(); alive(); alive(); alive(); alive(); alive(); alive(); alive(); alive(); alive(); nTemp = typeAlive; break; } case typeIntegerAssignConstant: { iArray[00] = 250; iArray[01] = 250; iArray[02] = 250; iArray[03] = 250; iArray[04] = 250; iArray[05] = 250; iArray[06] = 250; iArray[07] = 250; iArray[08] = 250; iArray[09] = 250; nTemp = typeIntegerAssignConstant; break; } case typeIntegerAssignVariable: { iArray[00] = i; iArray[01] = i; iArray[02] = i; iArray[03] = i; iArray[04] = i; iArray[05] = i; iArray[06] = i; iArray[07] = i; iArray[08] = i; iArray[09] = i; nTemp = typeIntegerAssignVariable; break; } case typeIntegerAssignSensor: { iArray[00] = SensorValue[in1]; iArray[01] = SensorValue[in1]; iArray[02] = SensorValue[in1]; iArray[03] = SensorValue[in1]; iArray[04] = SensorValue[in1]; iArray[05] = SensorValue[in1]; iArray[06] = SensorValue[in1]; iArray[07] = SensorValue[in1]; iArray[08] = SensorValue[in1]; iArray[09] = SensorValue[in1]; nTemp = typeIntegerAssignSensor; break; } case typeIntegerAssignIndexed: { iArray[i + 00] = 50; iArray[i + 01] = 50; iArray[i + 02] = 50; iArray[i + 03] = 50; iArray[i + 04] = 50; iArray[i + 05] = 50; iArray[i + 06] = 50; iArray[i + 07] = 50; iArray[i + 08] = 50; iArray[i + 09] = 50; nTemp = typeIntegerAssignIndexed; break; } case typeIntegerAddVariable: { // Have to use alternating assignments to 'i' and 'j'; otherwise // code optimizer will simplify into single 'i += 20*333' iArray[00] += i; iArray[01] += i; iArray[02] += i; iArray[03] += i; iArray[04] += i; iArray[05] += i; iArray[06] += i; iArray[07] += i; iArray[08] += i; iArray[09] += i; nTemp = typeIntegerAddVariable; break; } case typeIntegerAddConstant: { // Have to use alternating assignments to 'i' and 'j'; otherwise // code optimizer will simplify into single 'i ++= 20*333' iArray[00] += 250; iArray[01] += 250; iArray[02] += 250; iArray[03] += 250; iArray[04] += 250; iArray[05] += 250; iArray[06] += 250; iArray[07] += 250; iArray[08] += 250; iArray[09] += 250; nTemp = typeIntegerAddConstant; break; } #if defined(useLongs) case typeLongAssignConstant: { lArray[00] = 175; lArray[01] = 175; lArray[02] = 175; lArray[03] = 175; lArray[04] = 175; lArray[05] = 175; lArray[06] = 175; lArray[07] = 175; lArray[08] = 175; lArray[09] = 175; nTemp = typeLongAssignConstant; break; } case typeLongAssignVariable: { lArray[00] = lArray[9]; lArray[01] = lArray[9]; lArray[02] = lArray[9]; lArray[03] = lArray[9]; lArray[04] = lArray[9]; lArray[05] = lArray[9]; lArray[06] = lArray[9]; lArray[07] = lArray[9]; lArray[08] = lArray[9]; lArray[09] = lArray[9]; nTemp = typeLongAssignVariable; break; } case typeLongAssignIndexed: { lArray[i + 00] = 50; lArray[i + 01] = 50; lArray[i + 02] = 50; lArray[i + 03] = 50; lArray[i + 04] = 50; lArray[i + 05] = 50; lArray[i + 06] = 50; lArray[i + 07] = 50; lArray[i + 08] = 50; lArray[i + 09] = 50; nTemp = typeLongAssignIndexed; break; } #endif #if defined(useLongs) case typeLongAddConstant: { lArray[00] += 120; lArray[01] += 120; lArray[02] += 120; lArray[03] += 120; lArray[04] += 120; lArray[05] += 120; lArray[06] += 120; lArray[07] += 120; lArray[08] += 120; lArray[09] += 120; nTemp = typeLongAddConstant; break; } case typeLongAddVariable: { lArray[00] += iArray[09]; lArray[01] += iArray[09]; lArray[02] += iArray[09]; lArray[03] += iArray[09]; lArray[04] += iArray[09]; lArray[05] += iArray[09]; lArray[06] += iArray[09]; lArray[07] += iArray[09]; lArray[08] += iArray[09]; lArray[09] += iArray[09]; nTemp = typeLongAddVariable; break; } case typeLongTimes: { lArray[00] *= 3; lArray[01] *= 3; lArray[02] *= 3; lArray[03] *= 3; lArray[04] *= 3; lArray[05] *= 3; lArray[06] *= 3; lArray[07] *= 3; lArray[08] *= 3; lArray[09] *= 3; nTemp = typeLongTimes; break; } #endif #if defined(useFloats) case typeFloatAssignConstant: { fArray[00] = 3.357; fArray[01] = 3.357; fArray[02] = 3.357; fArray[03] = 3.357; fArray[04] = 3.357; fArray[05] = 3.357; fArray[06] = 3.357; fArray[07] = 3.357; fArray[08] = 3.357; fArray[09] = 3.357; nTemp = typeFloatAssignConstant; break; } case typeFloatAssignVariable: { fArray[00] = fTemp; fArray[01] = fTemp; fArray[02] = fTemp; fArray[03] = fTemp; fArray[04] = fTemp; fArray[05] = fTemp; fArray[06] = fTemp; fArray[07] = fTemp; fArray[08] = fTemp; fArray[09] = fTemp; nTemp = typeFloatAssignVariable; break; } case typeFloatAddVariable: { fArray[00] += fTemp; fArray[01] += fTemp; fArray[02] += fTemp; fArray[03] += fTemp; fArray[04] += fTemp; fArray[05] += fTemp; fArray[06] += fTemp; fArray[07] += fTemp; fArray[08] += fTemp; fArray[09] += fTemp; nTemp = typeFloatAddVariable; break; } case typeFloatAddConstant: { fArray[00] += 102.3; fArray[01] += 102.3; fArray[02] += 102.3; fArray[03] += 102.3; fArray[04] += 102.3; fArray[05] += 102.3; fArray[06] += 102.3; fArray[07] += 102.3; fArray[08] += 102.3; fArray[09] += 102.3; nTemp = typeFloatAddConstant; break; } case typeFloatTimes: { fArray[00] *= fTemp; fArray[01] *= fTemp; fArray[02] *= fTemp; fArray[03] *= fTemp; fArray[04] *= fTemp; fArray[05] *= fTemp; fArray[06] *= fTemp; fArray[07] *= fTemp; fArray[08] *= fTemp; fArray[09] *= fTemp; nTemp = typeFloatTimes; break; } #endif #if defined(NXT) || defined(TETRIX) case typeClearScreen: { eraseDisplay(); eraseDisplay(); eraseDisplay(); eraseDisplay(); eraseDisplay(); eraseDisplay(); eraseDisplay(); eraseDisplay(); eraseDisplay(); eraseDisplay(); nTemp = typeClearScreen; break; } case typeClearPixel: { nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nxtClearPixel(22, 22); nTemp = typeClearPixel ; break; } case typeRectangleErase: { nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nxtEraseRect(22, 22, 52, 52); nTemp = typeRectangleErase; break; } case typeRectangleFill: { nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nxtFillRect(22, 22, 52, 52); nTemp = typeRectangleFill; break; } case typeRectangleDraw: { nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nxtDrawRect(22, 22, 52, 52); nTemp = typeRectangleDraw; break; } #if (0) case typeIOMapRead: { const int kOffset = 0; const int kNumberOfBytes = 1; byte nBuffer[10]; const string pzModule1 = "UI.mod"; const string pzModule2 = "Display.mod"; #if (0) nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); nxtReadIOMap(pzModule1, ioResult, nBuffer[0], kOffset, kNumberOfBytes); #else nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 1; nUiOMap.OBPPointer += 20; #endif nTemp = typeIOMapRead; break; } #endif #endif #if defined(hasTranscendentalSupport) case typeSine: { fTemp *= 1.05; fArray[00] = sin(fTemp); fArray[01] = sin(fTemp); fArray[02] = sin(fTemp); fArray[03] = sin(fTemp); fArray[04] = sin(fTemp); fArray[05] = sin(fTemp); fArray[06] = sin(fTemp); fArray[07] = sin(fTemp); fArray[08] = sin(fTemp); fArray[09] = sin(fTemp); nTemp = typeSine; break; } case typeCosine: { fTemp *= 1.05; fArray[00] = cos(fTemp); fArray[01] = cos(fTemp); fArray[02] = cos(fTemp); fArray[03] = cos(fTemp); fArray[04] = cos(fTemp); fArray[05] = cos(fTemp); fArray[06] = cos(fTemp); fArray[07] = cos(fTemp); fArray[08] = cos(fTemp); fArray[09] = cos(fTemp); nTemp = typeCosine; break; } #endif case typeLogical: { #if defined(useLongs) iArray[00] = (l1 < l2); iArray[01] = (l1 < l2); iArray[02] = (l1 < l2); iArray[03] = (l1 < l2); iArray[04] = (l1 < l2); iArray[07] = (l1 < l2); iArray[06] = (l1 < l2); iArray[07] = (l1 < l2); iArray[08] = (l1 < l2); iArray[09] = (l1 < l2); #endif nTemp = typeLogical; break; } case typeTrinaryAdd: { iArray[00] = i + k; iArray[01] = i + k; iArray[02] = k + i; iArray[03] = i + k; iArray[04] = i + k; iArray[05] = k + i; iArray[06] = i + k; iArray[07] = i + k; iArray[08] = k + i; iArray[09] = i + k; nTemp = typeTrinaryAdd; break; } case typeTrinaryAddConstant1: { iArray[00] = i + 333; iArray[01] = i + 333; iArray[02] = 333 + i; iArray[03] = i + 333; iArray[04] = i + 333; iArray[05] = 333 + i; iArray[06] = i + 333; iArray[07] = i + 333; iArray[08] = 333 + i; iArray[09] = i + 333; nTemp = typeTrinaryAddConstant1; break; } case typeTrinaryAddConstant2: { iArray[00] = i + 127; iArray[01] = i + 127; iArray[02] = 127 + i; iArray[03] = i + 127; iArray[04] = i + 127; iArray[05] = 127 + i; iArray[06] = i + 127; iArray[07] = i + 127; iArray[08] = 127 + i; iArray[09] = i + 127; nTemp = typeTrinaryAddConstant2; break; } } } loopTime = time1[executionTimer]; #if defined(useFloats) loopTime /= 100.0; #endif if (nInstructionType == typeIdleLoop) elapsed = loopTime; else elapsed = loopTime - idleLoopTime; nElapsedTime[nInstructionType] = elapsed; // 'elapsed' contains the number of 10 msec 'ticks' to execute 100,000 (5,000 loops // each with 20) statements. Thus if 'elapsed' is 146, a single statement took // 14.6 microseconds: // - 1,460 millisecons total 'adjusted' time in loop (146 ticks x 10 milliseconds) // - 1,460 milliseconds is same as 1,460,000 microseconds // - divide by 100,000 to get 14.6 microseconds playSound(soundBlip); return; }
void sumPoseStruct() { float tempStruct[10][10]; memset(tempStruct,0,400); char x,y,z; float sumActive = 0; for(x = 0; x<10; x++) { for(y=0; y<10; y++) { for(z=0; z<6; z++) { tempStruct[x][y] += poseWorld.poseActivity[x].array2D[y][z]; } } } sumActive = tempStruct[poseWorld.maxActivatedCell.x][poseWorld.maxActivatedCell.y]; eraseDisplay(); nxtDrawRect(1,1,62,62); nxtDisplayStringAt(64,60,"x: %2d",poseWorld.maxActivatedCell.x); nxtDisplayStringAt(64,50,"y: %2d",poseWorld.maxActivatedCell.y); nxtDisplayStringAt(64,40,"T: %1d",poseWorld.maxActivatedCell.theta); for(x = 0; x<10; x++) { for(y = 0; y<10; y++) { char tempPercent = 0; tempPercent = (tempStruct[x][y] / sumActive)*100; if(tempPercent<=2) {} else if(tempPercent>2 && tempPercent<=7) { drawRect(x,y,2); } else if(tempPercent>7 && tempPercent<=15) { drawRect(x,y,15); } else if(tempPercent>15 && tempPercent<=25) { drawRect(x,y,25); } else if(tempPercent>25 && tempPercent<=33) { drawRect(x,y,33); } else if(tempPercent>33 && tempPercent<=50) { drawRect(x,y,50); } else if(tempPercent>50 && tempPercent<=66) { drawRect(x,y,66); } else if(tempPercent>66 && tempPercent<=75) { drawRect(x,y,75); } else if(tempPercent>75 && tempPercent<=97) { drawRect(x,y,85); } else if(tempPercent>97 && tempPercent<=100) { drawRect(x,y,100); } } } }