From 7377a6081c647deb99d6d337bca5f7d5af6f869d Mon Sep 17 00:00:00 2001 From: uvos Date: Thu, 27 Jan 2022 20:14:44 +0100 Subject: [PATCH] Only act if 2 packets with the same data are recived --- main.cpp | 59 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/main.cpp b/main.cpp index 6665041..2d944c4 100644 --- a/main.cpp +++ b/main.cpp @@ -14,26 +14,56 @@ static constexpr uint8_t T1_B = PB4; static volatile bool turnout0Flag = false; static volatile bool turnout1Flag = false; +uint16_t oldData = 0; + +constexpr bool debug = false; + void handler(uint16_t data, void *user_data) { (void)user_data; - if(((bool)(data & (1 << 9))) != ((bool)(data & (1 << 8))) || - ((bool)(data & (1 << 7))) != ((bool)(data & (1 << 6))) || - ((bool)(data & (1 << 5))) != ((bool)(data & (1 << 4)))) - return; - bool set = data & (1 << 9); - if(data & (1 << 7)) - turnout0Flag = set; - if(data & (1 << 5)) - turnout1Flag = set; + if constexpr(debug) + { + writePin(&PORTB, T0_A, false); + _delay_ms(1); + for(uint8_t i = 0; i < 10; ++i) + { + writePin(&PORTB, T0_A, true); + _delay_us(25); + if(data & (1 << (9-i))) + _delay_us(75); + writePin(&PORTB, T0_A, false); + _delay_us(25); + if(!(data & (1 << (9-i)))) + _delay_us(75); + } + + } + + if(data == oldData) + { + if(((bool)(data & (1 << 9))) != ((bool)(data & (1 << 8))) || + ((bool)(data & (1 << 7))) != ((bool)(data & (1 << 6))) || + ((bool)(data & (1 << 5))) != ((bool)(data & (1 << 4)))) + return; + bool set = data & (1 << 9); + + if(data & (1 << 7)) + turnout0Flag = set; + if(data & (1 << 5)) + turnout1Flag = set; + } + else + { + oldData = data; + } } static void setTurnout0(bool in) { writePin(&PORTB, T0_A, in); writePin(&PORTB, T0_B, !in); - _delay_ms(50); + _delay_ms(150); writePin(&PORTB, T0_A, false); writePin(&PORTB, T0_B, false); @@ -44,13 +74,13 @@ static void setTurnout1(bool in) { writePin(&PORTB, T1_A, in); writePin(&PORTB, T1_B, !in); - _delay_ms(50); + _delay_ms(150); writePin(&PORTB, T1_A, false); writePin(&PORTB, T1_B, false); } -Decoder decoder(&PINB, PB2, &TCNT1, &handler, nullptr, 0x02); +Decoder decoder(&PINB, PB2, &TCNT1, &handler, nullptr, 0x01); ISR(TIMER1_OVF_vect) { @@ -59,13 +89,16 @@ ISR(TIMER1_OVF_vect) ISR(INT0_vect) { + if constexpr(debug) + { + writePin(&PORTB, T1_A, !readPin(&PORTB, T1_A)); + } decoder.interrupt(); } int main() { DDRB = (1 << PB0) | (1 << PB1) | (1 << PB3) | (1 << PB4); - bool turnout0 = turnout0Flag; bool turnout1 = turnout1Flag; _delay_ms(100);