working marklin digital support
This commit is contained in:
65
train.cpp
65
train.cpp
@ -2,7 +2,7 @@
|
||||
|
||||
static volatile unsigned char *_port = &PORTD;
|
||||
|
||||
Train::Train(const uint8_t address): _address(address)
|
||||
Train::Train(const uint8_t address, uint8_t protocol): _address(address), _protocol(protocol)
|
||||
{
|
||||
}
|
||||
|
||||
@ -21,6 +21,18 @@ uint8_t Train::getAddress()
|
||||
return _address;
|
||||
}
|
||||
|
||||
void Train::stop()
|
||||
{
|
||||
lastDataPacket = 0;
|
||||
resendData();
|
||||
}
|
||||
|
||||
bool Train::isActive()
|
||||
{
|
||||
return lastDataPacket & 0b0000000111111111;
|
||||
}
|
||||
|
||||
|
||||
void Train::off()
|
||||
{
|
||||
writePin(_port, _pinHighA, false);
|
||||
@ -34,14 +46,14 @@ void Train::setOutput(const uint8_t state)
|
||||
if(state == HIGH)
|
||||
{
|
||||
off();
|
||||
_delay_us(5);
|
||||
_delay_us(3);
|
||||
writePin(_port, _pinHighA, true);
|
||||
writePin(_port, _pinLowB, false);
|
||||
}
|
||||
else if (state == LOW)
|
||||
{
|
||||
off();
|
||||
_delay_us(5);
|
||||
_delay_us(3);
|
||||
writePin(_port, _pinHighB, true);
|
||||
writePin(_port, _pinLowA, false);
|
||||
}
|
||||
@ -77,12 +89,12 @@ void Train::sendAddress()
|
||||
}
|
||||
}
|
||||
|
||||
void Train::sendRaw(const uint8_t data)
|
||||
void Train::sendRaw(const uint16_t data)
|
||||
{
|
||||
for(uint8_t j = 0; j < SEND_COUNT; j++)
|
||||
{
|
||||
sendAddress();
|
||||
for(uint8_t i = 0; i < 5; i++)
|
||||
for(uint8_t i = 0; i < 10; i++)
|
||||
{
|
||||
sendBit(data & (1 << i));
|
||||
}
|
||||
@ -92,31 +104,50 @@ void Train::sendRaw(const uint8_t data)
|
||||
|
||||
void Train::setSpeed(uint8_t speed)
|
||||
{
|
||||
if(speed > 0) ++speed;
|
||||
if(speed <= 15)
|
||||
if(speed != 0)speed = speed + 1;
|
||||
else if(speed > 15) speed = 15;
|
||||
for(uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
lastSpeed = speed;
|
||||
sendData((speed << 1));
|
||||
lastDataPacket = (lastDataPacket & ~(1 << (i+1)*2)) | (((uint16_t)speed & (1 << i)) << (i+1)*2-i);
|
||||
if(_protocol == M_DELTA) lastDataPacket = (lastDataPacket & ~(1 << ((i+1)*2+1))) | (((uint16_t)speed & (1 << i)) << ((i+1)*2+1-i));
|
||||
}
|
||||
resendData();
|
||||
}
|
||||
|
||||
void Train::resendSpeed()
|
||||
void Train::resendData()
|
||||
{
|
||||
uint8_t data = lastSpeed;
|
||||
sendData((data << 1));
|
||||
sendRaw(lastDataPacket);
|
||||
}
|
||||
|
||||
uint8_t Train::getSpeed()
|
||||
uint16_t Train::getLastPacket()
|
||||
{
|
||||
return lastSpeed;
|
||||
return lastDataPacket;
|
||||
}
|
||||
|
||||
void Train::reverse()
|
||||
{
|
||||
sendData((1 << 1));
|
||||
sendRaw(0b000001100);
|
||||
sendRaw(0b000001100);
|
||||
}
|
||||
|
||||
void Train::sendFunction(const uint16_t function)
|
||||
void Train::sendFunction(const uint8_t function, bool enable)
|
||||
{
|
||||
sendDataUndoubled(function);
|
||||
if(function == 0) lastDataPacket = (lastDataPacket & ~0b00000011) | (enable ? 0b00000011 : 0b00000000);
|
||||
else if(_protocol == M_DIGITAL && function <= 3)
|
||||
{
|
||||
lastDataPacket = lastDataPacket | 0b1000000000;
|
||||
if(enable) lastDataPacket = lastDataPacket | (1 << (9-function*2));
|
||||
else lastDataPacket = lastDataPacket & ~(1 << (9-function*2));
|
||||
}
|
||||
resendData();
|
||||
}
|
||||
|
||||
void Train::setProtocol(const uint8_t protocol)
|
||||
{
|
||||
_protocol = protocol;
|
||||
}
|
||||
|
||||
uint8_t Train::getProtocol()
|
||||
{
|
||||
return _protocol;
|
||||
}
|
||||
|
Reference in New Issue
Block a user