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.

129 lines
2.0 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;
uint16_t exptime;
uint16_t explong;
uint16_t longtime;
uint16_t logtime;
uint8_t logging;
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 *)8);
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(uint8_t channel, uint8_t count, uint8_t flag)
{
if(channel == 0)
PORTD ^= (1<<ExpLED);
}
int main(void)
{
uint16_t now;
uint8_t state;
DDRD |= (1<<ExpLED);
DDRD &= ~(1<<ExpButton);
PORTD &= ~(1<<ExpLED);
PORTD |= (1<<ExpButton);
expled = 0;
expbutton = 0;
exptime = 0;
explong = 0;
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 (bit_is_set(PIND, ExpButton)) {
if (exptime) {
if (now - exptime > 50 && !explong) {
expbutton++;
state = 2;
}
if (now - exptime > 50 && explong)
state = 0;
exptime = 0;
}
}
if (state != 255)
if (hbw_send_key(1, expbutton, state))
state = 255;
}
}