LCC Fusion Project 1.0
LCC Automation
Loading...
Searching...
No Matches
SensorConfiguredProducer Class Reference

Version of the SensorConfiguredProducer that can handle many GPIO sensor pins with two events each. More...

#include <SensorConfiguredProducer.h>

Inheritance diagram for SensorConfiguredProducer:
Collaboration diagram for SensorConfiguredProducer:

Public Types

typedef ButtonCardConfig ButtonCardConfig_type
 
typedef SensorCardConfig SensorCardConfig_type
 
typedef BodCardConfig BodCardConfig_type
 
typedef PodCardConfig PodCardConfig_type
 

Public Member Functions

template<unsigned NUM_CARDS>
 SensorConfiguredProducer (openlcb::Node *pNode, const openlcb::RepeatedGroup< ButtonCardConfig_type, NUM_CARDS > &configOffset, uint8_t numDevicesPerCard, EventDeviceTypes eventDeviceType, const char *cardDescription) __attribute__((noinline))
 constructor More...
 
bool processPinState (int cardIndex, int pinIndex)
 Polls the state of pins on sensor cards and triggers events on state changes. More...
 
void factory_reset (int fd) OVERRIDE
 
bool ensureUniqueI2cOffset (uint8_t cardIndex, uint8_t bus, uint8_t offset)
 

Public Attributes

union I2cCardHelper::EventUserArgsUnion userArgs
 

Protected Member Functions

void _do_unregister ()
 Unregisters this event handler from the global registry.
 
void handle_identify_consumer (const openlcb::EventRegistryEntry &registry_entry, openlcb::EventReport *event, BarrierNotifiable *done) OVERRIDE
 Process request to identify produced events.
 
void handle_identify_producer (const openlcb::EventRegistryEntry &registry_entry, openlcb::EventReport *event, BarrierNotifiable *done) OVERRIDE
 Process request to identify produced events.
 
virtual void SendEventIdentified (const openlcb::EventRegistryEntry &registry_entry, openlcb::EventReport *event, BarrierNotifiable *done)
 Sends Producer- or Consumer-Identified for registry_entry. More...
 
void applyI2cInfo (int fd, uint8_t cardIndex, i2cInfoConfig_type cfgI2cInfoRef)
 sets up the MCP card's I2C connection using the user provided (CDI) configuration information (bus and address offset) More...
 
void resetCardInfo (int fd, cardInfoConfig_type cfgI2cInfoRef)
 resets the card configuration information (description field). More...
 
void resetI2cInfo (int fd, i2cInfoConfig_type cfgI2cInfoRef)
 resets the card's I2C configuration information (bus and address fields). More...
 
virtual void drive_pin (uint8_t card, uint8_t pin, bool set)
 
virtual void drive_pin_ext (uint8_t card, uint8_t pin, bool set, uint8_t intArg)
 
EventUserArgsUnion unpack_user_args (const openlcb::EventRegistryEntry &re) const
 Small helper to unpack our 4-byte user_arg payload from the registry entry.
 
void handle_event_report (const openlcb::EventRegistryEntry &registry_entry, openlcb::EventReport *event, BarrierNotifiable *done) OVERRIDE
 

Private Member Functions

template<typename GroupType >
void _processApplyCards (GroupType &grpCardsRef, int fd)
 Applies the current CDI configuration for all cards and lines in a group. More...
 
void _checkPins (uint8_t cardIndex, uint8_t firstPin, uint8_t lastPin)
 Sanity-checks a contiguous range of MCP23017 GPIO pins on one card and logs the results. More...
 
template<typename GroupType >
void _processResetCards (GroupType &grpCardsRef, int fd)
 Factory-resets CDI configuration for all sensors on all configured cards in a group. More...
 
void _setPowerOnState (uint8_t cardNum, uint8_t pinNum, DefaultDeviceSettings::DeviceStates state, uint8_t debounceCount)
 Sets the power-on state for a specific pin on a specific card. More...
 
void _initI2cCards (bool isProducer)
 Initializes the array of I2C card objects using the configured card type. More...
 

Private Attributes

DefaultDeviceSettings defaultDeviceSettings_
 Shadow copy of the event IDs produced by each of the UOD cards.
 

Detailed Description

Version of the SensorConfiguredProducer that can handle many GPIO sensor pins with two events each.

Note
Typically used to support a HW Sensor card.

Definition at line 112 of file SensorConfiguredProducer.h.

Member Typedef Documentation

◆ BodCardConfig_type

Type for the sensor card CDI configuration entry

Definition at line 117 of file SensorConfiguredProducer.h.

◆ ButtonCardConfig_type

Type for the sensor card CDI configuration entry

Definition at line 115 of file SensorConfiguredProducer.h.

◆ PodCardConfig_type

Type for the sensor card CDI configuration entry

Definition at line 118 of file SensorConfiguredProducer.h.

◆ SensorCardConfig_type

Type for the sensor card CDI configuration entry

Definition at line 116 of file SensorConfiguredProducer.h.

Constructor & Destructor Documentation

◆ SensorConfiguredProducer()

template<unsigned NUM_CARDS>
SensorConfiguredProducer::SensorConfiguredProducer ( openlcb::Node *  pNode,
const openlcb::RepeatedGroup< ButtonCardConfig_type, NUM_CARDS > &  configOffset,
uint8_t  numDevicesPerCard,
EventDeviceTypes  eventDeviceType,
const char *  cardDescription 
)
inline

constructor

Parameters
pNodeOpenLCB node object from the stack
configOffsetrepeated group object from the configuration space that represents all the events for each sensor card
numDevicesPerCardnumber of sensors configured for each sensor card.
eventDeviceTypeEventDeviceTypes value identify the type of device, used when setting default event ID
cardDescriptionhuman-friendly card description for log messages and debug output ( e .g. "Input Card").
Usage:
REGISTER_CARD(SensorConfiguredPC, cfg.sensorCardsSeg().sensorCardConfig(), "Producer", "Input Card", NUM_INPUTS_PER_CARD, EventDeviceTypes::INPUT)
#define REGISTER_CARD(_ClassName, _CdiCardConfig, _CardType, _CardDescription, _CardNumDevices, _Card_Event_Type)
creates a unqiue name for the objects used in registering the card, then registers the card

Definition at line 133 of file SensorConfiguredProducer.h.

Member Function Documentation

◆ _checkPins()

void SensorConfiguredProducer::_checkPins ( uint8_t  cardIndex,
uint8_t  firstPin,
uint8_t  lastPin 
)
inlineprivate

Sanity-checks a contiguous range of MCP23017 GPIO pins on one card and logs the results.

Iterates pins firstPin through lastPin (inclusive) on the specified card and probes each pin's digital state via the MCP23017. For each pin, a short, human-readable status string is emitted to the Serial Monitor (e.g., "OK", "GND (Error or triggered)"), using the project’s SENSOR_TESTING_SELECTOR_INFO message key.

This helper is meant for assembly/bring-up testing when g_isTesting is enabled.

Parameters
cardIndexZero-based card index into I2cCardHelper::i2cCards.
firstPinFirst MCP23017 pin to check (0–15).
lastPinLast MCP23017 pin to check (0–15), inclusive.
Precondition
The card’s MCP23017 has been initialized and set to INPUT with pull-ups as needed (e.g., via initI2cCardPins()).
g_pSerialMonitor is non-null if log output is desired.
Note
MCP23017 pins are zero-indexed: 0–7 map to GPA0..GPA7, 8–15 to GPB0..GPB7.
The range is inclusive. Passing (0, 15) checks all pins.
Warning
Reading outside 0–15 is invalid. Ensure firstPin <= lastPin.
Floating inputs (no pull-up / external drive) may read unpredictably.
See also
I2cCardHelper::i2cCards
I2cCardHelper::isMcpPinLow()
initI2cCardPins()
Example
// Verify selector rails and user inputs on card 0
_checkPins(0, 0, 15);
void _checkPins(uint8_t cardIndex, uint8_t firstPin, uint8_t lastPin)
Sanity-checks a contiguous range of MCP23017 GPIO pins on one card and logs the results.

Definition at line 553 of file SensorConfiguredProducer.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _initI2cCards()

void I2cCardHelper::_initI2cCards ( bool  isProducer)
inlineprivateinherited

Initializes the array of I2C card objects using the configured card type.

Allocates and initializes each I2C card in the array to the type specified by the cardType_ member. If isProducer is true, registers each card's polling callback.

Parameters
isProducerTrue if the card is used as a producer and requires polling.

Definition at line 460 of file I2cCardHelper.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _processApplyCards()

template<typename GroupType >
void SensorConfiguredProducer::_processApplyCards ( GroupType &  grpCardsRef,
int  fd 
)
inlineprivate

Applies the current CDI configuration for all cards and lines in a group.

Invoked on first CDI load (after reboot) and whenever CDI is updated. This routine walks each configured card in the supplied repeated group and performs the following per-card steps:

  1. I²C setup: Reads card-level I²C info from CDI and calls I2cCardHelper::applyI2cInfo(...) to validate offsets, check for duplicates, and (re)open the device connection.
  2. GPIO init: Initializes all MCP23017 pins to a known state (e.g., INPUT with pull-ups) via initI2cCardPins(...).
  3. Pin shadowing: Loads the persisted pin-state shadow into defaultDeviceSettings_ with retrievePinStates(...) so subsequent logic has an accurate baseline.
  4. Line configuration (for each device/line on the card):
    • Reads initialSensorState, eventOn, eventOff, and debounceCount from CDI.
    • Marks this helper as a producer and registers both events with OpenMRN.
    • Seeds the debouncer/power-on state via _setPowerOnState(...) without emitting an event.
    • Stores the (on/off) event IDs into producerEventIds_ for later use by notify().
  5. Persist shadow: Stores the updated pin-state shadow back for this card.

If testing mode (g_isTesting) is enabled, the function also probes specific MCP pins and prints human-readable diagnostics to the serial monitor to assist bring-up.

Template Parameters
GroupTypeA CDI repeated-group type providing:
  • entry(unsigned) → per-card config proxy
  • On card proxy: i2cInfoConfig(), sensorConfig()
  • On sensor group: entry(unsigned) → per-line group
  • On line group: sensorLineConfig(), initialSensorState(), debounceCount()
  • On line config: eventOn(), eventOff()
Parameters
grpCardsRefReference to the repeated group of card configurations (e.g., openlcb::RepeatedGroup<ButtonCardConfig_type, ...>).
fdFile descriptor used by the OpenMRN configuration backend for CDI reads/writes.
Precondition
  • numCards_ and numDevicesPerCard_ reflect the active CDI layout.
  • grpCardsRef references a valid CDI segment; card indices in [0, numCards_) are present.
  • The I²C hardware for addressed cards is powered and physically reachable.
Postcondition
  • OpenMRN event registry contains all eventOn/eventOff producer events found in CDI.
  • Debouncer/power-on states are initialized but no events are emitted as a side effect.
  • Pin-state shadow is synchronized for each processed card.
Note
  • MCP23017 pins are zero-indexed (0–15). Ranges passed to initI2cCardPins() must respect the helper’s inclusive/exclusive contract.
  • If CDI assigns identical event IDs to multiple lines, registration will still occur; conflict handling is deferred to higher layers.
Warning
  • Floating inputs (no pull-up or external drive) may read unpredictably; ensure pull-ups are enabled where appropriate.
  • This function writes to CDI-backed storage; avoid concurrent configuration writes.
See also
apply_configuration()
I2cCardHelper::applyI2cInfo()
I2cCardHelper::registerEvent()
defaultDeviceSettings_.retrievePinStates()
defaultDeviceSettings_.storePinStates()
Example
// Apply all configured Sensor Cards (type deduced from grp)
auto grp = cfg.inputCardsSeg().sensorCards(); // RepeatedGroup<SensorCardConfig_type, ...>
void _processApplyCards(GroupType &grpCardsRef, int fd)
Applies the current CDI configuration for all cards and lines in a group.

Definition at line 451 of file SensorConfiguredProducer.h.

Here is the call graph for this function:

◆ _processResetCards()

template<typename GroupType >
void SensorConfiguredProducer::_processResetCards ( GroupType &  grpCardsRef,
int  fd 
)
inlineprivate

Factory-resets CDI configuration for all sensors on all configured cards in a group.

Walks every card (0..numCards_-1) in grpCardsRef and:

No events are emitted; this only updates CDI storage through fd.

Template Parameters
GroupTypeA CDI repeated-group type providing:
  • entry(unsigned) -> card entry proxy
  • On card proxy: cardInfoConfig(), i2cInfoConfig(), sensorConfig()
  • On line-group proxy: entry(unsigned) -> line group
  • On line group: initialSensorState(), debounceCount(), sensorLineConfig()
  • On line config: description(), eventOn(), eventOff()
Parameters
grpCardsRefReference to the card repeated group to reset (e.g., RepeatedGroup<ButtonCardConfig_type, ...> or RepeatedGroup<SensorCardConfig_type, ...>).
fdFile descriptor used by the OpenMRN configuration backend for reads/writes.
Precondition
pNode_ is valid; numCards_ and numDevicesPerCard_ reflect the CDI layout.
The GroupType instance refers to the correct CDI segment (offset is valid).
Postcondition
Card and line CDI entries are reset to factory defaults; subsequent identify or apply passes will observe the new values.
Note
Whether I2cCardHelper::nextDefaultEvent is advanced internally by NEXT_EVENT is implementation-specific. If not, ensure that it is post-incremented at the call sites (e.g., pass I2cCardHelper::nextDefaultEvent++ as the macro argument).
Warning
This performs persistent writes through fd. Ensure the configuration space is mounted/available and not concurrently modified.
See also
I2cCardHelper::resetCardInfo()
I2cCardHelper::resetI2cInfo()
NEXT_EVENT
CDI_FACTORY_RESET
Example
// Reset all cards of the currently-selected group (type deduced from grpCardsRef)
_processResetCards(grpCardsRef, fd);
void _processResetCards(GroupType &grpCardsRef, int fd)
Factory-resets CDI configuration for all sensors on all configured cards in a group.

Definition at line 614 of file SensorConfiguredProducer.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _setPowerOnState()

void SensorConfiguredProducer::_setPowerOnState ( uint8_t  cardNum,
uint8_t  pinNum,
DefaultDeviceSettings::DeviceStates  state,
uint8_t  debounceCount 
)
inlineprivate

Sets the power-on state for a specific pin on a specific card.

Seed debouncer/state at power-up from CDI initial setting (no event emission here).

Parameters
cardNumThe card number on which the pin resides. This is used to index into the I2cCardHelper::i2cCards array of I2C card helpers.
pinNumThe pin number to set the state for. This should be within the valid range for the card.
stateThe desired initial state of the pin specified by the DefaultDeviceSettings::DeviceStates enum.
debounceCountThe debounce count configured by user

Definition at line 646 of file SensorConfiguredProducer.h.

Here is the caller graph for this function:

◆ applyI2cInfo()

void I2cCardHelper::applyI2cInfo ( int  fd,
uint8_t  cardIndex,
i2cInfoConfig_type  cfgI2cInfoRef 
)
inlineprotectedinherited

sets up the MCP card's I2C connection using the user provided (CDI) configuration information (bus and address offset)

Note
producer/consumers apply_configuration() callback invoke this method to initialize the I2C / MCP connection information for the card
all I2C connected cards have CDI section for user provided bus and address information (which must match the settings of the I2C enabeled card)
HOOK: Typical call site is within your apply_configuration() loop:
  • read bus & offset from CDI
  • initialize the corresponding i2cCards[cardIndex]
Parameters
fdfile handle to access the CDI configuration information (provided by openmrn)
cardIndexspecifies which card the configuration is for. Used to update the i2cCards[] array and initialize the connection
cfgI2cInfoRefoffset into the intenal configuration space for the CDI configuration for the MCP card

Definition at line 295 of file I2cCardHelper.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ drive_pin()

virtual void I2cCardHelper::drive_pin ( uint8_t  card,
uint8_t  pin,
bool  set 
)
inlineprotectedvirtualinherited

Hook for consumers: drive one output pin (simple case). Default does nothing; simple consumers override this.

Parameters
cardcard index (as stored in user_arg)
pinpin index (as stored in user_arg)
setdesired logical state from the event semantics (SET/ON = true, CLR/OFF = false)

Reimplemented in IoConfiguredPC, OutputConfiguredConsumer, ServoConfiguredConsumer, TurnoutConfiguredPC, and NeoPixelConfiguredConsumer.

Definition at line 392 of file I2cCardHelper.h.

Here is the caller graph for this function:

◆ drive_pin_ext()

virtual void I2cCardHelper::drive_pin_ext ( uint8_t  card,
uint8_t  pin,
bool  set,
uint8_t  intArg 
)
inlineprotectedvirtualinherited

Extended hook for consumers that also need the CDI intArg (e.g., audio message index). Default forwards to drive_pin(...) so existing consumers remain unchanged.

Reimplemented in AudioConfiguredConsumer.

Definition at line 402 of file I2cCardHelper.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ensureUniqueI2cOffset()

bool I2cCardHelper::ensureUniqueI2cOffset ( uint8_t  cardIndex,
uint8_t  bus,
uint8_t  offset 
)
inlineinherited

Check for duplicate offsets on the same I2C bus. Automatically resets the tracker on the first call per config pass.

Remarks
Call this once per card during your apply_configuration loop, with the CDI values.
Parameters
cardIndexIndex of the card in the config loop.
busI2C bus number (0 or 1).
offsetI2C offset (0–7).
Returns
true if this (bus, offset) was already claimed.

Definition at line 183 of file I2cCardHelper.h.

Here is the caller graph for this function:

◆ factory_reset()

void SensorConfiguredProducer::factory_reset ( int  fd)
inline

Reset CDI configuration entries to their initial (default) values

Parameters
fdused to access the CDI information in the file system

Definition at line 311 of file SensorConfiguredProducer.h.

Here is the call graph for this function:

◆ handle_event_report()

void I2cCardHelper::handle_event_report ( const openlcb::EventRegistryEntry &  registry_entry,
openlcb::EventReport *  event,
BarrierNotifiable *  done 
)
inlineprotectedinherited

Default consumer handler: maps an incoming Event Report to (card,pin,set,intArg) and calls drive_pin_ext(...). Producers are ignored.

Definition at line 418 of file I2cCardHelper.h.

Here is the call graph for this function:

◆ processPinState()

bool SensorConfiguredProducer::processPinState ( int  cardIndex,
int  pinIndex 
)
inline

Polls the state of pins on sensor cards and triggers events on state changes.

Asynchronous callback when the previous polling message has left via the bus. Used as a poor man's iterative state machine.

Iteratively checks each pin of every connected sensor card for state changes. When a change in state is detected, it triggers a corresponding event. This function maintains state across calls, allowing it to resume polling where it left off in the previous call. Only one state change is handled per call to ensure load distribution over time.

Note
This function is repeatedly invoked by openMRN stack (registered via poll_33hz)
After processing all cards, the function resets to start from the first card in the next call.

Definition at line 241 of file SensorConfiguredProducer.h.

Here is the call graph for this function:

◆ resetCardInfo()

void I2cCardHelper::resetCardInfo ( int  fd,
cardInfoConfig_type  cfgI2cInfoRef 
)
inlineprotectedinherited

resets the card configuration information (description field).

Note
invoke from the consumer/producer factory_reset() to reset the card configuration
Parameters
fdfile handle to access the CDI configuration information (provided by openmrn)
cfgI2cInfoRefoffset into the intenal configuration space for the CDI configuration block for the card's info (description, etc)

Definition at line 309 of file I2cCardHelper.h.

Here is the caller graph for this function:

◆ resetI2cInfo()

void I2cCardHelper::resetI2cInfo ( int  fd,
i2cInfoConfig_type  cfgI2cInfoRef 
)
inlineprotectedinherited

resets the card's I2C configuration information (bus and address fields).

Note
invoke from the consumer/producer factory_reset() to reset the card configuration
Parameters
fdfile handle to access the CDI configuration information (provided by openmrn)
cfgI2cInfoRefoffset into the intenal configuration space for the CDI configuration for card's I2C info (bus/offset)

Definition at line 321 of file I2cCardHelper.h.

Here is the caller graph for this function:

◆ SendEventIdentified()

virtual void I2cCardHelper::SendEventIdentified ( const openlcb::EventRegistryEntry &  registry_entry,
openlcb::EventReport *  event,
BarrierNotifiable *  done 
)
inlineprotectedvirtualinherited

Sends Producer- or Consumer-Identified for registry_entry.

Identification type is inferred from the registry entry’s packed user args (see

See also
isProducer). Override to customize the MTI or payload.

Reimplemented in TurnoutConfiguredPC.

Definition at line 273 of file I2cCardHelper.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ userArgs

union I2cCardHelper::EventUserArgsUnion I2cCardHelper::userArgs
inherited

event arguments


The documentation for this class was generated from the following file: