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.

194 lines
3.7 KiB
C

#include "hw.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
#include "hbw.h"
uint8_t expled;
uint8_t expbutton;
uint8_t expdouble;
uint16_t exptime;
uint16_t explong;
uint16_t longtime;
uint16_t logtime;
uint16_t logtimer;
uint8_t logging;
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);
logging = ee & 0x01;
ee = eeprom_read_byte((const uint8_t *)7);
if (ee == 0xFF)
longtime = 1000;
else
longtime = 100L * ee;
}
uint8_t hbw_get_channel(uint8_t channel, uint8_t data[])
{
if (channel == 1) {
if (bit_is_clear(PIND, ExpButton))
data[0] = 200;
else
data[0] = 0;
return 1;
}
if (channel == 0) {
if (bit_is_clear(PIND, ExpLED))
data[0] = 0;
else
data[0] = 200;
return 1;
}
return 0;
}
void hbw_set_channel(uint8_t channel, uint8_t len, uint8_t data[])
{
if (channel == 0) {
if (data[0])
PORTD |= (1<<ExpLED);
else
PORTD &= ~(1<<ExpLED);
}
}
void hbw_receive_key(uint32_t saddress, uint8_t schannel, uint8_t channel, uint8_t countflag)
{
uint8_t i, type;
if (channel != 0)
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:
PORTD |= (1<<ExpLED);
break;
case 1:
PORTD &= ~(1<<ExpLED);
break;
case 3:
PORTD ^= (1<<ExpLED);
}
if (logging) {
logtimer = hbw_timer + logtime;
if (!logtimer) logtimer = 1;
}
return;
}
}
}
int main(void)
{
uint16_t now;
uint8_t state;
uint8_t led;
DDRD |= (1<<ExpLED);
DDRD &= ~(1<<ExpButton);
PORTD &= ~(1<<ExpLED);
PORTD |= (1<<ExpButton);
hbw_init();
state = 255;
while(1) {
hbw_loop();
now = hbw_timer?hbw_timer:1;
if (bit_is_clear(PIND, ExpButton)) {
if (exptime) {
if (explong) {
if (now - explong >= 300) {
explong = now;
state = 3;
}
} else {
if (now - exptime > longtime) {
explong = now;
expbutton++;
state = 3;
}
}
} else {
exptime = now;
explong = 0;
if (expdouble == 1)
expdouble = 2;
}
}
if (bit_is_set(PIND, ExpButton)) {
if (exptime) {
if (now - exptime > 50 && !explong && (expdouble == 2)) {
expbutton++;
state = 1;
expdouble = 0;
}
if (now - exptime > 300 && !explong && (expdouble == 1)) {
expbutton++;
state = 2;
expdouble = 0;
}
if (now - exptime > 50 && !explong && (expdouble == 0))
expdouble = 1;
if (now - exptime > 50 && explong) {
state = 0;
expdouble = 0;
}
exptime = 0;
}
}
if (state != 255)
if (hbw_send_key(1, expbutton, state))
state = 255;
if (logtimer && (hbw_timer - logtimer < 100)) {
led = 0;
if (bit_is_set(PIND, ExpLED))
led = 200;
if (!hbw_send_channel(0, 1, &led))
logtimer += 300;
else
logtimer = 0;
}
}
}