class documentation

class DCCCommand:

Constructor: DCCCommand()

View In Hierarchy

DCC Command

This class manages the DCC command packets. It provides the APIs for the registration of DCC commands to be serialised. It performs the scheduling and tranmission of command packets.

Class Method get_instance Get the DCC Command instance.
Method __init__ DCC Command object constructor
Method power DCC Power On/Off
Method read_cv Read CV (POM)
Method reset_counts Reset the command counts
Method set_fg1 Set Function Group 1
Method set_speed Set Speed (including direction)
Async Method wait_for_flag Wait for the new state available flag
Method write_cv Write CV (POM)
Constant FWD Forward direction
Constant OFF Power Off
Constant ON Power On
Constant REV Reverse direction
Constant STOP Stopped N.B this is included for completeness
Property counts Get the command counts
Method _add_cmd Add Command to Packet List
Method _get_cmd Get a command from the packet list
Method _nxt_packet Generate next packet.
Method _pom_cmd Process Program on Main command
Class Variable _dcc_cmd Undocumented
Instance Variable _active_address Undocumented
Instance Variable _counts Undocumented
Instance Variable _dcc_gen_pio Undocumented
Instance Variable _idle_packet Undocumented
Instance Variable _packet_iter Undocumented
Instance Variable _packet_list Undocumented
Instance Variable _pom_packet Undocumented
Instance Variable _ready_flag Undocumented
@classmethod
def get_instance(cls):

Get the DCC Command instance.

This returns the singleton DCC Command instance. It instantiates DCC Command on the first call.

Returns
The DCC Command instance
def __init__(self):

DCC Command object constructor

This initialises the DCC command manager singleton. An attempt to create a 2nd instance will cause an assertion error.

The dictionary for the packet list is created and the FIFO buffer allocated.

def power(self, p=None):

DCC Power On/Off

Start and stop command packet transmission scheduling. PIO stop start and power to track delegated to the DCC tx class (pio_pwr).

Changing the power state will cause the new power state available flag to be set.

Parameters
p1 for power on, 0 for power off, None for get power status
Returns
power status as held by the DCC generator
def read_cv(self, address, cv_num):

Read CV (POM)

This initiates reading a CV using Programming on Main in conjunction with RailCom.

The command is validated and the read request scheduled for action. The addressed decoder must be active and the command will be rejected by the command generator class this is not true.

Parameters
addressdecoder address
cv_numcv number as entered - users count from 1, DCC counts from 0!
def reset_counts(self):

Reset the command counts

This resets the command counts to zero. It is used to reset the command counts after a report has been printed.

def set_fg1(self, address, f_num, state):

Set Function Group 1

This sets or clears a function in group 1. The forward light is usually function number 0.

If there is a function group 1 command in the packet list for the addressed decoder it is updated. Otherwise the command is added to the list.

See NMRA S-9.2.1 Section 2.3.4

Parameters
addressthe address of the decoder - may be short or long
f_numfunction number to set or clear
state1 for set, 0 for clear
Returns
True if validation is passed and the command is added to the list or modified. False if validation fails.
def set_speed(self, address, dir, speed=0):

Set Speed (including direction)

If there is a speed command object for the adressed decoder in the list already the object is updated otherwise a new speed command is created. The input is validated. The packet generated will be for a 128 step speed setting and decoders must be configured for 28/128 speed steps.

See NMRA S-9.2.1 Section 2.3.2.1

Parameters
addressthe address of the decoder - may be short or long
dirthe direction - forward or reverse - stop is treated as invalid
speedthe speed to be set - range 0 to 127 - default 0
Returns
True if validation is passed and the command is added to the list or modified. False if validation fails.
async def wait_for_flag(self):

Wait for the new state available flag

This waits for the asynchio thread safe flag to be set. Setting the flag is triggered by a change to the power on/off status.

def write_cv(self, address, cv_num, new_val):

Write CV (POM)

This initiates writing a CV using Programming on Main in conjunction with RailCom.

The command is validated and the write request scheduled for action. The addressed decoder must be active and the command will be rejected by the command generator class this is not true.

Parameters
self
address
cv_numcv number as entered - users count from 1, DCC counts from 0!
new_valthe new value for the CV
FWD =

Forward direction

Value
const(1)
OFF =

Power Off

Value
const(0)
ON =

Power On

Value
const(1)
REV =

Reverse direction

Value
const((-1))
STOP =

Stopped N.B this is included for completeness

Value
const(0)
@property
counts =

Get the command counts

This returns the counts of commands sent by type. The counts are reset by the reset_counts method.

Returns
A dictionary of command counts by type.
def _add_cmd(self, command):

Add Command to Packet List

def _get_cmd(self, key):

Get a command from the packet list

raises KeyError if not found

def _nxt_packet(self, _):

Generate next packet.

This runs in soft interrupt or timer callback context.

This is called when the next packet in the list is to be serialised out on the DCC interface. If the list is empty or if the next packet is unavailable (e.g. being updated) the DCC Idle packet is serialised.

The function is triggered via a soft ISR. If RailCom is enabled, this is connected to the enable pin rising indicating the end of the cutout period assocated with the preceding command.

TODO If RailCom not enabled this will be triggered by the PIO program itself when serialising the packet end bit.

This function instructs the next Command object in the list to send its command.

def _pom_cmd(self, command):

Process Program on Main command

The response to a POM command may be delayed. The decoder does not have to put the POM response in the immediately following window and may respond following a subsequent command to that decoder.

To ensure that there is a subsequent command the address is checked to see if in the active list. The command will be rejected if the address is not in the active list or there is a POM command already being processed.

Returns
True if command accepted False if command rejected
_dcc_cmd =

Undocumented

_active_address: set =

Undocumented

_counts: dict =

Undocumented

_dcc_gen_pio =

Undocumented

_idle_packet =

Undocumented

_packet_iter =

Undocumented

_packet_list: dict =

Undocumented

_pom_packet =

Undocumented

_ready_flag =

Undocumented