Skip to content

Instantly share code, notes, and snippets.

@Annon201
Last active May 17, 2018 09:58
Show Gist options
  • Save Annon201/9ff64a029f2a92dc61aefebdea55de3d to your computer and use it in GitHub Desktop.
Save Annon201/9ff64a029f2a92dc61aefebdea55de3d to your computer and use it in GitHub Desktop.
#define CONSERVATIVE
#define BUFSIZE 8
#define pulse() pinMode(ioPin, OUTPUT); delayMicroseconds(16); pinMode(ioPin, INPUT)
#define qPulse() pinMode(ioPin, OUTPUT); delayMicroseconds(8); pinMode(ioPin, INPUT)
#define ioPin 3
unsigned char dynBits[] =
{
B10100010,
B01100010,
B00110100,
B10111100,
B00111000,
B11101111,
B11111100,
B00000000
};
const PROGMEM unsigned char bits[] =
{
B10100011,
B11100000,
B00100001,
B00010011,
B00000000
};
const PROGMEM unsigned char bits2[] =
{
B10011010,
B11110010,
B10000000
};
const PROGMEM unsigned char mask = B10111011;
bool bval = LOW;
unsigned char buf[BUFSIZE];
void bitBang(byte index)
{
char currByte;
byte arraySize[] = {7, 4, 2};
for (byte i=0; i<40; ++i) {
pulse();
delayMicroseconds(190);
}
for (byte i=0; i<arraySize[index]; ++i) {
switch(index) {
case 0:
currByte = pgm_read_byte_near(bits[i]);
break;
case 1:
currByte = pgm_read_byte_near(bits2[i]);
break;
case 2:
currByte = dynBits[i];
break;
default:
break;
}
for (byte j=0;j<8;++j) {
if (currByte & (1<<j)) {
pulse();
delayMicroseconds(56);
pulse();
delayMicroseconds(112);
} else {
pulse();
delayMicroseconds(186);
}
}
}
if (index == 2) {
sendLow(3);
}
pulse();
}
void wentLow() {
bval = HIGH;
}
void sendLow(byte count) {
for (byte i=0; i<count; ++i) {
for (byte j=0; j<44; ++j) {
pulse();
delayMicroseconds(186);
}
}
}
void setup() {}
void loop() {
pinMode(ioPin, INPUT);
digitalWrite(ioPin, LOW);
pinMode(ioPin, OUTPUT);
digitalWrite(ioPin, LOW);
bitBang(0);
bval = LOW;
attachInterrupt(1, wentLow, FALLING);
unsigned int i;
for (i=0; i<32767; i++) {
if (digitalRead(ioPin) == LOW) break;
}
if (i == 32767) return;
delayMicroseconds(20);
for (byte i=0; i<BUFSIZE; ++i) buf[i] = 0;
for (byte i=0; i<BUFSIZE; ++i) {
for (byte j=0; j<7; ++j) {
qPulse();
bval = LOW;
delayMicroseconds(184);
if ((((i+1)*(j+1)) > 21) && (((i+1)*(j+1)) < 56)) {
buf[i] ^= bval && (1<<j);
}
}
}
byte checksum;
checksum = (dynBits[1] && (1<<2)) ^ (dynBits[2] && (1<<3)) ^ (dynBits[3] && (1<<4)) ^ (dynBits[4] && (1<<5));
checksum ^= mask;
dynBits[5] &= 0xF0;
dynBits[5] |= (checksum >> 4);
dynBits[6] &= 0x0F;
dynBits[6] |= (checksum << 4);
#ifdef CONSERVATIVE
delay(100);
#endif
bitBang(2);
#ifdef CONSERVATIVE
delayMicroseconds(2700);
#else
delayMicroseconds(500);
#endif
sendLow(9);
bitBang(1);
#ifdef CONSERVATIVE
delayMicroseconds(2700);
#else
delayMicroseconds(500);
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment