char get_IR()
{
char IRCodeLookup[30] = "efgd`c'5)69-8(‹zr";
char IRNames[30] = "123456789*0#ULPRD";
char tmp = '\0';
if (IRResult > 0) // Result available
{
IRMode = 2;
int qq = IRResult >> 16; // Check for & remove preamble
if (qq == 0x40FF || qq == 0x20FF)
{
byte result = (IRResult & 0x0000FF00) >> 8;
result = bit_reverse(result);
result += 32; // shift into visible characters
char* r2 = strchr(IRCodeLookup, result);
if (r2 != NULL)
{
tmp = IRNames[r2 - IRCodeLookup];
if (key_click) tone(13, 600, 2);
}
}
IRResult = 0;
IRMode = 1;
}
return (tmp);
}
void ISR_IR()
// Interrupt Service Routine for IR Remote.
{
if (IRMode != 2)
{
if (IRMode == 0 && IRCount < 32)
{
int q = digitalRead(PIN_IR);
long dly = micros() - micro_interval;
micro_interval = micros();
if (dly > 5000)
{
IRMode = 1; // Result available
}
else if (dly > 1500)
{
IRBuf <<= 1;
IRBuf += 1;
}
else if (dly > 500)
{
IRBuf <<= 1;
}
if (IRMode == 0)
IRCount++;
}
else
{ // All done, so make result available and await next code
IRResult = IRBuf;
IRBuf = 0;
IRCount = 0;
IRMode = 0;
}
}
}
void setup()
...
attachInterrupt(digitalPinToInterrupt(2), ISR_IR, FALLING);
...
long micro_interval = micros();
unsigned long IRBuf = 0;
unsigned long IRResult = 0;
int IRMode = 0;
int IRCount = 0;
© Copyright 2017 WebinaWeek ltd. trading as The Imagino Project, 14/ME10 2GE
Callers by appointment only.
0800 612 8875