Skip to content

Commit

Permalink
Abstract the "generic" portions of DccAccConsumer into a "base" class. (
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerstu authored Dec 6, 2022
1 parent f6624db commit 6ded55b
Showing 1 changed file with 43 additions and 13 deletions.
56 changes: 43 additions & 13 deletions src/openlcb/DccAccyConsumer.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,15 @@
namespace openlcb
{

class DccAccyConsumer : public SimpleEventHandler
/// Base (generic protocol) implementation of a DCC accessory consumer.
class DccAccyConsumerBase : public SimpleEventHandler
{
public:
protected:
/// Constructs a listener for DCC accessory control.
/// @param node is the virtual node that will be listening for events and
/// responding to Identify messages.
/// @param track is the interface through which we will be writing DCC
/// accessory packets.
DccAccyConsumer(Node *node, dcc::TrackIf *track)
DccAccyConsumerBase(Node *node)
: node_(node)
, track_(track)
{
EventRegistry::instance()->register_handler(
EventRegistryEntry(
Expand All @@ -67,7 +65,8 @@ public:
memset(isStateKnown_, 0, sizeof(isStateKnown_));
}

~DccAccyConsumer()
/// Destructor.
~DccAccyConsumerBase()
{
EventRegistry::instance()->unregister_handler(this);
}
Expand Down Expand Up @@ -111,11 +110,7 @@ public:
lastSetState_[eventOfs_] &= ~m;
}

dcc::TrackIf::message_type *pkt;
mainBufferPool->alloc(&pkt);
pkt->data()->add_dcc_basic_accessory(dccAddress_, onOff_);
pkt->data()->packet_header.rept_count = 3;
track_->send(pkt);
send_accy_command();
}

void handle_identify_consumer(const EventRegistryEntry &entry,
Expand Down Expand Up @@ -156,7 +151,9 @@ public:
done->new_child());
}

private:
/// Send the actual accessory command.
virtual void send_accy_command() = 0;

/// Parses an event into an openlcb accessory offset.
/// @return true if the event is in the accessory range, false if this
/// event can be ignored.
Expand Down Expand Up @@ -216,6 +213,39 @@ private:

/// OpenLCB node to export the consumer on.
Node *node_;
};

/// Specialized (DCC protocol) implementation of a DCC accessory consumer.
class DccAccyConsumer : public DccAccyConsumerBase
{
public:
/// Constructs a listener for DCC accessory control.
/// @param node is the virtual node that will be listening for events and
/// responding to Identify messages.
/// @param track is the interface through which we will be writing DCC
/// accessory packets.
DccAccyConsumer(Node *node, dcc::TrackIf *track)
: DccAccyConsumerBase(node)
, track_(track)
{
}

/// Destructor.
~DccAccyConsumer()
{
}

private:
/// Send the actual accessory command.
void send_accy_command() override
{
dcc::TrackIf::message_type *pkt;
mainBufferPool->alloc(&pkt);
pkt->data()->add_dcc_basic_accessory(dccAddress_, onOff_);
pkt->data()->packet_header.rept_count = 3;
track_->send(pkt);
}

/// Track to send DCC packets to.
dcc::TrackIf *track_;
};
Expand Down

0 comments on commit 6ded55b

Please sign in to comment.