You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

310 lines
6.5 KiB
C

7 years ago
#include "hw.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "hbw.h"
uint8_t in0state;
uint8_t in1state;
uint8_t in2state;
uint8_t in3state;
uint8_t in4state;
uint8_t in5state;
uint8_t scount;
uint16_t logtime;
uint8_t logging0;
uint8_t logging1;
uint16_t logtimer0;
uint16_t logtimer1;
uint8_t scount;
void hbw_read_config(void)
{
uint8_t ee;
ee = eeprom_read_byte((const uint8_t *)1);
logtime = 100L * ee;
ee = eeprom_read_byte((const uint8_t *)6);
logging0 = ee & 0x01;
ee = eeprom_read_byte((const uint8_t *)8);
logging1 = ee & 0x01;
}
uint8_t hbw_get_channel(uint8_t channel, uint8_t data[])
{
if (channel == 0) {
if (bit_is_clear(PINB, RedLED))
data[0] = 0;
else
data[0] = 200;
return 1;
}
if (channel == 1) {
if (bit_is_clear(PINB, GreenLED))
data[0] = 0;
else
data[0] = 200;
return 1;
}
if (channel == 2) {
if (bit_is_clear(PINC, In0))
data[0] = 200;
else
data[0] = 0;
return 1;
}
if (channel == 3) {
if (bit_is_clear(PINC, In1))
data[0] = 200;
else
data[0] = 0;
return 1;
}
if (channel == 4) {
if (bit_is_clear(PINC, In2))
data[0] = 200;
else
data[0] = 0;
return 1;
}
if (channel == 5) {
if (bit_is_clear(PINC, In3))
data[0] = 200;
else
data[0] = 0;
return 1;
}
if (channel == 6) {
if (bit_is_clear(PINC, In4))
data[0] = 200;
else
data[0] = 0;
return 1;
}
if (channel == 7) {
if (bit_is_clear(PINC, In5))
data[0] = 200;
else
data[0] = 0;
return 1;
}
return 0;
}
void hbw_set_channel(uint8_t channel, uint8_t len, uint8_t data[])
{
if (channel == 0) {
if (data[0])
PORTB |= (1<<RedLED);
else
PORTB &= ~(1<<RedLED);
}
if (channel == 1) {
if (data[0])
PORTB |= (1<<GreenLED);
else
PORTB &= ~(1<<GreenLED);
}
}
void hbw_receive_key(uint32_t saddress, uint8_t schannel, uint8_t channel, uint8_t countflag)
{
uint8_t i, type;
if (channel > 1)
return;
if (scount == countflag)
return;
for (i=0; i < 16; i++) {
if ((eeprom_read_byte((const uint8_t *)(i*7)+0x10) == ((uint8_t*)&saddress)[0]) &&
(eeprom_read_byte((const uint8_t *)(i*7)+0x11) == ((uint8_t*)&saddress)[1]) &&
(eeprom_read_byte((const uint8_t *)(i*7)+0x12) == ((uint8_t*)&saddress)[2]) &&
(eeprom_read_byte((const uint8_t *)(i*7)+0x13) == ((uint8_t*)&saddress)[3]) &&
(eeprom_read_byte((const uint8_t *)(i*7)+0x14) == schannel) &&
(eeprom_read_byte((const uint8_t *)(i*7)+0x15) == channel)) {
type = eeprom_read_byte((const uint8_t *)(i*7)+0x16);
switch (countflag & 3) {
case 1:
type = (type >> 6) & 0x3;
break;
case 0:
type = (type >> 4) & 0x3;
break;
case 3:
type = (type >> 2) & 0x3;
break;
case 2:
type = type & 0x3;
}
switch (type) {
case 0:
if(channel == 0)
PORTB |= (1<<RedLED);
else
PORTB |= (1<<GreenLED);
break;
case 1:
if(channel == 0)
PORTB &= ~(1<<RedLED);
else
PORTB &= ~(1<<GreenLED);
break;
case 3:
if(channel == 0)
PORTB ^= (1<<RedLED);
else
PORTB ^= (1<<GreenLED);
}
if ((channel == 0) && logging0) {
logtimer0 = hbw_timer + logtime;
if (!logtimer0) logtimer0 = 1;
}
if ((channel == 1) && logging1) {
logtimer1 = hbw_timer + logtime;
if (!logtimer1) logtimer1 = 1;
}
return;
}
}
}
int main(void)
{
uint8_t state;
DDRB |= (1<<RedLED);
DDRB |= (1<<GreenLED);
DDRC &= ~(1<<In0);
DDRC &= ~(1<<In1);
DDRC &= ~(1<<In2);
DDRC &= ~(1<<In3);
DDRC &= ~(1<<In4);
DDRC &= ~(1<<In5);
PORTB &= ~(1<<RedLED);
PORTB &= ~(1<<GreenLED);
PORTC |= (1<<In0);
PORTC |= (1<<In1);
PORTC |= (1<<In2);
PORTC |= (1<<In3);
PORTC |= (1<<In4);
PORTC |= (1<<In5);
hbw_init();
while(1) {
hbw_loop();
if (bit_is_clear(PINC, In0) && (!in0state)) {
state = 200;
if (hbw_send_channel(2, 1, &state))
in0state = ~0;
}
if (bit_is_set(PINC, In0) && in0state) {
state = 0;
if (hbw_send_channel(2, 1, &state))
in0state = 0;
}
if (bit_is_clear(PINC, In1) && (!in1state)) {
state = 200;
if (hbw_send_channel(3, 1, &state))
in1state = ~0;
}
if (bit_is_set(PINC, In1) && in1state) {
state = 0;
if (hbw_send_channel(3, 1, &state))
in1state = 0;
}
if (bit_is_clear(PINC, In2) && (!in2state)) {
state = 200;
if (hbw_send_channel(4, 1, &state))
in2state = ~0;
}
if (bit_is_set(PINC, In2) && in2state) {
state = 0;
if (hbw_send_channel(4, 1, &state))
in2state = 0;
}
if (bit_is_clear(PINC, In3) && (!in3state)) {
state = 200;
if (hbw_send_channel(5, 1, &state))
in3state = ~0;
}
if (bit_is_set(PINC, In3) && in3state) {
state = 0;
if (hbw_send_channel(5, 1, &state))
in3state = 0;
}
if (bit_is_clear(PINC, In4) && (!in4state)) {
state = 200;
if (hbw_send_channel(6, 1, &state))
in4state = ~0;
}
if (bit_is_set(PINC, In4) && in4state) {
state = 0;
if (hbw_send_channel(6, 1, &state))
in4state = 0;
}
if (bit_is_clear(PINC, In5) && (!in5state)) {
state = 200;
if (hbw_send_channel(7, 1, &state))
in5state = ~0;
}
if (bit_is_set(PINC, In5) && in5state) {
state = 0;
if (hbw_send_channel(7, 1, &state))
in5state = 0;
}
if (hbw_timer - logtimer0 < 100) {
state = 0;
if (bit_is_clear(PINB, RedLED))
state = 200;
if (!hbw_send_channel(0, 1, &state))
logtimer0 += 300;
else
logtimer0 = 0;
}
if (hbw_timer - logtimer1 < 100) {
state = 0;
if (bit_is_clear(PINB, GreenLED))
state = 200;
if (!hbw_send_channel(1, 1, &state))
logtimer1 += 300;
else
logtimer1 = 0;
}
}
}