Skip to content

Instantly share code, notes, and snippets.

@rashidkpc
Created March 21, 2015 04:58
Show Gist options
  • Save rashidkpc/ad1ec6102ae288dcb067 to your computer and use it in GitHub Desktop.
Save rashidkpc/ad1ec6102ae288dcb067 to your computer and use it in GitHub Desktop.
AM digital transmission + wiringPi
// This uses those super cheap 433mhz modules to receive a 32 bit integer
// You should be able to implement many 315 & 433mhz protocols in this, though more complex lock logic may be required
#include <wiringPi.h>
#include <time.h>
#include <iostream>
#include <bitset>
using namespace std;
#define MAX_CHANGE_TO_KEEP 32
#define LOW_PULSE 350 // microseconds
#define HIGH_PULSE 1100 // microseconds
#define LOCK 11200
#define TOLERANCE 150 // LOW/HIGH +/- TOLERANCE, LOCK +/- TOLERANCE*10
static bitset<32> bits; // The bits we care about
static bitset<32> checkBits; // The previous value, as a sanity check
void handleInterrupt() {
static unsigned int duration;
static unsigned int changeCount;
static unsigned long lastTime;
static int lockPassed;
long time = micros();
duration = time - lastTime;
//wait for lock
if (duration > LOCK - (TOLERANCE*10) && duration < LOCK + (TOLERANCE*10) && lockPassed == 0) {
changeCount = 0;
lockPassed = 1;
bits.reset();
}
else if (lockPassed != 0) { // REST OF DATA
// We're done! Print the packet
if (changeCount >= MAX_CHANGE_TO_KEEP || duration > 2000) {
if (changeCount >= MAX_CHANGE_TO_KEEP) {
if (checkBits == bits) {
cout << bits.to_ulong() << '\n';
}
// Copy to sanity check bitset
for (unsigned int i = 0; i < MAX_CHANGE_TO_KEEP; i++) {
checkBits[i] = bits[i];
}
}
lockPassed = 0;
// Otherwise, store the bit
} else {
if ((duration > LOW_PULSE - TOLERANCE && duration < LOW_PULSE + TOLERANCE) || (duration > HIGH_PULSE - TOLERANCE && duration < HIGH_PULSE + TOLERANCE) ) {
// HIGH
if (digitalRead(1) == 0 && (duration > HIGH_PULSE - TOLERANCE && duration < HIGH_PULSE + TOLERANCE)) {
bits.set(MAX_CHANGE_TO_KEEP - changeCount++ - 1, true);
}
// LOW
if (digitalRead(1) == 0 && (duration > LOW_PULSE - TOLERANCE && duration < LOW_PULSE + TOLERANCE)) {
bits.set(MAX_CHANGE_TO_KEEP - changeCount++ - 1, false);
}
}
}
}
lastTime = time;
}
int main(void) {
if(wiringPiSetup() == -1)
return 0;
//attach interrupt to changes on the pin
if ( wiringPiISR (1, INT_EDGE_BOTH, &handleInterrupt) < 0 ) {
return 1;
}
//interrupt will handle changes
while(true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment