83 lines
1.4 KiB
C
83 lines
1.4 KiB
C
/*
|
|
* 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 <Arduino.h>
|
|
/*
|
|
* We need to remove some all send functions, and some protocols to save space.
|
|
*/
|
|
#include <tiny_IRremote.h>
|
|
#include <TinyWireS.h>
|
|
#include <avr/wdt.h>
|
|
#include <avr/io.h>
|
|
#include <avr/interrupt.h>
|
|
|
|
|
|
#define I2C_ADDR 0x20
|
|
|
|
//REGISTER
|
|
#define I2C_SEND_IR 0x01
|
|
#define I2C_REG_RESET 0x05
|
|
|
|
|
|
#define PIN_INT 4
|
|
|
|
IRsend irsend;
|
|
|
|
volatile boolean shoot = false;
|
|
volatile uint8_t code;
|
|
|
|
|
|
|
|
volatile uint8_t reg, opt;
|
|
|
|
|
|
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_RESET) {
|
|
/* Wait for watchdog to reset us */
|
|
wdt_enable(WDTO_15MS);
|
|
while(1) ;
|
|
}
|
|
|
|
if(reg == I2C_SEND_IR) {
|
|
|
|
shoot = true;
|
|
code = opt;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void setup() {
|
|
pinMode(4, OUTPUT);
|
|
TinyWireS.begin(I2C_ADDR);
|
|
TinyWireS.onReceive(receiveEvent);
|
|
}
|
|
|
|
void loop() {
|
|
// put your main code here, to run repeatedly:
|
|
TinyWireS_stop_check();
|
|
if(shoot) {
|
|
irsend.sendNEC(code, 32);
|
|
shoot = false;
|
|
}
|
|
}
|