diff --git a/item.h b/item.h index a336c28..a62bcd6 100644 --- a/item.h +++ b/item.h @@ -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); diff --git a/main.cpp b/main.cpp index b87633c..dd1629c 100644 --- a/main.cpp +++ b/main.cpp @@ -29,12 +29,10 @@ SVector 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< 0 || turnouts.count())) + if(resendEvent && (trains.count() || turnouts.count() || signals.count())) { timer0InterruptEnable(false); if(itemToResend < trains.count()) diff --git a/signal.cpp b/signal.cpp index ab4a76a..7387ff6 100644 --- a/signal.cpp +++ b/signal.cpp @@ -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() diff --git a/train.cpp b/train.cpp index 66ce359..7630a37 100644 --- a/train.cpp +++ b/train.cpp @@ -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() diff --git a/turnout.cpp b/turnout.cpp index 21f137b..f21d005 100644 --- a/turnout.cpp +++ b/turnout.cpp @@ -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()