Skip to content

@tadzik /gist:3745164
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#define IRpin_PIN PIND
#define IRpin 2
// the maximum pulse we'll listen for - 65 milliseconds is a long time
#define MAXPULSE 6500
// what our timing resolution should be, larger is better
// as its more 'precise' - but too large and you wont get
// accurate timing
#define RESOLUTION 20
// we will store up to 100 pulse pairs (this is -a lot-)
uint16_t pulses[100][2]; // pair is high and low pulse
uint8_t currentpulse = 0; // index for pulses we're storing
void setup(void) {
Serial.begin(9600);
Serial.println("ready");
}
void loop(void) {
uint16_t highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
//while (digitalRead(IRpin)) { // this is too slow!
while (IRpin_PIN & (1 << IRpin)) {
// pin is still HIGH
// count off another few microseconds
highpulse++;
delayMicroseconds(RESOLUTION);
// If the pulse is too long, we 'timed out' - either nothing
// was received or the code is finished, so print what
// we've grabbed so far, and then reset
if ((highpulse >= MAXPULSE) && (currentpulse != 0)) {
handlesignal();
currentpulse=0;
return;
}
}
// we didn't time out so lets stash the reading
pulses[currentpulse][0] = highpulse;
//Serial.print(highpulse);
//Serial.print(" ");
//while (!digitalRead(IRpin)) { // this is too slow!
while (! (IRpin_PIN & _BV(IRpin))) {
// pin is still LOW
lowpulse++;
delayMicroseconds(RESOLUTION);
if ((lowpulse >= MAXPULSE) && (currentpulse != 0)) {
handlesignal();
currentpulse=0;
return;
}
}
pulses[currentpulse][1] = lowpulse;
//Serial.println(lowpulse);
// we read one high-low pulse successfully, continue!
currentpulse++;
if (currentpulse >= 100) currentpulse = 0;
//Serial.println(currentpulse);
}
void printpulses(void) {
/*Serial.println("\n\r\n\rReceived: \n\rOFF \tON");
for (uint8_t i = 0; i < currentpulse; i++) {
Serial.print(pulses[i][0] * RESOLUTION, DEC);
Serial.print(" usec, ");
Serial.print(pulses[i][1] * RESOLUTION, DEC);
Serial.println(" usec");
}*/
// print it in a 'array' format
Serial.println("[");
for (uint8_t i = 0; i < currentpulse-1; i++) {
uint16_t high = pulses[i][1] * RESOLUTION / 100;
uint16_t low = pulses[i+1][0] * RESOLUTION / 100;
int h = (int)high / 6;
int l = (int)low / 6;
Serial.print("\t");
Serial.print(h);
Serial.println(",");
}
Serial.print("]");
}
int next[] = { 2, 1, 1, 1 };
int pause[] = { 2, 1, 1, 2 };
int prev[] = { 1, 1, 1, 1 };
int stop[] = { 1, 1, 1, 2 };
int play[] = { 1, 2, 1, 1 };
// transform IR shit to smallest sensemaking number possible
int f(uint16_t x) {
return (int)(x * RESOLUTION / 100) / 6;
}
int high(int x) {
return f(pulses[x][1]);
}
void handlesignal(void) {
sendhighpulses();
}
void sendhighpulses() {
for (int i = 0; i < 5; i++)
Serial.print(high(i));
Serial.println("");
}
void handlesignal_text(void)
{
int i; // position of leading 4
for (i = 0; i < currentpulse - 1; i++) {
if (f(pulses[i][1]) == 4) break;
}
if (i == currentpulse - 1) return;
i++;
if (high(i) == 2) {
// pause or next
i += 3;
if (high(i) == 2)
Serial.println("pause");
else if (high(i) == 1)
Serial.println("next");
} else if (high(i) == 1) {
i++;
if (high(i) == 2) {
// probably play
Serial.println("play");
} else if (high(i) == 1) {
// prev or stop
i += 2;
if (high(i) == 1) {
Serial.println("prev");
} else if (high(i) == 2) {
Serial.println("stop");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.