+ if (demod->analyze || (demod->out_file == stdout)) { // We don't want to decode devices when outputting to stdout
+ pwm_analyze(demod, demod->am_buf, len / 2);
+ } else {
+ // Detect a package and loop through demodulators with pulse data
+ int package_type = 1; // Just to get us started
+ while(package_type) {
+ int p_events = 0; // Sensor events successfully detected per package
+ package_type = pulse_detect_package(demod->am_buf, demod->fm_buf, len/2, demod->level_limit, samp_rate, &demod->pulse_data, &demod->fsk_pulse_data);
+ if (package_type == 1) {
+ if(demod->analyze_pulses) fprintf(stderr, "Detected OOK package\t@ %s\n", local_time_str(0, time_str));
+ for (i = 0; i < demod->r_dev_num; i++) {
+ switch (demod->r_devs[i]->modulation) {
+ case OOK_PULSE_PCM_RZ:
+ p_events += pulse_demod_pcm(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ case OOK_PULSE_PPM_RAW:
+ p_events += pulse_demod_ppm(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ case OOK_PULSE_PWM_PRECISE:
+ p_events += pulse_demod_pwm_precise(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ case OOK_PULSE_PWM_RAW:
+ p_events += pulse_demod_pwm(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ case OOK_PULSE_PWM_TERNARY:
+ p_events += pulse_demod_pwm_ternary(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ case OOK_PULSE_MANCHESTER_ZEROBIT:
+ p_events += pulse_demod_manchester_zerobit(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ case OOK_PULSE_CLOCK_BITS:
+ p_events += pulse_demod_clock_bits(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ case OOK_PULSE_PWM_OSV1:
+ p_events += pulse_demod_osv1(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ // FSK decoders
+ case FSK_PULSE_PCM:
+ case FSK_PULSE_PWM_RAW:
+ break;
+ case FSK_PULSE_MANCHESTER_ZEROBIT:
+ p_events += pulse_demod_manchester_zerobit(&demod->pulse_data, demod->r_devs[i]);
+ break;
+ default:
+ fprintf(stderr, "Unknown modulation %d in protocol!\n", demod->r_devs[i]->modulation);
+ }
+ } // for demodulators
+ if(debug_output > 1) pulse_data_print(&demod->pulse_data);
+ if(demod->analyze_pulses && (include_only == 0 || (include_only == 1 && p_events == 0) || (include_only == 2 && p_events > 0)) ) {
+ pulse_analyzer(&demod->pulse_data, samp_rate);
+ }
+ } else if (package_type == 2) {
+ if(demod->analyze_pulses) fprintf(stderr, "Detected FSK package\t@ %s\n", local_time_str(0, time_str));
+ for (i = 0; i < demod->r_dev_num; i++) {
+ switch (demod->r_devs[i]->modulation) {
+ // OOK decoders
+ case OOK_PULSE_PCM_RZ:
+ case OOK_PULSE_PPM_RAW:
+ case OOK_PULSE_PWM_PRECISE:
+ case OOK_PULSE_PWM_RAW:
+ case OOK_PULSE_PWM_TERNARY:
+ case OOK_PULSE_MANCHESTER_ZEROBIT:
+ case OOK_PULSE_CLOCK_BITS:
+ case OOK_PULSE_PWM_OSV1:
+ break;
+ case FSK_PULSE_PCM:
+ p_events += pulse_demod_pcm(&demod->fsk_pulse_data, demod->r_devs[i]);
+ break;
+ case FSK_PULSE_PWM_RAW:
+ p_events += pulse_demod_pwm(&demod->fsk_pulse_data, demod->r_devs[i]);
+ break;
+ case FSK_PULSE_MANCHESTER_ZEROBIT:
+ p_events += pulse_demod_manchester_zerobit(&demod->fsk_pulse_data, demod->r_devs[i]);
+ break;
+ default:
+ fprintf(stderr, "Unknown modulation %d in protocol!\n", demod->r_devs[i]->modulation);
+ }
+ } // for demodulators
+ if(debug_output > 1) pulse_data_print(&demod->fsk_pulse_data);
+ if(demod->analyze_pulses && (include_only == 0 || (include_only == 1 && p_events == 0) || (include_only == 2 && p_events > 0)) ) {
+ pulse_analyzer(&demod->fsk_pulse_data, samp_rate);
+ }
+ } // if (package_type == ...
+ } // while(package_type)...
+ } // if (demod->analyze...
+
+ if (demod->out_file) {
+ uint8_t* out_buf = iq_buf; // Default is to dump IQ samples
+ if (demod->debug_mode == 1) { // AM data
+ out_buf = (uint8_t*)demod->am_buf;
+ } else if (demod->debug_mode == 2) { // FM data
+ out_buf = (uint8_t*)demod->fm_buf;
+ }
+ if (fwrite(out_buf, 1, len, demod->out_file) != len) {
+ fprintf(stderr, "Short write, samples lost, exiting!\n");
+ rtlsdr_cancel_async(dev);
+ }
+ }