// // main loop // int main(void) { #ifdef DEBUG #if __USE_USB usbCDC ser; ser.connect(); #else CSerial ser; ser.settings(115200); #endif CDebug dbg(ser); dbg.start(); #else #undef __USE_USB #endif /************************************************************************* * * your setup code here * **************************************************************************/ // // BLE Engine (Serial Stream) // myBLE ble; ble.advertising(100, -59); // set adv. interval = 100ms, calibrater tx power = -59dBm ble.enable(); // start the ble engine first!! // // Device Information Service // bleDeviceInfo info(ble); info.setManufactureName(u8"英倍達國際"); // u8 mean to use the UTF-8 string info.setModelNumber("nano11U37"); info.setSerialNumber("140226000"); info.setFirmwareRevision(uCXpresso_VER_STR); info.setHardwareRevision("R1"); info.setPnP(VS_USB, 1, 2, 0x3456); // vendor Id=1, product Id = 2, product ver. = 0x3456 SYS_ID_T sysId = { {0x00, 0x01, 0x02, 0x03, 0x04}, // Manufacturer Identifier {0x05, 0x06, 0x07} // Organizationally Unique Identifier }; info.setSystemId(sysId); // // Proximity Service // myProximity alert(ble); // declare Proximity Service (Immediate alert + Lose Link) // // Battery Level Service // bleBatteryLevel bl(ble); // declare Battery Level Service object // // Health Thermometer Service // bleHealthThermometer ht(ble); // declare Health Thermometer Service object ht.measurementInterval(1); // set measurement interval = 1 second // // Arduino Firmata // myFirmata.begin(ble); // begin the Firmata Object with the ble serial stream. callback_init(); // initialize the callback functions for myFirmata Object. // // A key input for Alert (for Proximity) // CPin keyAlert(P8); // define P8 as a push button keyAlert.input(); PIN_LEVEL_T pinVal = keyAlert; // // Timeout for time interval // CTimeout t1, t2, t3; // t1=analog input check, t2=temperature check, t3=battery check #ifndef DEBUG // // Power Save Feature // myPowerSave ps; // use power Save feature #endif float value; uint8_t level; while(1) { /********************************************************************** * * your loop code here * **********************************************************************/ if ( ble.isConnected() ) { #ifndef DEBUG // // Power On // ps.disable(); // disable power save mode #else ledACT = !ledACT; #endif // // UART Service // if ( ble.isAvailable() ) { // // BLE Firmata Demo // if ( myFirmata.available() ) { do { myFirmata.processInput(); } while(myFirmata.available()); } else { // // check the Digital Input // checkDigitalInputs(); // // check the Analog Input with a sampling Interval // if (t1.isExpired(samplingInterval) ) { t1.reset(); checkAnalogInputs(); } } } // // Proximity Service // if ( alert.isAvailable() ) { // // push button check (Proximity Service) // if ( keyAlert != pinVal ) { pinVal = keyAlert; // keep last status of key alert.sendEvent(keyAlert.read()==LOW ? 1 : 0); } } // // Health Thermometer Service // if ( ht.isAvailable() ) { // // check temperature // if ( t2.isExpired(1000) ) { t2.reset(); if ( ht.readTemperature(value) ) { ht.sendMeasure(value); DBG("temp=%0.2f\n", value); } // */ } } // // Battery Service // if ( bl.isAvailable() ) { // // update Battery Level // if ( t3.isExpired(3000) ) { t3.reset(); if ( bl.readSystemVoltage(value) ) { if ( value>=2.4 && value<=3.6 ) { level = map(value, 2.0, 3.3, 0, 100); bl.sendBatteryLevel(level); DBG("battery:%0.2fv %d%c\n", value, level, '%'); } } } } } // isConnected else { #ifndef DEBUG // // Power Save // ps.enable(POWER_DOWN); // enable power save mode #else ledACT = LED_ON; #endif sleep(1000); } } return 0 ; }
// Get Balance factor of node N int getBalance(struct node *N) { if (N == NULL) return 0; return ht(N->left) - ht(N->right); }
int main(int argc, char *argv[]) { int i; int retval = 0; kpse_set_progname(argv[0]); progname = xstrdup(program_invocation_short_name); #ifdef _WIN32 SetConsoleCtrlHandler((PHANDLER_ROUTINE)sigint_handler, TRUE); #else # ifdef SIGHUP signal (SIGHUP, sigint_handler); # endif # ifdef SIGINT signal (SIGINT, sigint_handler); # endif # ifdef SIGQUIT signal (SIGQUIT, sigint_handler); # endif # ifdef SIGEMT signal (SIGEMT, sigint_handler); # endif # ifdef SIGPIPE signal (SIGPIPE, sigint_handler); # endif # ifdef SIGTERM signal (SIGTERM, sigint_handler); # endif #endif if (strlen(progname) > 4 && FILESTRNCASEEQ(progname + strlen(progname) - 4, ".exe", 4)) { *(progname + strlen(progname) - 4) = '\0'; } is_ht = FILESTRCASEEQ(progname, "ht"); argv = parse_arguments(&argc, argv); #if 0 fprintf(stderr, "%s:\n", progname); for (i = 1; i < argc; i++) fprintf(stderr, "\t argv[%d] = %s\n", i, argv[i]); fprintf(stderr, "\nconfig(%d,%d) = ", math, dtd); for (i = 0; i < 3; i++) fprintf(stderr, "%s ", stdcfg[math][dtd][i]); fprintf(stderr, "\n"); #endif texargs = (force ? "--interaction=nonstopmode" : ""); if (TESTZ(argv[1])) { fprintf(stderr, "%s: error, no file specified.\n", progname); exit(1); } texsrc = xstrdup(argv[1]); #if 0 /* Rely on latex / kpathsea to find the right source file. */ if ((strlen(texsrc) < 4) || _strnicmp(texsrc + strlen(texsrc) - 4, ".tex", 4) != 0) { texsrc = concat(texsrc, ".tex"); } #endif texfile = xstrdup(texsrc); if ((strlen(texfile) >= 4) && _strnicmp(texfile + strlen(texfile) - 4, ".tex", 4) == 0) { *(texfile + strlen(texfile) - 4) = '\0'; } if (TESTZ(output_name)) { output_name = xstrdup(texfile); } else { if ((strlen(output_name) >= 4) && _strnicmp(output_name + strlen(output_name) - 4, ".tex", 4) == 0) { *(output_name + strlen(output_name) - 4) = '\0'; } texargs = concat3(texargs, " --jobname=", output_name); } #if 0 // copy the original name to output name if needed if (TESTNZ(output_dir)) { texfile = concat3(output_dir, "\\", output_name); } else if (TESTNZ(output_name)) { texfile = xstrdup(output_name); } else texfile = NULL; if (texfile) { unixtodos_filename(texfile); if (dry_run) { fprintf(stderr, "%s: copying %s to %s\n", progname, texsrc, texfile); } else { if (CopyFile(texsrc, texfile, false) == 0) { fprintf(stderr, "%s: failed to copy %s to %s (Error %d)\n", progname, texsrc, texfile, GetLastError()); } } free(texfile); } texfile = xstrdup(output_name); if (TESTNZ(output_dir)) { pushd(output_dir); xputenv("KPSE_DOT", cwd); xputenv("TEXINPUTS", concatn(cwd, "/", output_dir, ";", NULL)); if (dry_run) { fprintf(stderr, "%s: changing directory to %s\n", progname, output_dir); fprintf(stderr, "%s: setting KPSE_DOT to %s\n", progname, cwd); } } #endif if (is_ht) { retval = ht(argc, argv); } else if (FILESTRCASEEQ(progname, "httex")) { retval = ht_engine(runstr_tex, argc, argv); } else if (FILESTRCASEEQ(progname, "htlatex")) { retval = ht_engine(runstr_latex, argc, argv); } else if (FILESTRCASEEQ(progname, "httexi")) { retval = ht_engine(runstr_texi, argc, argv); } else { fprintf(stderr, "%s: %s is unknown, aborting.\n", argv[0], progname); retval = 1; } if (opt_index) { /* copy dest_dir/output_name.html to dest_dir/index.html */ char *destfile = output_name; char *indexfile = "index.html"; if (TESTNZ(output_dir)) { destfile = concat3(output_dir, "\\", output_name); indexfile = concat(output_dir, "\\index.html"); } if ((strlen(destfile) < 5) || _strnicmp(destfile + strlen(destfile) - 5, ".html", 5) != 0) { destfile = concat(destfile, ".html"); } if (CopyFile(destfile, indexfile, false) == 0) { fprintf(stderr, "%s: failed to copy %s to %s (Error %d)\n", progname, destfile, indexfile, GetLastError()); } } mt_exit(retval); /* Not Reached */ return retval; }
void readConfig( const char* filename ) { /* States: * 0 : reading new entities ( to: 1, 3, 4, 5 ) * 1 : reading machine * 2 : reading role * 3 : reading core * 4 : reading file * 5 : reading test ( to: 2 ) */ int state = 0; struct machine m; struct role r; struct file f; struct core c; struct test t; xmlTextReaderPtr xmlRead = xmlReaderForFile( filename, NULL, XML_PARSE_NONET | XML_PARSE_NOENT | XML_PARSE_NOCDATA | XML_PARSE_NOXINCNODE | XML_PARSE_COMPACT ); if( !xmlRead ) quit( "Could not open %s for reading as XML config\n", filename ); xmlTextReaderSetErrorHandler( xmlRead, xmlErrorHandler, 0 ); int nodeType, ret; xmlChar* nodeName; while( 1 ) { if( ( ret = xmlTextReaderRead( xmlRead ) ) != 1 ) { if( ret < 0 ) quit( "Error occurred\n" ); if( state ) quit( "No more nodes to read, but state is not 0. Incomplete elements.\n" ); xmlTextReaderClose( xmlRead ); return; } nodeType = xmlTextReaderNodeType( xmlRead ); nodeName = NULL; switch( nodeType ) { case XmlNodeType.Element : nodeName = xmlTextReaderLocalName( xmlRead ); switch( state ) { case 0 : if( !strcmp( nodeName, "machine" ) ) { state = 1; bzero( m, sizeof( struct machine ) ); readValidRequiredAttribute( m.name, "machine", "name" ); readValidRequiredAttribute( m.address, "machine", "address" ); if( !strcmp( m.address, "DAS4" ) ) { readValidRequiredAttribute( m.count, "machine", "DAS4 node count" ); char* end = NULL; m.icount = strtol( m.count, &end, 10 ); if( end == m.count || *end || m.icount < 1 ) quit( "Invalid DAS4 node count for machine %s\n", m.name ); } break; } if( !strcmp( nodeName, "core" ) ) { state = 3; bzero( c, sizeof( struct core ) ); readValidRequiredAttribute( c.name, "core", "name" ); break; } if( !strcmp( nodeName, "file" ) ) { state = 4; bzero( f, sizeof( struct file ) ); readValidRequiredAttribute( f.name, "file", "name" ); readValidRequiredAttribute( f.size, "file", "size" ); char* end = NULL; f.isize = strtol( f.size, &end, 10 ); if( end == f.size || f.isize < 1 ) quit( "Invalid size specifier for file %s\n", f.name ); if( *end && *(end+1) ) quit( "Invalid size specifier for file %s\n", f.name ); if( *end ) { switch( *end ) { case 'b' : case 'B' : break; case 'k' : case 'K' : f.isize *= 1024; break; case 'm' : case 'M' : f.isize *= 1024 * 1024; break; case 'g' : case 'G' : f.isize *= 1024 * 1024 * 1024; break; case 't' : case 'T' : f.isize *= 1024 * 1024 * 1024 * 1024; break; default : quit( "Invalid size specifier for file %s\n", f.name ); } } if( f.isize > 512 * 1024 * 1024 ) { int p = 512*1024*1024; while( p < f.isize ) p <<= 1; if( p != f.isize ) quit( "Invalid size specifier for file %s: sizes above 512M should be powers of 2\n", f.name ); } if( f.isize & 0x3 ) quit( "Invalid size specifier for file %s: sizes should be a multiple of 4\n", f.name ); break; } if( !strcmp( nodeName, "test" ) ) { state = 5; bzero( t, sizeof( struct test ) ); readValidRequiredAttribute( t.name, "test", "name" ); break; } break; case 1 : if( !strcmp( nodeName, "tmpDir" ) ) { onlyOne( m.tmpdir, "machine", "tmpDir" ); readValidString( m.tmpdir, "temporary directory location" ); skipToEndElement( "tmpDir" ); break; } if( !strcmp( nodeName, "params" ) ) { onlyOne( m.params, "machine", "params" ); readValidString( m.params, "params" ); skipToEndElement( "params" ); break; } printf( "Unexpected element %s in machine %s\n", nodeName, m.name ); break; case 2 : if( !strcmp( nodeName, "machine" ) ) { if( r.machineCount > 255 ) quit( "Maximum of 256 machines per role passed on line %d\n", xmlTextReaderGetParserLineNumber( xmlRead ) ); readValid( r.machine[r.machineCount], "machine name" ); r.machineCount++; skipToEndElement( "machine" ); break; } if( !strcmp( nodeName, "user" ) ) { onlyOne( r.user, "role", "user" ); readValidString( r.user, "user" ); skipToEndElement( "user" ); break; } if( !strcmp( nodeName, "core" ) ) { onlyOne( r.core, "role", "core" ); readValidString( r.core, "core name" ); skipToEndElement( "core" ); break; } if( !strcmp( nodeName, "file" ) ) { onlyOne( r.file, "role", "file" ); readValidString( r.file, "file name" ); skipToEndElement( "file" ); break; } if( !strcmp( nodeName, "params" ) ) { onlyOne( r.params, "role", "params" ); readValidString( r.params, "params" ); skipToEndElement( "params" ); break; } printf( "Unexpected element %s in role on line %d\n", nodeName, xmlTextReaderGetParserLineNumber( xmlRead ) ); break; case 3 : if( !strcmp( nodeName, "params" ) ) { onlyOne( c.params, "core", "params" ); readValidString( c.params, "params" ); skipToEndElement( "params" ); break; } if( !strcmp( nodeName, "localDir" ) ) { onlyOne( c.localdir, "core", "localDir" ); readValidString( c.localdir, "local core directory" ); skipToEndElement( "localDir" ); break; } if( !strcmp( nodeName, "compilationDir" ) ) { onlyOne( c.compdir, "core", "compilationDir" ); readValidString( c.compdir, "relative compilation directory" ); skipToEndElement( "compilationDir" ); break; } if( !strcmp( nodeName, "program" ) ) { onlyOne( c.program, "core", "program" ); readValidString( c.program, "program name" ); skipToEndElement( "program" ); break; } printf( "Unexpected element %s in core %s\n", nodeName, c.name ); skipToEndElement( nodeName ); break; case 4 : if( !strcmp( nodeName, "offset" ) ) { onlyOne( c.offset, "file", "offset" ); readValidString( c.offset, "offset" ); char* end = NULL; f.ioffset = strtol( f.offset, &end, 10 ); if( end == f.offset || *end || f.ioffset < 0 || f >= f.isize ) quit( "Invalid file offset for file %s\n", f.name ); skipToEndElement( "offset" ); break; } printf( "Unexpected element %s in file %s\n", nodeName, f.name ); skipToEndElement( nodeName ); break; case 5 : if( !strcmp( nodeName, "role" ) ) { state = 2; bzero( r, sizeof( struct role ) ); readValidRequiredAttribute( r.type, "role", "type" ); if( strcmp( r.type, "seed" ) && strcmp( r.type, "leech" ) ) quit( "Invalid value for attribute 'type' for role on line %d, expected 'seed' or 'leech'\n", xmlTextReaderGetParserLineNumber( xmlRead ) ); break; } quit( "Unexpected element %s in test on line %d\n", nodeName, xmlTextReaderGetParserLineNumber( xmlRead ) ); default : quit( "State sanity\n", nodeName ); } break; case XmlNodeType.EndElement : nodeName = xmlTextReaderLocalName( xmlRead ); switch( state ) { case 0 : quit( "End element %s found while not in entity\n", nodeName ); case 1 : if( !strcmp( nodeName, "machine" ) ) { memcpy( machines + machineCount, &m, sizeof( struct machine ) ); machineCount++; state = 0; } break; case 2 : if( !strcmp( nodeName, "role" ) ) { mempcy( t.roles + r.roleCount, &r, sizeof( struct role ) ); t.roleCount++; state = 5; } break; case 3 : if( !strcmp( nodeName, "core" ) ) { memcpy( cores + coreCount, &c, sizeof( struct core ) ); coreCount++; state = 0; } break; case 4 : if( !strcmp( nodeName, "file" ) ) { memcpy( files + fileCount, &f, sizeof( struct file ) ); fileCount++; state = 0; } break; case 5 : if( !strcmp( nodeName, "test" ) ) { memcpy( tests + testCount, &t, sizeof( struct test ) ); testCount++; state = 0; } break; default : quit( "State sanity\n" ); } default : } if( nodeName ) free( nodeName ); } } void validateConfigAndGenerateScripts( ) { int i, j, k, l; bool found; // == Validate test-role references to machines, cores and files // == Also register which machine uses which cores and which files for( i = 0; i < testCount; i++ ) { for( j = 0; j < tests[i].roleCount; j++ ) { // Check for validity of each role's core found = false; for( k = 0; k < coreCount; k++ ) { if( !strcmp( cores[k].name, tests[i].roles[j].core ) ) { found = true; break; } } if( !found ) quit( "Test %s role %i refers to core %s which does not exist\n", tests[i].name, j, tests[i].roles[j].core ); // Check for validity of each role's file found = false; for( k = 0; k < fileCount; k++ ) { if( !strcmp( files[k].name, tests[i].roles[j].file ) ) { found = true; break; } } if( !found ) quit( "Test %s role %i refers to file %s which does not exist\n", tests[i].name, j, tests[i].roles[j].file ); // Check for validity of each role's machines for( l = 0; l < tests[i].roles[j].machineCount; l++ ) { found = false; for( k = 0; k < machineCount; k++ ) { if( !strcmp( machines[k].name, tests[i].roles[j].machines[l] ) ) { // Machine found: register used core with the machine if needed for( m = 0; m < machines[k].coreCount; m++ ) { if( !strcmp( machines[k].cores[m], tests[i].roles[j].core ) ) { found = true; break; } } if( !found ) machines[k].cores[machines[k].coreCount++] = tests[i].roles[j].core; // Machine found: register used file with the machine if needed, only if seeding if( !strcmp( tests[i].roles[j].type, "seed" ) ) { found = false; for( m = 0; m < machines[k].fileCount; m++ ) { if( !strcmp( machines[k].files[m], tests[i].roles[j].file ) ) { found = true; break; } } if( !found ) machines[k].files[machines[k].fileCount++] = tests[i].roles[j].file; } // Machine found found = true; break; } } if( !found ) quit( "Test %s role %i refers to machine %s which does not exist\n", tests[i].name, j, tests[i].roles[j].machines[l] ); } } } // Create temporary script FILE* script = tmpfile(); if( !script ) quit( "Can't create temporary script\n" ); fprintf( script, "#!/bin/bash\n" ); // == Check validity of machines and users: can each machine be accessed? for( i = 0; i < machineCount; i++ ) { // Creates a function in the script for sending command to the machine using ssh // Call using // ssh_machine_%i "commands" || cleanup -1 // where %i is the index of the machine in machines. Also be sure to return non-zero from your commands on error. fprintf( script, "function ssh_machine_%i {\n", i ); if( strcmp( machines[i].address, "DAS4" ) ) fprintf( script, " ssh -T -n -o BatchMode=yes -h \"%s\"", machines[i].address ); else fprintf( script, " ssh -T -n -o BatchMode=yes -h fs3.das4.tudelft.nl" ); if( machines[i].user ) fprintf( script, " -l \"%s\"", machines[i].user ); if( machines[i].params ) fprintf( script, " %s", machines[i].params ); fprintf( script, " $1 || return -1;\n" ); fprintf( script, "}\n" ); // Creates a function in the script for sending files to the machine using scp // Call using // scp_to_machine_%i localfile remotefile fprintf( script, "function scp_to_machine_%i {\n", i ); fprintf( script, "scp -o BatchMode=yes " ); if( machines[i].params ) fprintf( script, "%s ", machines[i].params ); fprintf( script, "$1 ", l ); if( machines[i].user ) fprintf( script, "\"%s\"@", machines[i].user ); if( strcmp( machines[i].address, "DAS4" ) ) fprintf( script, "\"%s\"", machines[i].address ); else fprintf( script, "fs3.das4.tudelft.nl" ); fprintf( script, ":$2 || cleanup -1\n", i, l ) fprintf( script, "}\n" ); // Creates a function in the script for retrieving files from the machine using scp // Call using // scp_from_machine_%i remotefile localfile fprintf( script, "function scp_from_machine_%i {\n", i ); fprintf( script, "scp -o BatchMode=yes " ); if( machines[i].params ) fprintf( script, "%s ", machines[i].params ); if( machines[i].user ) fprintf( script, "\"%s\"@", machines[i].user ); if( strcmp( machines[i].address, "DAS4" ) ) fprintf( script, "\"%s\"", machines[i].address ); else fprintf( script, "fs3.das4.tudelft.nl" ); fprintf( script, ":$1 $2 || cleanup -1\n", i, l ) fprintf( script, "}\n" ); // Checks reachability of machine fprintf( script, "ssh_machine_%i || exit -1\n", i ); } // == Check validity of each core: can each core be packaged? Can it be compiled locally and does the program then exist? // Create a cleanup file. This file should have code appended to cleanup things when errors occur or testing has finished. See existing code for examples of concatenating to it. // The cleanup function is available after this as well. Call it with an exit argument to end the script cleanly. fprintf( script, "CLEANUPFILE=`mktemp`\n" ); fprintf( script, "chmod +x CLEANUPFILE\n" ); fprintf( script, "[ -x CLEANUPFILE ] || exit -1\n" ); fprintf( script, "function cleanup {\n" ); fprintf( script, " (\ncat <<EOL\nrm $CLEANUPFILE\nEOL\n) >> $CLEANUPFILE\n" ); fprintf( script, " . $CLEANUPFILE\n" ); fprintf( script " exit $1\n" ); fprintf( script, "}\n" ); // Create a local temporary directory for storage fprintf( script, "TARDIR=`mktemp -d`\n[ \"X${TARDIR}X\" == \"XX\" ] && exit -1\n" ); fprintf( script, "(\ncat <<EOL\n#!/bin/bash\nrm -rf $TARDIR\nEOL\n) >> $CLEANUPFILE\n" ); fprintf( script, "CURDIR=`pwd`\n" ); // Create a tarball for the testenvironment in the temporary local storage fprintf( script, "make clean || cleanup -1\n" ); fprintf( script, "tar cf ${TARDIR}/testenvironment.tar . || cleanup -1\n" ); fprintf( script, "bzip2 ${TARDIR}/testenvironment.tar || cleanup -1\n" ); // Check whether the needed tools of the testenvironment compile locally fprintf( script, "make genfakedata || cleanup -1\n" ); for( i = 0; i < coreCount; i++ ) { if( !cores[i].localdir ) cores[i].localdir = strdup( "../" ); // Create a tarball for the core in the temporary local storage fprintf( script, "cd %s || cleanup -1\n", cores[i].localdir ); fprintf( script, "make clean\n" ); // Not checked: SHOULD be available, but... fprintf( script, "tar cf ${TARDIR}/core_%i.tar . || cleanup -1\n", i ); fprintf( script, "bzip2 ${TARDIR}/core_%i.tar || cleanup -1\n", i ); if( !cores[i].compdir ) cores[i].compdir = strdup( "testenvironment/" ); // Check whether the core compiles locally and the program exists after fprintf( script, "cd %s || cleanup -1\n", cores[i].compdir ); fprintf( script, "make || cleanup -1\n" ); if( !cores[i].program ) cores[i].program = strdup( "swift" ); fprintf( script, "[ -x %s ] || cleanup -1\n", cores[i].program ); fprintf( script, "cd ${CURDIR}\n" ); } // For each file, precalculate the hash for( i = 0; i < fileCount; i++ ) { // Create some temporary file to write fake data to. These fake data files will be regenerated at each machine since generating is faster than copying. size_t size = files[i].isize; FILE* data = tmpfile(); if( !data ) quit( "can't create temporary data file\n" ); int datan = fileno( data ); int filesize; if( size > 512*1024*1024 ) filesize = 512*1024*1024; else filesize = size; if( generateFakeData( datan, filesize ) ) quit( "could not write fake data\n" ); MemoryHashStorage mhs; FileOffsetDataStorage fods( datan, files[i].ioffset ); if( !fods.valid() ) quit( "can't read back from temporary data file\n" ); HashTree ht( fods, Sha1Hash::ZERO, mhs ); Sha1Hash hash = ht.root_hash(); if( size > filesize ) { MemoryHashStorage mhs2; int max = size/filesize; for( j = 0; j < max; j++ ) mhs2.setHash( bin64(0,j), hash ); int lvl = 0; do { max >>= 1; lvl++; for( j = 0; j < max; j++ ) mhs2.hashLeftRight( bin64_t(lvl, j) ); } while( max > 1 ); files[i].hash = mhs.getHash( bin64_t(lvl, 0) ); } else
// // main task // int main(void) { #ifdef DEBUG #if __USE_USB usbCDC ser; ser.connect(); #else CSerial ser; ser.settings(115200); #endif CDebug dbg(ser); dbg.start(); #endif /************************************************************************* * * your setup code here * **************************************************************************/ // // BLE engine (serial stream) // bleSerial ble("myHRM"); ble.enable(96); // // Heart Beat Rate Service // bleHeartRate hrm(ble); hrm.supportContact(true); hrm.contactStatus(true); hrm.setSensorLocation(HRSL_FINGER); // // Battery Level & Health Thermometer Service (Use internal sensors) // bleBatteryLevel bat(ble); bleHealthThermometer ht(ble); // // Pulse Sensor // myPulseSensor pulse(AD0); // signal on AD0 pulse.start(); // // Fade LED task // ledTask led(pulse); led.start("LED", 50); // reduce stack size to 58 to increase the heap memory // // // CTimeout t1, t2, t3; float value; uint8_t level; while(1) { /********************************************************************** * * your loop code here * **********************************************************************/ // // Heart Rate Measurement // if ( hrm.isAvailable() ) { if ( t1.isExpired(1000) ) { // interval 1 second t1.reset(); hrm.sendMeasure((uint8_t)pulse.BPM); } } // // Battery Level // if ( t2.isExpired(10000) ) { // interval 10 seconds t2.reset(); if ( bat.readSystemVoltage(value) ) { level = map(value, 2.0, 3.3, 0, 100); // 0%~100% bat.sendBatteryLevel(level); } } // // Health Thermometer Service // if ( t3.isExpired(5000) ) { // interval 5 seconds t3.reset(); if ( ht.readTemperature(value) ) { ht.sendMeasure(value); } } } return 0 ; }
char *cc(pTHX_ char *c,int l) { char *x; char *h; char z; char t; int i,j,k,p,r; int f; for(i=f=0;i<l;i++){ if(c[i] == '#' || (c[i] == '/' && c[(i+1)] == '/') || (c[i] == '/' && c[(i+1)] == '*')){ f++; break; } } if(! f){ return c; } if((x = (char *)malloc(l+1)) == NULL){ Perl_croak(aTHX_ "failed malloc in function cc"); return NULL; } for(i=j=f=0;i<l;i++){ if(c[i] == '#'){ for(;i<l;i++){ if(c[i] == '\r' || c[i] == '\n'){ j = j + sc(x+(j-1),j); x[j++] = c[i]; break; } } } else if((c[i] == '/' && c[(i+1)] == '*')){ if(l > (i+=2)){ j = j + sc(x+(j-1),j); for(;i<l;i++){ if(c[i] == '\r' || c[i] == '\n'){ x[j++] = c[i]; } else if(c[i] == '*' && c[(i+1)] == '/'){ i++; f++; break; } } } if(!f){ Perl_croak(aTHX_ "could not find end of comment in function cc(/**/)"); return NULL; } } else if(c[i] == '/' && c[(i+1)] == '/'){ for(r=i-1;r>0;r--){ if(c[r] == 0x09 || c[r] == 0x20){ continue; } if(c[r] == '~'){ f++; } break; } if(f){ x[j++] = c[i]; x[j++] = c[++i]; } else{ for(;i<l;i++){ if(c[i] == '\r' || c[i] == '\n'){ j = j + sc(x+(j-1),j); x[j++] = c[i]; break; } } } } else if(c[i] == '/'){ x[j++] = c[i]; for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == '/'){ f++; break; } } if(!f){ Perl_croak(aTHX_ "could not find end of regexp in function cc(//)"); return NULL; } } else if(c[i] == '<' && c[(i+1)] == '<'){ x[j++] = c[i]; x[j++] = c[(i+1)]; if(l > (i+=2)){ if((h = ht(c+i)) == NULL){ x[j++] = c[i]; continue; } p = strlen(h); for(k=0;k<p;k++){ x[j++] = c[(i+k)]; } for(i+=p;i<l;i++){ if(strncmp(c+i,h,p) == 0){ f++; i--; break; } else{ x[j++] = c[i]; } } free(h); } if(!f){ Perl_croak(aTHX_ "could not find end of here-document in function cc(>>)"); return NULL; } } else if(c[i] == '<'){ x[j++] = c[i]; for(r=i-1;r>0;r--){ if(c[r] == 0x09 || c[r] == 0x20){ continue; } if(c[r] == '~'){ f++; } break; } if(!f){ continue; } else{ for(i+=1,f=0;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == '>'){ f++; break; } } if(!f){ Perl_croak(aTHX_ "could not find end of regexp in function cc(<>)"); return NULL; } } } else if(c[i] == 0x22 || c[i] == 0x27 || c[i] == '`'){ x[j++] = c[i]; z = c[i]; for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ f++; break; } } if(!f){ Perl_croak(aTHX_ "could not find end of quote in function cc(%c)",z); return NULL; } } else if(c[i] == 'q' && (c[(i+1)] == 'q' || c[(i+1)] == 'r' || c[(i+1)] == 'w' || c[(i+1)] == 'x')){ x[j++] = c[i]; x[j++] = c[(i+1)]; t = c[(i+1)]; if(l > (i+=2)){ if(ns(c[i])){ x[j++] = c[i]; continue; } x[j++] = c[i]; switch(c[i]){ case '(': z = ')'; break; case '[': z = ']'; break; case '{': z = '}'; break; case '<': z = '>'; break; default: z = c[i]; } for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ f++; break; } } } if(!f){ Perl_croak(aTHX_ "could not find end of quote in function in cc(q%c)",t); return NULL; } } else if(c[i] == 'm' || c[i] == 'q'){ x[j++] = c[i]; t = c[i]; if(ns(c[(i+1)])){ continue; } if(l > ++i){ x[j++] = c[i]; switch(c[i]){ case '(': z = ')'; break; case '[': z = ']'; break; case '{': z = '}'; break; case '<': z = '>'; break; default: z = c[i]; } for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ f++; break; } } } if(!f){ if(t == 'm'){ Perl_croak(aTHX_ "could not find end of regexp in function in cc(%c)",t); } else{ Perl_croak(aTHX_ "could not find end of quote in function in cc(%c)",t); } return NULL; } } else if(c[i] == 's'){ x[j++] = c[i]; if(ns(c[(i+1)])){ continue; } if(l > ++i){ switch(c[i]){ case '(': t = c[i]; z = ')'; break; case '[': t = c[i]; z = ']'; break; case '{': t = c[i]; z = '}'; break; case '<': t = c[i]; z = '>'; break; default: t = '\0'; z = c[i]; } x[j++] = c[i]; if(t == '\0'){ for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ if(++f > 1){ break; } } } } else{ for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ f++; break; } } if(! f || c[(i+1)] != t){ Perl_croak(aTHX_ "could not find end of regexp in function in cc(s)"); return NULL; } else{ for(i+=1,f=0;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ f++; break; } } } } } if(!f){ Perl_croak(aTHX_ "could not find end of regexp in function in cc(s)"); return NULL; } } else if(c[i] == 't' && c[(i+1)] == 'r'){ x[j++] = c[i]; x[j++] = c[(i+1)]; if(l > (i+=2)){ if(ns(c[i])){ x[j++] = c[i]; continue; } switch(c[i]){ case '(': t = c[i]; z = ')'; break; case '[': t = c[i]; z = ']'; break; case '{': t = c[i]; z = '}'; break; case '<': t = c[i]; z = '>'; break; default: t = '\0'; z = c[i]; } x[j++] = c[i]; if(t == '\0'){ for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ if(++f > 1){ break; } } } } else{ for(i+=1;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ f++; break; } } if(! f || c[(i+1)] != t){ Perl_croak(aTHX_ "could not find end of regexp in function in cc(tr)"); return NULL; } else{ for(i+=1,f=0;i<l;i++){ x[j++] = c[i]; if(c[(i-1)] != '\\' && c[i] == z){ f++; break; } } } } } if(!f){ Perl_croak(aTHX_ "could not find end of regexp in function in cc(tr)"); return NULL; } } else{ x[j++] = c[i]; } f = 0; } x[j] = '\0'; return x; }
// // main loop // int main(void) { #ifdef DEBUG #if __USE_USB usbCDC ser; ser.connect(); #else CSerial ser; ser.settings(115200); #endif CDebug dbg(ser); dbg.start(); #endif // // your setup code here // myBLE ble; ble.enable(128); bleBatteryLevel bl(ble); // declare Battery Level Service object bleHealthThermometer ht(ble); // declare Health Thermometer Service object myFirmata.begin(ble); // begin the Firmata Object with the ble serial stream. callback_init(); // initialize the callback functions for myFirmata Object. CTimeout t1, t2; // t1=battery level update, t2=analog-input interval #ifndef DEBUG myPowerSave ps; // use power Save #endif ledACT = LED_ON; float value; uint8_t level; // // your loop code here // while(1) { if ( ble.isConnected() ) { #ifndef DEBUG ps.disable(); // disable power save mode #endif // // myFirmataClass Check // if ( myFirmata.available() ) { do { myFirmata.processInput(); } while(myFirmata.available()); } else { if ( t1.isExpired(3000)==false ) { // // check the Digital Input // checkDigitalInputs(); // // check the Analog Input with a sampling Interval // if (t2.isExpired(samplingInterval) ) { t2.reset(); checkAnalogInputs(); } } else { t1.reset(); // update Battery Level // if ( bl.readSystemVoltage(value) ) { if ( value>=2.0 && value<=3.6 ) { level = map(value, 2.0, 3.3, 0, 100); bl.sendBatteryLevel(level); DBG("battery:%0.2f %d\n", value, level); } } // // update Temperature // if ( ht.readTemperature(value) ) { ht.sendMeasure(value); DBG("temp=%0.2f\n", value); } // */ } }// */ } else { #ifndef DEBUG ps.enable(POWER_DOWN); // enable power save mode #endif sleep(200); } } return 0 ; }
int main() { hash_table ht(13); //1 cout << "hash table" << endl; for(int i = 0; i < 30; ++ i) ht.h_insert(i); ht.h_print(); hash_node *p1 = ht.h_find(24); if(p1) cout << "find 24 in hash table: " << p1->h_value << endl; else cout << "can't find 24 in hash table" << endl; ht.h_delete(24); p1 = ht.h_find(24); cout << "after delete 24 from hash table" << endl; if(p1) cout << "find 24 in hash table: " << p1->h_value << endl; else cout << "can't find 24 in hash table" << endl; ht.h_print(); segment_tree st; //2 cout << endl << "segment tree [0, 10]" << endl; st.s_print(); st.s_build(0, 10); st.s_insert(2, 4); st.s_insert(6, 9); cout << "insert [2, 4], [6, 9], length: " << st.s_length() << endl; st.s_print(); st.s_insert(5, 7); cout << "insert [5, 7], length: " << st.s_length() << endl; st.s_print(); binary_index_tree bit; //3 cout << endl << "binary index tree" << endl; for(int i = 1; i < 20; ++ i){ cout << "array s[" << i << "] add " << 2 * i << endl; bit.b_add(i, 2 * i); } bit.b_print(); cout << "array sum from 1 to 10: " << bit.b_sum(10) << endl; disjoint_set ds; //4 cout << endl << "disjoint set" << endl; cout << "union" << endl; for(int i = 0; i < 10; ++ i){ cout << "number " << i << " and " << 3 * i << " is in one set" << endl; ds.d_union(i, 3 * i); } ds.d_print(); cout << "query" << endl; if(ds.d_query(1, 6)) cout << "number 1 and 6 in one set" << endl; else cout << "number 1 and 6 not in one set" << endl; if(ds.d_query(1, 4)) cout << "number 1 and 4 in one set" << endl; else cout << "number 1 and 4 not in one set" << endl; if(ds.d_query(2, 6)) cout << "number 2 and 6 in one set" << endl; else cout << "number 2 and 6 not in one set" << endl; cout << endl << "leftist tree" << endl; leftist_tree lt, rt; //5 left_node tr[51]; for(int i = 0; i < 51; ++ i) tr[i].l_idx = i; lt.l_root = &tr[6]; tr[6].l_lc = &tr[11]; tr[6].l_rc = &tr[8]; tr[6].l_dist = 2; tr[11].l_lc = &tr[17]; tr[11].l_rc = &tr[14]; tr[11].l_dist = 2; tr[17].l_lc = &tr[18]; tr[17].l_rc = &tr[20]; tr[17].l_dist = 1; tr[14].l_lc = &tr[19]; tr[14].l_rc = &tr[24]; tr[14].l_dist = 1; tr[8].l_lc = &tr[10]; tr[8].l_rc = &tr[13]; tr[8].l_dist = 1; tr[10].l_lc = &tr[15]; tr[10].l_rc = &tr[30]; tr[10].l_dist = 1; tr[13].l_lc = &tr[28]; rt.l_root = &tr[7]; tr[7].l_lc = &tr[9]; tr[7].l_rc = &tr[12]; tr[7].l_dist = 2; tr[9].l_lc = &tr[21]; tr[9].l_rc = &tr[16]; tr[9].l_dist = 1; tr[12].l_lc = &tr[31]; tr[12].l_rc = &tr[26]; tr[12].l_dist = 1; tr[31].l_lc = &tr[42]; tr[31].l_rc = &tr[33]; tr[31].l_dist = 1; tr[42].l_lc = &tr[50]; tr[26].l_lc = &tr[27]; cout << "tree lt:" << endl; lt.l_print(); cout << "tree rt:" << endl; rt.l_print(); lt.l_merge(&rt); cout << endl << "after lt merge rt:" << endl; lt.l_print(); cout << endl << "after pop the top node:" << endl; lt.l_pop(); lt.l_print(); return(0); }