Skip to content

Instantly share code, notes, and snippets.

@rashidkpc
Created March 21, 2015 19:22
Show Gist options
  • Save rashidkpc/efc92d132b8d43411c95 to your computer and use it in GitHub Desktop.
Save rashidkpc/efc92d132b8d43411c95 to your computer and use it in GitHub Desktop.
Pulse based signal debugger
/*
This is useful for debugging systems that use low/high durations for communicating digital information.
I wrote this specific one for figuring out a wireless temperature/humidity gauge, thus to the 352+1
*/
#include <iostream>
#include <time.h>
#include <string.h>
#include <wiringPi.h>
using namespace std;
#define MESSAGE_LENGTH 353 // How long is our transmission? (Bits * 2) Consider adding 1 for the trailing quiet time
#define SHORT_FAIL 200 // Durations less than this are considered noise, we reset if we hit noise
static int durations[MESSAGE_LENGTH];
void handleInterrupt() {
static unsigned int duration;
static unsigned int changeCount;
static unsigned long lastTime;
long time = micros();
duration = time - lastTime;
if (changeCount >= MESSAGE_LENGTH || duration < SHORT_FAIL) {
changeCount = 0;
// Something of a sanity check, really no element should be 0, but we're ok with it for debugging
if (durations[MESSAGE_LENGTH/2] != 0) {
cout << '\n' << '\n';
for (int i=0; i < MESSAGE_LENGTH; i=i++) {
cout << durations[i] << " ";
}
cout << endl;
}
// 0 the array - quickly
memset(durations, 0, sizeof(durations));
} else {
//store duration
durations[changeCount++] = duration;
}
lastTime = time;
}
int main(void) {
if(wiringPiSetup() == -1)
return 0;
// Trip interrupt on rising and falling edges
if ( wiringPiISR (1, INT_EDGE_BOTH, &handleInterrupt) < 0 ) {
return 1;
}
// Keep it running
while(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment