Handle INIT messages and add set_init_handler() callback#22
Open
brocci wants to merge 1 commit into
Open
Conversation
Route INIT ('I') packets through the data decoder and expose
the configuration payload to sketches via an optional callback.
Replaces the old behavior of silently discarding INIT payloads.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Route INIT ('I') packets through the data decoder and expose the configuration payload to sketches via an optional callback.
Summary
NMRA CMRInet (LCS-9.10.1) defines four message types: Init (
I), Poll (P), Transmit Data / SET (T), and Receive Data / GET (R).The Init message is the first message sent to a node after power-up, carrying configuration data such as the node type, transmit character delay, and card layout.
Previously the library had two problems:
This PR fixes both: Init payloads are decoded and stored, and a
callback mechanism lets the sketch inspect NDP, delay, and option bytes.
Changes
Init message handling (
_decode)Treat
Ithe same asTinDECODE_CMD— enterDECODE_DATAto consume the payload bytes (with DLE un-escaping) until ETX. The existing (unused)_rx_packet_typemember stores the current command type, soPOSTAMBLE_SETreturns eitherSETorINITto the caller.void set_init_handler(void (*handler)(const uint8_t *data, int len))Registers a callback that fires on every complete INIT packet.
datalenINIT payload format (NMRA LCS-9.10.1)
C)(DLH * 256 + DLL) * 10microsecondsInternal changes
_rx_packet_typeis now written duringDECODE_CMD(repurposed from its previous unused declaration)_rx_data_lentracks the actual number of payload bytes received (saved inPOSTAMBLE_SETbefore resetting_rx_index)_init_handlerfunction pointer, defaultnullptr(no overhead when unused)process_char()when_decode()returnsINITprocess_char()returnstruefor INIT messages (consistent with SET)No memory allocation, no extra includes, no performance impact when no handler is registered.
Example
See examples/init_handler/init_handler.ino for the full example.
Closes #20