public
Created

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
#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");
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.