HomeWaterLeaksDetection
PulseGenerator Class Reference

#include <PulseGenerator.h>

Public Member Functions

 PulseGenerator (time_t delayHigh, time_t delayLow, int outputPin)
 Creates an instance of the class. More...
 
void update ()
 Updates the state of the output pin. More...
 
void reset ()
 Resets the pulse generator. More...
 

Private Attributes

time_t delayHigh
 delay of the pulse in HIGH state More...
 
time_t delayLow
 delay of the pulse in LOW state More...
 
int outputPin
 number of the pin where the pulses are generated More...
 
int value
 current state of the output pin (HIGH/LOW) More...
 
int pulseCount
 number of pulses that have been generated so far More...
 
time_t timeLastValueChange
 time when the state of the pin changed for the last time More...
 

Detailed Description

Author
silhavyj A17B0362P

Automatic pulse generator used for unit testing.

This class provides the functionality of generating pulses on the output pin. The number of the pin is passed into the constructor along with the parameters of the generated pulses. This class is used only when it comes to unit testing.

Definition at line 19 of file PulseGenerator.h.

Constructor & Destructor Documentation

◆ PulseGenerator()

PulseGenerator::PulseGenerator ( time_t  delayHigh,
time_t  delayLow,
int  outputPin 
)

Creates an instance of the class.

Parameters
delayHighdelay of the pulse in HIGH state
delayLowdelay of the pulse in LOW state
outputPinnumber of the pin where the pulses are generated

Definition at line 3 of file PulseGenerator.cpp.

3  {
4  // stores the values passed through the
5  // constructor into member variables
6  this->delayHigh = delayHigh;
7  this->delayLow = delayLow;
8  this->outputPin = outputPin;
9 
10  // initially reset (default state)
11  reset();
12 }

References delayHigh, delayLow, outputPin, and reset().

Member Function Documentation

◆ reset()

void PulseGenerator::reset ( )

Resets the pulse generator.

When running unit tests, the generator needs to be reset every time when moving on to the next test case (scenario).

Definition at line 14 of file PulseGenerator.cpp.

14  {
15  // resets the member variables
16  // to their default values
17  value = LOW;
19  pulseCount = 0;
20 
21  // set the output pin into LOW state
22  pinMode(outputPin, OUTPUT);
23  digitalWrite(outputPin, value);
24 }

References outputPin, pulseCount, timeLastValueChange, and value.

Referenced by PulseGenerator().

◆ update()

void PulseGenerator::update ( )

Updates the state of the output pin.

It changes the state of the pin depending if it is time to change it from HIGH to LOW, or otherwise. Both parameters delayHigh and delayLow are defined via the constructor

Definition at line 26 of file PulseGenerator.cpp.

26  {
27  // calculate the time difference
28  // between the current time and the time when
29  // the state of the pin changed for the last time
30  time_t currentTime = millis();
31  time_t deltaTime = DELTA_TIME(currentTime, timeLastValueChange);
32 
33  // if it is time to change the state of the pin,
34  // either from HIGH to LOW or from LOW to HIGH, change it.
35  if ((value == LOW && deltaTime >= delayLow) || (value == HIGH && deltaTime >= delayHigh)) {
36  // update the values of the member function
37  timeLastValueChange = currentTime;
38  value = !value;
39 
40  // update the state of the output pin
41  digitalWrite(outputPin, value);
42 
43  // if the state of the pin just went from HIGH to LOW,
44  // increment the counter of pulses
45  // (another pulse was just generated)
46  if (value == LOW) {
47  pulseCount++;
48  }
49  }
50 }

References delayHigh, delayLow, DELTA_TIME, outputPin, pulseCount, timeLastValueChange, and value.

Member Data Documentation

◆ delayHigh

time_t PulseGenerator::delayHigh
private

delay of the pulse in HIGH state

Definition at line 23 of file PulseGenerator.h.

Referenced by PulseGenerator(), and update().

◆ delayLow

time_t PulseGenerator::delayLow
private

delay of the pulse in LOW state

Definition at line 24 of file PulseGenerator.h.

Referenced by PulseGenerator(), and update().

◆ outputPin

int PulseGenerator::outputPin
private

number of the pin where the pulses are generated

Definition at line 25 of file PulseGenerator.h.

Referenced by PulseGenerator(), reset(), and update().

◆ pulseCount

int PulseGenerator::pulseCount
private

number of pulses that have been generated so far

Definition at line 28 of file PulseGenerator.h.

Referenced by reset(), and update().

◆ timeLastValueChange

time_t PulseGenerator::timeLastValueChange
private

time when the state of the pin changed for the last time

Definition at line 30 of file PulseGenerator.h.

Referenced by reset(), and update().

◆ value

int PulseGenerator::value
private

current state of the output pin (HIGH/LOW)

Definition at line 27 of file PulseGenerator.h.

Referenced by reset(), and update().


The documentation for this class was generated from the following files:
PulseGenerator::value
int value
current state of the output pin (HIGH/LOW)
Definition: PulseGenerator.h:27
PulseGenerator::reset
void reset()
Resets the pulse generator.
Definition: PulseGenerator.cpp:14
PulseGenerator::pulseCount
int pulseCount
number of pulses that have been generated so far
Definition: PulseGenerator.h:28
time_t
unsigned long time_t
Referring to the data type unsigned long as time_t.
Definition: DateTime.h:20
PulseGenerator::delayLow
time_t delayLow
delay of the pulse in LOW state
Definition: PulseGenerator.h:24
PulseGenerator::delayHigh
time_t delayHigh
delay of the pulse in HIGH state
Definition: PulseGenerator.h:23
DELTA_TIME
#define DELTA_TIME(time1, time2)
Calculates the time difference between two times given as parameters.
Definition: DateTime.h:35
PulseGenerator::timeLastValueChange
time_t timeLastValueChange
time when the state of the pin changed for the last time
Definition: PulseGenerator.h:30
PulseGenerator::outputPin
int outputPin
number of the pin where the pulses are generated
Definition: PulseGenerator.h:25