split xpressnet protocol files

Dieser Commit ist enthalten in:
Reinder Feenstra 2020-08-04 17:44:56 +02:00
Ursprung 6afb6eb66d
Commit 5be0bedd3d
11 geänderte Dateien mit 495 neuen und 489 gelöschten Zeilen

Datei anzeigen

@ -37,6 +37,8 @@ file(GLOB SOURCES
"src/hardware/protocol/*.cpp"
"src/hardware/protocol/loconet/*.hpp"
"src/hardware/protocol/loconet/*.cpp"
"src/hardware/protocol/xpressnet/*.hpp"
"src/hardware/protocol/xpressnet/*.cpp"
"src/train/*.hpp"
"src/train/*.cpp"
"src/vehicle/*.hpp"

Datei anzeigen

@ -25,8 +25,7 @@
#include "../../world/world.hpp"
#include "../../core/eventloop.hpp"
#include "../decoder/decoderchangeflags.hpp"
#include "../protocol/xpressnet.hpp"
#include "../protocol/xpressnet/messages.hpp"
#include "../protocol/z21.hpp"
#include "../../utils/to_hex.hpp"
@ -201,7 +200,7 @@ void RocoZ21::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes,
if(decoder.direction.value() == Direction::Forward)
cmd.speedAndDirection |= 0x80;
cmd.checksum = XpressNet::calcChecksum(&cmd.xheader);
cmd.checksum = XpressNet::calcChecksum(*reinterpret_cast<const XpressNet::Message*>(&cmd.xheader));
send(&cmd);
}
else if(has(changes, DecoderChangeFlags::FunctionValue))
@ -215,7 +214,7 @@ void RocoZ21::decoderChanged(const Decoder& decoder, DecoderChangeFlags changes,
cmd.header = Z21_LAN_X;
SET_ADDRESS;
cmd.db3 = (f->value ? 0x40 : 0x00) | static_cast<uint8_t>(functionNumber);
cmd.checksum = XpressNet::calcChecksum(&cmd.xheader);
cmd.checksum = XpressNet::calcChecksum(*reinterpret_cast<const XpressNet::Message*>(&cmd.xheader));
send(&cmd);
}
}

Datei anzeigen

@ -30,7 +30,7 @@ USBXpressNetInterface::USBXpressNetInterface(const std::weak_ptr<World>& world,
xpressnet{this, "xpressnet", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject}
{
name = "USB XpressNet interface";
xpressnet.setValueInternal(std::make_shared<XpressNet>(*this, xpressnet.name(), std::bind(&USBXpressNetInterface::send, this, std::placeholders::_1)));
xpressnet.setValueInternal(std::make_shared<XpressNet::XpressNet>(*this, xpressnet.name(), std::bind(&USBXpressNetInterface::send, this, std::placeholders::_1)));
m_interfaceItems.insertBefore(serial, notes);
m_interfaceItems.insertBefore(address, notes);

Datei anzeigen

@ -24,7 +24,7 @@
#define TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_USBXPRESSNETINTERFACE_HPP
#include "commandstation.hpp"
#include "../protocol/xpressnet.hpp"
#include "../protocol/xpressnet/xpressnet.hpp"
#include <usbxpressnet.h>
class USBXpressNetInterface : public CommandStation
@ -48,7 +48,7 @@ class USBXpressNetInterface : public CommandStation
Property<std::string> serial;
Property<uint8_t> address;
ObjectProperty<::XpressNet> xpressnet;
ObjectProperty<XpressNet::XpressNet> xpressnet;
USBXpressNetInterface(const std::weak_ptr<World>& world, std::string_view _id);
~USBXpressNetInterface() final;

Datei anzeigen

@ -55,29 +55,12 @@ XpressNetSerial::XpressNetSerial(const std::weak_ptr<World>& world, std::string_
xpressnet{this, "xpressnet", nullptr, PropertyFlags::ReadOnly | PropertyFlags::Store | PropertyFlags::SubObject}
{
name = "XpressNet (serial)";
xpressnet.setValueInternal(std::make_shared<XpressNet>(*this, xpressnet.name(), std::bind(&XpressNetSerial::send, this, std::placeholders::_1)));
xpressnet.setValueInternal(std::make_shared<XpressNet::XpressNet>(*this, xpressnet.name(), std::bind(&XpressNetSerial::send, this, std::placeholders::_1)));
m_interfaceItems.insertBefore(interface, baudrate);
m_interfaceItems.insertBefore(xpressnet, notes);
}
/*
bool XpressNetSerial::setOnline(bool& value)
{
if(!m_serialPort.is_open() && value)
{
if(!start())
{
value = false;
return false;
}
read();
}
else if(m_serialPort.is_open() && !value)
stop();
return true;
}
*/
void XpressNetSerial::emergencyStopChanged(bool value)
{
CommandStation::emergencyStopChanged(value);
@ -101,32 +84,7 @@ void XpressNetSerial::decoderChanged(const Decoder& decoder, DecoderChangeFlags
if(online)
xpressnet->decoderChanged(decoder, changes, functionNumber);
}
/*
bool XpressNetSerial::start()
{
boost::system::error_code ec;
m_serialPort.open(port, ec);
if(ec)
{
logError("open: " + ec.message());
return false;
}
m_serialPort.set_option(boost::asio::serial_port_base::baud_rate(baudrate));
m_serialPort.set_option(boost::asio::serial_port_base::character_size(8));
m_serialPort.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
m_serialPort.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
m_serialPort.set_option(boost::asio::serial_port_base::flow_control(boost::asio::serial_port_base::flow_control::hardware));
return true;
}
void XpressNetSerial::stop()
{
// send bus off cmd
m_serialPort.close();
}
*/
bool XpressNetSerial::send(const XpressNet::Message& msg)
{
assert(XpressNet::isChecksumValid(msg));

Datei anzeigen

@ -20,42 +20,29 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_LI10X_HPP
#define TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_LI10X_HPP
#ifndef TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_XPRESSNETSERIAL_HPP
#define TRAINTASTIC_SERVER_HARDWARE_COMMANDSTATION_XPRESSNETSERIAL_HPP
#include "serialcommandstation.hpp"
#include "../../enum/xpressnetserialinterface.hpp"
#include "../protocol/xpressnet.hpp"
//#include <boost/asio/serial_port.hpp>
//#include "../../core/objectproperty.hpp"
//#include "protocol/xpressnet.hpp"
#include "../protocol/xpressnet/xpressnet.hpp"
class XpressNetSerial : public SerialCommandStation
{
protected:
//boost::asio::serial_port m_serialPort;
//std::array<uint8_t, 32> m_readBuffer;
//std::unique_ptr<uint8_t[]> m_readMessage;
//uint8_t m_readMessageTodo;
//uint8_t m_readMessagePos;
//bool setOnline(bool& value) final;
void emergencyStopChanged(bool value) final;
void trackVoltageOffChanged(bool value) final;
void decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber) final;
bool start();
void stop();
bool send(const XpressNet::Message& msg);
void receive(std::unique_ptr<uint8_t[]> message);
void read();
void read() final;
public:
CLASS_ID("command_station.xpressnet_serial")
CREATE(XpressNetSerial)
Property<XpressNetSerialInterface> interface;
ObjectProperty<XpressNet> xpressnet;
ObjectProperty<XpressNet::XpressNet> xpressnet;
XpressNetSerial(const std::weak_ptr<World>& world, std::string_view _id);
};

Datei anzeigen

@ -1,370 +0,0 @@
/**
* server/src/hardware/protocol/xpressnet.hpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2019-2020 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_HPP
#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_HPP
#include "../../core/subobject.hpp"
#include "../../core/property.hpp"
#include "../../enum/direction.hpp"
#include "../../enum/xpressnetcommandstation.hpp"
#include "../../hardware/decoder/decoderchangeflags.hpp"
class CommandStation;
class Decoder;
class XpressNet : public SubObject
{
public:
struct Message;
static uint8_t calcChecksum(const void* msg);
static uint8_t calcChecksum(const Message& msg);
static bool isChecksumValid(const Message& msg);
struct Message
{
uint8_t header;
uint8_t identification() const
{
return header & 0xF0;
}
uint8_t dataSize() const
{
return header & 0x0F;
}
uint8_t size() const
{
return 2 + dataSize();
}
};
static_assert(sizeof(Message) == 1);
struct NormalOperationResumed : Message
{
uint8_t db1 = 0x01;
uint8_t checksum = 0x60;
NormalOperationResumed()
{
header = 0x61;
}
};
static_assert(sizeof(NormalOperationResumed) == 3);
struct TrackPowerOff : Message
{
uint8_t db1 = 0x00;
uint8_t checksum = 0x61;
TrackPowerOff()
{
header = 0x61;
}
};
static_assert(sizeof(TrackPowerOff) == 3);
struct EmergencyStop : Message
{
uint8_t db1 = 0x00;
uint8_t checksum = 0x81;
EmergencyStop()
{
header = 0x81;
}
};
static_assert(sizeof(EmergencyStop) == 3);
struct EmergencyStopLocomotive : Message
{
uint8_t addressHigh;
uint8_t addressLow;
uint8_t checksum;
EmergencyStopLocomotive(uint16_t address, bool longAddress)
{
header = 0x92;
if(longAddress)
{
assert(address >= 1 && address <= 9999);
addressHigh = 0x00;
addressLow = address & 0x7f;
}
else
{
assert(address >= 1 && address <= 127);
addressHigh = 0x00;
addressLow = address & 0x7f;
}
}
};
static_assert(sizeof(EmergencyStopLocomotive) == 4);
struct LocomotiveInstruction : Message
{
uint8_t identification;
uint8_t addressHigh;
uint8_t addressLow;
LocomotiveInstruction(uint16_t address, bool longAddress)
{
header = 0xE4;
if(longAddress)
{
assert(address >= 1 && address <= 9999);
addressHigh = 0xc0 | address >> 8;
addressLow = address & 0xff;
}
else
{
assert(address >= 1 && address <= 127);
addressHigh = 0x00;
addressLow = address & 0x7f;
}
}
};
static_assert(sizeof(LocomotiveInstruction) == 4);
struct SpeedAndDirectionInstruction : LocomotiveInstruction
{
uint8_t speedAndDirection;
uint8_t checksum;
SpeedAndDirectionInstruction(uint16_t address, bool longAddress, bool emergencyStop, Direction direction) :
LocomotiveInstruction(address, longAddress)
{
speedAndDirection = emergencyStop ? 0x01 : 0x00;
if(direction == Direction::Forward)
speedAndDirection |= 0x80;
}
};
struct SpeedAndDirectionInstruction14 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction14(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep, bool fl) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 14);
identification = 0x10;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= speedStep + 1;
if(fl)
speedAndDirection |= 0x10;
checksum = calcChecksum(*this);
}
};
struct SpeedAndDirectionInstruction27 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction27(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 27);
identification = 0x11;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1);
checksum = calcChecksum(*this);
}
};
struct SpeedAndDirectionInstruction28 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction28(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 28);
identification = 0x12;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1);
checksum = calcChecksum(*this);
}
};
struct SpeedAndDirectionInstruction128 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction128(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 126);
identification = 0x13;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= speedStep + 1;
checksum = calcChecksum(*this);
}
};
struct FunctionInstructionGroup : LocomotiveInstruction
{
uint8_t functions = 0x00;
uint8_t checksum;
FunctionInstructionGroup(uint16_t address, bool longAddress, uint8_t group) :
LocomotiveInstruction(address, longAddress)
{
assert(group >= 1 && group <= 3);
identification = 0x1F + group;
}
};
struct FunctionInstructionGroup1 : FunctionInstructionGroup
{
FunctionInstructionGroup1(uint16_t address, bool longAddress, bool f0, bool f1, bool f2, bool f3, bool f4) :
FunctionInstructionGroup(address, longAddress, 1)
{
if(f0)
functions |= 0x10;
if(f1)
functions |= 0x01;
if(f2)
functions |= 0x02;
if(f3)
functions |= 0x04;
if(f4)
functions |= 0x08;
checksum = calcChecksum(*this);
}
};
struct FunctionInstructionGroup2 : FunctionInstructionGroup
{
FunctionInstructionGroup2(uint16_t address, bool longAddress, bool f5, bool f6, bool f7, bool f8) :
FunctionInstructionGroup(address, longAddress, 2)
{
if(f5)
functions |= 0x01;
if(f6)
functions |= 0x02;
if(f7)
functions |= 0x04;
if(f8)
functions |= 0x08;
checksum = calcChecksum(*this);
}
};
struct FunctionInstructionGroup3 : FunctionInstructionGroup
{
FunctionInstructionGroup3(uint16_t address, bool longAddress, bool f9, bool f10, bool f11, bool f12) :
FunctionInstructionGroup(address, longAddress, 3)
{
if(f9)
functions |= 0x01;
if(f10)
functions |= 0x02;
if(f11)
functions |= 0x04;
if(f12)
functions |= 0x08;
checksum = calcChecksum(*this);
}
};
/*
struct setFunctionStateGroup : LocomotiveInstruction
{
uint8_t state = 0x00;
uint8_t checksum;
setFunctionStateGroup(uint8_t group, uint16_t address)
{
assert(group >= 1 && group <= 3);
header = 0xE4;
identification = 0x23 + group;
addressLowHigh(address, addressLow, addressHigh);
}
} __attribute__((packed));
*/
struct RocoFunctionInstructionF13F20 : LocomotiveInstruction
{
uint8_t functions = 0x00;
uint8_t checksum;
RocoFunctionInstructionF13F20(uint16_t address, bool longAddress, bool f13, bool f14, bool f15, bool f16, bool f17, bool f18, bool f19, bool f20) :
LocomotiveInstruction(address, longAddress)
{
identification = 0xF3;
if(f13)
functions |= 0x01;
if(f14)
functions |= 0x02;
if(f15)
functions |= 0x04;
if(f16)
functions |= 0x08;
if(f17)
functions |= 0x10;
if(f18)
functions |= 0x20;
if(f19)
functions |= 0x40;
if(f20)
functions |= 0x80;
checksum = calcChecksum(*this);
}
};
protected:
static bool getFunctionValue(const Decoder& decoder, uint32_t number);
CommandStation* const m_commandStation; // valid if parent is command station, else nullptr
std::function<bool(const Message&)> m_send;
std::atomic_bool m_debugLog;
void worldEvent(WorldState state, WorldEvent event) final;
public:
CLASS_ID("protocol.xpressnet")
Property<XpressNetCommandStation> commandStation;
Property<bool> useEmergencyStopLocomotiveCommand;
Property<bool> useFunctionStateCommands;
Property<bool> useRocoF13F20Command;
Property<bool> debugLog;
XpressNet(Object& _parent, const std::string& parentPropertyName, std::function<bool(const Message&)> send);
bool send(const Message& msg) { return m_send(msg); }
void receive(const Message& msg);
void emergencyStopChanged(bool value);
void trackVoltageOffChanged(bool value);
void decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber);
};
inline bool operator ==(const XpressNet::Message& lhs, const XpressNet::Message& rhs)
{
return lhs.size() == rhs.size() && std::memcmp(&lhs, &rhs, lhs.size()) == 0;
}
std::string to_string(const XpressNet::Message& message, bool raw = false);
#endif

Datei anzeigen

@ -0,0 +1,70 @@
/**
* server/src/hardware/protocol/xpressnet/messages.cpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2019-2020 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "messages.hpp"
#include "../../../utils/to_hex.hpp"
namespace XpressNet {
uint8_t calcChecksum(const Message& msg)
{
const uint8_t* p = reinterpret_cast<const uint8_t*>(&msg);
const int dataSize = msg.dataSize();
uint8_t checksum = p[0];
for(int i = 1; i <= dataSize; i++)
checksum ^= p[i];
return checksum;
}
bool isChecksumValid(const Message& msg)
{
return calcChecksum(msg) == *(reinterpret_cast<const uint8_t*>(&msg) + msg.dataSize() + 1);
}
std::string to_string(const Message& message, bool raw)
{
std::string s;
switch(message.identification())
{
default:
raw = true;
break;
}
if(raw)
{
s.append("[");
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&message);
for(int i = 0; i < message.size(); i++)
{
if(i != 0)
s.append(" ");
s.append(to_hex(bytes[i]));
}
s.append("]");
}
return s;
}
}

Datei anzeigen

@ -0,0 +1,337 @@
/**
* server/src/hardware/protocol/xpressnet/messages.hpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2019-2020 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_MESSAGES_HPP
#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_MESSAGES_HPP
#include <cstdint>
#include <cassert>
#include <string>
#include "../../../enum/direction.hpp"
namespace XpressNet {
struct Message;
uint8_t calcChecksum(const Message& msg);
bool isChecksumValid(const Message& msg);
std::string to_string(const Message& message, bool raw = false);
struct Message
{
uint8_t header;
uint8_t identification() const
{
return header & 0xF0;
}
uint8_t dataSize() const
{
return header & 0x0F;
}
uint8_t size() const
{
return 2 + dataSize();
}
};
static_assert(sizeof(Message) == 1);
struct NormalOperationResumed : Message
{
uint8_t db1 = 0x01;
uint8_t checksum = 0x60;
NormalOperationResumed()
{
header = 0x61;
}
};
static_assert(sizeof(NormalOperationResumed) == 3);
struct TrackPowerOff : Message
{
uint8_t db1 = 0x00;
uint8_t checksum = 0x61;
TrackPowerOff()
{
header = 0x61;
}
};
static_assert(sizeof(TrackPowerOff) == 3);
struct EmergencyStop : Message
{
uint8_t db1 = 0x00;
uint8_t checksum = 0x81;
EmergencyStop()
{
header = 0x81;
}
};
static_assert(sizeof(EmergencyStop) == 3);
struct EmergencyStopLocomotive : Message
{
uint8_t addressHigh;
uint8_t addressLow;
uint8_t checksum;
EmergencyStopLocomotive(uint16_t address, bool longAddress)
{
header = 0x92;
if(longAddress)
{
assert(address >= 1 && address <= 9999);
addressHigh = 0x00;
addressLow = address & 0x7f;
}
else
{
assert(address >= 1 && address <= 127);
addressHigh = 0x00;
addressLow = address & 0x7f;
}
}
};
static_assert(sizeof(EmergencyStopLocomotive) == 4);
struct LocomotiveInstruction : Message
{
uint8_t identification;
uint8_t addressHigh;
uint8_t addressLow;
LocomotiveInstruction(uint16_t address, bool longAddress)
{
header = 0xE4;
if(longAddress)
{
assert(address >= 1 && address <= 9999);
addressHigh = 0xc0 | address >> 8;
addressLow = address & 0xff;
}
else
{
assert(address >= 1 && address <= 127);
addressHigh = 0x00;
addressLow = address & 0x7f;
}
}
};
static_assert(sizeof(LocomotiveInstruction) == 4);
struct SpeedAndDirectionInstruction : LocomotiveInstruction
{
uint8_t speedAndDirection;
uint8_t checksum;
SpeedAndDirectionInstruction(uint16_t address, bool longAddress, bool emergencyStop, Direction direction) :
LocomotiveInstruction(address, longAddress)
{
speedAndDirection = emergencyStop ? 0x01 : 0x00;
if(direction == Direction::Forward)
speedAndDirection |= 0x80;
}
};
struct SpeedAndDirectionInstruction14 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction14(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep, bool fl) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 14);
identification = 0x10;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= speedStep + 1;
if(fl)
speedAndDirection |= 0x10;
checksum = calcChecksum(*this);
}
};
struct SpeedAndDirectionInstruction27 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction27(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 27);
identification = 0x11;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1);
checksum = calcChecksum(*this);
}
};
struct SpeedAndDirectionInstruction28 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction28(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 28);
identification = 0x12;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= (((speedStep + 1) & 0x01) << 4) | ((speedStep + 1) >> 1);
checksum = calcChecksum(*this);
}
};
struct SpeedAndDirectionInstruction128 : SpeedAndDirectionInstruction
{
SpeedAndDirectionInstruction128(uint16_t address, bool longAddress, bool emergencyStop, Direction direction, uint8_t speedStep) :
SpeedAndDirectionInstruction(address, longAddress, emergencyStop, direction)
{
assert(speedStep <= 126);
identification = 0x13;
if(!emergencyStop && speedStep > 0)
speedAndDirection |= speedStep + 1;
checksum = calcChecksum(*this);
}
};
struct FunctionInstructionGroup : LocomotiveInstruction
{
uint8_t functions = 0x00;
uint8_t checksum;
FunctionInstructionGroup(uint16_t address, bool longAddress, uint8_t group) :
LocomotiveInstruction(address, longAddress)
{
assert(group >= 1 && group <= 3);
identification = 0x1F + group;
}
};
struct FunctionInstructionGroup1 : FunctionInstructionGroup
{
FunctionInstructionGroup1(uint16_t address, bool longAddress, bool f0, bool f1, bool f2, bool f3, bool f4) :
FunctionInstructionGroup(address, longAddress, 1)
{
if(f0)
functions |= 0x10;
if(f1)
functions |= 0x01;
if(f2)
functions |= 0x02;
if(f3)
functions |= 0x04;
if(f4)
functions |= 0x08;
checksum = calcChecksum(*this);
}
};
struct FunctionInstructionGroup2 : FunctionInstructionGroup
{
FunctionInstructionGroup2(uint16_t address, bool longAddress, bool f5, bool f6, bool f7, bool f8) :
FunctionInstructionGroup(address, longAddress, 2)
{
if(f5)
functions |= 0x01;
if(f6)
functions |= 0x02;
if(f7)
functions |= 0x04;
if(f8)
functions |= 0x08;
checksum = calcChecksum(*this);
}
};
struct FunctionInstructionGroup3 : FunctionInstructionGroup
{
FunctionInstructionGroup3(uint16_t address, bool longAddress, bool f9, bool f10, bool f11, bool f12) :
FunctionInstructionGroup(address, longAddress, 3)
{
if(f9)
functions |= 0x01;
if(f10)
functions |= 0x02;
if(f11)
functions |= 0x04;
if(f12)
functions |= 0x08;
checksum = calcChecksum(*this);
}
};
/*
struct setFunctionStateGroup : LocomotiveInstruction
{
uint8_t state = 0x00;
uint8_t checksum;
setFunctionStateGroup(uint8_t group, uint16_t address)
{
assert(group >= 1 && group <= 3);
header = 0xE4;
identification = 0x23 + group;
addressLowHigh(address, addressLow, addressHigh);
}
} __attribute__((packed));
*/
struct RocoFunctionInstructionF13F20 : LocomotiveInstruction
{
uint8_t functions = 0x00;
uint8_t checksum;
RocoFunctionInstructionF13F20(uint16_t address, bool longAddress, bool f13, bool f14, bool f15, bool f16, bool f17, bool f18, bool f19, bool f20) :
LocomotiveInstruction(address, longAddress)
{
identification = 0xF3;
if(f13)
functions |= 0x01;
if(f14)
functions |= 0x02;
if(f15)
functions |= 0x04;
if(f16)
functions |= 0x08;
if(f17)
functions |= 0x10;
if(f18)
functions |= 0x20;
if(f19)
functions |= 0x40;
if(f20)
functions |= 0x80;
checksum = calcChecksum(*this);
}
};
}
inline bool operator ==(const XpressNet::Message& lhs, const XpressNet::Message& rhs)
{
return lhs.size() == rhs.size() && std::memcmp(&lhs, &rhs, lhs.size()) == 0;
}
#endif

Datei anzeigen

@ -1,5 +1,5 @@
/**
* server/src/hardware/protocol/xpressnet.cpp
* server/src/hardware/protocol/xpressnet/xpressnet.cpp
*
* This file is part of the traintastic source code.
*
@ -21,31 +21,11 @@
*/
#include "xpressnet.hpp"
#include "../../core/traintastic.hpp"
#include "../../core/eventloop.hpp"
#include "../decoder/decoder.hpp"
#include "../../utils/to_hex.hpp"
#include "../../../core/traintastic.hpp"
#include "../../../core/eventloop.hpp"
#include "../../decoder/decoder.hpp"
uint8_t XpressNet::calcChecksum(const void* msg)
{
assert(msg);
return calcChecksum(*reinterpret_cast<const Message*>(msg));
}
uint8_t XpressNet::calcChecksum(const Message& msg)
{
const uint8_t* p = reinterpret_cast<const uint8_t*>(&msg);
const int dataSize = msg.dataSize();
uint8_t checksum = p[0];
for(int i = 1; i <= dataSize; i++)
checksum ^= p[i];
return checksum;
}
bool XpressNet::isChecksumValid(const Message& msg)
{
return calcChecksum(msg) == *(reinterpret_cast<const uint8_t*>(&msg) + msg.dataSize() + 1);
}
namespace XpressNet {
XpressNet::XpressNet(Object& _parent, const std::string& parentPropertyName, std::function<bool(const Message&)> send) :
SubObject(_parent, parentPropertyName),
@ -272,30 +252,4 @@ void XpressNet::decoderChanged(const Decoder& decoder, DecoderChangeFlags change
}
}
std::string to_string(const XpressNet::Message& message, bool raw)
{
std::string s;
switch(message.identification())
{
default:
raw = true;
break;
}
if(raw)
{
s.append("[");
const uint8_t* bytes = reinterpret_cast<const uint8_t*>(&message);
for(int i = 0; i < message.size(); i++)
{
if(i != 0)
s.append(" ");
s.append(to_hex(bytes[i]));
}
s.append("]");
}
return s;
}

Datei anzeigen

@ -0,0 +1,69 @@
/**
* server/src/hardware/protocol/xpressnet/xpressnet.hpp
*
* This file is part of the traintastic source code.
*
* Copyright (C) 2019-2020 Reinder Feenstra
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#ifndef TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_XPRESSNET_HPP
#define TRAINTASTIC_SERVER_HARDWARE_PROTOCOL_XPRESSNET_XPRESSNET_HPP
#include "../../../core/subobject.hpp"
#include "../../../core/property.hpp"
#include "../../../enum/xpressnetcommandstation.hpp"
#include "../../../hardware/decoder/decoderchangeflags.hpp"
#include "messages.hpp"
class CommandStation;
class Decoder;
namespace XpressNet {
class XpressNet : public SubObject
{
protected:
static bool getFunctionValue(const Decoder& decoder, uint32_t number);
CommandStation* const m_commandStation; // valid if parent is command station, else nullptr
std::function<bool(const Message&)> m_send;
std::atomic_bool m_debugLog;
void worldEvent(WorldState state, WorldEvent event) final;
public:
CLASS_ID("protocol.xpressnet")
Property<XpressNetCommandStation> commandStation;
Property<bool> useEmergencyStopLocomotiveCommand;
Property<bool> useFunctionStateCommands;
Property<bool> useRocoF13F20Command;
Property<bool> debugLog;
XpressNet(Object& _parent, const std::string& parentPropertyName, std::function<bool(const Message&)> send);
bool send(const Message& msg) { return m_send(msg); }
void receive(const Message& msg);
void emergencyStopChanged(bool value);
void trackVoltageOffChanged(bool value);
void decoderChanged(const Decoder& decoder, DecoderChangeFlags changes, uint32_t functionNumber);
};
}
#endif