avoid sending user command directly if manny user commands are issued in a short period of time
This commit is contained in:
		
							parent
							
								
									ab2767af18
								
							
						
					
					
						commit
						395c9f3e2c
					
				
					 5 changed files with 31 additions and 26 deletions
				
			
		
							
								
								
									
										2
									
								
								item.h
									
										
									
									
									
								
							
							
						
						
									
										2
									
								
								item.h
									
										
									
									
									
								
							| 
						 | 
					@ -34,6 +34,8 @@ protected:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public:
 | 
					public:
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
 | 
						inline static bool directSendBlock = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	static void setOutput(const uint8_t state);
 | 
						static void setOutput(const uint8_t state);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	Item(const uint8_t address);
 | 
						Item(const uint8_t address);
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										32
									
								
								main.cpp
									
										
									
									
									
								
							
							
						
						
									
										32
									
								
								main.cpp
									
										
									
									
									
								
							| 
						 | 
					@ -29,12 +29,10 @@ SVector<Signal, 32> signals;
 | 
				
			||||||
bool autoff = true;
 | 
					bool autoff = true;
 | 
				
			||||||
bool powerIsOn = true;
 | 
					bool powerIsOn = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#define TICK_MAX 255
 | 
					 | 
				
			||||||
volatile uint8_t tick = 0;
 | 
					volatile uint8_t tick = 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
volatile bool resendEvent = false;
 | 
					volatile bool resendEvent = false;
 | 
				
			||||||
 | 
					uint8_t itemToResend = 0;
 | 
				
			||||||
constexpr bool USE_PULSES = false;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
void setPower(bool on);
 | 
					void setPower(bool on);
 | 
				
			||||||
void save_state();
 | 
					void save_state();
 | 
				
			||||||
| 
						 | 
					@ -45,15 +43,9 @@ void save_state();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
ISR(TIMER0_OVF_vect) 
 | 
					ISR(TIMER0_OVF_vect) 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	++tick;
 | 
						if(tick == 0)
 | 
				
			||||||
	if(tick  == 0)
 | 
					 | 
				
			||||||
		resendEvent = true;
 | 
							resendEvent = true;
 | 
				
			||||||
	if constexpr(USE_PULSES)
 | 
						tick = !tick;
 | 
				
			||||||
	{
 | 
					 | 
				
			||||||
		if((tick & 0b00000111) < 1)
 | 
					 | 
				
			||||||
			Train::setOutput(Train::HIGH);
 | 
					 | 
				
			||||||
		else Train::setOutput(Train::LOW);
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void timer0InterruptEnable(const bool enable)
 | 
					void timer0InterruptEnable(const bool enable)
 | 
				
			||||||
| 
						 | 
					@ -229,6 +221,7 @@ int powerDispatch(char* token, Serial* serial)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void serialDispatch(Serial* serial)
 | 
					void serialDispatch(Serial* serial)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
 | 
						static uint8_t lastItemToResend = 0;
 | 
				
			||||||
	if(serial->dataIsWaiting())
 | 
						if(serial->dataIsWaiting())
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		char buffer[COMMAND_BUFFER_SIZE];
 | 
							char buffer[COMMAND_BUFFER_SIZE];
 | 
				
			||||||
| 
						 | 
					@ -237,6 +230,10 @@ void serialDispatch(Serial* serial)
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			int ret = -1;
 | 
								int ret = -1;
 | 
				
			||||||
			char* token = strtok(buffer, " ");
 | 
								char* token = strtok(buffer, " ");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								if(lastItemToResend == itemToResend)
 | 
				
			||||||
 | 
									Item::directSendBlock = true;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if(strcmp(token, "train") == 0 || strcmp(token, "t") == 0 )
 | 
								if(strcmp(token, "train") == 0 || strcmp(token, "t") == 0 )
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				token = strtok(NULL, " ");
 | 
									token = strtok(NULL, " ");
 | 
				
			||||||
| 
						 | 
					@ -309,15 +306,18 @@ void serialDispatch(Serial* serial)
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			else
 | 
								else
 | 
				
			||||||
			{
 | 
								{
 | 
				
			||||||
				for(uint8_t i = 0; i < length; ++i)
 | 
									lastItemToResend = itemToResend;
 | 
				
			||||||
 | 
									/*for(uint8_t i = 0; i < length; ++i)
 | 
				
			||||||
				{
 | 
									{
 | 
				
			||||||
					if(buffer[i] == '\0' || buffer[i] == '\n')
 | 
										if(buffer[i] == '\0' || buffer[i] == '\n')
 | 
				
			||||||
						buffer[i] = ' ';
 | 
											buffer[i] = ' ';
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				serial->write_p(PSTR("Sucess \""));
 | 
									serial->write_p(PSTR("Sucess \""));
 | 
				
			||||||
				serial->write(buffer, length);
 | 
									serial->write(buffer, length);
 | 
				
			||||||
				serial->write("\"\n");
 | 
									serial->write("\"\n");*/
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
								Item::directSendBlock = false;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -325,7 +325,7 @@ void serialDispatch(Serial* serial)
 | 
				
			||||||
int main() 
 | 
					int main() 
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	TCNT0 = 0;
 | 
						TCNT0 = 0;
 | 
				
			||||||
	TCCR0B = (1<<CS00); // run timer0 with /1 scaler
 | 
						TCCR0B = (1<<CS02); // run timer0 with /256 scaler
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	DDRD =  (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5);
 | 
						DDRD =  (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -348,11 +348,9 @@ int main()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	serial.write_p(PSTR("TrainController v0.5 starting\n"));
 | 
						serial.write_p(PSTR("TrainController v0.5 starting\n"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	uint8_t itemToResend = 0;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	while(true)
 | 
						while(true)
 | 
				
			||||||
	{        
 | 
						{        
 | 
				
			||||||
		if(resendEvent && (trains.count() > 0 || turnouts.count())) 
 | 
							if(resendEvent && (trains.count() || turnouts.count() || signals.count())) 
 | 
				
			||||||
		{
 | 
							{
 | 
				
			||||||
			timer0InterruptEnable(false);
 | 
								timer0InterruptEnable(false);
 | 
				
			||||||
			if(itemToResend < trains.count())
 | 
								if(itemToResend < trains.count())
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,8 @@ Signal::Signal(uint8_t address, uint8_t subaddress, uint8_t type): Item(address)
 | 
				
			||||||
void Signal::setState(uint8_t state)
 | 
					void Signal::setState(uint8_t state)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	_state = state;
 | 
						_state = state;
 | 
				
			||||||
	sendData();
 | 
						if(!directSendBlock)
 | 
				
			||||||
 | 
							sendData();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t Signal::getState()
 | 
					uint8_t Signal::getState()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										11
									
								
								train.cpp
									
										
									
									
									
								
							
							
						
						
									
										11
									
								
								train.cpp
									
										
									
									
									
								
							| 
						 | 
					@ -105,7 +105,7 @@ void Train::setSpeed(int8_t speed)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		sei();
 | 
							sei();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	else
 | 
						else if(!directSendBlock)
 | 
				
			||||||
	{
 | 
						{
 | 
				
			||||||
		sendData();
 | 
							sendData();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					@ -121,7 +121,8 @@ void Train::stop()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	_speed = 0;
 | 
						_speed = 0;
 | 
				
			||||||
	_function = 0;
 | 
						_function = 0;
 | 
				
			||||||
	sendData();
 | 
						if(!directSendBlock)
 | 
				
			||||||
 | 
							sendData();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -158,7 +159,8 @@ uint16_t Train::getLastPacket()
 | 
				
			||||||
void Train::reverse()
 | 
					void Train::reverse()
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	_direction = !_direction;
 | 
						_direction = !_direction;
 | 
				
			||||||
	sendData();
 | 
						if(!directSendBlock)
 | 
				
			||||||
 | 
							sendData();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -177,7 +179,8 @@ void Train::sendFunction(const uint8_t function, bool enable)
 | 
				
			||||||
	if(function > 3)
 | 
						if(function > 3)
 | 
				
			||||||
		return;
 | 
							return;
 | 
				
			||||||
	_function = (_function & ~(1 << function)) | (enable << function);
 | 
						_function = (_function & ~(1 << function)) | (enable << function);
 | 
				
			||||||
	sendData();
 | 
						if(!directSendBlock)
 | 
				
			||||||
 | 
							sendData();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t Train::getQuirks()
 | 
					uint8_t Train::getQuirks()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -7,7 +7,8 @@ Turnout::Turnout(uint8_t address, uint8_t subaddress): Item(address), _subaddres
 | 
				
			||||||
void Turnout::setDirection(uint8_t direction)
 | 
					void Turnout::setDirection(uint8_t direction)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	_direction = direction;
 | 
						_direction = direction;
 | 
				
			||||||
	sendData();
 | 
						if(!directSendBlock)
 | 
				
			||||||
 | 
							sendData();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
uint8_t Turnout::getDirection()
 | 
					uint8_t Turnout::getDirection()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue