fix train encodeing make, negative idle pulse optional

This commit is contained in:
2022-01-20 19:51:03 +01:00
parent ebd3953755
commit 86c36f7a21
4 changed files with 45 additions and 29 deletions

View File

@ -31,7 +31,7 @@ set(CMAKE_LINKER /usr/bin/avr-ld)
# Compiler flags # Compiler flags
add_definitions(-mmcu=${MCU} -DF_CPU=${CPU_SPEED}) add_definitions(-mmcu=${MCU} -DF_CPU=${CPU_SPEED})
add_definitions(-s -c -g -Os -Wall -std=c++11 ) add_definitions(-s -c -g -Os -Wall -std=c++17 )
add_definitions(-fno-exceptions -ffunction-sections -fdata-sections) add_definitions(-fno-exceptions -ffunction-sections -fdata-sections)
# Linker flags # Linker flags

View File

@ -28,20 +28,27 @@ volatile uint8_t tick = 0;
volatile bool resendEvent = false; volatile bool resendEvent = false;
constexpr bool USE_PULSES = false;
ISR(TIMER0_OVF_vect) ISR(TIMER0_OVF_vect)
{ {
++tick; ++tick;
if(tick == 0) if(tick == 0)
resendEvent = true; resendEvent = true;
if constexpr(USE_PULSES)
{
if((tick & 0b00000111) < 1) if((tick & 0b00000111) < 1)
Train::setOutput(Train::HIGH); Train::setOutput(Train::HIGH);
else Train::setOutput(Train::LOW); else Train::setOutput(Train::LOW);
} }
}
void timer0InterruptEnable(const bool enable) void timer0InterruptEnable(const bool enable)
{ {
if(enable) TIMSK0 = 0b00000001; if(enable)
else TIMSK0 = 0b00000000; TIMSK0 = 0b00000001;
else
TIMSK0 = 0b00000000;
} }
void save_state() void save_state()
@ -109,11 +116,11 @@ int trainDispatch(char* inBuffer, Serial* serial)
if(token != NULL) if(token != NULL)
trains[storedTrains].setFunctionMask(strtol(token, nullptr, 2 )); trains[storedTrains].setFunctionMask(strtol(token, nullptr, 2 ));
uint8_t size = snprintf(buffer, SNPRINTF_BUFFER_SIZE, "TRAIN saved! NUMBER: %u ADDRESS: %u FUNCTIONS: %u FUNCTIONMASK: %u\n", uint8_t size = snprintf(buffer, SNPRINTF_BUFFER_SIZE, "TRAIN saved! NUMBER: %u ADDRESS: %u FUNCTIONS: %s FUNCTIONMASK: %s\n",
storedTrains, storedTrains,
address, address,
trains[storedTrains].getFunctions(), bit_rep[trains[storedTrains].getFunctions() & 0x0F],
trains[storedTrains].getFunctionMask()); bit_rep[trains[storedTrains].getFunctionMask() & 0x0F]);
serial->write(buffer, size); serial->write(buffer, size);
storedTrains++; storedTrains++;
@ -138,10 +145,10 @@ int trainDispatch(char* inBuffer, Serial* serial)
serial->write_p(PSTR("Trains:\n")); serial->write_p(PSTR("Trains:\n"));
for(uint8_t i = 0; i < storedTrains; i++) for(uint8_t i = 0; i < storedTrains; i++)
{ {
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "NUMBER: %u ID: %u CURRENT PACKET: %x SPEED: %i FUNCTIONS: %u FUNCTIONMASK: %u\n", snprintf(buffer, SNPRINTF_BUFFER_SIZE, "NUMBER: %u ID: %u CURRENT PACKET: %x SPEED: %i FUNCTIONS: %s FUNCTIONMASK: %s\n",
i, trains[i].getAddress(), i, trains[i].getAddress(),
trains[i].getLastPacket(), trains[i].getSpeed(), trains[i].getLastPacket(), trains[i].getSpeed(),
trains[i].getFunctions(), trains[i].getFunctionMask()); bit_rep[trains[i].getFunctions() & 0x0F], bit_rep[trains[i].getFunctionMask() & 0x0F]);
serial->write(buffer, SNPRINTF_BUFFER_SIZE); serial->write(buffer, SNPRINTF_BUFFER_SIZE);
} }
serial->putChar('\n'); serial->putChar('\n');
@ -226,7 +233,7 @@ int trainDispatch(char* inBuffer, Serial* serial)
uint16_t i = strtol(token, nullptr, 16 ); uint16_t i = strtol(token, nullptr, 16 );
snprintf(buffer, SNPRINTF_BUFFER_SIZE, "SENDING: %x to %x\n", i, trains[id].getAddress()); snprintf(buffer, SNPRINTF_BUFFER_SIZE, "SENDING: %x to %x\n", i, trains[id].getAddress());
serial->write(buffer, strlen(buffer)); serial->write(buffer, strlen(buffer));
for(uint8_t j = 0; j < 100; j++) //for(uint8_t j = 0; j < 100; j++)
{ {
trains[id].sendRaw(i); trains[id].sendRaw(i);
_delay_ms(20); _delay_ms(20);
@ -392,7 +399,7 @@ void serialDispatch(Serial* serial)
int main() int main()
{ {
TCNT0 = 0; TCNT0 = 0;
TCCR0B = (1<<CS01) /*| (1<<CS00)*/; // run timer0 with /64 scaler TCCR0B = (1<<CS00); // run timer0 with /1 scaler
DDRD = (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5); DDRD = (1 << PD2) | (1 << PD3) | (1 << PD4) | (1 << PD5);
@ -421,9 +428,9 @@ int main()
{ {
if(resendEvent && storedTrains > 0) if(resendEvent && storedTrains > 0)
{ {
//_delay_ms(100);
timer0InterruptEnable(false); timer0InterruptEnable(false);
_delay_us(255); trains[trainToResend].sendData();
trains[trainToResend].resendData();
trainToResend++; trainToResend++;
if(storedTrains <= trainToResend) if(storedTrains <= trainToResend)
trainToResend = 0; trainToResend = 0;

View File

@ -26,7 +26,7 @@ void Train::stop()
{ {
_speed = 0; _speed = 0;
_function = 0; _function = 0;
resendData(); sendData();
} }
void Train::off() void Train::off()
@ -95,7 +95,7 @@ void Train::sendRawAddr(const uint16_t address, const uint16_t data)
{ {
sendBit(data & (1 << i)); sendBit(data & (1 << i));
} }
_delay_ms(2); _delay_ms(1);
} }
} }
@ -173,7 +173,7 @@ uint16_t Train::packetAddFunction(const uint8_t function)
void Train::setSpeed(uint8_t speed) void Train::setSpeed(uint8_t speed)
{ {
_speed = speed; _speed = speed;
resendData(); sendData();
} }
uint8_t Train::getSpeed() uint8_t Train::getSpeed()
@ -187,13 +187,22 @@ uint16_t Train::assemblePacket()
return packet; return packet;
} }
void Train::resendData() void Train::sendData()
{ {
sendRaw(assemblePacket()); sendRaw(assemblePacket());
for(uint8_t i = 1; i < 4; ++i) if(_functionmask)
{ {
if(_functionmask & (1 << i)) uint8_t functionToResend = (_function & 0xF0) >> 4;
sendRaw(packetAddSpeed() | packetAddFunction(i) | packetAddFunction(0)); ++functionToResend;
if (functionToResend == 4)
functionToResend = 1;
if(_functionmask & (1 << functionToResend))
{
_delay_ms(2);
sendRaw(packetAddSpeed() | packetAddFunction(functionToResend) | packetAddFunction(0));
}
_function &= ~0xF0;
_function |= functionToResend << 4;
} }
} }
@ -205,13 +214,13 @@ uint16_t Train::getLastPacket()
void Train::reverse() void Train::reverse()
{ {
_direction = !_direction; _direction = !_direction;
resendData(); sendData();
} }
uint8_t Train::getFunctions() uint8_t Train::getFunctions()
{ {
return _function; return _function & 0x0F;
} }
uint8_t Train::getFunctionMask() uint8_t Train::getFunctionMask()
@ -224,5 +233,5 @@ 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);
resendData(); sendData();
} }

View File

@ -43,13 +43,13 @@ public:
Train(const uint8_t address, uint8_t functionmask = 0b1111); Train(const uint8_t address, uint8_t functionmask = 0b1111);
Train(); Train();
void resendData(); void sendData();
void reverse(); void reverse();
void stop(); void stop();
bool isActive() {return _speed || _function;} bool isActive() {return getSpeed() || getFunctions();}
uint8_t getAddress(); uint8_t getAddress();