Пример #1
0
void  run_in_analysis_mode(int verbosity_level) {
	unsigned char bytearray[ANALYZEBYTECOUNT]; // Explicitly declare as unsigned char because depending on compiler, char may be unsigned or signed
	int prvsamp;
	
	sleep(1);
	
	printf("\nEfergy Power Monitor Decoder - Running in analysis mode using verbosity level %d\n\n", verbosity_level);
	analysis_wavecenter = 0;
	
	while( !feof(stdin) ) {

		// Look for a valid Efergy Preamble sequence which we'll define as
		// a sequence of at least MIN_PEAMBLE_SIZE positive and negative or negative and positive pulses. eg 50N+50P or 50P+50N
		int negative_preamble_count=0;
		int positive_preamble_count=0;
		prvsamp = 0;
		while ( !feof(stdin) ) {	
			int cursamp  = (int16_t) (fgetc(stdin) | fgetc(stdin)<<8);			
			// Check for preamble 
			if ((prvsamp >= analysis_wavecenter) && (cursamp >= analysis_wavecenter)) {
				positive_preamble_count++;
			} else if ((prvsamp < analysis_wavecenter) && (cursamp < analysis_wavecenter)) {
				negative_preamble_count++;				
			} else if ((prvsamp >= analysis_wavecenter) && (cursamp < analysis_wavecenter)) {
				if ((positive_preamble_count > MIN_POSITIVE_PREAMBLE_SAMPLES) &&
					(negative_preamble_count > MIN_NEGATIVE_PREAMBLE_SAMPLES))
					break;
				negative_preamble_count=0;
			} else if ((prvsamp < analysis_wavecenter) && (cursamp >= analysis_wavecenter)) {
				if ((positive_preamble_count > MIN_POSITIVE_PREAMBLE_SAMPLES) &&
					(negative_preamble_count > MIN_NEGATIVE_PREAMBLE_SAMPLES))
					break;
				positive_preamble_count=0;
			}	
			prvsamp = cursamp;
		} // end of find preamble while loop
			
		sample_store_index=0;
		while( !feof(stdin) ) {
			int cursamp  = (int16_t) (fgetc(stdin) | fgetc(stdin)<<8);
			sample_storage[sample_store_index] = cursamp;
			if (sample_store_index < (SAMPLE_STORE_SIZE-1))
				sample_store_index++;
			else {
				analyze_efergy_message(verbosity_level);
				break;
			}
		} // Frame processing while 
	} // outermost while 
	
	exit(0);
}
Пример #2
0
void  main (int argc, char**argv)
{
	int debug_level = 0;

	// Give rtl_fm program some time to get initialized so its startup messages don't interleave with ours
	sleep(1);

	if ((argc == 2) && (strncmp(argv[1], "-h", 2) == 0)) {
		printf("\nUsage: %s              - Normal mode\n", argv[0]);
		printf("       %s <filename>   - Normal mode plus log samples to output file\n", argv[0]);
		printf("       %s -d [1,2,3,4] - Set debug/verbosity.  Default level 0 has minimum output\n", argv[0]);
		exit(0);
	} else if ((argc == 3) && (strncmp(argv[1], "-d", 2) == 0)) {
		debug_level = strtol(argv[2], NULL, 0);
		if ((debug_level < 1) || (debug_level > 4)) {
			fprintf(stderr, "\nDebug level (-d option) must be between 1 and 4\n");
			exit(EXIT_FAILURE);
		}
	} else if ((argc == 2) && (strcmp(argv[1], "-d") == 0))
		debug_level = 3;
	else if (argc == 2) {
		//fp = fopen(argv[1], "a"); // Log file opened in append mode to avoid destroying data
		samplecount = 0; // Reset sample counter
		loggingok = 1;
		/*if (fp == NULL) {
            fprintf(stderr, "\nFailed to open log file!\n");
            exit(EXIT_FAILURE);
        }*/
	} else {
		loggingok = 0;
	}

	if (debug_level > 0)
		printf("\nEfergy Power Monitor Decoder - (debug level %d)\n\n", debug_level);
	else
		printf("\nEfergy Energy Monitor Decoder\n\n");

	int prvsamp;
	analysis_wavecenter = 0;

	while ( !feof(stdin) ) {

		// Look for a valid Efergy Preamble sequence which we'll define as
		// a sequence of at least MIN_PEAMBLE_SIZE positive and negative or negative and positive pulses. eg 50N+50P or 50P+50N
		int negative_preamble_count = 0;
		int positive_preamble_count = 0;
		prvsamp = 0;
		while ( !feof(stdin) ) {
			int cursamp  = (int16_t) (fgetc(stdin) | fgetc(stdin) << 8);
			// Check for preamble
			if ((prvsamp >= analysis_wavecenter) && (cursamp >= analysis_wavecenter)) {
				positive_preamble_count++;
			} else if ((prvsamp < analysis_wavecenter) && (cursamp < analysis_wavecenter)) {
				negative_preamble_count++;
			} else if ((prvsamp >= analysis_wavecenter) && (cursamp < analysis_wavecenter)) {
				if ((positive_preamble_count > MIN_POSITIVE_PREAMBLE_SAMPLES) &&
						(negative_preamble_count > MIN_NEGATIVE_PREAMBLE_SAMPLES))
					break;
				negative_preamble_count = 0;
			} else if ((prvsamp < analysis_wavecenter) && (cursamp >= analysis_wavecenter)) {
				if ((positive_preamble_count > MIN_POSITIVE_PREAMBLE_SAMPLES) &&
						(negative_preamble_count > MIN_NEGATIVE_PREAMBLE_SAMPLES))
					break;
				positive_preamble_count = 0;
			}
			prvsamp = cursamp;
		} // end of find preamble while loop

		sample_store_index = 0;
		while ( !feof(stdin) ) {
			int cursamp  = (int16_t) (fgetc(stdin) | fgetc(stdin) << 8);
			sample_storage[sample_store_index] = cursamp;
			if (sample_store_index < (SAMPLE_STORE_SIZE - 1))
				sample_store_index++;
			else {
				analyze_efergy_message(debug_level);
				break;
			}
		} // Frame processing while
	} // outermost while

	if (loggingok) {
		fclose(fp); // If rtl-fm gives EOF and program terminates, close file gracefully.
	}
}