|
OpenLcbCLib 1.0 Alpha
OpenSource C Library to create OpenLcb/Lcc Nodes
|
Application-level Train Control Protocol module. More...
Go to the source code of this file.
Data Structures | |
| struct | interface_openlcb_application_train_t |
| Application-provided callbacks for the train module. More... | |
Functions | |
| void | OpenLcbApplicationTrain_initialize (const interface_openlcb_application_train_t *interface) |
| Initialises the train module and stores the callback interface. | |
| train_state_t * | OpenLcbApplicationTrain_setup (openlcb_node_t *openlcb_node) |
| Allocates a train state slot and assigns it to the node. | |
| train_state_t * | OpenLcbApplicationTrain_get_state (openlcb_node_t *openlcb_node) |
| Returns the train state for a node. | |
| void | OpenLcbApplicationTrain_100ms_timer_tick (uint8_t current_tick) |
| Decrements the heartbeat countdown for all active train nodes. | |
| void | OpenLcbApplicationTrain_send_set_speed (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id, uint16_t speed) |
| Sends a Set Speed/Direction command to a train node. | |
| void | OpenLcbApplicationTrain_send_set_function (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id, uint32_t fn_address, uint16_t fn_value) |
| Sends a Set Function command to a train node. | |
| void | OpenLcbApplicationTrain_send_emergency_stop (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id) |
| Sends an Emergency Stop command to a train node. | |
| void | OpenLcbApplicationTrain_send_query_speeds (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id) |
| Sends a Query Speeds command to a train node. | |
| void | OpenLcbApplicationTrain_send_query_function (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id, uint32_t fn_address) |
| Sends a Query Function command to a train node. | |
| void | OpenLcbApplicationTrain_send_assign_controller (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id) |
| Sends an Assign Controller command to a train node. | |
| void | OpenLcbApplicationTrain_send_release_controller (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id) |
| Sends a Release Controller command to a train node. | |
| void | OpenLcbApplicationTrain_send_noop (openlcb_node_t *openlcb_node, uint16_t train_alias, node_id_t train_node_id) |
| Sends a NOOP (no-operation) management command to a train node. | |
| void | OpenLcbApplicationTrain_set_dcc_address (openlcb_node_t *openlcb_node, uint16_t dcc_address, bool is_long_address) |
| Sets the DCC address and address type for a train node. | |
| uint16_t | OpenLcbApplicationTrain_get_dcc_address (openlcb_node_t *openlcb_node) |
| Returns the DCC address for a train node. | |
| bool | OpenLcbApplicationTrain_is_long_address (openlcb_node_t *openlcb_node) |
| Returns true if the train node uses long DCC addressing. | |
| void | OpenLcbApplicationTrain_set_speed_steps (openlcb_node_t *openlcb_node, uint8_t speed_steps) |
| Sets the speed-step mode for a train node. | |
| uint8_t | OpenLcbApplicationTrain_get_speed_steps (openlcb_node_t *openlcb_node) |
| Returns the speed-step mode for a train node. | |
Application-level Train Control Protocol module.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Provides per-node train state, a fixed-size allocation pool, throttle-side send helpers, and a heartbeat countdown timer for the OpenLCB Train Control Protocol.
The protocol handler (protocol_train_handler.c) handles incoming commands automatically. This module provides the application developer API: state allocation, state access, throttle send functions, and the heartbeat tick. State is drawn from a pool sized by USER_DEFINED_TRAIN_NODE_COUNT. Each train node gets a slot via OpenLcbApplicationTrain_setup(), which stores a pointer in node->train_state. Non-train nodes have train_state == NULL.
|
extern |
Initialises the train module and stores the callback interface.
Clears the train state pool and stores the interface pointer. Must be called once before any other function in this module.
| interface | Pointer to a interface_openlcb_application_train_t. |
Algorithm:
* @param interface Pointer to a interface_openlcb_application_train_t. *
|
extern |
Allocates a train state slot and assigns it to the node.
Draws the next free slot from the pool, zeroes it, stores a pointer in openlcb_node->train_state, and registers the standard train event IDs on the node (Train, Emergency Off/Stop, Clear Emergency Off/Stop).
Returns the existing slot immediately if the node already has one.
| openlcb_node | Pointer to the openlcb_node_t to configure as a train. |
Algorithm:
* @param openlcb_node Pointer to the openlcb_node_t to configure as a train. *
|
extern |
Returns the train state for a node.
| openlcb_node | Pointer to the openlcb_node_t. |
Algorithm:
* @param openlcb_node Pointer to the openlcb_node_t. *
|
extern |
Decrements the heartbeat countdown for all active train nodes.
Called from the main loop with the current global tick. At the halfway point of each node's heartbeat timeout, a NOOP request is sent to the controller. When the counter reaches zero the train is emergency-stopped and the on_heartbeat_timeout callback is fired.
| current_tick | Current value of the global 100ms tick counter. |
Algorithm:
* @param current_tick Current value of the global 100ms tick counter. *
|
extern |
Sends a Set Speed/Direction command to a train node.
Builds and sends an addressed Train Protocol message that sets the speed and direction of a target train. The speed value uses the OpenLCB float16 encoding where the sign bit encodes direction.
| openlcb_node | Pointer to the sending (throttle) openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
| speed | 16-bit speed/direction value in OpenLCB float16 format. |
Algorithm:
* @param openlcb_node Pointer to the sending openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. * @param speed 16-bit speed/direction in OpenLCB float16 format. *
|
extern |
Sends a Set Function command to a train node.
Builds and sends an addressed Train Protocol message that sets a DCC function on the target train. Function 0 is the headlight; higher numbers follow the NMRA function mapping.
| openlcb_node | Pointer to the sending openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
| fn_address | 24-bit function address. |
| fn_value | 16-bit function value (0 = off, non-zero = on or analog value). |
Algorithm:
* @param openlcb_node Pointer to the sending openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. * @param fn_address 24-bit function address. * @param fn_value 16-bit function value. *
|
extern |
Sends an Emergency Stop command to a train node.
Commands the target train to stop immediately. The train node sets its speed to zero and activates its emergency-stop flag.
| openlcb_node | Pointer to the sending openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
Algorithm:
* @param openlcb_node Pointer to the sending openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. *
|
extern |
Sends a Query Speeds command to a train node.
Asks the train node to reply with its current set speed, actual speed (if available), and emergency-stop status.
| openlcb_node | Pointer to the sending openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
Algorithm:
* @param openlcb_node Pointer to the sending openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. *
|
extern |
Sends a Query Function command to a train node.
Asks the train node to reply with the current value of the specified function address.
| openlcb_node | Pointer to the sending openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
| fn_address | 24-bit function address to query. |
Algorithm:
* @param openlcb_node Pointer to the sending openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. * @param fn_address 24-bit function address to query. *
|
extern |
Sends an Assign Controller command to a train node.
Sends the throttle node's own Node ID as the controller to assign. Once assigned, the train node accepts speed/function commands only from the assigned controller and begins heartbeat monitoring.
| openlcb_node | Pointer to the sending (throttle) openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
Algorithm:
* @param openlcb_node Pointer to the sending (throttle) openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. *
|
extern |
Sends a Release Controller command to a train node.
Sends the throttle node's own Node ID as the controller to release. The train node clears its controller assignment and stops heartbeat monitoring.
| openlcb_node | Pointer to the sending (throttle) openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
Algorithm:
* @param openlcb_node Pointer to the sending (throttle) openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. *
|
extern |
Sends a NOOP (no-operation) management command to a train node.
Resets the train's heartbeat countdown timer without changing speed, functions, or any other state. Throttle applications should send this periodically to prevent heartbeat timeout and emergency stop.
| openlcb_node | Pointer to the sending openlcb_node_t. |
| train_alias | 12-bit CAN alias of the target train node. |
| train_node_id | 48-bit node_id_t of the target train node. |
Sends a NOOP (no-operation) management command to a train node.
Algorithm:
* @param openlcb_node Pointer to the sending openlcb_node_t. * @param train_alias 12-bit CAN alias of the target train node. * @param train_node_id 48-bit node_id_t of the target train node. *
|
extern |
Sets the DCC address and address type for a train node.
| openlcb_node | Pointer to the openlcb_node_t with an assigned train state. |
| dcc_address | DCC address (1–9999 for long, 1–127 for short). |
| is_long_address | true for long (four-digit) addressing, false for short. |
Algorithm:
* @param openlcb_node Pointer to the openlcb_node_t. * @param dcc_address DCC address value. * @param is_long_address true for long addressing, false for short. *
|
extern |
Returns the DCC address for a train node.
| openlcb_node | Pointer to the openlcb_node_t. |
Algorithm:
* @param openlcb_node Pointer to the openlcb_node_t. *
|
extern |
Returns true if the train node uses long DCC addressing.
| openlcb_node | Pointer to the openlcb_node_t. |
Algorithm:
* @param openlcb_node Pointer to the openlcb_node_t. *
|
extern |
Sets the speed-step mode for a train node.
| openlcb_node | Pointer to the openlcb_node_t with an assigned train state. |
| speed_steps | Speed-step count (14, 28, or 128). |
Algorithm:
* @param openlcb_node Pointer to the openlcb_node_t. * @param speed_steps Speed-step count (14, 28, or 128). *
|
extern |
Returns the speed-step mode for a train node.
| openlcb_node | Pointer to the openlcb_node_t. |
Algorithm:
* @param openlcb_node Pointer to the openlcb_node_t. *
Copyright (c) 2026 Jim Kueneman all rights reserved. See the License