Skip to content

Commit

Permalink
Query function for throttles. (#461)
Browse files Browse the repository at this point in the history
The query function allows the throttle to request the current function state from the remote (train).
Adds query_fn to the Throttle interface.
Fixes todo to invoke client callback when a function state response arrives.
  • Loading branch information
balazsracz authored Nov 7, 2020
1 parent cb7ec93 commit 065bbc1
Showing 1 changed file with 32 additions and 6 deletions.
38 changes: 32 additions & 6 deletions src/openlcb/TractionThrottle.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,16 @@ class TractionThrottleInterface
: public openlcb::TrainImpl
{
public:
/// Flips a function on<>off.
virtual void toggle_fn(uint32_t fn) = 0;

/// Sends a query for a function to the server. The response will be
/// asynchronously reported by the throttle listener update callback.
/// @param fn function number.
virtual void query_fn(uint32_t fn)
{
}

/// Determine if a train is currently assigned to this trottle.
/// @return true if a train is assigned, else false
virtual bool is_train_assigned() = 0;
Expand Down Expand Up @@ -295,7 +303,15 @@ public:
}
set_fn(fn, fnstate);
}


/// Sends out a function query command. The throttle listener will be
/// called when the response is available.
/// @param address function to query.
void query_fn(uint32_t address) override
{
send_traction_message(TractionDefs::fn_get_payload(address));
}

uint32_t legacy_address() override
{
return 0;
Expand Down Expand Up @@ -567,15 +583,19 @@ private:
}
}

void pending_reply_arrived()
/// Notifies that a pending query during load has gotten a reply.
/// @return true if we were in the load state.
bool pending_reply_arrived()
{
if (pendingQueries_ > 0)
{
if (!--pendingQueries_)
{
timer_.trigger();
}
return true;
}
return false;
}

void speed_reply(Buffer<GenMessage> *msg)
Expand All @@ -592,26 +612,32 @@ private:
{
case TractionDefs::RESP_QUERY_SPEED:
{
pending_reply_arrived();
bool expected = pending_reply_arrived();
Velocity v;
if (TractionDefs::speed_get_parse_last(p, &v))
{
lastSetSpeed_ = v;
/// @todo (balazs.racz): call a callback for the client.

if (updateCallback_ && !expected)
{
updateCallback_(-1);
}
/// @todo (Stuart.Baker): Do we need to do anything with
/// estopActive_?
}
return;
}
case TractionDefs::RESP_QUERY_FN:
{
pending_reply_arrived();
bool expected = pending_reply_arrived();
uint16_t v;
unsigned num;
if (TractionDefs::fn_get_parse(p, &v, &num))
{
lastKnownFn_[num] = v;
if (updateCallback_ && !expected)
{
updateCallback_(num);
}
}
}
}
Expand Down

0 comments on commit 065bbc1

Please sign in to comment.