Skip to content
This repository has been archived by the owner on Mar 22, 2021. It is now read-only.

Commit

Permalink
Timer does not support changing period at runtime.
Browse files Browse the repository at this point in the history
  • Loading branch information
m-spiessens committed Jun 9, 2017
1 parent 2b27be9 commit d013592
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 124 deletions.
6 changes: 3 additions & 3 deletions include/flow/components.h
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,14 @@ typedef char Tick;
class Timer: public Flow::Component
{
public:
Flow::InPort<unsigned int> inPeriod;
Flow::OutPort<Tick> outTick;

Timer(unsigned int period);

void run();

private:
unsigned int period = 0;
unsigned int nextPeriod = 0;
const unsigned int period;
unsigned int sysTicks = 0;
};

Expand Down
12 changes: 5 additions & 7 deletions source/flow/components.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,23 @@

#include "flow/components.h"

Timer::Timer(unsigned int period) :
period(period)
{
}

void Timer::run()
{
sysTicks++;

inPeriod.receive(nextPeriod);

if (period > 0)
{
if (sysTicks >= period)
{
sysTicks = 0;
outTick.send(TICK);
period = nextPeriod;
}
}
else
{
period = nextPeriod;
}
}

void Toggle::run()
Expand Down
118 changes: 4 additions & 114 deletions source/flow_test/component_timer_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,19 @@ using Flow::connect;

TEST_GROUP(Component_Timer_TestBench)
{
OutPort<unsigned int> outStimulus;
Connection* outStimulusConnection;
Timer* unitUnderTest;
Connection* inResponseConnection;
InPort<Tick> inResponse;

void setup()
{
unitUnderTest = new Timer();
unitUnderTest = new Timer(100);

outStimulusConnection = connect(outStimulus, unitUnderTest->inPeriod);
inResponseConnection = connect(unitUnderTest->outTick, inResponse);
}

void teardown()
{
disconnect(outStimulusConnection);
disconnect(inResponseConnection);

delete unitUnderTest;
Expand All @@ -71,11 +67,7 @@ TEST(Component_Timer_TestBench, TickPeriod100)
{
CHECK(!inResponse.peek());

unsigned int period = 100;

CHECK(outStimulus.send(period));

for (unsigned int i = 0; i < period - 1; i++)
for (unsigned int i = 0; i < 100 - 1; i++)
{
unitUnderTest->run();
}
Expand All @@ -88,109 +80,7 @@ TEST(Component_Timer_TestBench, TickPeriod100)
CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

for (unsigned int i = 0; i < period - 1; i++)
{
unitUnderTest->run();
}

CHECK(!inResponse.peek());

unitUnderTest->run();

CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

for (unsigned int i = 0; i < period - 1; i++)
{
unitUnderTest->run();
}

CHECK(!inResponse.peek());

unitUnderTest->run();

CHECK(inResponse.receive(tick));
CHECK(tick == TICK);
}

TEST(Component_Timer_TestBench, TickPeriodChange)
{
CHECK(!inResponse.peek());

unsigned int firstPeriod = 100;

CHECK(outStimulus.send(firstPeriod));

for (unsigned int i = 0; i < firstPeriod - 1; i++)
{
unitUnderTest->run();
}

CHECK(!inResponse.peek());

unitUnderTest->run();

Tick tick;
CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

unsigned int secondPeriod = 50;

CHECK(outStimulus.send(secondPeriod));

for (unsigned int i = 0; i < firstPeriod - 1; i++)
{
unitUnderTest->run();
}

CHECK(!inResponse.peek());

unitUnderTest->run();

CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

for (unsigned int i = 0; i < secondPeriod - 1; i++)
{
unitUnderTest->run();
}

CHECK(!inResponse.peek());

unitUnderTest->run();

CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

for (unsigned int i = 0; i < secondPeriod - 1; i++)
{
unitUnderTest->run();
}

CHECK(!inResponse.peek());

unitUnderTest->run();

CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

unsigned int thirdPeriod = 500;

CHECK(outStimulus.send(thirdPeriod));

for (unsigned int i = 0; i < secondPeriod - 1; i++)
{
unitUnderTest->run();
}

CHECK(!inResponse.peek());

unitUnderTest->run();

CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

for (unsigned int i = 0; i < thirdPeriod - 1; i++)
for (unsigned int i = 0; i < 100 - 1; i++)
{
unitUnderTest->run();
}
Expand All @@ -202,7 +92,7 @@ TEST(Component_Timer_TestBench, TickPeriodChange)
CHECK(inResponse.receive(tick));
CHECK(tick == TICK);

for (unsigned int i = 0; i < thirdPeriod - 1; i++)
for (unsigned int i = 0; i < 100 - 1; i++)
{
unitUnderTest->run();
}
Expand Down

0 comments on commit d013592

Please sign in to comment.