From bd96036c6466dda938419494802fcebb2178b50c Mon Sep 17 00:00:00 2001 From: Hilmar Tuneke Date: Sat, 2 May 2026 00:13:30 +0200 Subject: [PATCH] Add puls length, protocol and bits as sniffed values Add puls length, protocol and bits as sniffed values. Also refactor main function for clarity and error handling. --- RPi_utils/RFSniffer.cpp | 72 ++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/RPi_utils/RFSniffer.cpp b/RPi_utils/RFSniffer.cpp index 3c608a8..df786e1 100644 --- a/RPi_utils/RFSniffer.cpp +++ b/RPi_utils/RFSniffer.cpp @@ -13,53 +13,51 @@ #include #include - RCSwitch mySwitch; - - int main(int argc, char *argv[]) { - // This pin is not the first pin on the RPi GPIO header! - // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/ - // for more information. - int PIN = 2; + // This pin is not the first pin on the RPi GPIO header! + // Consult https://projects.drogon.net/raspberry-pi/wiringpi/pins/ + // for more information. + int PIN = 2; - if(wiringPiSetup() == -1) { - printf("wiringPiSetup failed, exiting..."); - return 0; - } - - int pulseLength = 0; - if (argv[1] != NULL) pulseLength = atoi(argv[1]); - - mySwitch = RCSwitch(); - if (pulseLength != 0) mySwitch.setPulseLength(pulseLength); - mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2 + if(wiringPiSetup() == -1) { + printf("wiringPiSetup failed, exiting...\n"); + return 1; + } + + mySwitch = RCSwitch(); + + if (argv[1] != NULL) { + int pulseLength = atoi(argv[1]); + mySwitch.setPulseLength(pulseLength); + } + mySwitch.enableReceive(PIN); // Receiver on interrupt 0 => that is pin #2 - - while(1) { - + printf("Setup completed, reading from PIN %d\n", PIN); + while(1) { if (mySwitch.available()) { + unsigned long value = mySwitch.getReceivedValue(); - unsigned long value = mySwitch.getReceivedValue(); + if (value == 0) { + printf("Unknown encoding\n"); + } else { + int pulseLength = mySwitch.getReceivedDelay(); + int protocol = mySwitch.getReceivedProtocol(); + int bitLength = mySwitch.getReceivedBitlength(); + + printf("Code: %lu\n", value); + printf("Puls Length: %d µs\n", pulseLength); + printf("Protocol: %d\n", protocol); + printf("Bits: %d\n", bitLength); + } - if (value == 0) { - printf("Unknown encoding\n"); - } else { - - printf("Received %lu\n", mySwitch.getReceivedValue() ); - } - - fflush(stdout); - mySwitch.resetAvailable(); + fflush(stdout); + mySwitch.resetAvailable(); } usleep(100); - - } - - exit(0); - + } + return 0; } -