Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
SimpleUpdateLoop.cxx
Go to the documentation of this file.
1
36#include "dcc/SimpleUpdateLoop.hxx"
37#include "dcc/Packet.hxx"
38#include "dcc/PacketSource.hxx"
39
40namespace dcc
41{
42
43SimpleUpdateLoop::SimpleUpdateLoop(Service *service, TrackIf *track_send)
44 : StateFlow(service)
45 , trackSend_(track_send)
46 , nextRefreshIndex_(0)
47 , lastCycleStart_(os_get_time_monotonic())
48{
49}
50
51SimpleUpdateLoop::~SimpleUpdateLoop()
52{
53}
54
55StateFlowBase::Action SimpleUpdateLoop::entry()
56{
57 long long current_time = os_get_time_monotonic();
58 long long prev_cycle_start = lastCycleStart_;
59 if (nextRefreshIndex_ >= refreshSources_.size())
60 {
61 nextRefreshIndex_ = 0;
62 lastCycleStart_ = current_time;
63 }
64 if (nextRefreshIndex_ == 0 &&
65 (current_time - prev_cycle_start < MSEC_TO_NSEC(5) ||
66 refreshSources_.empty()))
67 {
68 // We do not want to send another packet to the same locomotive too
69 // quick. We send an idle packet instead. OR: We do not have any
70 // locomotives at all. We will keep sending idle packets.
71 message()->data()->set_dcc_idle();
72 }
73 else
74 {
75 // Send an update to the current loco.
76 refreshSources_[nextRefreshIndex_]
77 ->get_next_packet(0, message()->data());
78 nextRefreshIndex_++;
79 }
80 // We pass on the filled packet to the track processor.
81 trackSend_->send(transfer_message());
82 return exit();
83}
84
85} // namespace dcc
Collection of related state machines that pend on incoming messages.
Return type for a state flow callback.
State flow with a given typed input queue.
long long os_get_time_monotonic(void)
Get the monotonic time since the system started.
Definition os.c:571
#define MSEC_TO_NSEC(_msec)
Convert a millisecond value to a nanosecond value.
Definition os.h:268