Only act if 2 packets with the same data are recived
This commit is contained in:
		
							parent
							
								
									783fc72004
								
							
						
					
					
						commit
						7377a6081c
					
				
					 1 changed files with 46 additions and 13 deletions
				
			
		
							
								
								
									
										59
									
								
								main.cpp
									
										
									
									
									
								
							
							
						
						
									
										59
									
								
								main.cpp
									
										
									
									
									
								
							| 
						 | 
					@ -14,26 +14,56 @@ static constexpr uint8_t T1_B = PB4;
 | 
				
			||||||
static volatile bool turnout0Flag = false;
 | 
					static volatile bool turnout0Flag = false;
 | 
				
			||||||
static volatile bool turnout1Flag = false;
 | 
					static volatile bool turnout1Flag = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					uint16_t oldData = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					constexpr bool debug = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void handler(uint16_t data, void *user_data)
 | 
					void handler(uint16_t data, void *user_data)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	(void)user_data;
 | 
						(void)user_data;
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	if(((bool)(data & (1 << 9))) != ((bool)(data & (1 << 8))) ||
 | 
						if constexpr(debug)
 | 
				
			||||||
	   ((bool)(data & (1 << 7))) != ((bool)(data & (1 << 6))) ||
 | 
						{
 | 
				
			||||||
	   ((bool)(data & (1 << 5))) != ((bool)(data & (1 << 4))))
 | 
							writePin(&PORTB, T0_A, false);
 | 
				
			||||||
		return;
 | 
							_delay_ms(1);
 | 
				
			||||||
	bool set = data & (1 << 9);
 | 
							for(uint8_t i = 0; i < 10; ++i)
 | 
				
			||||||
	if(data & (1 << 7))
 | 
							{
 | 
				
			||||||
		turnout0Flag = set;
 | 
								writePin(&PORTB, T0_A, true);
 | 
				
			||||||
	if(data & (1 << 5))
 | 
								_delay_us(25);
 | 
				
			||||||
		turnout1Flag = set;
 | 
								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)
 | 
					static void setTurnout0(bool in)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	writePin(&PORTB, T0_A, in);
 | 
						writePin(&PORTB, T0_A, in);
 | 
				
			||||||
	writePin(&PORTB, T0_B, !in);
 | 
						writePin(&PORTB, T0_B, !in);
 | 
				
			||||||
	_delay_ms(50);
 | 
						_delay_ms(150);
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	writePin(&PORTB, T0_A, false);
 | 
						writePin(&PORTB, T0_A, false);
 | 
				
			||||||
	writePin(&PORTB, T0_B, false);
 | 
						writePin(&PORTB, T0_B, false);
 | 
				
			||||||
| 
						 | 
					@ -44,13 +74,13 @@ static void setTurnout1(bool in)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	writePin(&PORTB, T1_A, in);
 | 
						writePin(&PORTB, T1_A, in);
 | 
				
			||||||
	writePin(&PORTB, T1_B, !in);
 | 
						writePin(&PORTB, T1_B, !in);
 | 
				
			||||||
	_delay_ms(50);
 | 
						_delay_ms(150);
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	writePin(&PORTB, T1_A, false);
 | 
						writePin(&PORTB, T1_A, false);
 | 
				
			||||||
	writePin(&PORTB, T1_B, 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)
 | 
					ISR(TIMER1_OVF_vect)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
| 
						 | 
					@ -59,13 +89,16 @@ ISR(TIMER1_OVF_vect)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ISR(INT0_vect)
 | 
					ISR(INT0_vect)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						if constexpr(debug)
 | 
				
			||||||
 | 
						{
 | 
				
			||||||
 | 
							writePin(&PORTB, T1_A, !readPin(&PORTB, T1_A));
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
    decoder.interrupt();
 | 
					    decoder.interrupt();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int main()
 | 
					int main()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	DDRB = (1 << PB0) | (1 << PB1) | (1 << PB3) | (1 << PB4);
 | 
						DDRB = (1 << PB0) | (1 << PB1) | (1 << PB3) | (1 << PB4);
 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	bool turnout0 = turnout0Flag;
 | 
						bool turnout0 = turnout0Flag;
 | 
				
			||||||
	bool turnout1 = turnout1Flag;
 | 
						bool turnout1 = turnout1Flag;
 | 
				
			||||||
	_delay_ms(100);
 | 
						_delay_ms(100);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue