/* * IR 2 I2C * Since IR decoding can sometimes take too much time, I decided to implement * a decoder on an ATtiny85. It acts as an I2C slave. */ #include /* * We need to remove some all send functions, and some protocols to save space. */ #include #include #include #include #include /* Pin where the IR receiver is connected */ #define PIN_IR 1 /* Length of the buffer (how many codes can be saved before overflowing) */ #define BUF_LEN 4 #define PIN_MOTOR 4 /* * Address of the slave, choose something that doesn't conflict with the rest * of your hardware */ #define I2C_ADDR 0x10 /* You should probably leave the following as-is */ #define I2C_REG_BUFLEN 0x01 #define I2C_REG_READ 0x02 #define I2C_REG_RESET 0x05 #define I2C_REG_LED 0x06 struct result { uint8_t type; uint32_t value; }; struct result buf; IRrecv irrecv(PIN_IR); decode_results prev, res; volatile uint8_t reg, opt; volatile int counter = 0; int state = 0; bool blink = false; void receiveEvent(uint8_t howMany) { if(howMany < 1) return; /* First byte is the register */ reg = TinyWireS.receive(); /* If there is an option, receive it */ if(TinyWireS.available()) opt = TinyWireS.receive(); else opt = 0; /* And we ignore the rest */ while(TinyWireS.available()) (void) TinyWireS.receive(); if(reg == I2C_REG_LED) { if(opt == 0x01) { blink = false; } else if(opt == 0x02) { blink = true; } } } void requestEvent() { if(reg == I2C_REG_READ) { TinyWireS.send(buf.type); TinyWireS.send(buf.value >> 24); TinyWireS.send((buf.value >> 16) & 0xff); TinyWireS.send((buf.value >> 8) & 0xff); TinyWireS.send(buf.value & 0xff); //if (buf.type) { // state = 1; // counter = 10; //} buf.type = 0; buf.value = 0; } } void setup() { pinMode(PIN_IR, INPUT); pinMode(PINB3, OUTPUT); digitalWrite(PIN_MOTOR, HIGH); TinyWireS.begin(I2C_ADDR); TinyWireS.onReceive(receiveEvent); TinyWireS.onRequest(requestEvent); irrecv.enableIRIn(); } void loop() { TinyWireS_stop_check(); if(irrecv.decode(&res)) { if((res.decode_type == NEC) && (res.value == REPEAT)) res = prev; prev = res; if (buf.type == 0 && !blink) { buf.type = res.decode_type; buf.value = res.value; } irrecv.resume(); } if (blink) { state = 1-state; //digitalWrite(PINB3, !state); //counter--; PINB |= _BV(PINB3); tws_delay(500); } else { digitalWrite(PINB3, 0); } }