avoid sending user command directly if manny user commands are issued in a short period of time

This commit is contained in:
2022-03-03 16:46:37 +01:00
parent ab2767af18
commit 395c9f3e2c
5 changed files with 31 additions and 26 deletions

8
item.h
View File

@ -31,13 +31,15 @@ protected:
uint16_t packetAddDirection();
uint16_t packetAddFunction(const uint8_t function);
uint16_t assemblePacket();
public:
inline static bool directSendBlock = false;
static void setOutput(const uint8_t state);
Item(const uint8_t address);
void sendRaw(const uint16_t data);
static void sendRawAddr(const uint8_t address, const uint16_t data);

View File

@ -29,12 +29,10 @@ SVector<Signal, 32> signals;
bool autoff = true;
bool powerIsOn = true;
#define TICK_MAX 255
volatile uint8_t tick = 0;
volatile bool resendEvent = false;
constexpr bool USE_PULSES = false;
uint8_t itemToResend = 0;
void setPower(bool on);
void save_state();
@ -45,15 +43,9 @@ void save_state();
ISR(TIMER0_OVF_vect)
{
++tick;
if(tick == 0)
if(tick == 0)
resendEvent = true;
if constexpr(USE_PULSES)
{
if((tick & 0b00000111) < 1)
Train::setOutput(Train::HIGH);
else Train::setOutput(Train::LOW);
}
tick = !tick;
}
void timer0InterruptEnable(const bool enable)
@ -229,6 +221,7 @@ int powerDispatch(char* token, Serial* serial)
void serialDispatch(Serial* serial)
{
static uint8_t lastItemToResend = 0;
if(serial->dataIsWaiting())
{
char buffer[COMMAND_BUFFER_SIZE];
@ -237,6 +230,10 @@ void serialDispatch(Serial* serial)
{
int ret = -1;
char* token = strtok(buffer, " ");
if(lastItemToResend == itemToResend)
Item::directSendBlock = true;
if(strcmp(token, "train") == 0 || strcmp(token, "t") == 0 )
{
token = strtok(NULL, " ");
@ -309,15 +306,18 @@ void serialDispatch(Serial* serial)
}
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')
buffer[i] = ' ';
}
serial->write_p(PSTR("Sucess \""));
serial->write(buffer, length);
serial->write("\"\n");
serial->write("\"\n");*/
}
Item::directSendBlock = false;
}
}
}
@ -325,7 +325,7 @@ void serialDispatch(Serial* serial)
int main()
{
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);
@ -348,11 +348,9 @@ int main()
serial.write_p(PSTR("TrainController v0.5 starting\n"));
uint8_t itemToResend = 0;
while(true)
{
if(resendEvent && (trains.count() > 0 || turnouts.count()))
if(resendEvent && (trains.count() || turnouts.count() || signals.count()))
{
timer0InterruptEnable(false);
if(itemToResend < trains.count())

View File

@ -7,7 +7,8 @@ Signal::Signal(uint8_t address, uint8_t subaddress, uint8_t type): Item(address)
void Signal::setState(uint8_t state)
{
_state = state;
sendData();
if(!directSendBlock)
sendData();
}
uint8_t Signal::getState()

View File

@ -105,7 +105,7 @@ void Train::setSpeed(int8_t speed)
}
sei();
}
else
else if(!directSendBlock)
{
sendData();
}
@ -121,7 +121,8 @@ void Train::stop()
{
_speed = 0;
_function = 0;
sendData();
if(!directSendBlock)
sendData();
}
@ -158,7 +159,8 @@ uint16_t Train::getLastPacket()
void Train::reverse()
{
_direction = !_direction;
sendData();
if(!directSendBlock)
sendData();
}
@ -177,7 +179,8 @@ void Train::sendFunction(const uint8_t function, bool enable)
if(function > 3)
return;
_function = (_function & ~(1 << function)) | (enable << function);
sendData();
if(!directSendBlock)
sendData();
}
uint8_t Train::getQuirks()

View File

@ -7,7 +7,8 @@ Turnout::Turnout(uint8_t address, uint8_t subaddress): Item(address), _subaddres
void Turnout::setDirection(uint8_t direction)
{
_direction = direction;
sendData();
if(!directSendBlock)
sendData();
}
uint8_t Turnout::getDirection()