move to tab indentaion
This commit is contained in:
140
train.cpp
140
train.cpp
@ -7,98 +7,98 @@ Train::Train(const uint8_t address, uint8_t functionmask): Item(address), _func
|
||||
|
||||
uint16_t Train::packetAddSpeed()
|
||||
{
|
||||
uint16_t packet = 0;
|
||||
if(_speed > 14)
|
||||
_speed = 14;
|
||||
for(uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
packet |= ((bool)((_speed+1) & (1 << i))) << (i*2+2);
|
||||
}
|
||||
return packet;
|
||||
uint16_t packet = 0;
|
||||
if(_speed > 14)
|
||||
_speed = 14;
|
||||
for(uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
packet |= ((bool)((_speed+1) & (1 << i))) << (i*2+2);
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
uint16_t Train::packetAddDirection()
|
||||
{
|
||||
uint16_t packet = 0;
|
||||
uint16_t packet = 0;
|
||||
|
||||
uint8_t data = 0;
|
||||
if(!_direction)
|
||||
{
|
||||
if(_speed > 6)
|
||||
data = 0b0100;
|
||||
else if(_speed > 0)
|
||||
data = 0b0101;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_speed > 0)
|
||||
data = 0b1011;
|
||||
else if (_speed > 6)
|
||||
data = 0b1010;
|
||||
}
|
||||
uint8_t data = 0;
|
||||
if(!_direction)
|
||||
{
|
||||
if(_speed > 6)
|
||||
data = 0b0100;
|
||||
else if(_speed > 0)
|
||||
data = 0b0101;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(_speed > 0)
|
||||
data = 0b1011;
|
||||
else if (_speed > 6)
|
||||
data = 0b1010;
|
||||
}
|
||||
|
||||
if(_speed == 0)
|
||||
data = !_direction ? 0b0101 : 0b1011;
|
||||
if(_speed == 0)
|
||||
data = !_direction ? 0b0101 : 0b1011;
|
||||
|
||||
for(uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
uint8_t bit = (data & (1 << (3-i))) >> (3-i);
|
||||
packet |= bit << ((i*2)+3);
|
||||
}
|
||||
return packet;
|
||||
for(uint8_t i = 0; i < 4; i++)
|
||||
{
|
||||
uint8_t bit = (data & (1 << (3-i))) >> (3-i);
|
||||
packet |= bit << ((i*2)+3);
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
uint16_t Train::packetAddFunction(const uint8_t function)
|
||||
{
|
||||
uint16_t packet = 0;
|
||||
bool enabled = _function & (1 << function);
|
||||
if(function == 0)
|
||||
{
|
||||
packet |= enabled ? 0b00000011 : 0b00000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(enabled)
|
||||
packet |= 0b1000000000;
|
||||
for(uint8_t i = 0; i < 4; ++i )
|
||||
{
|
||||
if(function > i)
|
||||
packet = packet | (1 << (7-i*2));
|
||||
else
|
||||
packet = packet & ~(1 << (7-i*2));
|
||||
}
|
||||
}
|
||||
return packet;
|
||||
uint16_t packet = 0;
|
||||
bool enabled = _function & (1 << function);
|
||||
if(function == 0)
|
||||
{
|
||||
packet |= enabled ? 0b00000011 : 0b00000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(enabled)
|
||||
packet |= 0b1000000000;
|
||||
for(uint8_t i = 0; i < 4; ++i )
|
||||
{
|
||||
if(function > i)
|
||||
packet = packet | (1 << (7-i*2));
|
||||
else
|
||||
packet = packet & ~(1 << (7-i*2));
|
||||
}
|
||||
}
|
||||
return packet;
|
||||
}
|
||||
|
||||
void Train::setSpeed(uint8_t speed)
|
||||
{
|
||||
_speed = speed;
|
||||
sendData();
|
||||
_speed = speed;
|
||||
sendData();
|
||||
}
|
||||
|
||||
uint8_t Train::getSpeed()
|
||||
{
|
||||
return _speed;
|
||||
return _speed;
|
||||
}
|
||||
|
||||
void Train::stop()
|
||||
{
|
||||
_speed = 0;
|
||||
_function = 0;
|
||||
sendData();
|
||||
_speed = 0;
|
||||
_function = 0;
|
||||
sendData();
|
||||
}
|
||||
|
||||
|
||||
uint16_t Train::assembleSpeedPacket()
|
||||
{
|
||||
uint16_t packet = packetAddSpeed() | packetAddFunction(0) | packetAddDirection();
|
||||
return packet;
|
||||
uint16_t packet = packetAddSpeed() | packetAddFunction(0) | packetAddDirection();
|
||||
return packet;
|
||||
}
|
||||
|
||||
void Train::sendData()
|
||||
{
|
||||
sendRaw(assembleSpeedPacket());
|
||||
sendRaw(assembleSpeedPacket());
|
||||
if(_functionmask)
|
||||
{
|
||||
uint8_t functionToResend = (_function & 0xF0) >> 4;
|
||||
@ -117,30 +117,30 @@ void Train::sendData()
|
||||
|
||||
uint16_t Train::getLastPacket()
|
||||
{
|
||||
return assembleSpeedPacket();
|
||||
return assembleSpeedPacket();
|
||||
}
|
||||
|
||||
void Train::reverse()
|
||||
{
|
||||
_direction = !_direction;
|
||||
sendData();
|
||||
_direction = !_direction;
|
||||
sendData();
|
||||
}
|
||||
|
||||
|
||||
uint8_t Train::getFunctions()
|
||||
{
|
||||
return _function & 0x0F;
|
||||
return _function & 0x0F;
|
||||
}
|
||||
|
||||
|
||||
uint8_t Train::getFunctionMask()
|
||||
{
|
||||
return _functionmask;
|
||||
return _functionmask;
|
||||
}
|
||||
|
||||
void Train::sendFunction(const uint8_t function, bool enable)
|
||||
{
|
||||
if(function > 3)
|
||||
return;
|
||||
_function = (_function & ~(1 << function)) | (enable << function);
|
||||
sendData();
|
||||
if(function > 3)
|
||||
return;
|
||||
_function = (_function & ~(1 << function)) | (enable << function);
|
||||
sendData();
|
||||
}
|
||||
|
Reference in New Issue
Block a user