52static_assert(
sizeof(Packet) ==
sizeof(
DCCPacket),
"DCCPacket size missmatch");
65 for (
int i = 0; i <
dlc; ++i)
73 for (
int i = 0; i <
dlc; ++i)
117 payload[
dlc++] = DCC_BASIC_ACCESSORY_B1 | ((address >> 2) & 0b111111);
118 uint8_t b2 = is_basic ? DCC_BASIC_ACCESSORY_B2 : DCC_EXT_ACCESSORY_B2;
119 b2 |= ((~(address >> 8)) & 0b111) << 4;
120 b2 |= (address & 0b11) << 1;
128 uint8_t b1 = DCC_BASELINE_SPEED;
130 b1 |= DCC_BASELINE_SPEED_FORWARD;
132 b1 |= DCC_BASELINE_SPEED_LIGHT;
152 uint8_t b1 = DCC_BASELINE_SPEED;
154 b1 |= DCC_BASELINE_SPEED_FORWARD;
167 b1 |= DCC_BASELINE_SPEED_LIGHT;
168 b1 |= (speed >> 1) & 0xf;
179 b |= DCC_EXT_SPEED_FORWARD;
196 uint8_t b1 = DCC_FUNCTION1;
198 b1 |= DCC_FUNCTION1_F0;
199 b1 |= (values >> 1) & 0xf;
206 uint8_t b1 = DCC_FUNCTION2_F5;
214 uint8_t b1 = DCC_FUNCTION2_F9;
227 payload[
dlc++] = DCC_FEATURE_EXP_FNHI | (base & 0b111);
242 payload[
dlc++] = (fn & 0x7F) | (value ? 0x80 : 0);
257 uint8_t cmd_hi,
unsigned cv_number, uint8_t value)
259 payload[
dlc++] = (cmd_hi & (~3)) | ((cv_number >> 8) & 3);
287 unsigned cv_number,
unsigned bit,
bool expected)
290 uint8_t vvv = DCC_SVC_BITVAL_VERIFY |
291 (expected ? DCC_SVC_BITVAL_VALUE : 0) | (bit & 7);
296 unsigned cv_number,
unsigned bit,
bool desired)
300 DCC_SVC_BITVAL_WRITE | (desired ? DCC_SVC_BITVAL_VALUE : 0) | (bit & 7);
326 payload[1] |= DCC_BASIC_ACCESSORY_B2_CLOSED;
330 payload[1] |= DCC_BASIC_ACCESSORY_B2_THROWN;
334 payload[1] |= DCC_BASIC_ACCESSORY_B2_ACTIVATE;
338 payload[1] |= DCC_BASIC_ACCESSORY_B2_DEACTIVATE;
395 payload[
dlc++] = ((address >> 8) & 0xff) ^ 0b11000000;
406 header_raw_data = MARKLIN_DEFAULT_CMD;
413 0b00010100, 0b01010100};
419 uint8_t address = a.
value;
460 payload[2] = MARKLIN_CHANGE_DIR_B2;
462 header_raw_data |= 0b01100000;
478 payload[2] = MARKLIN_CHANGE_DIR_B2;
480 header_raw_data |= 0b01100000;
static const uint8_t marklin_address[3]
Helper array to translate a marklin address to a bit sequence.
static const uint8_t marklin_fn_bits[5]
Helper array to translate a marklin function set command to a packet.
Helper class for computing CRC-8 according to Dallas/Maxim specification for 1-wire protocol.
void update16(uint8_t message_byte)
Processes one byte of the incoming message.
LogonEnableParam
Parameters for the Logon Enable command.
@ DCC_LOGON_ENABLE
Logon enable in the 254 address partition.
@ ADDRESS_LOGON
Address partition used for logon features per S-9.2.1.1 and RCN-218.
@ DCC_LONG_ADDRESS_FIRST
Prefix for DCC long addresses (first byte).
@ DCC_SELECT
Select command in the 254 address partition.
@ DCC_LOGON_ASSIGN
Logon Assign command the 254 address partition.
@ ADDRESS_EXT
Address partition used for advanced extended packets per S-9.2.1.1.
#define HASSERT(x)
Checks that the value of expression x is true, else terminates the current process.
uint8_t skip_ec
typically for DCC packets: 1: do NOT append an EC byte to the end of the packet.
Stores a DCC packet in memory.
uintptr_t feedback_key
An opaque key used by the hardware driver to attribute feedback information to the source of the pack...
uint8_t payload[DCC_PACKET_MAX_PAYLOAD]
Packet payload bytes.
uint8_t dlc
Specifies the number of used payload bytes.
Strongly typed wrapper representing a long DCC address.
uint16_t value
Address value.
Strongly typed wrapper representing a short DCC address.
uint8_t value
Address value.
Strongly typed wrapper representing a marklin-motorola protocol address.
uint8_t value
Address value.
void start_mm_packet()
Sets the packet type to marklin-motorola.
void set_dcc_reset_all_decoders()
Creates a DCC reset-all-decoders packet.
void add_mm_new_speed(bool is_fwd, unsigned speed)
Sets the packet to a direction-aware 14-step MM speed-and-light packet.
void add_dcc_speed28(bool is_fwd, unsigned speed)
Adds a speed-and-direction command (dcc baseline command) to the packet.
void start_dcc_svc_packet()
Initializes the packet structure for a regular DCC packet.
void add_dcc_pom_write1(unsigned cv_number, uint8_t value)
Adds a DCC POM write single CV command and the xor byte.
static const unsigned EMERGENCY_STOP
Send this speed step to emergency-stop the locomotive.
void add_dcc_basic_accessory(unsigned address, bool is_activate)
Adds a DCC basic accessory decoder command packet and the checksum byte.
void add_mm_speed(unsigned speed)
Sets the packet to a 14-step MM speed-and-light packet.
void set_dcc_svc_write_byte(unsigned cv_number, uint8_t value)
Sets the packet to a DCC service mode packet writing the contents of an entire CV.
void set_dcc_basic_accy_params(bool is_normal, bool is_activate)
Call this function after setting a basic accy address to set the accessory packet options.
void set_dcc_svc_paged_verify_reg(uint8_t reg, uint8_t value)
Sets the packet to a DCC service mode packet in Paged Mode, setting the page register.
void set_dcc_logon_assign(uint64_t decoder_id, uint16_t address)
Sets the packet to a logon assign packet.
void add_dcc_function_hi(uint8_t base, uint8_t values)
Adds a DCC function group command to the packet.
void set_dcc_svc_write_bit(unsigned cv_number, unsigned bit, bool desired)
Sets the packet to a DCC service mode packet verifying the contents of a single bit in a CV.
unsigned set_mm_speed_bits(unsigned speed)
Sets the speed bits of an MM packet.
void add_dcc_pom_read1(unsigned cv_number)
Adds a DCC POM read single CV command and the xor byte.
void add_dcc_analog_function(uint8_t fn, uint8_t value)
Adds a DCC analog function control command to the packet.
void set_dcc_select_shortinfo(uint64_t decoder_id)
Sets the packet to a Select+GetShortInfo packet.
void add_dcc_speed128(bool is_fwd, unsigned speed)
Adds a speed-and-direction command (dcc extended command) for 128 speed steps to the packet.
void add_dcc_prog_command(uint8_t cmd_hi, unsigned cv_number, uint8_t value)
Helper function for adding programming mode packets.
void set_dcc_svc_verify_byte(unsigned cv_number, uint8_t value)
Sets the packet to a DCC service mode packet verifying the contents of an entire CV.
void set_dcc_svc_verify_bit(unsigned cv_number, unsigned bit, bool expected)
Sets the packet to a DCC service mode packet verifying the contents of a single bit in a CV.
void add_dcc_function0_4(unsigned values)
Adds a DCC function group command to the packet.
void add_dcc_function5_8(unsigned values)
Adds a DCC function group command to the packet.
void add_dcc_speed14(bool is_fwd, bool light, unsigned speed)
Adds a speed-and-direction command (dcc baseline command) ot the packet.
void add_mm_new_fn(unsigned fn_num, bool value, unsigned speed)
Creates a speed-and-fn packet for the new MM format.
void start_dcc_packet()
Initializes the packet structure for a regular DCC packet.
void set_dcc_logon_enable(Defs::LogonEnableParam param, uint16_t cid, uint8_t session_id)
Sets the packet to a logon enable packet.
static const unsigned MAX_PAYLOAD
Maximum number of payload bytes.
static const unsigned CHANGE_DIR
Send this speed step to switch direction of the locomotive.
void add_dcc_address(DccShortAddress address)
Adds the header to the packet needed for addressing a DCC locomotive.
void add_dcc_ext_accessory(unsigned address, uint8_t aspect)
Adds a DCC extended accessory decoder command packet and the checksum byte.
void add_dcc_checksum()
Appends one byte to the packet payload that represents the XOR checksum for DCC.
void add_mm_address(MMAddress address, bool light)
Sets the address and F0 bits of an MM packet to a specific loco address.
void add_dcc_accy_address(bool is_basic, unsigned address)
Adds the header to the packet needed for addressing a DCC accessory (e.g.
void add_dcc_binary_state(uint16_t fn, bool value)
Adds a DCC binary state control command to the packet.
void mm_shift()
Shifts a MM packet to the second half of the packet buffer.
void set_dcc_svc_paged_write_reg(uint8_t reg, uint8_t value)
Sets the packet to a DCC service mode packet in Paged Mode, setting any register.
void add_dcc_function9_12(unsigned values)
Adds a DCC function group command to the packet.
void set_dcc_idle()
Creates a DCC idle packet.