Skip to content

Commit

Permalink
server: Z21 reply queue, use encoded speed and steps
Browse files Browse the repository at this point in the history
- To save space we use the same encoding as done
  in Z21 protocol.
- This way we also can directly compare without decoding
- Now also direction and emergency stop are checked
  • Loading branch information
gfgit committed Jul 6, 2024
1 parent 8a8772e commit fc2ff35
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 3 additions & 2 deletions server/src/hardware/protocol/z21/clientkernel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1047,9 +1047,10 @@ std::optional<ClientKernel::PendingRequest> ClientKernel::matchPendingReplyAndRe
if(message.header() == LAN_X
&& static_cast<const LanX&>(message).xheader == LAN_X_LOCO_INFO)
{
// TODO: Do extensive matching
const LanXLocoInfo& locoInfo = static_cast<const LanXLocoInfo&>(message);
if(locoInfo.speedStep() != request->reply.speedStep)
if(locoInfo.speedAndDirection != request->reply.speedAndDirection)
continue;
if(locoInfo.speedSteps() != request->reply.speedSteps())
continue;
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/hardware/protocol/z21/messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,8 @@ MessageReplyType getReplyType(const Message &message)
setLocoDrive.db0 >= 0x10 && setLocoDrive.db0 <= 0x13)
{
reply.address = setLocoDrive.address();
reply.speedStep = setLocoDrive.speedStep();
reply.speedAndDirection = setLocoDrive.speedAndDirection;
reply.setSpeedSteps(setLocoDrive.speedSteps());
reply.setFlag(MessageReplyType::Flags::CheckSpeedStep);
}
else if(const auto& setLocoFunction = static_cast<const LanXSetLocoFunction&>(message);
Expand Down
29 changes: 28 additions & 1 deletion server/src/hardware/protocol/z21/messages.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1749,14 +1749,41 @@ struct MessageReplyType
m_flags = uint8_t(flags) << 4 | (uint8_t(priority()) & 0xF);
}

inline uint8_t speedSteps() const
{
switch(speedStepsEncoded & LanXLocoInfo::db2_speed_steps_mask)
{
case LanXLocoInfo::db2_speed_steps_14: return 14;
case LanXLocoInfo::db2_speed_steps_28: return 28;
case LanXLocoInfo::db2_speed_steps_128: return 126;
}
return 0;
}

inline void setSpeedSteps(uint8_t value)
{
speedStepsEncoded &= ~LanXLocoInfo::db2_speed_steps_mask;
switch(value)
{
case 14: speedStepsEncoded |= LanXLocoInfo::db2_speed_steps_14; break;
case 28: speedStepsEncoded |= LanXLocoInfo::db2_speed_steps_28; break;
case 126:
case 128:
default: speedStepsEncoded |= LanXLocoInfo::db2_speed_steps_128; break;
}
}

static constexpr Header noReply = Header(0);

Header header = noReply;
uint8_t xHeader = 0;
uint8_t db0 = 0;
uint16_t address = 0;
uint8_t m_flags = uint8_t(Priority::Normal);
uint8_t speedStep = 0;

// Encoded as LAN_X_
uint8_t speedAndDirection = 0;
uint8_t speedStepsEncoded = 0;
};

MessageReplyType getReplyType(const Message &message);
Expand Down

0 comments on commit fc2ff35

Please sign in to comment.