Attiny85 code

This commit is contained in:
Marvin Martinson 2019-05-09 23:27:46 +03:00
parent 25bc1ebd44
commit 153426f1c2
2 changed files with 205 additions and 0 deletions

123
attiny85/ir_reciver.c Normal file
View File

@ -0,0 +1,123 @@
/*
* 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>
/* 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);
}
}

82
attiny85/ir_sender.c Normal file
View File

@ -0,0 +1,82 @@
/*
* 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;
}
}