Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
ConfiguredProducer.hxx
Go to the documentation of this file.
1
36#ifndef _OPENLCB_CONFIGUREDPRODUCER_HXX_
37#define _OPENLCB_CONFIGUREDPRODUCER_HXX_
38
44#include "utils/Debouncer.hxx"
45
46namespace openlcb
47{
48
50CDI_GROUP(ProducerConfig);
53 Name("Description"), Description("User name of this input."));
56 debounce, Uint8ConfigEntry, Name("Debounce parameter"),
57 Default(3),
58 Description("Amount of time to wait for the input to stabilize before "
59 "producing the event. Unit is 30 msec of time. Usually a value "
60 "of 2-3 works well in a non-noisy environment. In high noise "
61 "(train wheels for example) a setting between 8 -- 15 makes "
62 "for a slower response time but a more stable "
63 "signal.\nFormally, the parameter tells how many times of "
64 "tries, each 30 msec apart, the input must have the same value "
65 "in order for that value to be accepted and the event "
66 "transition produced."),
67 Default(3));
70 event_on, EventConfigEntry, //
71 Name("Event On"),
72 Description("This event will be produced when the input goes to HIGH."));
75 event_off, EventConfigEntry, //
76 Name("Event Off"),
77 Description("This event will be produced when the input goes to LOW."));
79
80extern "C" {
81void ignore_fn();
82}
83
96{
97public:
98 using Impl = GPIOBit;
100
101 ConfiguredProducer(Node *node, const ProducerConfig &cfg, const Gpio *gpio)
102 : producer_(QuiesceDebouncer::Options(3), node, 0, 0, gpio)
103 , cfg_(cfg)
104 {
106 }
107
108 template <class HW>
109 ConfiguredProducer(Node *node, const ProducerConfig &cfg, const HW &,
110 const Gpio *g = HW::instance(), decltype(HW::instance) * = 0)
111 : producer_(QuiesceDebouncer::Options(3), node, 0, 0, g)
112 , cfg_(cfg)
113 {
115 }
116
117 UpdateAction apply_configuration(int fd, bool initial_load,
118 BarrierNotifiable *done) override
119 {
120 AutoNotify n(done);
121 uint8_t debounce_arg = cfg_.debounce().read(fd);
122 EventId cfg_event_on = cfg_.event_on().read(fd);
123 EventId cfg_event_off = cfg_.event_off().read(fd);
124 if (cfg_event_off != producer_.event_off() ||
125 cfg_event_on != producer_.event_on())
126 {
127 auto saved_gpio = producer_.gpio_;
128 auto saved_node = producer_.node();
129 // Need to reinitialize the producer. We do this with in-place
130 // destruction and construction.
131 producer_.ProducerClass::~ProducerClass();
132 new (&producer_) ProducerClass(
133 QuiesceDebouncer::Options(debounce_arg), saved_node,
134 cfg_event_on, cfg_event_off, saved_gpio);
135 return REINIT_NEEDED; // Causes events identify.
136 }
137 return UPDATED;
138 }
139
141 {
142 cfg_.description().write(fd, "");
143 CDI_FACTORY_RESET(cfg_.debounce);
144 }
145
146 Polling *polling()
147 {
148 return &producer_;
149 }
150
151private:
152 ProducerClass producer_;
153 const ProducerConfig cfg_;
154};
155
156} // namespace openlcb
157
158#endif // _OPENLCB_CONFIGUREDPRODUCER_HXX_
#define CDI_GROUP(GroupName, ARGS...)
Starts a CDI group.
#define CDI_GROUP_ENTRY(NAME, TYPE, ARGS...)
Adds an entry to a CDI group.
#define CDI_FACTORY_RESET(PATH)
Performs factory reset on a CDI variable.
This class sends a notification in its destructor.
A BarrierNotifiable allows to create a number of child Notifiable and wait for all of them to finish.
Abstract class for components that need to receive configuration from EEPROM.
UpdateAction
Specifies what additional steps are needed to apply the new configuration.
@ REINIT_NEEDED
Need to perform application-level reinitialization.
@ UPDATED
No additional step is necessary.
virtual void register_update_listener(ConfigUpdateListener *listener)=0
Adds a config update listener to be called upon configuration updates.
OS-independent abstraction for GPIO.
Definition Gpio.hxx:43
uint8_t Options
Type declaring what opeions we can supply to this class.
Definition Debouncer.hxx:70
static ConfigUpdateService * instance()
Definition Singleton.hxx:77
OpenLCB Producer class integrating a simple CDI-based configuration for two event IDs,...
UpdateAction apply_configuration(int fd, bool initial_load, BarrierNotifiable *done) override
Notifies the component that there is new configuration available for loading.
void factory_reset(int fd) OVERRIDE
Clears configuration file and resets the configuration settings to factory value.
Implementation class for event ID configuration entries.
Simple implementation of the BitEventInterface for going through GPIO ports.
Base class for NMRAnet nodes conforming to the asynchronous interface.
Definition Node.hxx:52
Implementation class for numeric configuration entries, templated by the integer type.
Abstract base class for components that need repeated execution (with a specified frequency,...
Implementation class for string configuration entries.
#define OVERRIDE
Function attribute for virtual functions declaring that this funciton is overriding a funciton that s...
Definition macros.h:180
CDI_GROUP_END()
Signals termination of the group.